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
impl Claims
Sourcepub fn new() -> Self
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());
Sourcepub fn with_registered_claims(self, registered: RegisteredClaims) -> Self
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()));
Sourcepub fn with_custom_string<S: Into<String>>(self, key: i32, value: S) -> Self
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"));
Sourcepub fn with_custom_binary<B: Into<Vec<u8>>>(self, key: i32, value: B) -> Self
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));
Sourcepub fn with_custom_int(self, key: i32, value: i64) -> Self
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));
Sourcepub fn with_custom_map(self, key: i32, value: BTreeMap<i32, CborValue>) -> Self
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");
}
}
Sourcepub fn to_map(&self) -> ClaimsMap
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
Sourcepub fn from_map(map: &ClaimsMap) -> Self
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));