Struct Claims

Source
pub struct Claims {
    pub registered: RegisteredClaims,
    pub custom: ClaimsMap,
}
Expand description

Claims for a Common Access Token.

This struct combines standard registered claims with custom application-specific claims. It provides a flexible way to include both standardized information and custom data in a token.

§Example

use common_access_token::{Claims, RegisteredClaims};
use common_access_token::current_timestamp;

let now = current_timestamp();
let registered_claims = RegisteredClaims::new()
    .with_issuer("example-issuer")
    .with_expiration(now + 3600);

let claims = Claims::new()
    .with_registered_claims(registered_claims)
    .with_custom_string(100, "custom-value")
    .with_custom_int(101, 42);

// Access claims
assert_eq!(claims.registered.iss, Some("example-issuer".to_string()));

Fields§

§registered: RegisteredClaims

Standard registered claims as defined in RFC 8392

§custom: ClaimsMap

Custom application-specific claims with integer keys

Implementations§

Source§

impl Claims

Source

pub fn new() -> Self

Creates a new empty claims set with no registered or custom claims.

§Example
use common_access_token::Claims;

let claims = Claims::new();
assert!(claims.registered.iss.is_none());
assert!(claims.custom.is_empty());
Source

pub fn with_registered_claims(self, registered: RegisteredClaims) -> Self

Sets the registered claims.

This method replaces any existing registered claims with the provided ones.

§Example
use common_access_token::{Claims, RegisteredClaims};

let registered = RegisteredClaims::new()
    .with_issuer("example-issuer")
    .with_subject("user-123");

let claims = Claims::new().with_registered_claims(registered);
assert_eq!(claims.registered.iss, Some("example-issuer".to_string()));
Source

pub fn with_custom_string<S: Into<String>>(self, key: i32, value: S) -> Self

Adds a custom claim with a string value.

§Example
use common_access_token::Claims;
use common_access_token::header::CborValue;

let claims = Claims::new().with_custom_string(100, "custom-value");
assert!(matches!(claims.custom.get(&100), Some(CborValue::Text(s)) if s == "custom-value"));
Source

pub fn with_custom_binary<B: Into<Vec<u8>>>(self, key: i32, value: B) -> Self

Adds a custom claim with a binary value.

§Example
use common_access_token::Claims;
use common_access_token::header::CborValue;

let binary_data = vec![0x01, 0x02, 0x03];
let claims = Claims::new().with_custom_binary(101, binary_data.clone());
assert!(matches!(claims.custom.get(&101), Some(CborValue::Bytes(b)) if b == &binary_data));
Source

pub fn with_custom_int(self, key: i32, value: i64) -> Self

Adds a custom claim with an integer value.

§Example
use common_access_token::Claims;
use common_access_token::header::CborValue;

let claims = Claims::new().with_custom_int(102, 42);
assert!(matches!(claims.custom.get(&102), Some(CborValue::Integer(i)) if *i == 42));
Source

pub fn with_custom_map(self, key: i32, value: BTreeMap<i32, CborValue>) -> Self

Adds a custom claim with a nested map value.

This allows for complex structured data to be included in the token.

§Example
use common_access_token::{Claims, CborValue};
use std::collections::BTreeMap;

let mut nested_map = BTreeMap::new();
nested_map.insert(1, CborValue::Text("nested-value".to_string()));

let claims = Claims::new().with_custom_map(103, nested_map);
if let Some(CborValue::Map(map)) = claims.custom.get(&103) {
    if let Some(CborValue::Text(value)) = map.get(&1) {
        assert_eq!(value, "nested-value");
    }
}
Source

pub fn to_map(&self) -> ClaimsMap

Converts all claims (registered and custom) to a combined claims map.

This method is primarily used internally for token encoding.

§Example
use common_access_token::{Claims, RegisteredClaims};

let claims = Claims::new()
    .with_registered_claims(RegisteredClaims::new().with_issuer("example-issuer"))
    .with_custom_string(100, "custom-value");

let map = claims.to_map();
assert_eq!(map.len(), 2); // One registered claim + one custom claim
Source

pub fn from_map(map: &ClaimsMap) -> Self

Creates a Claims struct from a claims map.

This method is primarily used internally for token decoding.

§Example
use common_access_token::{Claims, CborValue};
use common_access_token::claims::{keys, ClaimsMap};

let mut map = ClaimsMap::new();
map.insert(keys::ISS, CborValue::Text("example-issuer".to_string()));
map.insert(100, CborValue::Text("custom-value".to_string()));

let claims = Claims::from_map(&map);
assert_eq!(claims.registered.iss, Some("example-issuer".to_string()));
assert!(claims.custom.contains_key(&100));

Trait Implementations§

Source§

impl Clone for Claims

Source§

fn clone(&self) -> Claims

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Claims

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Claims

Source§

fn default() -> Claims

Returns the “default value” for a type. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<T> ErasedDestructor for T
where T: 'static,