Class AbstractTokenBuilder
java.lang.Object
uk.co.spudsoft.jwtvalidatorvertx.impl.AbstractTokenBuilder
- All Implemented Interfaces:
TokenBuilder
- Direct Known Subclasses:
JdkTokenBuilder
Abstract implementation of TokenBuilder.
The actual creation of keys is left to a subclass to implement.
This class can perform all the work of a TokenBuilder implementation apart from the generation of keys, however most methods are designed to be overrideable so that a specific implementation can do something different if that is useful.
- Author:
- jtalbut
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Base64.Encoder
Base64 encoded that implementations may (should) use.protected final com.google.common.cache.Cache
<String, AlgorithmAndKeyPair> The key cache that is shared with theJwksHandler
.protected static final SecureRandom
Secure random number generator that implementations may use. -
Constructor Summary
ConstructorsConstructorDescriptionAbstractTokenBuilder
(com.google.common.cache.Cache<String, AlgorithmAndKeyPair> keyCache) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected String
base64Claims
(io.vertx.core.json.JsonObject claims) Helper method to convert the payload to base64, possibly breaking it.protected String
base64Header
(io.vertx.core.json.JsonObject header) Helper method to convert the header to base64, possibly breaking it.protected String
base64JSon
(boolean notJson, boolean brokenBase64, io.vertx.core.json.JsonObject json) Helper method to convert a JsonObject into a base64 representation.protected String
base64Signature
(byte[] signature) Helper method to base6t4 encode the signature, possibly breaking it.buildToken
(JsonWebAlgorithm jwa, String kid, String iss, String sub, List<String> aud, Long nbf, Long exp, Map<String, Object> otherClaims) Construct a JWT.protected String
constructToken
(String headerBase64, String claimsBase64, String signatureBase64) Helper method to concatenate the three parts of the token.protected io.vertx.core.json.JsonObject
generateClaimsNode
(String iss, String sub, Long exp, Long nbf, List<String> aud, Map<String, Object> otherClaims) Helper method to build the payload for a token.protected io.vertx.core.json.JsonObject
generateHeaderNode
(String kid, JsonWebAlgorithm algorithm) Helper method to generate the token header node.protected abstract byte[]
generateSignature
(String kid, JsonWebAlgorithm algorithm, String headerBase64, String claimsBase64) Sign the token header and claims using the specified key.setHeaderNotJson
(boolean headerNotJson) If set the header will not be valid base 64.setHeaderNotValidBase64
(boolean headerNotValidBase64) If set the header will not be valid base 64.setKidInvalid
(boolean kidInvalid) If set the kid in the token will be set to 'INVALID'.setPayloadNotJson
(boolean payloadNotJson) If set the payload will not be valid base 64.setPayloadNotValidBase64
(boolean payloadNotValidBase64) If set the payload will not be valid base 64.setSignatureNotValidBase64
(boolean signatureNotValidBase64) If set the signature will not be valid base 64.setSignatureNotValidHash
(boolean signatureNotValidHash) If set the signature will not be a valid hash of the contents.
-
Field Details
-
BASE64
Base64 encoded that implementations may (should) use. -
RANDOM
Secure random number generator that implementations may use. -
keyCache
The key cache that is shared with theJwksHandler
.Note that it is the TokenBuilder that is responsible for causing keys to be created and cached, the
JwksHandler
just makes them available.
-
-
Constructor Details
-
AbstractTokenBuilder
Constructor.- Parameters:
keyCache
- The key cache that is shared with theJwksHandler
.
-
-
Method Details
-
setHeaderNotValidBase64
Description copied from interface:TokenBuilder
If set the header will not be valid base 64.- Specified by:
setHeaderNotValidBase64
in interfaceTokenBuilder
- Parameters:
headerNotValidBase64
- If true the header will not be valid base 64 (it will have one character removed from the end).- Returns:
- this, so that the method may be used in a fluent manner.
-
setPayloadNotValidBase64
Description copied from interface:TokenBuilder
If set the payload will not be valid base 64.- Specified by:
setPayloadNotValidBase64
in interfaceTokenBuilder
- Parameters:
payloadNotValidBase64
- If true the payload will not be valid base 64 (it will have one character removed from the end).- Returns:
- this, so that the method may be used in a fluent manner.
-
setSignatureNotValidBase64
Description copied from interface:TokenBuilder
If set the signature will not be valid base 64.- Specified by:
setSignatureNotValidBase64
in interfaceTokenBuilder
- Parameters:
signatureNotValidBase64
- If true the signature will not be valid base 64 (it will have one character removed from the end).- Returns:
- this, so that the method may be used in a fluent manner.
-
setHeaderNotJson
Description copied from interface:TokenBuilder
If set the header will not be valid base 64.- Specified by:
setHeaderNotJson
in interfaceTokenBuilder
- Parameters:
headerNotJson
- If true the header will not be valid JSON (strings will have quotes stripped from them).- Returns:
- this, so that the method may be used in a fluent manner.
-
setPayloadNotJson
Description copied from interface:TokenBuilder
If set the payload will not be valid base 64.- Specified by:
setPayloadNotJson
in interfaceTokenBuilder
- Parameters:
payloadNotJson
- If true the payload will not be valid JSON (strings will have quotes stripped from them).- Returns:
- this, so that the method may be used in a fluent manner.
-
setSignatureNotValidHash
Description copied from interface:TokenBuilder
If set the signature will not be a valid hash of the contents.- Specified by:
setSignatureNotValidHash
in interfaceTokenBuilder
- Parameters:
signatureNotValidHash
- If true signature will not be a valid hash of the contents (the final byte will be stripped).- Returns:
- this, so that the method may be used in a fluent manner.
-
setKidInvalid
Description copied from interface:TokenBuilder
If set the kid in the token will be set to 'INVALID'.- Specified by:
setKidInvalid
in interfaceTokenBuilder
- Parameters:
kidInvalid
- If true the kid in the token will be set to 'INVALID'.- Returns:
- this, so that the method may be used in a fluent manner.
-
buildToken
public String buildToken(JsonWebAlgorithm jwa, String kid, String iss, String sub, List<String> aud, Long nbf, Long exp, Map<String, Object> otherClaims) throws ExceptionDescription copied from interface:TokenBuilder
Construct a JWT. If any of the testing methods are set the resulting token will be invalid.- Specified by:
buildToken
in interfaceTokenBuilder
- Parameters:
jwa
- The algorithm to use to create the key if the key does not already exist in the cache. If the key is already in ths cache then the jwa is only used to set the "alg" header claim. If it permitted to use theJsonWebAlgorithm.none
algorithm to generate the token, but this should only be done for testing and all validators will reject it.kid
- The ID of the key to use to sign the token, may be null if (and only if) the jwa isJsonWebAlgorithm.none
.iss
- The issuer to put in the payload claims.sub
- The subject to put in the payload claims.aud
- The audience to put in the payload claims.vnbf
- The not-before to put in the payload claims.exp
- The expiry to put in the payload claims.otherClaims
- Other claims to put in the payload.- Returns:
- A fully constructed and signed JWS (that may be broken in various ways if other settings are set).
- Throws:
Exception
- If the security subsystem is unable to carry out required operations.
-
generateHeaderNode
Helper method to generate the token header node.- Parameters:
kid
- The key ID.algorithm
- The algorithm.- Returns:
- The created JsonObject header node.
-
generateClaimsNode
protected io.vertx.core.json.JsonObject generateClaimsNode(String iss, String sub, Long exp, Long nbf, List<String> aud, Map<String, Object> otherClaims) Helper method to build the payload for a token.- Parameters:
iss
- The iss (issuer) claim.sub
- The sub (subject) claim.exp
- The exp (expiry) claim.nbf
- The nbf (not before) claim.aud
- The aud (audience) claim.otherClaims
- Map of other claims that are to be added. Any claims in otherClaims will override anything else added to the claims.- Returns:
- a JsonObject of the payload for a token.
-
base64JSon
protected String base64JSon(boolean notJson, boolean brokenBase64, io.vertx.core.json.JsonObject json) Helper method to convert a JsonObject into a base64 representation. Optionally provides two ways in which the result can be invalidated.- Parameters:
notJson
- If the JSON should be broken before the base64 encoding.brokenBase64
- The the base64 encoding should be broken.json
- The JSON to be encoded.- Returns:
- The JSON encoded as base64 (possibly broken).
-
base64Header
Helper method to convert the header to base64, possibly breaking it. Uses the headerNotJson and headerNotValidBase64 fields to determine whether the result should be valid.- Parameters:
header
- The header to convert.- Returns:
- The JSON encoded as base64 (possibly broken).
-
base64Claims
Helper method to convert the payload to base64, possibly breaking it. Uses the payloadNotJson and payloadNotValidBase64 fields to determine whether the result should be valid.- Parameters:
claims
- The claims to convert.- Returns:
- The JSON encoded as base64 (possibly broken).
-
generateSignature
protected abstract byte[] generateSignature(String kid, JsonWebAlgorithm algorithm, String headerBase64, String claimsBase64) throws Exception Sign the token header and claims using the specified key.- Parameters:
kid
- The key to use to sign the header and claims, if this key is not found in the cache it will be generated.algorithm
- The algorithm to use to generate the key, if it is not found in the cache.headerBase64
- The header to include in the signature.claimsBase64
- The claims to include in the signature.- Returns:
- The signature of the header and claims.
- Throws:
Exception
- If the security subsystem is unable to complete the operation.
-
base64Signature
Helper method to base6t4 encode the signature, possibly breaking it. Uses the signatureNotValidBase64 fields to determine whether the result should be valid.- Parameters:
signature
- The signature of the header and payload.- Returns:
- The base64 encoded signature.
-
constructToken
Helper method to concatenate the three parts of the token.- Parameters:
headerBase64
- The header, base 64 encoded.claimsBase64
- The claims, base 64 encoded.signatureBase64
- The signature, base 64 encoded.- Returns:
- The final JWS.
-