Claims

Struct Claims 

Source
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

Source

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 time
  • exp (Expiration Time) set to the current time plus the specified duration
  • nbf (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();
Source

pub fn with_custom_claims<CustomClaims>( custom_claims: CustomClaims, valid_for: Duration, ) -> JWTClaims<CustomClaims>
where CustomClaims: Serialize + DeserializeOwned,

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 time
  • exp (Expiration Time) set to the current time plus the specified duration
  • nbf (Not Before) set to the current time
  • All other standard claims initialized to None
  • The provided custom claims
§Type Parameters
  • CustomClaims - A type that implements Serialize and DeserializeOwned for custom application data
§Arguments
  • custom_claims - The application-specific data to include in the token
  • valid_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 UnwindSafe for Claims

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,