Struct TokenSigner

Source
pub struct TokenSigner<Claims, Algo>
where Algo: Algorithm,
{ /* private fields */ }
Expand description

The TokenSigner is a convenience struct, which holds configuration values as well as a private key for generation JWTs.

For example, the crate::Authority uses it to automatically refresh the access/refresh token.

§Example

#[derive(Serialize, Clone)]
struct User {
    id: u32
}

let KeyPair {
    sk: secret_key,
    ..
} = KeyPair::generate();

let token_signer = TokenSigner::<User, _>::new()
    .signing_key(secret_key)
    .access_token_name("my_access_token")
    // makes every refresh token generated be valid for 2 hours
    .refresh_token_lifetime(Duration::from_secs(120 * 60))
    // generated tokens can still be used up to 10 seconds after they expired
    .time_options(TimeOptions::from_leeway(chrono::Duration::seconds(10)))
    .algorithm(Ed25519)
    .build().unwrap();

let cookie = token_signer.create_access_cookie(&User{
    id: 1
})?;

Please refer to the TokenSignerBuilder for a detailed description of Options available on this struct.

Implementations§

Source§

impl<Claims, Algorithm> TokenSigner<Claims, Algorithm>
where Algorithm: Algorithm + Clone, Claims: Serialize,

Source

pub fn new() -> TokenSignerBuilder<Claims, Algorithm>

Returns a new TokenSignerBuilder

Source

pub fn access_token_name(&self) -> &str

Returns the value of the access_token_name field on this struct.

Source

pub fn refresh_token_name(&self) -> &str

Returns the value of the refresh_token_name field on this struct.

Source

pub fn create_refresh_header_value( &self, claims: &Claims, ) -> AuthResult<HeaderValue>

Creates a refresh token header value.

Internally it calls Self::create_header_value while passing the previously defined refresh_token_lifetime value on this struct.

Source

pub fn create_access_header_value( &self, claims: &Claims, ) -> AuthResult<HeaderValue>

Creates a access token header value.

Internally it calls Self::create_header_value while passing the previously defined access_token_lifetime value on this struct.

Source

pub fn create_header_value( &self, claims: &Claims, token_lifetime: Duration, ) -> AuthResult<HeaderValue>

Creates a token and wraps it in a HeaderValue.

Internally it calls Self::create_signed_token while passing the claims as well as the token_lifetime.

Source

pub fn create_bearer_header_value( &self, claims: &Claims, ) -> AuthResult<HeaderValue>

Creates a Bearer HeaderValue wrapping a token.

This value is typically set as the Authorization header, also known as Bearer Authentication.

Internally it it calls Self::create_signed_token while passing the previously defined value of the access_token_lifetime on this struct.

Creates a access token cookie.

Internally it calls Self::create_cookie while passing the previously defined access_token_name and access_token_lifetime values on this struct.

Creates a refresh token cookie.

Internally it calls Self::create_cookie while passing the previously defined refresh_token_name and refresh_token_lifetime values on this struct.

Creates a token and wraps it in a Cookie.

Internally it calls Self::create_signed_token while passing the claims as well as the token_lifetime.

  • cookie_name the name of the resulting cookie
Source

pub fn create_signed_token( &self, claims: &Claims, token_lifetime: Duration, ) -> AuthResult<String>

Creates a signed token using the previously defined TimeOptions, Header and jwt_compact::Algorithm::SigningKey values on this struct.

  • claims reference to an object of the generic type Claims which will be incorporated inside of the JWT string

  • token_lifetime duration for which the token is valid for

Trait Implementations§

Source§

impl<Claims, Algo> Clone for TokenSigner<Claims, Algo>
where Algo: Algorithm + Clone, Algo::SigningKey: Clone,

Source§

fn clone(&self) -> TokenSigner<Claims, Algo>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<Claims, Algo> Freeze for TokenSigner<Claims, Algo>
where Algo: Freeze, <Algo as Algorithm>::SigningKey: Freeze,

§

impl<Claims, Algo> RefUnwindSafe for TokenSigner<Claims, Algo>
where Algo: RefUnwindSafe, <Algo as Algorithm>::SigningKey: RefUnwindSafe, Claims: RefUnwindSafe,

§

impl<Claims, Algo> Send for TokenSigner<Claims, Algo>
where Algo: Send, <Algo as Algorithm>::SigningKey: Send, Claims: Send,

§

impl<Claims, Algo> Sync for TokenSigner<Claims, Algo>
where Algo: Sync, <Algo as Algorithm>::SigningKey: Sync, Claims: Sync,

§

impl<Claims, Algo> Unpin for TokenSigner<Claims, Algo>
where Algo: Unpin, <Algo as Algorithm>::SigningKey: Unpin, Claims: Unpin,

§

impl<Claims, Algo> UnwindSafe for TokenSigner<Claims, Algo>
where Algo: UnwindSafe, <Algo as Algorithm>::SigningKey: UnwindSafe, Claims: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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
Source§

impl<T> ErasedDestructor for T
where T: 'static,