Skip to main content

JwtValidator

Struct JwtValidator 

Source
pub struct JwtValidator { /* private fields */ }
Available on crate feature auth-jwt only.
Expand description

The set of claim checks applied after a JWT’s signature verifies.

Signature, exp, iss and aud are checked by default. A validator with no issuer refuses every token, as does one with no audience, unless allow_any_issuer or allow_any_audience says that is meant (audit SEC-06). Without them, a key set that signs for more than one issuer or application (a multi-tenant identity provider, a token minted for another API) let any of its tokens in. Until this release both checks were skipped when unset.

Implementations§

Source§

impl JwtValidator

Source

pub fn new() -> JwtValidator

Creates a validator that checks signature, exp, iss and aud. Name the accepted issuers and audiences, or opt out of either check: until then it refuses every token.

Source

pub fn require_scope(self, scope: impl Into<String>) -> JwtValidator

Requires every token to grant scope in its scope claim (RFC 9068 §2.2.3: space-separated; an array is accepted too). A valid token without it is refused with A2aError::insufficient_scope, 403 naming the scopes it lacks (spec §3.3.2: servers SHOULD indicate what scope is missing), not 401: the credential is good, it does not permit the call. A token that fails any other check is refused with 401 before its scopes are looked at, so nothing about the required scopes reaches a caller without a valid token.

Applies to every operation. For a scope per operation or per task, write an interceptor that reads the method and CallContext::task_id.

Source

pub fn with_issuer(self, issuer: impl Into<String>) -> JwtValidator

Requires the token’s iss to equal one of the accepted issuers.

Source

pub fn with_audience(self, audience: impl Into<String>) -> JwtValidator

Requires the token’s aud to contain one of the accepted audiences.

Source

pub fn with_required_typ(self, typ: impl AsRef<str>) -> JwtValidator

Requires every token’s typ header to be typ, so that a token issued for another purpose (an ID token, say) cannot be presented as this one (RFC 8725 section 3.11). For access tokens that is at+jwt (RFC 9068 section 2.1).

typ is a media type, so the comparison ignores ASCII case and an application/ prefix on either side (RFC 7515 section 4.1.9): at+jwt admits AT+JWT and application/at+jwt. A token with no typ, or one that is not a string, is refused. Without this call typ is not looked at, as before, because many issuers still sign access tokens with typ: JWT or none.

Source

pub const fn allow_any_issuer(self) -> JwtValidator

Accepts a token whatever its iss when no issuer is set. For a key set only one issuer signs with (an HS256 secret you hold, keys you pinned); a remote key set should name its issuer instead.

Source

pub const fn allow_any_audience(self) -> JwtValidator

Accepts a token whatever its aud when no audience is set. A token minted for another API by the same issuer is then accepted here.

Source

pub const fn with_leeway(self, leeway: Duration) -> JwtValidator

Sets the clock-skew leeway for exp/nbf (default 60 s).

Source

pub const fn allow_missing_exp(self) -> JwtValidator

Allows tokens without an exp claim (default: exp is required).

Source

pub fn with_hs256_secret(self, secret: impl Into<Vec<u8>>) -> JwtValidator

Sets a shared secret for verifying HS256 tokens.

HS256 is only ever checked against this secret — never against a JWKS public key — which is what makes the RS256→HS256 confusion attack impossible.

The secret must be at least MIN_HS256_SECRET_LEN (32) bytes: RFC 7518 §3.2 requires an HS256 key at least as long as the hash output. A shorter one fails closed — it is not stored, an error is logged, and every HS256 token is rejected — because a guessable HMAC key lets anyone who guesses it mint tokens this validator accepts. Use try_with_hs256_secret to refuse such a secret at startup instead.

The secret is a key, not a password (RFC 8725 §3.5): at least 32 random bytes from a secret store, for instance what openssl rand -base64 32 writes, decoded. Only the length is checked: a passphrase of 32 or more characters is accepted as it stands, and anyone who guesses it can mint tokens this validator accepts.

Source

pub fn try_with_hs256_secret( self, secret: impl Into<Vec<u8>>, ) -> Result<JwtValidator, A2aError>

Sets a shared secret for verifying HS256 tokens, refusing one shorter than MIN_HS256_SECRET_LEN bytes.

§Errors

Returns an A2aError when the secret is shorter than MIN_HS256_SECRET_LEN bytes (RFC 7518 §3.2).

Trait Implementations§

Source§

impl Clone for JwtValidator

Source§

fn clone(&self) -> JwtValidator

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for JwtValidator

Source§

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

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

impl Default for JwtValidator

Source§

fn default() -> JwtValidator

Returns the “default value” for a type. Read more

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
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self> ⓘ

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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 more
§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more