pub struct Validator { /* private fields */ }Expand description
A helper type for validating JSON Web Tokens (JWTs) against multiple issuers and algorithms.
Validator manages a collection of public keys and associated validation rules
(wrapped in Endpoint structs) that can be used to decode and verify tokens.
It supports loading keys dynamically from JWKS endpoints or inserting them manually.
Typically used when your application needs to accept tokens from multiple providers (e.g., multiple OpenID Connect issuers) or support multiple signing algorithms.
Implementations§
Source§impl Validator
impl Validator
Sourcepub fn from_pubkey(
url: String,
audiance: String,
algorithm: String,
public_key: DecodingKey,
) -> Result<Self, Box<dyn Error>>
pub fn from_pubkey( url: String, audiance: String, algorithm: String, public_key: DecodingKey, ) -> Result<Self, Box<dyn Error>>
Creates a new Validator from a single public key.
This is useful when you already have a known key (for example, configured statically) and want to build a validator around it.
url- Issuer URL.audiance- Expected audience claim (usually your client ID).algorithm- Signing algorithm (e.g., “RS256”).public_key- Decoding key used to verify signatures.
Sourcepub fn from_rsa_pem(
url: String,
audiance: String,
algorithm: String,
pem: &str,
) -> Result<Self, Box<dyn Error>>
pub fn from_rsa_pem( url: String, audiance: String, algorithm: String, pem: &str, ) -> Result<Self, Box<dyn Error>>
Creates a new Validator from an RSA PEM encoded public key.
This is a convenience wrapper around from_pubkey that accepts a PEM string
(PKCS#1 / PKCS#8 public key) and builds the DecodingKey for you.
url- Issuer URL (used to construct the KeyID).audiance- Expected audience claim.algorithm- Signing algorithm (e.g., “RS256”).pem- RSA public key in PEM format.
Sourcepub fn get_supported_algorithms_for_issuer(
&self,
issuer: &str,
) -> Option<Vec<String>>
pub fn get_supported_algorithms_for_issuer( &self, issuer: &str, ) -> Option<Vec<String>>
Returns a sorted list of unique algorithms supported for the given issuer, based on the pubkeys map.
pub fn empty() -> Self
Sourcepub async fn new(
validation: Validation,
provider_metadata: &CoreProviderMetadata,
issuer_url: String,
) -> Result<Self, Box<dyn Error>>
pub async fn new( validation: Validation, provider_metadata: &CoreProviderMetadata, issuer_url: String, ) -> Result<Self, Box<dyn Error>>
Loads public keys dynamically from a JWKS endpoint discovered from provider metadata.
Fetches the JWKS, parses it, and builds validation rules for each key.
validation- Template validation rules to apply for each key.provider_metadata- OpenID Connect provider metadata (must include JWKS URI).issuer_url- The expected issuer URL.
Sourcepub async fn extend_from_oidc(
&mut self,
issuer_url: &str,
audiance: &str,
default_algorithm: &str,
) -> Result<(), Box<dyn Error>>
pub async fn extend_from_oidc( &mut self, issuer_url: &str, audiance: &str, default_algorithm: &str, ) -> Result<(), Box<dyn Error>>
Extends the validator by dynamically discovering and importing public keys (JWKS) from the OpenID Connect (OIDC) discovery endpoint of the given issuer.
This method performs the following steps:
- Initializes an HTTP client with redirect-following disabled for security reasons.
- Fetches the OpenID Connect provider metadata from the issuer’s well-known discovery endpoint.
- Retrieves the JWKS (JSON Web Key Set) URI from the provider metadata.
- Downloads the JWKS document and parses the keys.
- Inserts the discovered keys into the validator’s
pubkeysmap, associating them with the issuer.
§Parameters
issuer_url: The base URL of the OIDC issuer (e.g.,https://accounts.example.com).validation: The validation params used for this endpoint (make sure iss, aud, alg are set correctly)
§Returns
Ok(())if the keys were successfully fetched and added.Err(Box<dyn std::error::Error>)if any network, parsing, or validation step fails.
§Errors
Returns an error if:
- The HTTP client could not be created.
- The issuer URL is invalid.
- The provider metadata discovery fails.
- The JWKS document cannot be fetched or parsed.
§Security
- Redirects are explicitly disabled to prevent SSRF attacks when contacting the discovery endpoint.
§Example
use rocket_oidc::client::Validator;
let mut validator = Validator::empty();
validator.extend_from_oidc("https://accounts.example.com").await?;Sourcepub fn extend_from_jwks(
&mut self,
issuer_url: &str,
jwks_json: &str,
validation: Validation,
) -> Result<(), Box<dyn Error>>
pub fn extend_from_jwks( &mut self, issuer_url: &str, jwks_json: &str, validation: Validation, ) -> Result<(), Box<dyn Error>>
Extends the validator by parsing and adding public keys from a JWKS JSON document associated with the given issuer.
§Parameters
issuer_url: The base URL of the OIDC issuer (e.g.,https://accounts.example.com).jwks_json: The raw JWKS JSON string.
§Returns
Ok(())if the keys were successfully parsed and added.Err(crate::Error)if parsing fails.
§Example
let mut validator = Validator::empty();
let jwks_json = std::fs::read_to_string("keys.json")?;
validator.extend_from_jwks("https://accounts.example.com", &jwks_json)?;Sourcepub fn insert_endpoint(&mut self, keyid: KeyID, endpoint: Endpoint)
pub fn insert_endpoint(&mut self, keyid: KeyID, endpoint: Endpoint)
Inserts a new validation endpoint directly by its KeyID.
Useful when you already constructed an Endpoint yourself.
Sourcepub fn insert_pubkey(
&mut self,
url: String,
audiance: String,
algorithm: String,
public_key: DecodingKey,
) -> Result<(), Box<dyn Error>>
pub fn insert_pubkey( &mut self, url: String, audiance: String, algorithm: String, public_key: DecodingKey, ) -> Result<(), Box<dyn Error>>
Inserts a new public key and automatically builds its validation rules.
url- Issuer URL.audiance- Expected audience claim.algorithm- Signing algorithm.public_key- Decoding key.
Sourcepub fn decode_with_iss_alg<T: Serialize + Debug + DeserializeOwned + Send + CoreClaims + Clone>(
&self,
issuer: &str,
algorithm: &str,
access_token: &str,
) -> Result<TokenData<T>, Error>
pub fn decode_with_iss_alg<T: Serialize + Debug + DeserializeOwned + Send + CoreClaims + Clone>( &self, issuer: &str, algorithm: &str, access_token: &str, ) -> Result<TokenData<T>, Error>
Decodes and validates an access token for a specific issuer and algorithm.
issuer- Issuer URL.algorithm- Signing algorithm (e.g., “RS256”).access_token- The JWT string to decode.
Returns the token’s claims if valid, or an error otherwise.
Sourcepub fn decode<T: Serialize + Debug + DeserializeOwned + Send + CoreClaims + Clone>(
&self,
access_token: &str,
) -> Result<TokenData<T>, Error>
👎Deprecated
pub fn decode<T: Serialize + Debug + DeserializeOwned + Send + CoreClaims + Clone>( &self, access_token: &str, ) -> Result<TokenData<T>, Error>
Decodes and validates an access token using the default issuer and a default algorithm (“RS256”).
⚠️ Deprecated: May not be correct if you handle multiple issuers or algorithms.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Validator
impl RefUnwindSafe for Validator
impl Send for Validator
impl Sync for Validator
impl Unpin for Validator
impl UnwindSafe for Validator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);