The version 4 release contains several improvements:
- Support for
java.time.Instantwhen creating or verifying JWTs with Numeric Date claim values. - Improvements to JWT claim validation, including support for custom claim validation using Predicates.
- Improved exception handling when validating JWTs, to better inform of the reason for failed validation.
- Consistent handling of
nullclaim values both when creating and validation JWTs.
This guide captures the changes you should be aware of when planning and upgrading to version 4.
Classes or methods removed:
- The
implpackage has been removed as an export inmodule-info.java. This package contains implementation-specific code that may change at any point. - Support for the ES256K algorithm has been removed, as it is disabled in Java 15+. The
Algorithm#ECDSA256K(ECDSAKeyProvider keyProvider)andAlgorithm#ECDSA256K(ECPublicKey publicKey, ECPrivateKey privateKey)methods have been removed. com.auth0.jwt.interfaces.Clockhas been removed. Instead, an implementation ofjava.time.Clockcan be passed to theBaseVerificationfor testing purposes.com.auth0.jwt.impl.NullClaimhas been removed.Claim#isNullcan be used to determine if a claim's value isnull.com.auth0.jwt.impl.PublicClaimswas removed, and replaced bycom.auth0.jwt.RegisteredClaimsandcom.auth0.jwt.HeaderParams.com.auth0.jwt.interfaces.Verification#withAnyOfAudienceno longer provides a default implementation.
- All date/time claim values are now serialized as seconds since the epoch, in both the payload and header. In version 3, date/time claims nested in a list or map, as well as any header parameters with date/time values, were serialized as milliseconds since the epoch.
- When creating a JWT, passing
nullas the value no longer removes the claim if it was previously added to the builder. It now adds the claim with anullvalue.
- In version 3, specifying multiple claim expectations for the same claim name would override any previous expectations for that claim. In version 4, all expectations for that claim will be validated.
- In version 3, passing
nullfor the value of a claim expectation would remove that expectation from the validation. In version 4, passingnulldoes not remove that expectation, but instead validates that the claim has the literal valuenull. - When validating a JWT, if an expected claim is present in the JWT but contains a value different from the one expected, an
IncorrectClaimException(subclass ofInvalidClaimException) will now be thrown instead of anInvalidClaimException. - When validating a JWT, if an expected claim is not present in the JWT, an
MissingClaimException(subclass ofInvalidClaimException) will now be thrown instead of anInvalidClaimException. withClaimPresence(String claimName)now validates that the claim is present in the JWT, and a claim with anullvalue is considered present. Previously, a claim with a value ofnullwould be considered as missing and fail the validation.- When validating a date/time claim value, the validation no longer checks for strict equality of the claim's value and the provided
Date(orInstant). Instead, the expectedDateorInstantwill be compared to the claim's value only considering seconds (because JWT date/time claims are represented as seconds since the epoch).
com.auth0.jwt.interfaces.Claim#isNull()now returns true only if the claim is present and its value isnull. Previously, it returned true if the claim was present and its value wasnull, or if the claim was not present in the JWT. To check if the claim is present or not in the JWT, useisMissing().
This class extends InvalidClaimException and represents that when validating a JWT, an expected claim exists in the JWT but does not match the expected value.
This class extends InvalidClaimException and represents that when validating a JWT, an expected claim is missing from the JWT.
This class contains constants representing common header parameter names.
This class contains constants representing the registered claim names.
JWTCreator.Builder#withExpiresAt(Instant expiresAt)- adds theexpclaim to the JWT from ajava.time.Instant.JWTCreator.Builder#withNotBefore(Instant notBefore)- adds thenbfclaim to the JWT from ajava.time.Instant.JWTCreator.Builder#withIssuedAt(Instant issuedAt)- adds theiatclaim to the JWT from ajava.time.Instant.JWTCreator.Builder#withClaim(String claimName, Instant value)- adds a claim to the JWT from ajava.time.Instant.JWTCreator.Builder#withNullClaim(String claimName)- adds a claim to the JWT with the literal valuenull.
Instant getExpiresAtAsInstant()- Returns a JWT'sexpclaim as ajava.time.Instant.Instant getNotBeforeAsInstant()- Returns a JWT'snbfclaim as ajava.time.Instant.Instant getIssuedAtAsInstant()- Returns a JWT'siatclaim as ajava.time.Instant.
Instant asInstant()- Gets a claim as ajava.time.Instant.boolean isMissing()- Returns whether the claim is present or not.
Verification withClaim(String name, Instant value)- Adds an expectation that a claim with the provided name has a value equal to the providedjava.time.Instant.Verification withClaim(String name, BiPredicate<Claim, DecodedJWT> predicate)- Allows for a claim to be validated with the supplied predicate.Verification withNullClaim(String name)- Adds an expectation that a claim with the provided name has a value equal to the literalnull.