Struct cclm::Claims

source ·
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

source

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());
source

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")));
source

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")));
source

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()));
source

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());
source

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"));
source

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);
source

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());
source

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);
}

Trait Implementations§

source§

impl Clone for Claims

source§

fn clone(&self) -> Claims

Returns a copy 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

Implement the Default trait for Claims.

source§

fn default() -> Self

Create a new instance of Claims.

source§

impl<'de> Deserialize<'de> for Claims

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for Claims

Implement the Display trait for Claims.

source§

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

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

impl Serialize for Claims

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where
__S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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> ToOwned for Twhere
T: Clone,

§

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> ToString for Twhere
T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,