pub struct AuthToken {Show 16 fields
pub token_id: String,
pub user_id: String,
pub access_token: String,
pub token_type: Option<String>,
pub subject: Option<String>,
pub issuer: Option<String>,
pub refresh_token: Option<String>,
pub issued_at: DateTime<Utc>,
pub expires_at: DateTime<Utc>,
pub scopes: Scopes,
pub auth_method: String,
pub client_id: Option<String>,
pub user_profile: Option<ProviderProfile>,
pub permissions: Permissions,
pub roles: Roles,
pub metadata: TokenMetadata,
}Expand description
An issued authentication token with all associated metadata.
Created by TokenManager and returned from
AuthFramework::authenticate.
Contains the encoded access_token string, optional refresh_token,
granted scopes, and contextual TokenMetadata.
Fields§
§token_id: StringUnique token identifier
user_id: StringUser identifier this token belongs to
access_token: StringAccess token value
token_type: Option<String>Token type (e.g., “bearer”)
subject: Option<String>Subject claim
issuer: Option<String>Token issuer
refresh_token: Option<String>Optional refresh token
issued_at: DateTime<Utc>When the token was issued
expires_at: DateTime<Utc>When the token expires
scopes: ScopesScopes granted to this token
auth_method: StringAuthentication method used to obtain this token
client_id: Option<String>Client ID that requested this token
user_profile: Option<ProviderProfile>User profile data (optional)
permissions: PermissionsUser’s permissions
roles: RolesUser’s roles
metadata: TokenMetadataAdditional token metadata
Implementations§
Source§impl AuthToken
impl AuthToken
Sourcepub fn builder(
token_id: impl Into<String>,
user_id: impl Into<String>,
access_token: impl Into<String>,
) -> AuthTokenBuilder
pub fn builder( token_id: impl Into<String>, user_id: impl Into<String>, access_token: impl Into<String>, ) -> AuthTokenBuilder
Start building an AuthToken with fluent setters.
§Example
use auth_framework::tokens::AuthToken;
let token = AuthToken::builder("token123", "user456", "access_token")
.expires_at(chrono::Utc::now() + chrono::Duration::hours(2))
.build();Source§impl AuthToken
impl AuthToken
Sourcepub fn new(
user_id: impl Into<String>,
access_token: impl Into<String>,
expires_in: Duration,
auth_method: impl Into<String>,
) -> Self
pub fn new( user_id: impl Into<String>, access_token: impl Into<String>, expires_in: Duration, auth_method: impl Into<String>, ) -> Self
Create a new authentication token.
Sourcepub fn access_token(&self) -> &str
pub fn access_token(&self) -> &str
Get the access token string.
§Example
use auth_framework::tokens::AuthToken;
let token = AuthToken::builder("t1", "u1", "my_token").build();
assert_eq!(token.access_token(), "my_token");Sourcepub fn user_id(&self) -> &str
pub fn user_id(&self) -> &str
Get the user ID.
§Example
use auth_framework::tokens::AuthToken;
let token = AuthToken::builder("t1", "user42", "access").build();
assert_eq!(token.user_id(), "user42");Sourcepub fn expires_at(&self) -> DateTime<Utc>
pub fn expires_at(&self) -> DateTime<Utc>
Get the expiration time.
§Example
use auth_framework::tokens::AuthToken;
use chrono::Utc;
let token = AuthToken::builder("t1", "u1", "access").build();
assert!(token.expires_at() > Utc::now());Sourcepub fn token_value(&self) -> &str
pub fn token_value(&self) -> &str
Get the token value.
Alias for access_token().
§Example
use auth_framework::tokens::AuthToken;
let token = AuthToken::builder("t1", "u1", "tok_value").build();
assert_eq!(token.token_value(), "tok_value");Sourcepub fn token_type(&self) -> Option<&str>
pub fn token_type(&self) -> Option<&str>
Get the token type.
§Example
use auth_framework::tokens::AuthToken;
let token = AuthToken::builder("t1", "u1", "access")
.token_type("Bearer")
.build();
assert_eq!(token.token_type(), Some("Bearer"));Sourcepub fn subject(&self) -> Option<&str>
pub fn subject(&self) -> Option<&str>
Get the subject claim.
§Example
use auth_framework::tokens::AuthToken;
let token = AuthToken::builder("t1", "u1", "access")
.subject("sub-123")
.build();
assert_eq!(token.subject(), Some("sub-123"));Sourcepub fn issuer(&self) -> Option<&str>
pub fn issuer(&self) -> Option<&str>
Get the issuer.
§Example
use auth_framework::tokens::AuthToken;
let token = AuthToken::builder("t1", "u1", "access")
.issuer("my-service")
.build();
assert_eq!(token.issuer(), Some("my-service"));Sourcepub fn is_expired(&self) -> bool
pub fn is_expired(&self) -> bool
Check if the token has expired.
§Example
use auth_framework::tokens::AuthToken;
let token = AuthToken::builder("t1", "u1", "access").build();
assert!(!token.is_expired()); // 1-hour defaultSourcepub fn is_expiring(&self, within: Duration) -> bool
pub fn is_expiring(&self, within: Duration) -> bool
Check if the token is expiring within the given duration.
§Example
use auth_framework::tokens::AuthToken;
use std::time::Duration;
let token = AuthToken::builder("t1", "u1", "access").build();
assert!(token.is_expiring(Duration::from_secs(7200))); // within 2 hoursSourcepub fn is_revoked(&self) -> bool
pub fn is_revoked(&self) -> bool
Check if the token has been revoked.
§Example
use auth_framework::tokens::AuthToken;
let mut token = AuthToken::builder("t1", "u1", "access").build();
assert!(!token.is_revoked());
token.revoke(Some("user request".to_string()));
assert!(token.is_revoked());Sourcepub fn is_valid(&self) -> bool
pub fn is_valid(&self) -> bool
Check if the token is valid (not expired and not revoked).
§Example
use auth_framework::tokens::AuthToken;
let token = AuthToken::builder("t1", "u1", "access").build();
assert!(token.is_valid());Sourcepub fn has_refresh_token(&self) -> bool
pub fn has_refresh_token(&self) -> bool
Check whether this token carries a refresh token.
§Example
use auth_framework::tokens::AuthToken;
let token = AuthToken::builder("t1", "u1", "access")
.refresh_token("rt-abc")
.build();
assert!(token.has_refresh_token());Sourcepub fn get_refresh_token(&self) -> Option<&str>
pub fn get_refresh_token(&self) -> Option<&str>
Return the refresh token string, if present.
§Example
use auth_framework::tokens::AuthToken;
let token = AuthToken::builder("t1", "u1", "access")
.refresh_token("rt-xyz")
.build();
assert_eq!(token.get_refresh_token(), Some("rt-xyz"));Sourcepub fn revoke(&mut self, reason: Option<String>)
pub fn revoke(&mut self, reason: Option<String>)
Revoke the token.
§Example
use auth_framework::tokens::AuthToken;
let mut token = AuthToken::builder("t1", "u1", "access").build();
token.revoke(Some("compromised".to_string()));
assert!(token.is_revoked());Sourcepub fn mark_used(&mut self)
pub fn mark_used(&mut self)
Update the last used time and increment use count.
§Example
use auth_framework::tokens::AuthToken;
let mut token = AuthToken::builder("t1", "u1", "access").build();
assert_eq!(token.metadata.use_count, 0);
token.mark_used();
assert_eq!(token.metadata.use_count, 1);Sourcepub fn add_scope(&mut self, scope: impl Into<String>)
pub fn add_scope(&mut self, scope: impl Into<String>)
Add a scope to the token.
Duplicates are ignored.
§Example
use auth_framework::tokens::AuthToken;
let mut token = AuthToken::builder("t1", "u1", "access").build();
token.add_scope("read");
assert!(token.has_scope("read"));Sourcepub fn has_scope(&self, scope: &str) -> bool
pub fn has_scope(&self, scope: &str) -> bool
Check if the token has a specific scope.
§Example
use auth_framework::tokens::AuthToken;
let mut token = AuthToken::builder("t1", "u1", "access").build();
token.add_scope("write");
assert!(token.has_scope("write"));
assert!(!token.has_scope("admin"));Sourcepub fn with_refresh_token(self, refresh_token: impl Into<String>) -> Self
pub fn with_refresh_token(self, refresh_token: impl Into<String>) -> Self
Set the refresh token.
§Example
use auth_framework::tokens::AuthToken;
let token = AuthToken::builder("t1", "u1", "access").build()
.with_refresh_token("refresh_xyz");
assert!(token.refresh_token.is_some());Sourcepub fn with_client_id(self, client_id: impl Into<String>) -> Self
pub fn with_client_id(self, client_id: impl Into<String>) -> Self
Set the client ID.
§Example
use auth_framework::tokens::AuthToken;
let token = AuthToken::builder("t1", "u1", "access").build()
.with_client_id("app-client");
assert_eq!(token.client_id.as_deref(), Some("app-client"));Sourcepub fn with_scopes(self, scopes: impl Into<Scopes>) -> Self
pub fn with_scopes(self, scopes: impl Into<Scopes>) -> Self
Set the token scopes.
§Example
use auth_framework::tokens::AuthToken;
use auth_framework::types::Scopes;
let token = AuthToken::builder("t1", "u1", "access").build()
.with_scopes(Scopes::new(vec!["read".into()]));
assert!(token.has_scope("read"));Sourcepub fn with_metadata(self, metadata: TokenMetadata) -> Self
pub fn with_metadata(self, metadata: TokenMetadata) -> Self
Add metadata to the token.
§Example
use auth_framework::tokens::{AuthToken, TokenMetadata};
let meta = TokenMetadata::builder().issued_ip("192.168.1.1").build();
let token = AuthToken::builder("t1", "u1", "access").build()
.with_metadata(meta);
assert_eq!(token.metadata.issued_ip.as_deref(), Some("192.168.1.1"));Sourcepub fn time_until_expiry(&self) -> Duration
pub fn time_until_expiry(&self) -> Duration
Get time until expiration.
Returns Duration::ZERO if the token has already expired.
§Example
use auth_framework::tokens::AuthToken;
use std::time::Duration;
let token = AuthToken::builder("t1", "u1", "access").build();
assert!(token.time_until_expiry() > Duration::ZERO);Sourcepub fn add_custom_claim(&mut self, key: impl Into<String>, value: Value)
pub fn add_custom_claim(&mut self, key: impl Into<String>, value: Value)
Add a custom claim to the token metadata.
§Example
use auth_framework::tokens::AuthToken;
let mut token = AuthToken::builder("t1", "u1", "access").build();
token.add_custom_claim("tenant", serde_json::json!("acme"));
assert_eq!(token.get_custom_claim("tenant").unwrap(), &serde_json::json!("acme"));Sourcepub fn get_custom_claim(&self, key: &str) -> Option<&Value>
pub fn get_custom_claim(&self, key: &str) -> Option<&Value>
Get a custom claim from the token metadata.
§Example
use auth_framework::tokens::AuthToken;
let token = AuthToken::builder("t1", "u1", "access").build();
assert!(token.get_custom_claim("missing").is_none());Sourcepub fn has_permission(&self, permission: &str) -> bool
pub fn has_permission(&self, permission: &str) -> bool
Check if the token has a specific permission.
§Example
use auth_framework::tokens::AuthToken;
let mut token = AuthToken::builder("t1", "u1", "access").build();
token.add_permission("admin");
assert!(token.has_permission("admin"));Sourcepub fn add_permission(&mut self, permission: impl Into<String>)
pub fn add_permission(&mut self, permission: impl Into<String>)
Add a permission to the token.
Duplicates are ignored.
§Example
use auth_framework::tokens::AuthToken;
let mut token = AuthToken::builder("t1", "u1", "access").build();
token.add_permission("write");
assert!(token.has_permission("write"));Sourcepub fn add_role(&mut self, role: impl Into<String>)
pub fn add_role(&mut self, role: impl Into<String>)
Add a role to the token.
Duplicates are ignored.
§Example
use auth_framework::tokens::AuthToken;
let mut token = AuthToken::builder("t1", "u1", "access").build();
token.add_role("editor");
assert!(token.has_role("editor"));Sourcepub fn has_role(&self, role: &str) -> bool
pub fn has_role(&self, role: &str) -> bool
Check if the token has a specific role.
§Example
use auth_framework::tokens::AuthToken;
let mut token = AuthToken::builder("t1", "u1", "access").build();
token.add_role("admin");
assert!(token.has_role("admin"));
assert!(!token.has_role("guest"));Sourcepub fn with_permissions(self, permissions: impl Into<Permissions>) -> Self
pub fn with_permissions(self, permissions: impl Into<Permissions>) -> Self
Set the permissions.
§Example
use auth_framework::tokens::AuthToken;
use auth_framework::types::Permissions;
let token = AuthToken::builder("t1", "u1", "access").build()
.with_permissions(Permissions::new(vec!["read".into()]));
assert!(token.has_permission("read"));Sourcepub fn with_roles(self, roles: impl Into<Roles>) -> Self
pub fn with_roles(self, roles: impl Into<Roles>) -> Self
Set the roles.
§Example
use auth_framework::tokens::AuthToken;
use auth_framework::types::Roles;
let token = AuthToken::builder("t1", "u1", "access").build()
.with_roles(Roles::new(vec!["viewer".into()]));
assert!(token.has_role("viewer"));Trait Implementations§
Source§impl<'de> Deserialize<'de> for AuthToken
impl<'de> Deserialize<'de> for AuthToken
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<'a, R: Row> FromRow<'a, R> for AuthTokenwhere
&'a str: ColumnIndex<R>,
String: Decode<'a, R::Database> + Type<R::Database>,
Option<String>: Decode<'a, R::Database> + Type<R::Database>,
DateTime<Utc>: Decode<'a, R::Database> + Type<R::Database>,
Scopes: Decode<'a, R::Database> + Type<R::Database>,
Option<ProviderProfile>: Decode<'a, R::Database> + Type<R::Database>,
Permissions: Decode<'a, R::Database> + Type<R::Database>,
Roles: Decode<'a, R::Database> + Type<R::Database>,
TokenMetadata: Decode<'a, R::Database> + Type<R::Database>,
impl<'a, R: Row> FromRow<'a, R> for AuthTokenwhere
&'a str: ColumnIndex<R>,
String: Decode<'a, R::Database> + Type<R::Database>,
Option<String>: Decode<'a, R::Database> + Type<R::Database>,
DateTime<Utc>: Decode<'a, R::Database> + Type<R::Database>,
Scopes: Decode<'a, R::Database> + Type<R::Database>,
Option<ProviderProfile>: Decode<'a, R::Database> + Type<R::Database>,
Permissions: Decode<'a, R::Database> + Type<R::Database>,
Roles: Decode<'a, R::Database> + Type<R::Database>,
TokenMetadata: Decode<'a, R::Database> + Type<R::Database>,
Source§impl TokenToProfile for AuthToken
impl TokenToProfile for AuthToken
Source§fn to_profile<'life0, 'life1, 'async_trait>(
&'life0 self,
provider: &'life1 OAuthProvider,
) -> Pin<Box<dyn Future<Output = Result<ProviderProfile>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn to_profile<'life0, 'life1, 'async_trait>(
&'life0 self,
provider: &'life1 OAuthProvider,
) -> Pin<Box<dyn Future<Output = Result<ProviderProfile>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn to_profile_with_extractor<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
provider: &'life1 OAuthProvider,
extractor: &'life2 ProfileExtractor,
) -> Pin<Box<dyn Future<Output = Result<ProviderProfile>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn to_profile_with_extractor<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
provider: &'life1 OAuthProvider,
extractor: &'life2 ProfileExtractor,
) -> Pin<Box<dyn Future<Output = Result<ProviderProfile>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Source§impl TokenToProfile for AuthToken
impl TokenToProfile for AuthToken
Source§async fn to_profile(&self, _provider: &OAuthProvider) -> Result<ProviderProfile>
async fn to_profile(&self, _provider: &OAuthProvider) -> Result<ProviderProfile>
Auto Trait Implementations§
impl Freeze for AuthToken
impl RefUnwindSafe for AuthToken
impl Send for AuthToken
impl Sync for AuthToken
impl Unpin for AuthToken
impl UnsafeUnpin for AuthToken
impl UnwindSafe for AuthToken
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