pub struct JwtValidator { /* private fields */ }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
impl JwtValidator
Sourcepub fn new() -> JwtValidator
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.
Sourcepub fn require_scope(self, scope: impl Into<String>) -> JwtValidator
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.
Sourcepub fn with_issuer(self, issuer: impl Into<String>) -> JwtValidator
pub fn with_issuer(self, issuer: impl Into<String>) -> JwtValidator
Requires the token’s iss to equal one of the accepted issuers.
Sourcepub fn with_audience(self, audience: impl Into<String>) -> JwtValidator
pub fn with_audience(self, audience: impl Into<String>) -> JwtValidator
Requires the token’s aud to contain one of the accepted audiences.
Sourcepub fn with_required_typ(self, typ: impl AsRef<str>) -> JwtValidator
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.
Sourcepub const fn allow_any_issuer(self) -> JwtValidator
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.
Sourcepub const fn allow_any_audience(self) -> JwtValidator
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.
Sourcepub const fn with_leeway(self, leeway: Duration) -> JwtValidator
pub const fn with_leeway(self, leeway: Duration) -> JwtValidator
Sets the clock-skew leeway for exp/nbf (default 60 s).
Sourcepub const fn allow_missing_exp(self) -> JwtValidator
pub const fn allow_missing_exp(self) -> JwtValidator
Allows tokens without an exp claim (default: exp is required).
Sourcepub fn with_hs256_secret(self, secret: impl Into<Vec<u8>>) -> JwtValidator
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.
Sourcepub fn try_with_hs256_secret(
self,
secret: impl Into<Vec<u8>>,
) -> Result<JwtValidator, A2aError>
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
impl Clone for JwtValidator
Source§fn clone(&self) -> JwtValidator
fn clone(&self) -> JwtValidator
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for JwtValidator
impl Debug for JwtValidator
Source§impl Default for JwtValidator
impl Default for JwtValidator
Source§fn default() -> JwtValidator
fn default() -> JwtValidator
Auto Trait Implementations§
impl Forget for JwtValidator
impl Freeze for JwtValidator
impl Move for JwtValidator
impl RefUnwindSafe for JwtValidator
impl Send for JwtValidator
impl Sync for JwtValidator
impl Unpin for JwtValidator
impl UnsafeUnpin for JwtValidator
impl UnwindSafe for JwtValidator
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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 more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].