Skip to main content

JwtAuthInterceptor

Struct JwtAuthInterceptor 

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

A ServerInterceptor that authenticates requests with a signed JWT.

Reads Authorization: Bearer <jwt>, validates it with the configured JwtValidator, and rejects the request (generically) on any failure.

Implementations§

Source§

impl JwtAuthInterceptor

Source

pub fn new(validator: JwtValidator, jwks: Jwks) -> JwtAuthInterceptor

Creates an interceptor with a fixed key set.

Use an empty Jwks when validating HS256-only (the secret lives on the validator).

Source

pub fn from_jwks_url( validator: JwtValidator, jwks_url: impl Into<String>, ) -> JwtAuthInterceptor

Creates an interceptor that fetches keys from a JWKS URL, caching them for ttl (default 1 hour) and refetching once on a key-id miss (key rotation), at most once per refetch interval.

The keys are not fetched here — the first request triggers the fetch.

jwks_url must be https://; http:// is refused (every token is, with the reason logged) except to a loopback host (localhost or a loopback address; not a name under .localhost, which can reach DNS), since whoever can change the keys in transit can sign any identity (audit SEC-06, NEW-b-5).

Source

pub fn from_jwks_url_with_tls_config( validator: JwtValidator, jwks_url: impl Into<String>, tls_config: ClientConfig, ) -> JwtAuthInterceptor

Available on crate feature tls-rustls only.

Like from_jwks_url, but with a caller-supplied rustls ClientConfig for the JWKS fetch — for identity providers behind a private CA, where the default webpki-roots trust store cannot verify the JWKS endpoint.

Source

pub async fn from_oidc_issuer( issuer: &str, validator: JwtValidator, ) -> Result<JwtAuthInterceptor, A2aError>

Discovers the issuer’s jwks_uri via OIDC discovery ({issuer}/.well-known/openid-configuration) and builds a remote-JWKS interceptor for it.

The document’s issuer must be issuer (OpenID Connect Discovery 1.0 §4.3; a trailing / on either is ignored), and a validator that names no issuer is bound to the document’s, the value its tokens carry, so a token from another issuer that shares these keys (another tenant of a multi-tenant identity provider) is refused (audit SEC-06). issuer and the jwks_uri must be https://, except to a loopback host.

§Errors

Returns an A2aError when discovery fails, the document names another issuer or has no jwks_uri, or a URL is plaintext.

Source

pub async fn from_oidc_issuer_with_tls_config( issuer: &str, validator: JwtValidator, tls_config: ClientConfig, ) -> Result<JwtAuthInterceptor, A2aError>

Available on crate feature tls-rustls only.

Like from_oidc_issuer, verifying the identity provider (the discovery document and then the keys) against tls_config instead of the default roots: an issuer behind a private CA, or one that wants a client certificate. Until 2026-10-31 such an issuer could not be discovered at all (audit B-04).

§Errors

As from_oidc_issuer.

Source

pub fn with_jwks_ttl(self, ttl: Duration) -> JwtAuthInterceptor

Sets the remote-JWKS cache TTL. No-op for a static key set.

Source

pub fn with_jwks_refetch_interval( self, interval: Duration, ) -> JwtAuthInterceptor

Sets the least time between one fetch of the key set and the refetch a token whose key id is not in it asks for. Default: DEFAULT_JWKS_REFETCH_INTERVAL (10 s). No-op for a static key set.

A token naming an unknown key id is refused, without a fetch, while the last fetch began less than this long ago; requests that arrive while a refetch is under way wait for it and use what it fetched. Until 2026-10-30 every such token, unsigned and unauthenticated, forced a fetch of its own, so anyone could make the agent send the identity provider one request per request it received (audit SEC-04). An identity provider publishes a new key before it signs with it, so a key rotation is seen at the first refetch after the key appears.

Source

pub fn with_jwks_fetch_budget(self, budget: Duration) -> JwtAuthInterceptor

Bounds how long one request waits for keys, queueing behind another request’s fetch included, and each fetch. Default DEFAULT_JWKS_FETCH_BUDGET (30 s). A request whose wait runs out is refused, as every request is while the keys cannot be had. No-op for a static key set.

Trait Implementations§

Source§

impl Debug for JwtAuthInterceptor

Source§

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

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

impl ServerInterceptor for JwtAuthInterceptor

Source§

fn checks_scheme(&self, scheme: &SecurityScheme) -> Option<bool>

A bearer JWT: the HTTP bearer scheme, or an OAuth 2.0 or OpenID Connect scheme, whose access tokens this validates.

Source§

fn before<'a>( &'a self, ctx: &'a CallContext, ) -> Pin<Box<dyn Future<Output = Result<(), A2aError>> + Send + 'a>>

Called before the request handler processes the method call. Read more
Source§

fn after<'a>( &'a self, _ctx: &'a CallContext, ) -> Pin<Box<dyn Future<Output = Result<(), A2aError>> + Send + 'a>>

Called after the request handler has succeeded, before its response is returned. Read more
Source§

fn authenticates(&self) -> bool

Returns true if this interceptor authenticates requests — i.e. its before hook rejects callers that do not present valid credentials. Read more
Source§

fn on_complete<'a>( &'a self, ctx: &'a CallContext, outcome: CallOutcome<'a>, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>

Called once when a call ends, however it ends — with the response, with an error, or by being dropped unanswered — on every interceptor whose before was called for it. 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

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, 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