pub struct Claims {
pub claims: HashMap<String, String>,
}
Expand description
The Claims struct holds the claims of a JSON Web Token (JWT).
A JWT is a compact, URL-safe means of representing claims to be transferred between two parties. It consists of a header, a payload, and a signature. The payload is where the claims are stored.
The claims in a JWT are encoded as a JSON object and can be used to convey information such as the identity of an end user, the expiration time of the token, and more.
The Claims struct provides a convenient way to manipulate the claims of a JWT in Rust. It stores the claims as a HashMap<String, String>, allowing for fast and efficient access to each claim.
Fields§
§claims: HashMap<String, String>
The claims of the JWT as a HashMap.
Implementations§
source§impl Claims
impl Claims
sourcepub fn new() -> Claims
pub fn new() -> Claims
Creates a new instance of the Claims
struct with an empty
HashMap of claims.
Example
use self::cclm::Claims;
let claims = Claims::new();
assert!(claims.claims.is_empty());
sourcepub fn set_claim(&mut self, key: &str, value: &str)
pub fn set_claim(&mut self, key: &str, value: &str)
Adds or updates a claim in the Claims
struct with the given key and value.
Example
use self::cclm::Claims;
let mut claims = Claims::new();
claims.set_claim("sub", "1234567890");
assert_eq!(claims.get_claim("sub"), Some(&String::from("1234567890")));
sourcepub fn get_claim(&self, key: &str) -> Option<&String>
pub fn get_claim(&self, key: &str) -> Option<&String>
Gets a claim from the Claims
struct with the given key.
Returns None
if the key does not exist in the Claims
.
Example
use self::cclm::Claims;
let mut claims = Claims::new();
claims.set_claim("sub", "1234567890");
assert_eq!(claims.get_claim("sub"), Some(&String::from("1234567890")));
sourcepub fn remove_claim(&mut self, key: &str) -> Option<String>
pub fn remove_claim(&mut self, key: &str) -> Option<String>
Returns the value of the claim that was removed, if any.
Example
use self::cclm::Claims;
let mut claims = Claims::new();
claims.set_claim("sub", "1234567890");
assert_eq!(claims.remove_claim("sub"), Some("1234567890".to_owned()));
sourcepub fn clear_claims(&mut self)
pub fn clear_claims(&mut self)
Clears all claims from the Claims
struct.
Example
use self::cclm::Claims;
let mut claims = Claims::new();
claims.set_claim("sub", "1234567890");
claims.clear_claims();
assert!(claims.claims.is_empty());
sourcepub fn has_claim(&self, key: &str) -> bool
pub fn has_claim(&self, key: &str) -> bool
Checks if a claim with the given key exists in the Claims
struct.
Example
use self::cclm::Claims;
let mut claims = Claims::new();
claims.set_claim("sub", "1234567890");
assert!(claims.has_claim("sub"));
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the number of claims in the Claims
struct.
Example
use self::cclm::Claims;
let mut claims = Claims::new();
claims.set_claim("sub", "1234567890");
assert_eq!(claims.len(), 1);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks if the Claims
struct is empty.
Example
use self::cclm::Claims;
let mut claims = Claims::new();
assert!(claims.is_empty());
claims.set_claim("sub", "1234567890");
assert!(!claims.is_empty());
sourcepub fn get_claims(&self) -> &HashMap<String, String>
pub fn get_claims(&self) -> &HashMap<String, String>
Get a reference to the HashMap of claims in the Claims
struct.
This is useful if you need to iterate over the claims.
Example
use self::cclm::Claims;
let mut claims = Claims::new();
claims.set_claim("sub", "1234567890");
claims.set_claim("name", "John Doe");
for (key, value) in claims.get_claims() {
println!("{}: {}", key, value);
}