Skip to main content

TokenAuthenticator

Struct TokenAuthenticator 

Source
pub struct TokenAuthenticator { /* private fields */ }
Available on crate feature auth-token only.
Expand description

A production-ready, time-limited signed-token authenticator (feature = "auth-token").

A token binds a StreamKey to an expiry under an HMAC-SHA-256 signature, so it cannot be forged without the shared secret nor replayed past its deadline. The wire form is:

<expiry_unix_seconds>:<hex(hmac_sha256(secret, "app/stream:expiry"))>

Mint tokens out-of-band (e.g. in your sign-in / “get publish URL” endpoint) with sign; the engine verifies them on the publish path — and, when gate_playback is set, the play path too. Verification is constant-time and pulls in no crypto dependency (the HMAC is a small, test-vector-checked in-crate implementation).

use arcly_stream::auth::TokenAuthenticator;
use arcly_stream::StreamKey;

let auth = TokenAuthenticator::new("super-secret");
let key = StreamKey::new("live", "cam-1");
// Mint a token valid until some absolute Unix time:
let token = auth.sign(&key, 9_999_999_999);
assert!(auth.verify(&key, &token).is_ok());
// A token for a different stream is rejected:
assert!(auth.verify(&StreamKey::new("live", "other"), &token).is_err());

Implementations§

Source§

impl TokenAuthenticator

Source

pub fn new(secret: impl Into<Vec<u8>>) -> Self

New authenticator keyed by secret. Gates publish only by default; call gate_playback to gate play as well.

Source

pub fn gate_playback(self, gate: bool) -> Self

Also require a valid token to play (subscribe), not just publish.

Source

pub fn sign(&self, key: &StreamKey, expires_at: u64) -> String

Mint a token authorizing key until expires_at (Unix seconds).

Source

pub fn verify(&self, key: &StreamKey, token: &str) -> Result<()>

Verify token against key and the current wall clock. Returns StreamError::Unauthorized on a malformed, expired, or mis-signed token.

Trait Implementations§

Source§

impl StreamAuthenticator for TokenAuthenticator

Source§

fn authorize_publish<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: &'life1 StreamKey, creds: &'life2 Credentials, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Decide whether creds may publish to key. Return StreamError::Unauthorized to reject.
Source§

fn authorize_play<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: &'life1 StreamKey, creds: &'life2 Credentials, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Decide whether creds may subscribe to key.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more