pub struct Claims;Expand description
Factory for creating JWT claim sets with standard and custom claims.
This struct provides static methods for creating JWT claims with or without custom application-specific data.
Implementations§
Source§impl Claims
impl Claims
Sourcepub fn create(valid_for: Duration) -> JWTClaims<NoCustomClaims>
pub fn create(valid_for: Duration) -> JWTClaims<NoCustomClaims>
Creates a new set of claims with standard JWT fields but no custom data.
This method initializes a new claims object with:
iat(Issued At) set to the current timeexp(Expiration Time) set to the current time plus the specified durationnbf(Not Before) set to the current time- All other standard claims initialized to None
- No custom claims (using
NoCustomClaims)
§Arguments
valid_for- The duration for which the token should be valid
§Returns
- A new
JWTClaims<NoCustomClaims>object that can be further customized with the builder pattern
§Example
use jwt_simple::prelude::*;
// Create a token valid for 1 hour with standard fields
let claims = Claims::create(Duration::from_hours(1))
.with_issuer("auth.example.com")
.with_subject("user123");
// Token can be created with any supported algorithm
let key = HS256Key::generate();
let token = key.authenticate(claims).unwrap();Sourcepub fn with_custom_claims<CustomClaims: Serialize>(
custom_claims: CustomClaims,
valid_for: Duration,
) -> JWTClaims<CustomClaims>
pub fn with_custom_claims<CustomClaims: Serialize>( custom_claims: CustomClaims, valid_for: Duration, ) -> JWTClaims<CustomClaims>
Creates a new set of claims with both standard JWT fields and custom application data.
This method initializes a new claims object with:
iat(Issued At) set to the current timeexp(Expiration Time) set to the current time plus the specified durationnbf(Not Before) set to the current time- All other standard claims initialized to None
- The provided custom claims
§Type Parameters
CustomClaims- A type that implementsSerializefor custom application data
§Arguments
custom_claims- The application-specific data to include in the tokenvalid_for- The duration for which the token should be valid
§Returns
- A new
JWTClaims<CustomClaims>object that can be further customized with the builder pattern
§Example
use jwt_simple::prelude::*;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
struct UserClaims {
user_id: u64,
roles: Vec<String>,
email: String,
}
// Create custom claims
let user_data = UserClaims {
user_id: 1234,
roles: vec!["user".to_string(), "admin".to_string()],
email: "user@example.com".to_string(),
};
// Create a token valid for 1 hour with custom data
let claims = Claims::with_custom_claims(user_data, Duration::from_hours(1))
.with_issuer("auth.example.com");
// Token can be created with any supported algorithm
let key_pair = ES256KeyPair::generate();
let token = key_pair.sign(claims).unwrap();Auto Trait Implementations§
impl Freeze for Claims
impl RefUnwindSafe for Claims
impl Send for Claims
impl Sync for Claims
impl Unpin for Claims
impl UnsafeUnpin for Claims
impl UnwindSafe for Claims
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