Class JwtAuthenticator.Builder
java.lang.Object
com.jsgalactic.axiom.security.jwt.JwtAuthenticator.Builder
- Enclosing class:
JwtAuthenticator
Configures a
JwtAuthenticator. Not thread-safe; build once at startup.-
Method Summary
Modifier and TypeMethodDescriptionMaps verified claims to identity attributes with application code, for example to flatten a nested or array claim.Accepts tokens whoseaudcontains this value.build()Builds the immutable authenticator.Sets the clock for time-based claims, for tests; the system UTC clock by default.Sets the tolerated clock difference forexp,nbfandiat.exposeClaims(String... names) Exposes verified claims as identity attributes under the same names.hmacKey(JwsAlgorithm algorithm, byte[] secret) Registers an HMAC secret used for tokens without akid.hmacKey(String keyId, JwsAlgorithm algorithm, byte[] secret) Registers an HMAC secret for tokens whosekidis the key ID.Accepts tokens whoseissequals this value.jwks(JwksSource source) Takes further verification keys from the issuer's JSON Web Key Set, with default options.jwks(JwksSource source, JwksOptions options) Takes further verification keys from the issuer's JSON Web Key Set, which the application configures and the token cannot influence.maxTokenLength(int characters) Sets the longest accepted token, checked before decoding.permissionsClaim(String claim) Names the claim holding permissions (scopeby default).publicKey(JwsAlgorithm algorithm, PublicKey key) Registers a public key used for tokens without akid.publicKey(String keyId, JwsAlgorithm algorithm, PublicKey key) Registers a public key for tokens whosekidis the key ID.Sets the realm of theWWW-Authenticatechallenge (apiby default).rolesClaim(String claim) Names the claim holding roles (rolesby default).tokenCheck(TokenCheck check) Adds a check that runs after signature, time, issuer, audience and subject verification, for revocation orjtireplay detection.
-
Method Details
-
hmacKey
Registers an HMAC secret used for tokens without akid.- Parameters:
algorithm- HS256, HS384 or HS512secret- at least 32, 48 or 64 bytes respectively; copied- Returns:
- this builder
- Throws:
IllegalArgumentException- for a non-HMAC algorithm, a short secret, or a second key without key ID for the algorithm
-
hmacKey
Registers an HMAC secret for tokens whosekidis the key ID.- Parameters:
keyId- key ID, unique within this builderalgorithm- HS256, HS384 or HS512secret- at least 32, 48 or 64 bytes respectively; copied- Returns:
- this builder
- Throws:
IllegalArgumentException- for a non-HMAC algorithm, a short secret or a duplicate key ID
-
publicKey
Registers a public key used for tokens without akid.- Parameters:
algorithm- an RS, PS, ES or EdDSA algorithm matching the keykey- RSA key of at least 2048 bits (RS and PS), EC key on the algorithm's curve (ES) or Ed25519 key (EdDSA)- Returns:
- this builder
- Throws:
IllegalArgumentException- if the key does not fit the algorithm, or for a second key without key ID for the algorithm
-
publicKey
Registers a public key for tokens whosekidis the key ID.- Parameters:
keyId- key ID, unique within this builderalgorithm- an RS, PS, ES or EdDSA algorithm matching the keykey- RSA key of at least 2048 bits (RS and PS), EC key on the algorithm's curve (ES) or Ed25519 key (EdDSA)- Returns:
- this builder
- Throws:
IllegalArgumentException- if the key does not fit the algorithm or for a duplicate key ID
-
jwks
Takes further verification keys from the issuer's JSON Web Key Set, with default options. Seejwks(JwksSource, JwksOptions).- Parameters:
source- where the key set comes from, for exampleJwksSource.url(java.net.URI)- Returns:
- this builder
- Throws:
IllegalStateException- if a key set source was already configured
-
jwks
Takes further verification keys from the issuer's JSON Web Key Set, which the application configures and the token cannot influence. The set is fetched on first use, refreshed when older thanJwksOptions.refreshInterval(java.time.Duration), and refetched when a token names an unknownkid, at most once perJwksOptions.minRefreshInterval(java.time.Duration), so key rotation needs no restart and unknown key IDs cannot make the server fetch in a loop. A failed fetch keeps the last good set untilJwksOptions.maxStale(java.time.Duration)has passed.Keys of the set are bound to their declared
algexactly like statically registered keys and pass the same size and curve checks. Statically registered keys with the same key ID win over keys of the set. Tokens without akidare verified only with statically registered keys.- Parameters:
source- where the key set comes fromoptions- bounds and timings- Returns:
- this builder
- Throws:
IllegalStateException- if a key set source was already configuredIllegalArgumentException- if the options contradict each other
-
issuer
Accepts tokens whoseissequals this value. Call again to accept several issuers.- Parameters:
issuer- exact issuer- Returns:
- this builder
-
audience
Accepts tokens whoseaudcontains this value. Call again to accept several.- Parameters:
audience- exact audience, usually this API's identifier- Returns:
- this builder
-
clockSkew
Sets the tolerated clock difference forexp,nbfandiat.- Parameters:
skew- zero to five minutes, whole seconds; 30 seconds by default- Returns:
- this builder
-
maxTokenLength
Sets the longest accepted token, checked before decoding.- Parameters:
characters- 256 to 65,536; 8,192 by default- Returns:
- this builder
-
rolesClaim
Names the claim holding roles (rolesby default).- Parameters:
claim- claim name- Returns:
- this builder
-
permissionsClaim
Names the claim holding permissions (scopeby default).- Parameters:
claim- claim name- Returns:
- this builder
-
exposeClaims
Exposes verified claims as identity attributes under the same names. Only string, number and boolean claims are exposed, as text; a claim that is absent or JSONnullis skipped, and one that is an array or object makes the token invalid. Values over 2,048 characters make the token invalid too. Only claims listed here are exposed; every other claim stays unavailable to handlers.- Parameters:
names- claim names, at most 64 in total with the other attribute settings- Returns:
- this builder
-
attributes
Maps verified claims to identity attributes with application code, for example to flatten a nested or array claim. Runs after the token checks accepted the token; its entries are added to those ofexposeClaims(java.lang.String...)and replace same-named ones. AnIllegalArgumentExceptionfrom the function, or an attribute thatSecurityIdentityrefuses, makes the token invalid; other exceptions propagate.- Parameters:
mapper- thread-safe function from verified claims to attribute names and values- Returns:
- this builder
-
tokenCheck
Adds a check that runs after signature, time, issuer, audience and subject verification, for revocation orjtireplay detection. SeeTokenCheckfor the outcomes. Checks run in registration order.- Parameters:
check- thread-safe check- Returns:
- this builder
-
realm
Sets the realm of theWWW-Authenticatechallenge (apiby default).- Parameters:
realm- 1 to 64 letters, digits, spaces and. _ : / -- Returns:
- this builder
-
clock
Sets the clock for time-based claims, for tests; the system UTC clock by default.- Parameters:
clock- clock- Returns:
- this builder
-
build
Builds the immutable authenticator.- Returns:
- the authenticator
- Throws:
IllegalStateException- without a key, an issuer or an audience
-