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; provide PEM bytes from your key store.
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 create_client_jwt_token(
&self,
user_id: impl Into<String>,
client_id: impl AsRef<str>,
scopes: Vec<String>,
lifetime: Option<Duration>,
) -> Result<String>
pub fn create_client_jwt_token( &self, user_id: impl Into<String>, client_id: impl AsRef<str>, scopes: Vec<String>, lifetime: Option<Duration>, ) -> Result<String>
Create a JWT for a token issued to an OAuth client on a user’s behalf: it carries the
client_id claim, which marks it as delegated access and not as the user’s own session.
Sourcepub fn create_refresh_jwt_token(
&self,
user_id: impl Into<String>,
lifetime: Option<Duration>,
) -> Result<String>
pub fn create_refresh_jwt_token( &self, user_id: impl Into<String>, lifetime: Option<Duration>, ) -> Result<String>
Create a JWT that is a refresh token: it carries token_use: "refresh", which
/auth/refresh requires and which an access token never has.
Sourcepub fn validate_session_jwt_token(&self, token: &str) -> Result<JwtClaims>
pub fn validate_session_jwt_token(&self, token: &str) -> Result<JwtClaims>
Validate a JWT that stands for a user’s own session: like
validate_jwt_token, but a token issued to an OAuth client
(one that carries a client_id claim) is refused.
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 create_client_auth_token(
&self,
user_id: impl Into<String>,
client_id: impl Into<String>,
scopes: Vec<String>,
auth_method: impl Into<String>,
lifetime: Option<Duration>,
) -> Result<AuthToken>
pub fn create_client_auth_token( &self, user_id: impl Into<String>, client_id: impl Into<String>, scopes: Vec<String>, auth_method: impl Into<String>, lifetime: Option<Duration>, ) -> Result<AuthToken>
Create a complete authentication token for a token issued to an OAuth client
(see create_client_jwt_token).
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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