A JWT Claims set contains the claims conveyed by a JSON Web Token as specified by RFC7519 Section 4.
A JWT Claims set can be validated in various ways using methods: isValid()
, ifValid(java.lang.Runnable)
... JWT claims validation is performed by a list of
JWTClaimsSetValidator
which can be set using validate(io.inverno.mod.security.jose.jwt.JWTClaimsSetValidator)
or setValidators(java.util.List)
methods. By default,
expiration time and not before time are validated.
- Since:
- 1.5
- Author:
- Jeremy Kuhn
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
JWTClaimsSet.Builder<A extends JWTClaimsSet,
B extends JWTClaimsSet.Builder<A, B>> A JWT Claims set builder is used to build a JWT Claims set.static interface
A JWT Claim represents a piece of information asserted about a subject. -
Constructor Summary
ModifierConstructorDescriptionCreates a JWT Claims set.JWTClaimsSet
(String iss, String sub, String aud, Long exp, Long nbf, Long iat, String jti, Map<String, Object> customClaims) Creates a JWT Claims set.protected
JWTClaimsSet
(String iss, URI iss_uri, String sub, URI sub_uri, String aud, ZonedDateTime exp, ZonedDateTime nbf, ZonedDateTime iat, String jti, Map<String, Object> customClaims) Creates a JWT Claims set. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returns the audience claim.final Optional
<JWTClaimsSet.Claim> getCustomClaim
(String name) Returns the custom claim with the specified name.Returns the map of custom claims.Returns the expiration time claim as seconds since epoch.Returns the expiration time claim as zoned date time.Returns the issued at claim as seconds since epoch.Returns the issued at claim as zoned date time.Returns the issuer claim as String.Returns the issuer claim as URI.getJWTId()
Returns the JWT id claim.Returns the not before claim as seconds since epoch.Returns the not before claim as zoned date time.Returns the subject claim as String.Returns the subject claim as URI.final List
<JWTClaimsSetValidator> Returns the list of JWT claims set validators.int
hashCode()
final JWTClaimsSet
Returns the JWT Claims set or throws an exception if it is invalid.<T extends Throwable>
JWTClaimsSetifInvalidThrow
(Supplier<? extends T> exceptionSupplier) Returns the JWT Claims set or throws a custom exception if it is invalid.final void
Executes the specified action if the JWT Claims set is valid.final void
ifValidOrElse
(Runnable action, Runnable invalidAction) Executes the specified action if the JWT Claims set is valid or the specified invalid action if it is invalid.final boolean
isValid()
Determines whether the JWT Claims set is valid.static JWTClaimsSet.Builder
<JWTClaimsSet, ?> of()
Creates a new empty JWT Claims set builder.static JWTClaimsSet.Builder
<JWTClaimsSet, ?> Creates a new JWT Claims set builder with the specified issuer and expiration time claims.final void
setValidators
(List<JWTClaimsSetValidator> validators) Sets the JWT claims set validators.protected void
validate()
Validates the JWT claims set and throws an exception if it is invalid.final JWTClaimsSet
validate
(JWTClaimsSetValidator validator) Adds the specified validator to the JWT claims set.
-
Constructor Details
-
JWTClaimsSet
public JWTClaimsSet(String iss, String sub, String aud, Long exp, Long nbf, Long iat, String jti) throws JWTBuildException Creates a JWT Claims set.
- Parameters:
iss
- the issuer claimsub
- the subject claimaud
- the audience claimexp
- the expiration time claimnbf
- the not before claimiat
- the issue at claimjti
- the JWT id claim- Throws:
JWTBuildException
- if there was an error building the JWT claims set
-
JWTClaimsSet
public JWTClaimsSet(String iss, String sub, String aud, Long exp, Long nbf, Long iat, String jti, Map<String, Object> customClaims) throws JWTBuildExceptionCreates a JWT Claims set.
- Parameters:
iss
- the issuer claimsub
- the subject claimaud
- the audience claimexp
- the expiration time claimnbf
- the not before claimiat
- the issue at claimjti
- the JWT id claimcustomClaims
- custom claims map- Throws:
JWTBuildException
- if there was an error building the JWT claims set
-
JWTClaimsSet
protected JWTClaimsSet(String iss, URI iss_uri, String sub, URI sub_uri, String aud, ZonedDateTime exp, ZonedDateTime nbf, ZonedDateTime iat, String jti, Map<String, Object> customClaims) throws JWTBuildExceptionCreates a JWT Claims set.
- Parameters:
iss
- the issuer claim as stringiss_uri
- the issuer claim as URIsub
- the subject claim as stringsub_uri
- the issuer claim as URIaud
- the audience claimexp
- the expiration time claimnbf
- the not before claimiat
- the issue at claimjti
- the JWT id claimcustomClaims
- custom claims map- Throws:
JWTBuildException
- if there was an error building the JWT claims set
-
-
Method Details
-
getIssuer
Returns the issuer claim as String.
- Returns:
- the issuer claim as String
-
getIssuerAsURI
Returns the issuer claim as URI.
- Returns:
- the issuer claim as URI
-
getSubject
Returns the subject claim as String.
- Returns:
- the subject claim as String
-
getSubjectAsURI
Returns the subject claim as URI.
- Returns:
- the subject claim as URI
-
getAudience
Returns the audience claim.
- Returns:
- the audience claim
-
getExpirationTime
Returns the expiration time claim as seconds since epoch.
- Returns:
- the expiration time claim as seconds since epoch
-
getExpirationTimeAsDateTime
Returns the expiration time claim as zoned date time.
- Returns:
- the expiration time claim as zoned date time
-
getNotBefore
Returns the not before claim as seconds since epoch.
- Returns:
- the not before claim as seconds since epoch
-
getNotBeforeAsDateTime
Returns the not before claim as zoned date time.
- Returns:
- the not before claim as zoned date time
-
getIssuedAt
Returns the issued at claim as seconds since epoch.
- Returns:
- the issued at claim as seconds since epoch
-
getIssuedAtAsDateTime
Returns the issued at claim as zoned date time.
- Returns:
- the issued at claim as zoned date time
-
getJWTId
Returns the JWT id claim.
- Returns:
- the JWT id claim
-
getCustomClaims
Returns the map of custom claims.
- Returns:
- a map of custom claims
-
getCustomClaim
Returns the custom claim with the specified name.
The returned
JWTClaimsSet.Claim
object allows to convert the claim into various types.- Parameters:
name
- a custom claim name- Returns:
- an optional containing the custom claim or an empty optional if the JWT claims set does not contain that claim
-
validate
Adds the specified validator to the JWT claims set.
- Parameters:
validator
- the validator to add- Returns:
- the JWT claims set
-
setValidators
Sets the JWT claims set validators.
- Parameters:
validators
- a list of validators or null to clear the validators
-
getValidators
Returns the list of JWT claims set validators.
- Returns:
- the JWT claims set validators
-
validate
Validates the JWT claims set and throws an exception if it is invalid.
This method basically invoke the list of validators that have been set using
validate(io.inverno.mod.security.jose.jwt.JWTClaimsSetValidator)
.- Throws:
InvalidJWTException
- if the JWT claims set is invalid
-
isValid
public final boolean isValid()Determines whether the JWT Claims set is valid.
- Returns:
- true if the JWT Claims set is valid, false otherwise
-
ifValid
Executes the specified action if the JWT Claims set is valid.
- Parameters:
action
- the action to run if the JWT Claims set is valid
-
ifValidOrElse
Executes the specified action if the JWT Claims set is valid or the specified invalid action if it is invalid.
- Parameters:
action
- the action to run if the JWT Claims set is validinvalidAction
- the action to run if the JWT Claims set is invalid
-
ifInvalidThrow
Returns the JWT Claims set or throws an exception if it is invalid.
- Returns:
- the JWT Claims set
- Throws:
InvalidJWTException
- if the JWT Claims set is invalid
-
ifInvalidThrow
public <T extends Throwable> JWTClaimsSet ifInvalidThrow(Supplier<? extends T> exceptionSupplier) throws T Returns the JWT Claims set or throws a custom exception if it is invalid.
- Type Parameters:
T
- the custom exception type- Parameters:
exceptionSupplier
- the exception supplier- Returns:
- the JWT Claims set
- Throws:
T
- if the JWT Claims set is invalid
-
of
Creates a new empty JWT Claims set builder.
- Returns:
- a new empty JWT Claims set builder
-
of
Creates a new JWT Claims set builder with the specified issuer and expiration time claims.
- Parameters:
iss
- the issuerexp
- the expiration time in seconds since epoch- Returns:
- a new JWT Claims set builder with the specified issuer and expiration time claims.
-
hashCode
public int hashCode() -
equals
-