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 stringsAsString(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
impl Audiences
Sourcepub fn is_set(&self) -> bool
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());Sourcepub fn is_string(&self) -> bool
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());Sourcepub fn contains(&self, allowed_audiences: &HashSet<String>) -> bool
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_audiencesset - For a set of audiences, it checks if there’s any overlap with the
allowed_audiencesset
§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));Sourcepub fn into_set(self) -> HashSet<String>
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);Sourcepub fn into_string(self) -> Result<String, Error>
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 From<Audiences> for HashSet<String>
Implementation of From<Audiences> for HashSet<String>, allowing conversion to a set
using the standard into() method.
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§impl<T: ToString> From<T> for Audiences
Convenient conversion from any string-like type to Audiences.
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§impl TryInto<String> for Audiences
Implementation of TryInto<String> for Audiences, allowing conversion to a string
using the standard try_into() method.
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.