pub struct TokenManager { /* private fields */ }Expand description
Central token lifecycle manager: creation, validation, refresh, and revocation.
Constructed internally by AuthFramework
— most users interact with token operations through the
TokenOperations facade instead.
Implementations§
Source§impl TokenManager
impl TokenManager
Sourcepub fn export_public_jwks(&self) -> Result<Vec<JwksPublicKey>>
pub fn export_public_jwks(&self) -> Result<Vec<JwksPublicKey>>
Export the current and previous RSA verification keys as JWKS-compatible material.
Sourcepub fn new_hmac(
secret: &[u8],
issuer: impl Into<String>,
audience: impl Into<String>,
) -> Self
pub fn new_hmac( secret: &[u8], issuer: impl Into<String>, audience: impl Into<String>, ) -> Self
Create a new token manager with HMAC key.
Sourcepub fn new_rsa(
private_key: &[u8],
public_key: &[u8],
issuer: impl Into<String>,
audience: impl Into<String>,
) -> Result<Self>
pub fn new_rsa( private_key: &[u8], public_key: &[u8], issuer: impl Into<String>, audience: impl Into<String>, ) -> Result<Self>
Create a new token manager with RSA keys.
§RSA Key Format Support
This method supports RSA keys in both standard PEM formats:
- PKCS#1:
-----BEGIN RSA PRIVATE KEY-----(traditional RSA format) - PKCS#8:
-----BEGIN PRIVATE KEY-----(modern standard format, recommended)
Both formats are automatically detected and parsed. No format conversion is required.
§Example
use auth_framework::tokens::TokenManager;
// Both PKCS#1 and PKCS#8 formats work; provide PEM bytes from your key store.
let manager = TokenManager::new_rsa(
private_key,
public_key,
"my-service",
"my-audience"
)?;Sourcepub fn rotate_hmac_key(&mut self, new_secret: &[u8])
pub fn rotate_hmac_key(&mut self, new_secret: &[u8])
Rotate HMAC key, keeping the current key as the previous decoding key to seamlessly allow verification of tokens signed with the old key.
Sourcepub fn rotate_rsa_key(
&mut self,
private_key: &[u8],
public_key: &[u8],
) -> Result<()>
pub fn rotate_rsa_key( &mut self, private_key: &[u8], public_key: &[u8], ) -> Result<()>
Rotate RSA key, keeping the current key as the previous decoding key to seamlessly allow verification of tokens signed with the old key.
Sourcepub fn retire_previous_key(&mut self)
pub fn retire_previous_key(&mut self)
Retire the previous key (if any), so tokens signed with it are no longer valid.
Sourcepub fn with_default_lifetime(self, lifetime: Duration) -> Self
pub fn with_default_lifetime(self, lifetime: Duration) -> Self
Set the default token lifetime.
Sourcepub fn create_jwt_token(
&self,
user_id: impl Into<String>,
scopes: Vec<String>,
lifetime: Option<Duration>,
) -> Result<String>
pub fn create_jwt_token( &self, user_id: impl Into<String>, scopes: Vec<String>, lifetime: Option<Duration>, ) -> Result<String>
Create a new JWT token.
Sourcepub fn validate_jwt_token(&self, token: &str) -> Result<JwtClaims>
pub fn validate_jwt_token(&self, token: &str) -> Result<JwtClaims>
Validate and decode a JWT token.
Sourcepub fn create_auth_token(
&self,
user_id: impl Into<String>,
scopes: impl Into<Scopes>,
auth_method: impl Into<String>,
lifetime: Option<Duration>,
) -> Result<AuthToken>
pub fn create_auth_token( &self, user_id: impl Into<String>, scopes: impl Into<Scopes>, auth_method: impl Into<String>, lifetime: Option<Duration>, ) -> Result<AuthToken>
Create a complete authentication token with JWT.
Sourcepub fn validate_auth_token(&self, token: &AuthToken) -> Result<()>
pub fn validate_auth_token(&self, token: &AuthToken) -> Result<()>
Validate an authentication token.
Sourcepub fn refresh_token(&self, token: &AuthToken) -> Result<AuthToken>
pub fn refresh_token(&self, token: &AuthToken) -> Result<AuthToken>
Refresh a token (create a new one with extended lifetime).
Sourcepub fn extract_token_info(&self, token: &str) -> Result<TokenInfo>
pub fn extract_token_info(&self, token: &str) -> Result<TokenInfo>
Extract token information from a JWT.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for TokenManager
impl RefUnwindSafe for TokenManager
impl Send for TokenManager
impl Sync for TokenManager
impl Unpin for TokenManager
impl UnsafeUnpin for TokenManager
impl UnwindSafe for TokenManager
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more