Audiences

Enum Audiences 

Source
pub enum Audiences {
    AsSet(HashSet<String>),
    AsString(String),
}
Expand description

Representation of the JWT audience claim, which can be either a single string or a set of strings.

The JWT specification allows the aud (audience) claim to be represented either as a single string value or an array of strings. This enum provides a unified way to handle both formats.

§Variants

  • AsSet(HashSet<String>) - Represents multiple audience values as a set of strings
  • AsString(String) - Represents a single audience value as a string

§Example

use jwt_simple::prelude::*;
use std::collections::HashSet;

// Using a single audience
let claims = Claims::create(Duration::from_hours(2))
    .with_audience("https://api.example.com");

// Using multiple audiences
let mut audiences = HashSet::new();
audiences.insert("https://api.example.com".to_string());
audiences.insert("https://admin.example.com".to_string());
let claims = Claims::create(Duration::from_hours(2))
    .with_audiences(audiences);

Variants§

§

AsSet(HashSet<String>)

Multiple audience values stored as a set of strings

§

AsString(String)

Single audience value stored as a string

Implementations§

Source§

impl Audiences

Source

pub fn is_set(&self) -> bool

Returns true if the audiences are represented as a set.

This method checks whether the audience value is stored as a set of strings rather than a single string value.

§Example
use jwt_simple::prelude::*;
use std::collections::HashSet;

let mut audiences = HashSet::new();
audiences.insert("audience1".to_string());
audiences.insert("audience2".to_string());

let audience_set = Audiences::AsSet(audiences);
assert!(audience_set.is_set());
assert!(!audience_set.is_string());
Source

pub fn is_string(&self) -> bool

Returns true if the audiences are represented as a single string.

This method checks whether the audience value is stored as a single string rather than a set of strings.

§Example
use jwt_simple::prelude::*;

let audience_string = Audiences::AsString("audience1".to_string());
assert!(audience_string.is_string());
assert!(!audience_string.is_set());
Source

pub fn contains(&self, allowed_audiences: &HashSet<String>) -> bool

Returns true if the audiences include any of the allowed_audiences entries.

This method is used for audience verification during token validation to check if any of the allowed audiences matches the token’s audience claim.

  • For a string audience, it checks if the string exists in the allowed_audiences set
  • For a set of audiences, it checks if there’s any overlap with the allowed_audiences set
§Arguments
  • allowed_audiences - A set of allowed audience values to check against
§Example
use jwt_simple::prelude::*;
use std::collections::HashSet;

let mut allowed = HashSet::new();
allowed.insert("audience1".to_string());
allowed.insert("audience2".to_string());

// String audience
let audience_string = Audiences::AsString("audience1".to_string());
assert!(audience_string.contains(&allowed));

// Set of audiences with overlap
let mut audiences = HashSet::new();
audiences.insert("audience2".to_string());
audiences.insert("audience3".to_string());
let audience_set = Audiences::AsSet(audiences);
assert!(audience_set.contains(&allowed));
Source

pub fn into_set(self) -> HashSet<String>

Converts the audiences to a set of strings.

This method consumes the Audiences enum and returns a HashSet<String>:

  • If it’s already a set, returns the set directly
  • If it’s a string, wraps it in a singleton set (unless it’s empty)
§Example
use jwt_simple::prelude::*;
use std::collections::HashSet;

// From a string
let audience_string = Audiences::AsString("audience1".to_string());
let set = audience_string.into_set();
assert_eq!(set.len(), 1);
assert!(set.contains("audience1"));

// From a set
let mut original_set = HashSet::new();
original_set.insert("audience1".to_string());
original_set.insert("audience2".to_string());
let audience_set = Audiences::AsSet(original_set.clone());
let result_set = audience_set.into_set();
assert_eq!(result_set, original_set);
Source

pub fn into_string(self) -> Result<String, Error>

Converts the audiences to a single string value.

This method consumes the Audiences enum and attempts to return a single String:

  • If it’s already a string, returns the string directly
  • If it’s a set with 0 or 1 elements, returns the single element or an empty string
  • If it’s a set with more than 1 element, returns an error
§Errors

Returns JWTError::TooManyAudiences if the audiences are stored as a set with more than one element, since it cannot be unambiguously converted to a single string.

§Example
use jwt_simple::prelude::*;
use std::collections::HashSet;

// From a string - succeeds
let audience_string = Audiences::AsString("audience1".to_string());
let result = audience_string.into_string().unwrap();
assert_eq!(result, "audience1");

// From an empty set - succeeds with empty string
let audience_set = Audiences::AsSet(HashSet::new());
let result = audience_set.into_string().unwrap();
assert_eq!(result, "");

// From a set with one element - succeeds
let mut single_set = HashSet::new();
single_set.insert("audience1".to_string());
let audience_set = Audiences::AsSet(single_set);
let result = audience_set.into_string().unwrap();
assert_eq!(result, "audience1");

// From a set with multiple elements - fails
let mut multi_set = HashSet::new();
multi_set.insert("audience1".to_string());
multi_set.insert("audience2".to_string());
let audience_set = Audiences::AsSet(multi_set);
assert!(audience_set.into_string().is_err());

Trait Implementations§

Source§

impl Clone for Audiences

Source§

fn clone(&self) -> Audiences

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 Audiences

Source§

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

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

impl From<Audiences> for HashSet<String>

Implementation of From<Audiences> for HashSet<String>, allowing conversion to a set using the standard into() method.

This delegates to the into_set() method, which always succeeds.

Source§

fn from(audiences: Audiences) -> HashSet<String>

Converts to this type from the input type.
Source§

impl<T: ToString> From<T> for Audiences

Convenient conversion from any string-like type to Audiences.

This converts any type that implements ToString into an Audiences::AsString variant, making it simpler to create single-audience tokens.

§Example

use jwt_simple::prelude::*;

// String conversion
let audiences: Audiences = "https://api.example.com".into();
assert!(audiences.is_string());

// &str conversion
let audiences: Audiences = "https://api.example.com".into();
assert!(audiences.is_string());
Source§

fn from(audience: T) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Audiences

Source§

fn eq(&self, other: &Audiences) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryInto<String> for Audiences

Implementation of TryInto<String> for Audiences, allowing conversion to a string using the standard try_into() method.

This delegates to the into_string() method, which will return an error if there are multiple audience values that cannot be unambiguously converted to a single string.

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<String, Error>

Performs the conversion.
Source§

impl Eq for Audiences

Source§

impl StructuralPartialEq for Audiences

Auto Trait Implementations§

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

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V