pub struct TokenManager { /* private fields */ }
Expand description
Token manager for creating, validating, and managing tokens.
Implementations§
Source§impl TokenManager
impl TokenManager
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
let private_key = include_bytes!("../../private.pem"); // Either format
let public_key = include_bytes!("../../public.pem");
let manager = TokenManager::new_rsa(
private_key,
public_key,
"my-service",
"my-audience"
)?;
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: Vec<String>,
auth_method: impl Into<String>,
lifetime: Option<Duration>,
) -> Result<AuthToken>
pub fn create_auth_token( &self, user_id: impl Into<String>, scopes: Vec<String>, 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 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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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