Expand description
A Rust library for accessing and manipulating claims of a JSON Web Token (JWT)
Overview
The Claims library holds JSON Web Token (JWT) claims. It provides an RFC7519 compliant implementation of JSON Web Tokens (JWT) and JSON Web Signature (JWS) for Rust.
The Claims
type is provided to hold
the claims of a JWT. The claims are stored in a HashMap
and can be
accessed using the get_claim
, set_claim
, remove_claim
, and
has_claim
methods.
Features
The following table lists the optional reserved claims that are supported:
Claim | Description |
---|---|
aud (Audience) | Identifies the recipients that the JWT is intended for. |
custom (Custom) | Custom claims are used to share information between parties that agree on using them and are neither registered or public claims. |
did (Decentralized Identifier) | A string value that uniquely identifies a subject. |
exp (Expiration Time) | Identifies the expiration time on or after which the JWT MUST NOT be accepted for processing. |
iat (Issued At) | Identifies the time at which the JWT was issued. |
iss (Issuer) | Identifies the principal that issued the JWT. |
jti (JWT ID) | Provides a unique identifier for the JWT. |
nbf (Not Before) | Identifies the time before which the JWT MUST NOT be accepted for processing. |
sub (Subject) | Identifies the principal that is the subject of the JWT. |
vc (Verifiable Credential) | A Credential that is tamper-evident and has authorship that can be cryptographically verified. |
vp (Verifiable Presentation) | A Presentation that is tamper-evident and has authorship that can be cryptographically verified. |
Usage
serde
: Enable serialization/deserialization via serde
Examples
use self::cclm::Claims;
use std::collections::HashMap;
// Create a new instance of Claims
let mut claims = Claims::new();
// Set a claim
claims.set_claim("name", "John Doe");
// Get a claim
let name = claims.get_claim("name").unwrap(); // returns "John Doe"
// Remove a claim
claims.remove_claim("name");
// Clear all claims
claims.clear_claims();
// Has a claim
let has_claim = claims.has_claim("name"); // returns false
// Get the number of claims
let len = claims.len(); // returns 0
// Is the claims empty?
let is_empty = claims.is_empty(); // returns true
// Get the claims as a HashMap
let claims_map: &HashMap<String, String> = claims.get_claims();
Links
Structs
The Claims struct holds the claims of a JSON Web Token (JWT).