pub struct JwtManager { /* private fields */ }Expand description
JWT manager for creating and validating tokens.
Implementations§
Source§impl JwtManager
impl JwtManager
Sourcepub fn new(
secret: &[u8],
access_expiry: Duration,
refresh_expiry: Duration,
) -> Self
pub fn new( secret: &[u8], access_expiry: Duration, refresh_expiry: Duration, ) -> Self
Create a new JWT manager with a secret key.
The secret should be at least 32 bytes for security.
Sourcepub fn from_hex_secret(
hex_secret: &str,
access_expiry: Duration,
refresh_expiry: Duration,
) -> Result<Self, AuthError>
pub fn from_hex_secret( hex_secret: &str, access_expiry: Duration, refresh_expiry: Duration, ) -> Result<Self, AuthError>
Sourcepub fn generate_secret() -> [u8; 32]
pub fn generate_secret() -> [u8; 32]
Generate a random 256-bit secret key.
Sourcepub fn generate_hex_secret() -> String
pub fn generate_hex_secret() -> String
Generate a random secret as hex string.
Sourcepub fn create_access_token(
&self,
user_id: &str,
username: &str,
role: UserRole,
) -> Result<(String, DateTime<Utc>), AuthError>
pub fn create_access_token( &self, user_id: &str, username: &str, role: UserRole, ) -> Result<(String, DateTime<Utc>), AuthError>
Sourcepub fn create_refresh_token(
&self,
user_id: &str,
username: &str,
role: UserRole,
family_id: Option<String>,
) -> Result<(String, DateTime<Utc>), AuthError>
pub fn create_refresh_token( &self, user_id: &str, username: &str, role: UserRole, family_id: Option<String>, ) -> Result<(String, DateTime<Utc>), AuthError>
Sourcepub fn create_token_pair(
&self,
user_id: &str,
username: &str,
role: UserRole,
) -> Result<TokenPair, AuthError>
pub fn create_token_pair( &self, user_id: &str, username: &str, role: UserRole, ) -> Result<TokenPair, AuthError>
Sourcepub fn validate_access_token(&self, token: &str) -> Result<Claims, AuthError>
pub fn validate_access_token(&self, token: &str) -> Result<Claims, AuthError>
Validate an access token (must be access type).
§Errors
Returns error if token is invalid, expired, or not an access token.
Sourcepub fn refresh_tokens(
&self,
refresh_token: &str,
) -> Result<TokenPair, AuthError>
pub fn refresh_tokens( &self, refresh_token: &str, ) -> Result<TokenPair, AuthError>
Validate a refresh token and optionally create new tokens.
§Errors
Returns error if token is invalid, expired, or not a refresh token.
Sourcepub fn extract_from_header(header: &str) -> Option<&str>
pub fn extract_from_header(header: &str) -> Option<&str>
Extract token from Authorization header.
Expects format: “Bearer
Trait Implementations§
Auto Trait Implementations§
impl Freeze for JwtManager
impl RefUnwindSafe for JwtManager
impl Send for JwtManager
impl Sync for JwtManager
impl Unpin for JwtManager
impl UnwindSafe for JwtManager
Blanket Implementations§
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