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>
impl<Claims, Algorithm> TokenSigner<Claims, Algorithm>
Sourcepub fn new() -> TokenSignerBuilder<Claims, Algorithm>
pub fn new() -> TokenSignerBuilder<Claims, Algorithm>
Returns a new TokenSignerBuilder
Sourcepub fn access_token_name(&self) -> &str
pub fn access_token_name(&self) -> &str
Returns the value of the access_token_name
field on this struct.
Sourcepub fn refresh_token_name(&self) -> &str
pub fn refresh_token_name(&self) -> &str
Returns the value of the refresh_token_name
field on this struct.
Sourcepub fn create_refresh_header_value(
&self,
claims: &Claims,
) -> AuthResult<HeaderValue>
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.
Sourcepub fn create_access_header_value(
&self,
claims: &Claims,
) -> AuthResult<HeaderValue>
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.
Sourcepub fn create_header_value(
&self,
claims: &Claims,
token_lifetime: Duration,
) -> AuthResult<HeaderValue>
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
.
Sourcepub fn create_bearer_header_value(
&self,
claims: &Claims,
) -> AuthResult<HeaderValue>
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
Sourcepub fn create_signed_token(
&self,
claims: &Claims,
token_lifetime: Duration,
) -> AuthResult<String>
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 typeClaims
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>
impl<Claims, Algo> Clone for TokenSigner<Claims, Algo>
Source§fn clone(&self) -> TokenSigner<Claims, Algo>
fn clone(&self) -> TokenSigner<Claims, Algo>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more