Class JwtValidator
java.lang.Object
com.revetsec.jose.JwtValidator
Validates JWTs in the JWS compact serialization (RFC 7519, RFC 7515) signed with a public key, for one expected
issuer.
The validateResult(String) operation runs the checks in a fixed order and stops at the first failure:
- the token's length, before it is examined any further;
- the compact serialization and the canonical base64url of every segment;
- the JOSE header: a strict JSON object whose
algis an allowed algorithm (compared exactly, sononein any case fails here); then nocrit,b64orzip; then no key or key location in the header (jwk,jkuandx5uare rejected and never fetched); then an allowedtyp; then nocty; then akid, when present, that is a string of 1 to 256 characters; - the signature's length and form for the algorithm, before any key is looked up, so a malformed signature never causes a key set fetch;
- the key, selected from the
JwksSourceby the header'skidand the algorithm; a remote source may fetch or refresh its key set here, on the calling thread; - the signature, over the header and payload exactly as received;
- the claims: a strict JSON object whose registered claims have the right types;
issequal to the expected issuer; the key's JWKissuermember, when it has one, bound toiss; an expected audience inaud;exp, theniat, thennbfagainst the clock, with the clock skew; the required claims; and nocnf, because this validator does not check proof of possession.
JwtValidationResult.Rejected whose JoseException.Reason names the failed
check. A remote source that cannot supply its key set throws a JwksUnavailableException, unchanged.
Neither outcome nor exception diagnostics contain the token, a claim or a key ID.
Instances are immutable and safe for concurrent use. JwtValidator.Builder.build() does no I/O, even over a remote key
source. Validators compare by reference.
- Since:
- 1.0.0
- Author:
- Mark Allen
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptiontoString()Describes this validator's configuration.validateResult(@NonNull String compactSerialization) Validates once and returns a success or a fixed local input rejection.static @NonNull JwtValidator.BuilderwithIssuer(@NonNull String issuer) Starts building a validator for tokens fromissuer.
-
Method Details
-
withIssuer
Starts building a validator for tokens fromissuer.- Parameters:
issuer- the expectediss, compared exactly, code point by code point, with no normalization: a trailing slash or a difference in case is another issuer- Returns:
- a new builder
- Throws:
NullPointerException- ifissuerisnullIllegalArgumentException- ifissueris empty- Since:
- 1.0.0
-
validateResult
@CheckReturnValue public @NonNull JwtValidationResult validateResult(@NonNull String compactSerialization) Validates once and returns a success or a fixed local input rejection. Remote key-source failures remain exceptions; no rejected JWT, claims or credential are exposed. See the class description for the checks and their order.Over a
RemoteJwksSource, this may fetch the key set on the calling thread, or wait for another caller's fetch, for up to the source's request timeout.- Parameters:
compactSerialization- untrusted compact token- Returns:
- the validation outcome
- Throws:
NullPointerException- if the token is nullJwksUnavailableException- if remote keys are unavailable- Since:
- 1.0.0
-
toString
-