From 84defef39a1bd22b147d337a0b2207f10dc1c455 Mon Sep 17 00:00:00 2001
From: Les Hazlewood <121180+lhazlewood@users.noreply.github.com>
Date: Fri, 4 Aug 2023 18:33:34 -0700
Subject: [PATCH 01/24] impl checkpoint of modern builder-style setters
(without the 'set' prefix)
---
README.md | 6 +-
.../java/io/jsonwebtoken/ClaimsMutator.java | 101 ++++++++++++++++++
.../java/io/jsonwebtoken/HeaderMutator.java | 26 +----
.../io/jsonwebtoken/JweHeaderMutator.java | 14 +--
.../main/java/io/jsonwebtoken/JwtBuilder.java | 39 +++----
.../io/jsonwebtoken/JwtParserBuilder.java | 76 +++++++++++--
.../jsonwebtoken/ProtectedHeaderMutator.java | 8 +-
.../io/jsonwebtoken/SigningKeyResolver.java | 2 +-
.../SigningKeyResolverAdapter.java | 6 +-
.../security/AsymmetricJwkBuilder.java | 4 +-
.../io/jsonwebtoken/security/JwkBuilder.java | 16 +--
.../security/JwkParserBuilder.java | 4 +-
.../java/io/jsonwebtoken/security/Jwks.java | 2 +-
.../security/PrivateJwkBuilder.java | 6 +-
.../security/ProtoJwkBuilder.java | 96 ++++++++---------
.../security/PublicJwkBuilder.java | 4 +-
.../security/SecurityBuilder.java | 8 +-
.../io/jsonwebtoken/security/X509Builder.java | 8 +-
.../io/jsonwebtoken/security/X509Mutator.java | 8 +-
.../impl/AbstractX509Context.java | 8 +-
.../impl/DefaultClaimsBuilder.java | 35 ++++++
.../impl/DefaultJweHeaderMutator.java | 55 +++++-----
.../jsonwebtoken/impl/DefaultJwtBuilder.java | 69 +++++++++---
.../impl/DefaultJwtParserBuilder.java | 30 +++++-
.../AbstractAsymmetricJwkBuilder.java | 24 ++---
.../impl/security/AbstractJwkBuilder.java | 16 +--
.../impl/security/CryptoAlgorithm.java | 2 +-
.../impl/security/DefaultCurve.java | 2 +-
.../impl/security/DefaultJwkParser.java | 2 +-
.../security/DefaultJwkParserBuilder.java | 2 +-
.../impl/security/DefaultKeyPairBuilder.java | 4 +-
.../impl/security/DefaultProtoJwkBuilder.java | 66 ++++++------
.../security/DefaultSecretKeyBuilder.java | 6 +-
.../impl/security/EcSignatureAlgorithm.java | 4 +-
.../impl/security/EcdhKeyAlgorithm.java | 8 +-
.../impl/security/EdwardsCurve.java | 2 +-
.../security/EdwardsPublicKeyDeriver.java | 2 +-
.../impl/security/RsaSignatureAlgorithm.java | 4 +-
.../impl/security/StandardKeyAlgorithms.java | 2 +-
.../impl/security/X509BuilderSupport.java | 12 +--
.../groovy/io/jsonwebtoken/JwtsTest.groovy | 8 +-
.../impl/AbstractProtectedHeaderTest.groovy | 8 +-
.../impl/DefaultJweHeaderTest.groovy | 14 +--
.../impl/DefaultJwtBuilderTest.groovy | 8 +-
.../impl/DefaultJwtHeaderBuilderTest.groovy | 24 +++--
.../impl/DefaultJwtParserBuilderTest.groovy | 8 +-
.../jsonwebtoken/impl/DefaultJwtTest.groovy | 2 +-
.../impl/DefaultMutableJweHeaderTest.groovy | 18 ++--
.../io/jsonwebtoken/impl/IdLocatorTest.groovy | 4 +-
.../AbstractAsymmetricJwkBuilderTest.groovy | 22 ++--
.../security/AbstractJwkBuilderTest.groovy | 10 +-
.../impl/security/AbstractJwkTest.groovy | 18 ++--
.../security/DefaultJwkContextTest.groovy | 8 +-
.../DefaultJwkParserBuilderTest.groovy | 2 +-
.../impl/security/DefaultJwkParserTest.groovy | 6 +-
.../impl/security/EcdhKeyAlgorithmTest.groovy | 10 +-
.../security/FixedSecretKeyBuilder.groovy | 4 +-
.../impl/security/HashAlgorithmsTest.groovy | 2 +-
.../impl/security/JwkConverterTest.groovy | 6 +-
.../impl/security/JwkSerializationTest.groovy | 6 +-
.../impl/security/JwkThumbprintsTest.groovy | 8 +-
.../impl/security/JwksTest.groovy | 72 ++++++-------
.../impl/security/OctetJwksTest.groovy | 10 +-
.../security/Pbes2HsAkwAlgorithmTest.groovy | 4 +-
.../impl/security/RFC7517AppendixCTest.groovy | 2 +-
.../impl/security/RFC7518AppendixCTest.groovy | 2 +-
.../impl/security/RFC7520Section4Test.groovy | 2 +-
.../impl/security/RFC7520Section5Test.groovy | 2 +-
.../impl/security/RFC8037AppendixATest.groovy | 4 +-
.../security/RsaPrivateJwkFactoryTest.groovy | 16 +--
.../impl/security/SecretJwkFactoryTest.groovy | 30 +++---
.../security/EncryptionAlgorithmsTest.groovy | 2 +-
.../security/KeyAlgorithmsTest.groovy | 2 +-
.../io/jsonwebtoken/all/JavaReadmeTest.java | 20 ++--
74 files changed, 695 insertions(+), 458 deletions(-)
diff --git a/README.md b/README.md
index 1a08a672b..4ff87d64a 100644
--- a/README.md
+++ b/README.md
@@ -840,7 +840,7 @@ Each time `setHeaderParam` is called, it simply appends the key-value pair to an
potentially overwriting any existing identically-named key/value pair.
The downside with this approach is that you lose any type-safe setter methods or additional builder utility methods
-available on the `Jwts.headerBuilder()` such as `setContentType`,`setKeyId`, `withX509Sha256Thumbprint`, etc.
+available on the `Jwts.header()` builder such as `contentType`,`keyId`, `withX509Sha256Thumbprint`, etc.
> **Note**
>
@@ -1527,7 +1527,7 @@ If you want to specify a specific JCA `Provider` or `SecureRandom` to use during
as builder arguments. For example:
```java
-SecretKey key = Jwts.SIG.HS256.keyBuilder().setProvider(aProvider).setRandom(aSecureRandom).build();
+SecretKey key = Jwts.SIG.HS256.keyBuilder().provider(aProvider).random(aSecureRandom).build();
```
If you need to save this new `SecretKey`, you can Base64 (or Base64URL) encode it:
@@ -2312,7 +2312,7 @@ You can read/parse a JWK by building a `JwkParser` and parsing the JWK JSON stri
```java
String json = getJwkJsonString();
Jwk> jwk = Jwks.parser()
- //.setProvider(aJcaProvider) // optional
+ //.provider(aJcaProvider) // optional
//.deserializeJsonWith(deserializer) // optional
.build() // create the parser
.parse(json); // actually parse the JSON
diff --git a/api/src/main/java/io/jsonwebtoken/ClaimsMutator.java b/api/src/main/java/io/jsonwebtoken/ClaimsMutator.java
index 70a4718e8..0c5829ee1 100644
--- a/api/src/main/java/io/jsonwebtoken/ClaimsMutator.java
+++ b/api/src/main/java/io/jsonwebtoken/ClaimsMutator.java
@@ -33,27 +33,66 @@ public interface ClaimsMutator A JWT obtained after this timestamp should not be used. A JWT obtained before this timestamp should not be used. The value is the timestamp when the JWT was created. This value is a CaSe-SenSiTiVe unique identifier for the JWT. If specified, this value MUST be assigned in a
+ * manner that ensures that there is a negligible probability that the same value will be accidentally
+ * assigned to a different data object. The ID can be used to prevent the JWT from being replayed. Compatibility Note While the JWT family of specifications only defines the If not {@code null}, this is a convenience method that calls the equivalent of the following: If not {@code null}, this is a convenience method that calls the equivalent of the following: This is a convenience method that is effectively the same as: If you want the JWT payload to be JSON, use the
* {@link #setClaims(Claims)} or {@link #setClaims(java.util.Map)} methods instead. Unless you are confident that the JWT recipient will always know how to use
* the given byte array without additional metadata, it is strongly recommended to use the
- * {@link #setContent(byte[], String)} method instead of this one. That method ensures that a JWT recipient
+ * {@link #content(byte[], String)} method instead of this one. That method ensures that a JWT recipient
* can inspect the {@code cty} header to know how to handle the byte array without ambiguity. Note that the content and claims properties are mutually exclusive - only one of the two may be used.iss
(issuer) value. A {@code null} value will remove the property from the JSON map.
+ *
+ * @param iss the JWT {@code iss} value or {@code null} to remove the property from the JSON map.
+ * @return the {@code Claims} instance for method chaining.
+ * @since JJWT_RELEASE_VERSION
+ */
+ T issuer(String iss);
+
/**
* Sets the JWT
* sub
(subject) value. A {@code null} value will remove the property from the JSON map.
*
* @param sub the JWT {@code sub} value or {@code null} to remove the property from the JSON map.
* @return the {@code Claims} instance for method chaining.
+ * @deprecated since JJWT_RELEASE_VERSION in favor of the shorter and more modern builder-style named
+ * {@link #subject(String)}. This method will be removed before the JJWT 1.0 release.
*/
+ @Deprecated
T setSubject(String sub);
+ /**
+ * Sets the JWT
+ * sub
(subject) value. A {@code null} value will remove the property from the JSON map.
+ *
+ * @param sub the JWT {@code sub} value or {@code null} to remove the property from the JSON map.
+ * @return the {@code Claims} instance for method chaining.
+ * @since JJWT_RELEASE_VERSION
+ */
+ T subject(String sub);
+
/**
* Sets the JWT
* aud
(audience) value. A {@code null} value will remove the property from the JSON map.
*
* @param aud the JWT {@code aud} value or {@code null} to remove the property from the JSON map.
* @return the {@code Claims} instance for method chaining.
+ * @deprecated since JJWT_RELEASE_VERSION in favor of the shorter and more modern builder-style named
+ * {@link #audience(String)}. This method will be removed before the JJWT 1.0 release.
*/
+ @Deprecated
T setAudience(String aud);
+ /**
+ * Sets the JWT
+ * aud
(audience) value. A {@code null} value will remove the property from the JSON map.
+ *
+ * @param aud the JWT {@code aud} value or {@code null} to remove the property from the JSON map.
+ * @return the {@code Claims} instance for method chaining.
+ * @since JJWT_RELEASE_VERSION
+ */
+ T audience(String aud);
+
/**
* Sets the JWT
* exp
(expiration) timestamp. A {@code null} value will remove the property from the JSON map.
@@ -62,9 +101,24 @@ public interface ClaimsMutatorexp
(expiration) timestamp. A {@code null} value will remove the property from the JSON map.
+ *
+ * nbf
(not before) timestamp. A {@code null} value will remove the property from the JSON map.
@@ -73,9 +127,24 @@ public interface ClaimsMutatornbf
(not before) timestamp. A {@code null} value will remove the property from the JSON map.
+ *
+ * iat
(issued at) timestamp. A {@code null} value will remove the property from the JSON map.
@@ -84,9 +153,24 @@ public interface ClaimsMutatoriat
(issued at) timestamp. A {@code null} value will remove the property from the JSON map.
+ *
+ * jti
(JWT ID) value. A {@code null} value will remove the property from the JSON map.
@@ -97,6 +181,23 @@ public interface ClaimsMutatorjti
(JWT ID) value. A {@code null} value will remove the property from the JSON map.
+ *
+ * zip
- * (Compression Algorithm) header parameter value. A {@code null} value will remove
- * the property from the JSON map.
*
- * zip
header in the JWE
- * (JSON Web Encryption) specification, JJWT will also support compression for JWS as well if you choose to use it.
- * However, be aware that if you use compression when creating a JWS token, other libraries may not be able to
- * parse the JWS. However, Compression when creating JWE tokens should be universally accepted for any library
- * that supports JWE.
+ * {@link #agreementPartyUInfo(byte[]) agreementPartyUInfo}(info.getBytes(StandardCharsets.UTF_8))
*
* @param info information about the JWE producer to use with key agreement algorithms.
* @return the header for method chaining.
@@ -55,7 +55,7 @@ public interface JweHeaderMutator
- * {@link #setAgreementPartyUInfo(byte[]) setAgreementPartyUInfo}(info.getBytes(StandardCharsets.UTF_8))
+ * {@link #agreementPartyVInfo(byte[]) setAgreementPartVUInfo}(info.getBytes(StandardCharsets.UTF_8))
*
* @param info information about the JWE recipient to use with key agreement algorithms.
* @return the header for method chaining.
@@ -87,7 +87,7 @@ public interface JweHeaderMutator
- * {@link #setAgreementPartyVInfo(byte[]) setAgreementPartVUInfo}(info.getBytes(StandardCharsets.UTF_8))
+ * {@link #content(byte[]) setPayload}(payload.getBytes(StandardCharsets.UTF_8));
*
*
- * {@link #setContent(byte[]) setPayload}(payload.getBytes(StandardCharsets.UTF_8));
If for some reason you do not wish to adhere to the JWT specification recommendation, do not call this - * method - instead call {@link #setContent(byte[])} and set the header's - * {@link Header#setContentType(String) contentType} independently. For example:
+ * method - instead call {@link #content(byte[])} and set the header's + * {@link Header#contentType(String) contentType} independently. For example: * ** @@ -199,11 +200,11 @@ public interface JwtBuilder extends ClaimsMutator* Jwts.builder() - * .header().setContentType("application/whatever").and() - * .setContent(byteArray) + * .header().contentType("application/whatever").and() + * .content(byteArray) * ... * .build();
The payload and claims properties are mutually exclusive - only one of the two may be used.
@@ -216,7 +217,7 @@ public interface JwtBuilder extends ClaimsMutatorThe payload and claims properties are mutually exclusive - only one of the two may be used.
* diff --git a/api/src/main/java/io/jsonwebtoken/JwtParserBuilder.java b/api/src/main/java/io/jsonwebtoken/JwtParserBuilder.java index eadf800ea..d6e226c50 100644 --- a/api/src/main/java/io/jsonwebtoken/JwtParserBuilder.java +++ b/api/src/main/java/io/jsonwebtoken/JwtParserBuilder.java @@ -100,7 +100,7 @@ public interface JwtParserBuilder extends BuilderDeprecation Notice
* @@ -320,7 +347,7 @@ public interface JwtParserBuilder extends BuilderIf there is any chance that the parser will encounter JWSs * that need different signature verification keys based on the JWS being parsed, or JWEs, it is strongly * recommended to configure your own {@link Locator} via the - * {@link #setKeyLocator(Locator) setKeyLocator} method instead of using this one.
+ * {@link #keyLocator(Locator) keyLocator} method instead of using this one. * *Calling this method overrides any previously set signature verification key.
* @@ -332,7 +359,7 @@ public interface JwtParserBuilder extends BuilderThis is a convenience method to use in specific circumstances: when the parser will only ever encounter
@@ -342,7 +369,7 @@ public interface JwtParserBuilder extends Builder If there is any chance that the parser will encounter JWEs that need different decryption keys based on the
* JWE being parsed, or JWSs, it is strongly recommended to configure
- * your own {@link Locator Locator} via the {@link #setKeyLocator(Locator) setKeyLocator} method instead of
+ * your own {@link Locator Locator} via the {@link #keyLocator(Locator) keyLocator} method instead of
* using {@code decryptWith}. Calling this method overrides any previously set decryption key.
- * Jws<Claims> jws = Jwts.parser().setKeyLocator(new Locator<Key>() { + * Jws<Claims> jws = Jwts.parser().keyLocator(new Locator<Key>() { * @Override * public Key locate(Header<?> header) { * if (header instanceof JwsHeader) { @@ -386,14 +413,14 @@ public interface JwtParserBuilder extends Builder* *{ * @return the parser builder for method chaining. * @since JJWT_RELEASE_VERSION */ - JwtParserBuilder setKeyLocator(Locator keyLocator); + JwtParserBuilder keyLocator(Locator keyLocator); /** * Deprecation Notice
* *This method has been deprecated as of JJWT version JJWT_RELEASE_VERSION because it only supports key location * for JWSs (signed JWTs) instead of both signed (JWS) and encrypted (JWE) scenarios. Use the - * {@link #setKeyLocator(Locator) setKeyLocator} method instead to ensure a locator that can work for both JWS and + * {@link #keyLocator(Locator) keyLocator} method instead to ensure a locator that can work for both JWS and * JWE inputs. This method will be removed for the 1.0 release.
* *Previous Documentation
@@ -420,7 +447,7 @@ public interface JwtParserBuilder extends Builder{ * * @param signingKeyResolver the signing key resolver used to retrieve the signing key. * @return the parser builder for method chaining. - * @deprecated since JJWT_RELEASE_VERSION in favor of {@link #setKeyLocator(Locator)} + * @deprecated since JJWT_RELEASE_VERSION in favor of {@link #keyLocator(Locator)} */ @SuppressWarnings("DeprecatedIsStillUsed") @Deprecated @@ -531,6 +558,7 @@ public interface JwtParserBuilder extends Builder { * @param compressionCodecResolver the compression codec resolver used to decompress the JWT body. * @return the parser builder for method chaining. * @deprecated since JJWT_RELEASE_VERSION in favor of {@link #addCompressionAlgorithms(Collection)}. + * This method will be removed before the 1.0 release. */ @Deprecated JwtParserBuilder setCompressionCodecResolver(CompressionCodecResolver compressionCodecResolver); @@ -543,9 +571,23 @@ public interface JwtParserBuilder extends Builder { * * @param base64UrlDecoder the decoder to use when Base64Url-decoding * @return the parser builder for method chaining. + * @deprecated since JJWT_RELEASE_VERSION in favor of the shorter and more modern builder-style named + * {@link #base64UrlDecoder(Decoder)}. This method will be removed before the JJWT 1.0 release. */ + @Deprecated JwtParserBuilder base64UrlDecodeWith(Decoder base64UrlDecoder); + /** + * Perform Base64Url decoding with the specified Decoder + * + * JJWT uses a spec-compliant decoder that works on all supported JDK versions, but you may call this method + * to specify a different decoder if you desire.
+ * + * @param base64UrlDecoder the decoder to use when Base64Url-decoding + * @return the parser builder for method chaining. + */ + JwtParserBuilder base64UrlDecoder(Decoderbase64UrlDecoder); + /** * Uses the specified deserializer to convert JSON Strings (UTF-8 byte arrays) into Java Map objects. This is * used by the parser after Base64Url-decoding to convert JWT/JWS/JWT JSON headers and claims into Java Map @@ -558,9 +600,27 @@ public interface JwtParserBuilder extends Builder { * * @param deserializer the deserializer to use when converting JSON Strings (UTF-8 byte arrays) into Map objects. * @return the builder for method chaining. + * @deprecated since JJWT_RELEASE_VERSION in favor of the shorter and more modern builder-style named + * {@link #jsonDeserializer(Deserializer)}. This method will be removed before the JJWT 1.0 release. */ + @Deprecated JwtParserBuilder deserializeJsonWith(Deserializer
If you want the JWT payload to be JSON claims, use the {@link #claim(String, Object)} or - * {@link #claims(Map)} methods instead.
+ * {@link #claims()} methods instead. * *Note that the content and claims properties are mutually exclusive - only one of the two may be used.
* @@ -276,276 +236,183 @@ public interface JwtBuilder extends ClaimsMutator+ * + * @return the {@link JwtBuilder.Header} to use for header construction. + * @since JJWT_RELEASE_VERSION */ - JwtBuilder setClaims(Claims claims); + Claims claims(); /** - * Sets (and replaces) the JWT payload to be a JSON Claims instance populated by the specified name/value pairs. - * If you do not want the JWT payload to be JSON claims and instead want it to be a byte array for any content, use the + * Sets (and replaces) the JWT Claims payload with the specified name/value pairs. If you do not want the JWT + * payload to be JSON claims and instead want it to be a byte array for any content, use the * {@link #content(byte[])} or {@link #content(byte[], String)} methods instead. * *+ * String jwt = Jwts.builder() * - *The content and claims properties are mutually exclusive - only one of the two may be used.
+ * .claims() + * .subject("Joe") + * .audience("you") + * .issuer("me") + * .add("customClaim", customValue) + * .add(myClaimsMap) + * // ... etc ... + * .{@link JwtBuilder.Claims#and() and()} //return back to the JwtBuilder * - * @param claims the JWT claims to be set as the JWT payload. - * @return the builder for method chaining. - * @see #content(byte[], String) - * @deprecated since JJWT_RELEASE_VERSION in favor of {@link #empty()}{@code .}{@link #add(Map) add(map)}. This - * method will be removed before the 1.0 release. + * .signWith(key) // resume JwtBuilder calls + * // ... etc ... + * .compact();
The content and claims properties are mutually exclusive - only one of the two may be used.
* * @param claims the JWT Claims to be set as the JWT payload. * @return the builder for method chaining. - * @deprecated since JJWT_RELEASE_VERSION in favor of the more modern builder-style {@link #claims(Map)} method. + * @deprecated since JJWT_RELEASE_VERSION in favor of the more modern builder-style {@link #claims()} method. */ + @SuppressWarnings("DeprecatedIsStillUsed") @Deprecated JwtBuilder setClaims(MapFor example, when typing:
- *+ * Adds/appends all given name/value pairs to the JSON Claims in the payload. This is a convenience wrapper for: * - * @param claims the claims map to add to the JWT Claims payload. - * @return the builder for method chaining. - * @since JJWT_RELEASE_VERSION - */ - JwtBuilder claims(Map- * Jwts.builder().// press code-completion hotkeys to suggest methods and you type 'claim'
* *+ *{@link #claims()}.{@link MapMutator#add(Map) add(claims)}.{@link Claims#and() and()}
The content and claims properties are mutually exclusive - only one of the two may be used.
* * @param claims the JWT Claims to be added to the JWT payload. * @return the builder for method chaining. * @since 0.8 - * @deprecated since JJWT_RELEASE_VERSION in favor of {@link #add(Map)}. This method will be removed before the - * 1.0 release. + * @deprecated since JJWT_RELEASE_VERSION in favor of + *{@link #claims()}.{@link Claims#add(Map) add(Map)}.{@link Claims#and() and()}
.
+ * This method will be removed before the 1.0 release.
*/
+ @SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated
JwtBuilder addClaims(Mapiss
(issuer) value. A {@code null} value will remove the property from the Claims.
- *
- * This is a convenience method. It will first ensure a Claims instance exists as the JWT payload and then set - * the Claims {@link Claims#getIssuer() issuer} field with the specified value. This allows you to write - * code like this:
- * - *- * String jwt = Jwts.builder().issuer("Joe").compact(); - *+ * Sets a JWT Claims parameter value. A {@code null} value will remove the property from the Claims. + * This is a convenience wrapper for: + *
* - *+ * {@link #claims()}.{@link MapMutator#add(Object, Object) add(name, value)}.{@link Claims#and() and()}
instead of this:
- *- * Claims claims = Jwts.claims().issuer("Joe").build(); - * String jwt = Jwts.builder().setClaims(claims).compact(); - *- *
if desired.
+ * @param name the JWT Claims property name + * @param value the value to set for the specified Claims property name + * @return the builder instance for method chaining. + * @since 0.2 + */ + JwtBuilder claim(String name, Object value); + + /** + * Sets the JWT Claims + *iss
(issuer) value. A {@code null} value will remove the property from the Claims.
+ * This is a convenience wrapper for:
+ * * * @param iss the JWT {@code iss} value or {@code null} to remove the property from the Claims map. * @return the builder instance for method chaining. - * @since 0.2 */ - @Override - //only for better/targeted JavaDoc + @Override // for better/targeted JavaDoc JwtBuilder issuer(String iss); /** - * Sets the JWT Claims + * Sets the JWT Claims *+ * {@link #claims()}.{@link ClaimsMutator#issuer(String) issuer(iss)}.{@link Claims#and() and()}
sub
(subject) value. A {@code null} value will remove the property from the Claims.
- *
- * This is a convenience method. It will first ensure a Claims instance exists as the JWT payload and then set - * the Claims {@link Claims#getSubject() subject} field with the specified value. This allows you to write - * code like this:
- * + * This is a convenience wrapper for: *- * - *- * String jwt = Jwts.builder().subject("Me").compact();
instead of this:
- *- *- * Claims claims = Jwts.claims().subject("Me").build(); - * String jwt = Jwts.builder().setClaims(claims).compact();
if desired.
+ * {@link #claims()}.{@link ClaimsMutator#subject(String) subject(sub)}.{@link Claims#and() and()} * * @param sub the JWT {@code sub} value or {@code null} to remove the property from the Claims map. * @return the builder instance for method chaining. - * @since 0.2 */ - @Override - //only for better/targeted JavaDoc + @Override // for better/targeted JavaDoc JwtBuilder subject(String sub); /** - * Sets the JWT Claims + * Sets the JWT Claims *aud
(audience) value. A {@code null} value will remove the property from the Claims.
- *
- * This is a convenience method. It will first ensure a Claims instance exists as the JWT payload and then set - * the Claims {@link Claims#getAudience() audience} field with the specified value. This allows you to write - * code like this:
- * - *- * String jwt = Jwts.builder().audience("You").compact(); - *- * - *
instead of this:
- *- * Claims claims = Jwts.claims().audience("You").build(); - * String jwt = Jwts.builder().setClaims(claims).compact(); - *- *
if desired.
+ * This is a convenience wrapper for: + ** * @param aud the JWT {@code aud} value or {@code null} to remove the property from the Claims map. * @return the builder instance for method chaining. - * @since 0.2 */ - @Override - //only for better/targeted JavaDoc + @Override // for better/targeted JavaDoc JwtBuilder audience(String aud); /** - * Sets the JWT Claims + * Sets the JWT Claims *+ * {@link #claims()}.{@link ClaimsMutator#audience(String) audience(aud)}.{@link Claims#and() and()}
exp
(expiration) value. A {@code null} value will remove the property from the Claims.
*
* A JWT obtained after this timestamp should not be used.
* - *This is a convenience method. It will first ensure a Claims instance exists as the JWT payload and then set - * the Claims {@link Claims#getExpiration() expiration} field with the specified value. This allows - * you to write code like this:
- * - *- * String jwt = Jwts.builder().expiration(new Date(System.currentTimeMillis() + 3600000)).compact(); - *- * - *
instead of this:
- *- * Claims claims = Jwts.claims().expiration(new Date(System.currentTimeMillis() + 3600000)).build(); - * String jwt = Jwts.builder().setClaims(claims).compact(); - *- *
if desired.
+ *This is a convenience wrapper for:
+ ** * @param exp the JWT {@code exp} value or {@code null} to remove the property from the Claims map. * @return the builder instance for method chaining. - * @since 0.2 */ - @Override - //only for better/targeted JavaDoc + @Override // for better/targeted JavaDoc JwtBuilder expiration(Date exp); /** - * Sets the JWT Claims + * Sets the JWT Claims *+ * {@link #claims()}.{@link ClaimsMutator#expiration(Date) expiration(exp)}.{@link Claims#and() and()}
nbf
(not before) value. A {@code null} value will remove the property from the Claims.
*
* A JWT obtained before this timestamp should not be used.
* - *This is a convenience method. It will first ensure a Claims instance exists as the JWT payload and then set - * the Claims {@link Claims#getNotBefore() notBefore} field with the specified value. This allows - * you to write code like this:
- * - *- * String jwt = Jwts.builder().notBefore(new Date()).compact(); - *- * - *
instead of this:
- *- * Claims claims = Jwts.claims().notBefore(new Date()).build(); - * String jwt = Jwts.builder().setClaims(claims).compact(); - *- *
if desired.
+ *This is a convenience wrapper for:
+ ** * @param nbf the JWT {@code nbf} value or {@code null} to remove the property from the Claims map. * @return the builder instance for method chaining. - * @since 0.2 */ - @Override - //only for better/targeted JavaDoc - JwtBuilder notBefore(Date nbf); + @Override // for better/targeted JavaDoc + JwtBuilder setNotBefore(Date nbf); /** - * Sets the JWT Claims + * Sets the JWT Claims *+ * {@link #claims()}.{@link ClaimsMutator#notBefore(Date) notBefore(nbf)}.{@link Claims#and() and()}
iat
(issued at) value. A {@code null} value will remove the property from the Claims.
*
* The value is the timestamp when the JWT was created.
* - *This is a convenience method. It will first ensure a Claims instance exists as the JWT payload and then set - * the Claims {@link Claims#getIssuedAt() issuedAt} field with the specified value. This allows - * you to write code like this:
- * - *- * String jwt = Jwts.builder().issuedAt(new Date()).compact(); - *- * - *
instead of this:
- *- * Claims claims = Jwts.claims().issuedAt(new Date()).build(); - * String jwt = Jwts.builder().setClaims(claims).compact(); - *- *
if desired.
+ *This is a convenience wrapper for:
+ ** * @param iat the JWT {@code iat} value or {@code null} to remove the property from the Claims map. * @return the builder instance for method chaining. - * @since 0.2 */ - @Override - //only for better/targeted JavaDoc + @Override // for better/targeted JavaDoc JwtBuilder issuedAt(Date iat); /** - * Sets the JWT Claims + * Sets the JWT Claims *+ * {@link #claims()}.{@link ClaimsMutator#issuedAt(Date) issuedAt(iat)}.{@link Claims#and() and()}
jti
(JWT ID) value. A {@code null} value will remove the property from the Claims.
*
* The value is a CaSe-SenSiTiVe unique identifier for the JWT. If specified, this value MUST be assigned in a * manner that ensures that there is a negligible probability that the same value will be accidentally * assigned to a different data object. The ID can be used to prevent the JWT from being replayed.
* - *This is a convenience method. It will first ensure a Claims instance exists as the JWT payload and then set - * the Claims {@link Claims#getId() id} field with the specified value. This allows - * you to write code like this:
- * - *- * String jwt = Jwts.builder().id(UUID.randomUUID().toString()).compact(); - *- * - *
instead of this:
- *- * Claims claims = Jwts.claims().id(UUID.randomUUID().toString()).build(); - * String jwt = Jwts.builder().setClaims(claims).compact(); - *- *
if desired.
+ *This is a convenience wrapper for:
+ ** * @param jti the JWT {@code jti} (id) value or {@code null} to remove the property from the Claims map. * @return the builder instance for method chaining. - * @since 0.2 */ - @Override - //only for better/targeted JavaDoc + @Override // for better/targeted JavaDoc JwtBuilder id(String jti); - /** - * Named alias of {@link #add(String, Object)} which may be easier to find when searching during code completion. - *+ * {@link #claims()}.{@link ClaimsMutator#id(String) id(jti)}.{@link Claims#and() and()}
For example, when typing:
- *- * - * @param name the JWT Claims property name - * @param value the value to set for the specified Claims property name - * @return the builder instance for method chaining. - * @since 0.2 - */ - JwtBuilder claim(String name, Object value); - /** * Signs the constructed JWT with the specified key using the key's recommended signature algorithm * as defined below, producing a JWS. If the recommended signature algorithm isn't sufficient for your needs, @@ -949,6 +816,23 @@ public interface JwtBuilder extends ClaimsMutator- * Jwts.builder().// press code-completion hotkeys to suggest methods and you type 'claim'
+ * This is a convenience wrapper for: * *
+ * {@link #claims()}.{@link MapMutator#add(Map) add(claims)}.{@link Claims#and() and()} * *- *{@link #claims()}.{@link MapMutator#add(Map) add(claims)}.{@link Claims#and() and()}
The content and claims properties are mutually exclusive - only one of the two may be used.
* @@ -296,8 +298,8 @@ public interface JwtBuilder extends ClaimsMutator* @@ -308,6 +310,21 @@ public interface JwtBuilder extends ClaimsMutator* {@link #claims()}.{@link MapMutator#add(Object, Object) add(name, value)}.{@link Claims#and() and()}
+ * + *+ * {@link #claims()}.{@link MapMutator#add(Map) add(claims)}.{@link Claims#and() and()}
The content and claims properties are mutually exclusive - only one of the two may be used.
+ * + * @param claims the JWT Claims to be added to the JWT payload. + * @return the builder instance for method chaining + * @since JJWT_RELEASE_VERSION + */ + JwtBuilder claims(Mapiss
(issuer) value. A {@code null} value will remove the property from the Claims.
@@ -318,7 +335,8 @@ public interface JwtBuilder extends ClaimsMutatorJJWT uses a spec-compliant encoder that works on all supported JDK versions, but you may call this method * to specify a different encoder if you desire.
* * @param base64UrlEncoder the encoder to use when Base64Url-encoding * @return the builder for method chaining. + * @see #encoder(Encoder) * @since 0.10.0 + * @deprecated since JJWT_RELEASE_VERSION in favor of the more modern builder-style + * {@link #encoder(Encoder)} method. */ JwtBuilder base64UrlEncodeWith(EncoderJJWT uses a spec-compliant encoder that works on all supported JDK versions, but you may call this method + * to specify a different encoder if necessar.
+ * + * @param encoder the encoder to use when Base64Url-encoding + * @return the builder for method chaining. + * @since JJWT_RELEASE_VERSION + */ + JwtBuilder encoder(EncoderIf this method is not called, JJWT will use whatever serializer it can find at runtime, checking for the + * presence of well-known implementations such Jackson, Gson, and org.json. If one of these is not found + * in the runtime classpath, an exception will be thrown when the {@link #compact()} method is invoked.
+ * + * @param serializer the serializer to use when converting Map objects to JSON strings. + * @return the builder for method chaining. + * @since JJWT_RELEASE_VERSION + */ + JwtBuilder serializer(SerializerDuring JWE creation, this algorithm:
*During JWE creation, this algorithm:
*During JWE creation, this algorithm:
*During JWE creation, this algorithm:
*During JWE creation, this algorithm:
*During JWE creation, this algorithm:
*During JWE creation, this algorithm:
*During JWE creation, this algorithm:
*During JWE creation, this algorithm:
*
- * SecretKey key = aeadAlgorithm.keyBuilder().build();
+ * SecretKey key = aeadAlgorithm.key().build();
*
*
* The resulting {@code key} is guaranteed to have the correct algorithm parameters and strength/length necessary for
diff --git a/api/src/main/java/io/jsonwebtoken/security/KeyBuilderSupplier.java b/api/src/main/java/io/jsonwebtoken/security/KeyBuilderSupplier.java
index 29c1e53d2..97b3eb02a 100644
--- a/api/src/main/java/io/jsonwebtoken/security/KeyBuilderSupplier.java
+++ b/api/src/main/java/io/jsonwebtoken/security/KeyBuilderSupplier.java
@@ -22,8 +22,8 @@
* their associated cryptographic algorithm implementation.
*
* @param Deprecation Notice As of JJWT JJWT_RELEASE_VERSION, symmetric (secret) key algorithm instances can generate a key of suitable
- * length for that specific algorithm by calling their {@code keyBuilder()} method directly. For example:
- * {@link Jwts.SIG#HS256}.keyBuilder().build();
- * {@link Jwts.SIG#HS384}.keyBuilder().build();
- * {@link Jwts.SIG#HS512}.keyBuilder().build();
+ * {@link Jwts.SIG#HS256}.key().build();
+ * {@link Jwts.SIG#HS384}.key().build();
+ * {@link Jwts.SIG#HS512}.key().build();
*
*
* Call those methods as needed instead of this static {@code secretKeyFor} helper method - the returned @@ -124,7 +124,7 @@ public static SecretKey hmacShaKeyFor(byte[] bytes) throws WeakKeyException { * @throws IllegalArgumentException for any input value other than {@link io.jsonwebtoken.SignatureAlgorithm#HS256}, * {@link io.jsonwebtoken.SignatureAlgorithm#HS384}, or {@link io.jsonwebtoken.SignatureAlgorithm#HS512} * @deprecated since JJWT_RELEASE_VERSION. Use your preferred {@link MacAlgorithm} instance's - * {@link MacAlgorithm#keyBuilder() keyBuilder()} method directly. + * {@link MacAlgorithm#key() key()} builder method directly. */ @SuppressWarnings("DeprecatedIsStillUsed") @Deprecated @@ -135,21 +135,21 @@ public static SecretKey secretKeyFor(io.jsonwebtoken.SignatureAlgorithm alg) thr String msg = "The " + alg.name() + " algorithm does not support shared secret keys."; throw new IllegalArgumentException(msg); } - return ((MacAlgorithm) salg).keyBuilder().build(); + return ((MacAlgorithm) salg).key().build(); } /** *
Deprecation Notice
* *As of JJWT JJWT_RELEASE_VERSION, asymmetric key algorithm instances can generate KeyPairs of suitable strength - * for that specific algorithm by calling their {@code keyPairBuilder()} method directly. For example:
+ * for that specific algorithm by calling their {@code keyPair()} builder method directly. For example: * *+ * Jwts.SIG.{@link Jwts.SIG#ES512 ES512}.keyPair().build(); * *- * Jwts.SIG.{@link Jwts.SIG#RS256 RS256}.keyPairBuilder().build(); - * Jwts.SIG.{@link Jwts.SIG#RS384 RS384}.keyPairBuilder().build(); - * Jwts.SIG.{@link Jwts.SIG#RS512 RS512}.keyPairBuilder().build(); + * Jwts.SIG.{@link Jwts.SIG#RS256 RS256}.keyPair().build(); + * Jwts.SIG.{@link Jwts.SIG#RS384 RS384}.keyPair().build(); + * Jwts.SIG.{@link Jwts.SIG#RS512 RS512}.keyPair().build(); * ... etc ... - * Jwts.SIG.{@link Jwts.SIG#ES512 ES512}.keyPairBuilder().build();
Call those methods as needed instead of this static {@code keyPairFor} helper method - the returned * {@link KeyPairBuilder} allows callers to specify a preferred Provider or SecureRandom on the builder if @@ -229,7 +229,7 @@ public static SecretKey secretKeyFor(io.jsonwebtoken.SignatureAlgorithm alg) thr * @throws IllegalArgumentException if {@code alg} is not an asymmetric algorithm * @deprecated since JJWT_RELEASE_VERSION in favor of your preferred * {@link io.jsonwebtoken.security.SignatureAlgorithm} instance's - * {@link SignatureAlgorithm#keyPairBuilder() keyPairBuilder()} method directly. + * {@link SignatureAlgorithm#keyPair() keyPair()} builder method directly. */ @SuppressWarnings("DeprecatedIsStillUsed") @Deprecated @@ -241,7 +241,7 @@ public static KeyPair keyPairFor(io.jsonwebtoken.SignatureAlgorithm alg) throws throw new IllegalArgumentException(msg); } SignatureAlgorithm asalg = ((SignatureAlgorithm) salg); - return asalg.keyPairBuilder().build(); + return asalg.keyPair().build(); } /** diff --git a/api/src/main/java/io/jsonwebtoken/security/MacAlgorithm.java b/api/src/main/java/io/jsonwebtoken/security/MacAlgorithm.java index bbb1bc0ef..3d04e9747 100644 --- a/api/src/main/java/io/jsonwebtoken/security/MacAlgorithm.java +++ b/api/src/main/java/io/jsonwebtoken/security/MacAlgorithm.java @@ -47,7 +47,7 @@ * have a sufficient length and any algorithm parameters required by that algorithm. For example:
* *+ * SecretKey key = macAlgorithm.key().build(); * *- * SecretKey key = macAlgorithm.keyBuilder().build();
The resulting {@code key} is guaranteed to have the correct algorithm parameters and strength/length necessary for * that exact {@code MacAlgorithm} instance.
diff --git a/api/src/main/java/io/jsonwebtoken/security/SignatureAlgorithm.java b/api/src/main/java/io/jsonwebtoken/security/SignatureAlgorithm.java index f9d1aa445..b20192e81 100644 --- a/api/src/main/java/io/jsonwebtoken/security/SignatureAlgorithm.java +++ b/api/src/main/java/io/jsonwebtoken/security/SignatureAlgorithm.java @@ -38,7 +38,7 @@ * required by that algorithm. For example: * *+ * KeyPair pair = signatureAlgorithm.keyPair().build(); * *- * KeyPair pair = signatureAlgorithm.keyPairBuilder().build();
The resulting {@code pair} is guaranteed to have the correct algorithm parameters and length/strength necessary * for that exact {@code signatureAlgorithm} instance.
diff --git a/impl/src/main/java/io/jsonwebtoken/impl/security/AesAlgorithm.java b/impl/src/main/java/io/jsonwebtoken/impl/security/AesAlgorithm.java index c3301189b..892921897 100644 --- a/impl/src/main/java/io/jsonwebtoken/impl/security/AesAlgorithm.java +++ b/impl/src/main/java/io/jsonwebtoken/impl/security/AesAlgorithm.java @@ -81,7 +81,7 @@ public int getKeyBitLength() { } @Override - public SecretKeyBuilder keyBuilder() { + public SecretKeyBuilder key() { return new DefaultSecretKeyBuilder(KEY_ALG_NAME, getKeyBitLength()); } diff --git a/impl/src/main/java/io/jsonwebtoken/impl/security/CryptoAlgorithm.java b/impl/src/main/java/io/jsonwebtoken/impl/security/CryptoAlgorithm.java index 55a126e57..8e0785e6c 100644 --- a/impl/src/main/java/io/jsonwebtoken/impl/security/CryptoAlgorithm.java +++ b/impl/src/main/java/io/jsonwebtoken/impl/security/CryptoAlgorithm.java @@ -92,7 +92,7 @@ protected Provider getProvider(Request> request) { protected SecretKey generateKey(KeyRequest> request) { AeadAlgorithm enc = Assert.notNull(request.getEncryptionAlgorithm(), "Request encryptionAlgorithm cannot be null."); - SecretKeyBuilder builder = Assert.notNull(enc.keyBuilder(), "Request encryptionAlgorithm keyBuilder cannot be null."); + SecretKeyBuilder builder = Assert.notNull(enc.key(), "Request encryptionAlgorithm KeyBuilder cannot be null."); SecretKey key = builder.provider(getProvider(request)).random(request.getSecureRandom()).build(); return Assert.notNull(key, "Request encryptionAlgorithm SecretKeyBuilder cannot produce null keys."); } diff --git a/impl/src/main/java/io/jsonwebtoken/impl/security/DefaultCurve.java b/impl/src/main/java/io/jsonwebtoken/impl/security/DefaultCurve.java index af4d2f64c..7e5195897 100644 --- a/impl/src/main/java/io/jsonwebtoken/impl/security/DefaultCurve.java +++ b/impl/src/main/java/io/jsonwebtoken/impl/security/DefaultCurve.java @@ -74,7 +74,7 @@ public String toString() { return ID; } - public KeyPairBuilder keyPairBuilder() { + public KeyPairBuilder keyPair() { return new DefaultKeyPairBuilder(this.JCA_NAME).provider(this.PROVIDER); } } diff --git a/impl/src/main/java/io/jsonwebtoken/impl/security/DefaultMacAlgorithm.java b/impl/src/main/java/io/jsonwebtoken/impl/security/DefaultMacAlgorithm.java index d873d8bec..1fd7c0291 100644 --- a/impl/src/main/java/io/jsonwebtoken/impl/security/DefaultMacAlgorithm.java +++ b/impl/src/main/java/io/jsonwebtoken/impl/security/DefaultMacAlgorithm.java @@ -81,7 +81,7 @@ private boolean isJwaStandardJcaName(String jcaName) { } @Override - public SecretKeyBuilder keyBuilder() { + public SecretKeyBuilder key() { return new DefaultSecretKeyBuilder(getJcaName(), getKeyBitLength()); } @@ -142,8 +142,8 @@ protected void validateKey(Key k, boolean signing) { msg += " The JWT " + "JWA Specification (RFC 7518, Section 3.2) states that keys used with " + id + " MUST have a " + "size >= " + minKeyBitLength + " bits (the key size must be greater than or equal to the hash " + - "output size). Consider using the Jwts.SIG." + id + ".keyBuilder() " + - "method to create a key guaranteed to be secure enough for " + id + ". See " + + "output size). Consider using the Jwts.SIG." + id + ".key() " + + "builder to create a key guaranteed to be secure enough for " + id + ". See " + "https://tools.ietf.org/html/rfc7518#section-3.2 for more information."; } else { //custom algorithm - just indicate required key length: msg += " The " + id + " algorithm requires keys to have a size >= " + minKeyBitLength + " bits."; diff --git a/impl/src/main/java/io/jsonwebtoken/impl/security/ECCurve.java b/impl/src/main/java/io/jsonwebtoken/impl/security/ECCurve.java index 9b580107a..2ad9db553 100644 --- a/impl/src/main/java/io/jsonwebtoken/impl/security/ECCurve.java +++ b/impl/src/main/java/io/jsonwebtoken/impl/security/ECCurve.java @@ -61,7 +61,7 @@ public boolean contains(ECPoint point) { } @Override - public KeyPairBuilder keyPairBuilder() { + public KeyPairBuilder keyPair() { return new DefaultKeyPairBuilder(KEY_PAIR_GENERATOR_JCA_NAME, toParameterSpec()); } } diff --git a/impl/src/main/java/io/jsonwebtoken/impl/security/EcSignatureAlgorithm.java b/impl/src/main/java/io/jsonwebtoken/impl/security/EcSignatureAlgorithm.java index 8b40485e0..b5d65603c 100644 --- a/impl/src/main/java/io/jsonwebtoken/impl/security/EcSignatureAlgorithm.java +++ b/impl/src/main/java/io/jsonwebtoken/impl/security/EcSignatureAlgorithm.java @@ -100,7 +100,7 @@ public EcSignatureAlgorithm(int orderBitLength) { } @Override - public KeyPairBuilder keyPairBuilder() { + public KeyPairBuilder keyPair() { return new DefaultKeyPairBuilder(ECCurve.KEY_PAIR_GENERATOR_JCA_NAME, this.KEY_PAIR_GEN_PARAMS) .provider(getProvider()) .random(Randoms.secureRandom()); diff --git a/impl/src/main/java/io/jsonwebtoken/impl/security/EcdhKeyAlgorithm.java b/impl/src/main/java/io/jsonwebtoken/impl/security/EcdhKeyAlgorithm.java index e763c4e3d..84fdae4eb 100644 --- a/impl/src/main/java/io/jsonwebtoken/impl/security/EcdhKeyAlgorithm.java +++ b/impl/src/main/java/io/jsonwebtoken/impl/security/EcdhKeyAlgorithm.java @@ -98,7 +98,7 @@ protected KeyPair generateKeyPair(final Request> request, final ECParameterSpe //visible for testing, for Edwards elliptic curves protected KeyPair generateKeyPair(SecureRandom random, EdwardsCurve curve, Provider provider) { - return curve.keyPairBuilder().provider(provider).random(random).build(); + return curve.keyPair().provider(provider).random(random).build(); } protected byte[] generateZ(final KeyRequest> request, final PublicKey pub, final PrivateKey priv) { diff --git a/impl/src/main/java/io/jsonwebtoken/impl/security/EdSignatureAlgorithm.java b/impl/src/main/java/io/jsonwebtoken/impl/security/EdSignatureAlgorithm.java index 590b84d74..c9d68b18a 100644 --- a/impl/src/main/java/io/jsonwebtoken/impl/security/EdSignatureAlgorithm.java +++ b/impl/src/main/java/io/jsonwebtoken/impl/security/EdSignatureAlgorithm.java @@ -64,8 +64,8 @@ protected String getJcaName(Request> request) { } @Override - public KeyPairBuilder keyPairBuilder() { - return this.preferredCurve.keyPairBuilder(); + public KeyPairBuilder keyPair() { + return this.preferredCurve.keyPair(); } @Override diff --git a/impl/src/main/java/io/jsonwebtoken/impl/security/EdwardsCurve.java b/impl/src/main/java/io/jsonwebtoken/impl/security/EdwardsCurve.java index d5a06ed9a..b5cd00ac2 100644 --- a/impl/src/main/java/io/jsonwebtoken/impl/security/EdwardsCurve.java +++ b/impl/src/main/java/io/jsonwebtoken/impl/security/EdwardsCurve.java @@ -357,7 +357,7 @@ public boolean isSignatureCurve() { } @Override - public KeyPairBuilder keyPairBuilder() { + public KeyPairBuilder keyPair() { return new DefaultKeyPairBuilder(getJcaName(), KEY_PAIR_GENERATOR_BIT_LENGTH).provider(getProvider()); } diff --git a/impl/src/main/java/io/jsonwebtoken/impl/security/EdwardsPublicKeyDeriver.java b/impl/src/main/java/io/jsonwebtoken/impl/security/EdwardsPublicKeyDeriver.java index de257a20a..5fccab1d4 100644 --- a/impl/src/main/java/io/jsonwebtoken/impl/security/EdwardsPublicKeyDeriver.java +++ b/impl/src/main/java/io/jsonwebtoken/impl/security/EdwardsPublicKeyDeriver.java @@ -51,7 +51,7 @@ public PublicKey apply(PrivateKey privateKey) { // Since we already have a private key, we provide a RNG that 'generates' the existing private key // instead of a random one, and the corresponding public key will be computed for us automatically. SecureRandom random = new ConstantRandom(pkBytes); - KeyPair pair = curve.keyPairBuilder().random(random).build(); + KeyPair pair = curve.keyPair().random(random).build(); Assert.stateNotNull(pair, "Edwards curve generated keypair cannot be null."); return Assert.stateNotNull(pair.getPublic(), "Edwards curve KeyPair must have a PublicKey"); } diff --git a/impl/src/main/java/io/jsonwebtoken/impl/security/HmacAesAeadAlgorithm.java b/impl/src/main/java/io/jsonwebtoken/impl/security/HmacAesAeadAlgorithm.java index 9ca92f564..e64527f21 100644 --- a/impl/src/main/java/io/jsonwebtoken/impl/security/HmacAesAeadAlgorithm.java +++ b/impl/src/main/java/io/jsonwebtoken/impl/security/HmacAesAeadAlgorithm.java @@ -66,7 +66,7 @@ public int getKeyBitLength() { } @Override - public SecretKeyBuilder keyBuilder() { + public SecretKeyBuilder key() { // The Sun JCE KeyGenerator throws an exception if bitLengths are not standard AES 128, 192 or 256 values. // Since the JWA HmacAes algorithms require double that, we use secure-random keys instead: return new RandomSecretKeyBuilder(KEY_ALG_NAME, getKeyBitLength()); diff --git a/impl/src/main/java/io/jsonwebtoken/impl/security/RsaSignatureAlgorithm.java b/impl/src/main/java/io/jsonwebtoken/impl/security/RsaSignatureAlgorithm.java index 2dc049c6d..8e2f692e8 100644 --- a/impl/src/main/java/io/jsonwebtoken/impl/security/RsaSignatureAlgorithm.java +++ b/impl/src/main/java/io/jsonwebtoken/impl/security/RsaSignatureAlgorithm.java @@ -78,7 +78,7 @@ public Signature get() throws Exception { } @Override - public KeyPairBuilder keyPairBuilder() { + public KeyPairBuilder keyPair() { return new DefaultKeyPairBuilder("RSA", this.preferredKeyBitLength) .provider(getProvider()) .random(Randoms.secureRandom()); diff --git a/impl/src/test/groovy/io/jsonwebtoken/JwtsTest.groovy b/impl/src/test/groovy/io/jsonwebtoken/JwtsTest.groovy index 3ba0f2c83..cc9b74e0d 100644 --- a/impl/src/test/groovy/io/jsonwebtoken/JwtsTest.groovy +++ b/impl/src/test/groovy/io/jsonwebtoken/JwtsTest.groovy @@ -290,7 +290,7 @@ class JwtsTest { @Test void testParseWithMissingRequiredSignature() { - Key key = Jwts.SIG.HS256.keyBuilder().build() + Key key = Jwts.SIG.HS256.key().build() String compact = Jwts.builder().setSubject('foo').signWith(key).compact() int i = compact.lastIndexOf('.') String missingSig = compact.substring(0, i + 1) @@ -428,7 +428,7 @@ class JwtsTest { void testUncompressedJwt() { def alg = Jwts.SIG.HS256 - SecretKey key = alg.keyBuilder().build() + SecretKey key = alg.key().build() String id = UUID.randomUUID().toString() @@ -450,7 +450,7 @@ class JwtsTest { void testCompressedJwtWithDeflate() { def alg = Jwts.SIG.HS256 - SecretKey key = alg.keyBuilder().build() + SecretKey key = alg.key().build() String id = UUID.randomUUID().toString() @@ -472,7 +472,7 @@ class JwtsTest { void testCompressedJwtWithGZIP() { def alg = Jwts.SIG.HS256 - SecretKey key = alg.keyBuilder().build() + SecretKey key = alg.key().build() String id = UUID.randomUUID().toString() @@ -494,7 +494,7 @@ class JwtsTest { void testCompressedWithCustomResolver() { def alg = Jwts.SIG.HS256 - SecretKey key = alg.keyBuilder().build() + SecretKey key = alg.key().build() String id = UUID.randomUUID().toString() @@ -533,7 +533,7 @@ class JwtsTest { void testCompressedJwtWithUnrecognizedHeader() { def alg = Jwts.SIG.HS256 - SecretKey key = alg.keyBuilder().build() + SecretKey key = alg.key().build() String id = UUID.randomUUID().toString() @@ -552,7 +552,7 @@ class JwtsTest { void testCompressStringPayloadWithDeflate() { def alg = Jwts.SIG.HS256 - SecretKey key = alg.keyBuilder().build() + SecretKey key = alg.key().build() String payload = "this is my test for a payload" @@ -673,8 +673,8 @@ class JwtsTest { void testParseClaimsJwsWithWeakHmacKey() { def alg = Jwts.SIG.HS384 - def key = alg.keyBuilder().build() - def weakKey = Jwts.SIG.HS256.keyBuilder().build() + def key = alg.key().build() + def weakKey = Jwts.SIG.HS256.key().build() String jws = Jwts.builder().setSubject("Foo").signWith(key, alg).compact() @@ -744,7 +744,7 @@ class JwtsTest { def withoutSignature = "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZXN0IjoidGVzdCIsImlhdCI6MTQ2NzA2NTgyN30" def invalidEncodedSignature = "_____wAAAAD__________7zm-q2nF56E87nKwvxjJVH_____AAAAAP__________vOb6racXnoTzucrC_GMlUQ" String jws = withoutSignature + '.' + invalidEncodedSignature - def keypair = Jwts.SIG.ES256.keyPairBuilder().build() + def keypair = Jwts.SIG.ES256.keyPair().build() Jwts.parser().setSigningKey(keypair.public).build().parseClaimsJws(jws) } @@ -754,7 +754,7 @@ class JwtsTest { //create random signing key for testing: def alg = Jwts.SIG.HS256 - SecretKey key = alg.keyBuilder().build() + SecretKey key = alg.key().build() String notSigned = Jwts.builder().setSubject("Foo").compact() @@ -1025,8 +1025,8 @@ class JwtsTest { def id = realAlg.getId() + 'X' // custom id def alg = new MacAlgorithm() { @Override - SecretKeyBuilder keyBuilder() { - return realAlg.keyBuilder() + SecretKeyBuilder key() { + return realAlg.key() } @Override @@ -1065,7 +1065,7 @@ class JwtsTest { @Test void testParseJweWithCustomEncryptionAlgorithm() { def realAlg = Jwts.ENC.A128GCM // any alg will do, we're going to wrap it - def key = realAlg.keyBuilder().build() + def key = realAlg.key().build() def enc = realAlg.getId() + 'X' // custom id def encAlg = new AeadAlgorithm() { @Override @@ -1084,8 +1084,8 @@ class JwtsTest { } @Override - SecretKeyBuilder keyBuilder() { - return realAlg.keyBuilder() + SecretKeyBuilder key() { + return realAlg.key() } @Override @@ -1165,7 +1165,7 @@ class JwtsTest { //create random signing key for testing: def alg = Jwts.SIG.HS256 - SecretKey key = alg.keyBuilder().build() + SecretKey key = alg.key().build() //this is a 'real', valid JWT: String compact = Jwts.builder().setSubject("Joe").signWith(key, alg).compact() @@ -1300,8 +1300,8 @@ class JwtsTest { for (AeadAlgorithm enc : Jwts.ENC.get().values()) { SecretKey key = alg instanceof SecretKeyAlgorithm ? - ((SecretKeyAlgorithm) alg).keyBuilder().build() : - enc.keyBuilder().build() + ((SecretKeyAlgorithm) alg).key().build() : + enc.key().build() // encrypt: String jwe = Jwts.builder() @@ -1328,7 +1328,7 @@ class JwtsTest { for (AeadAlgorithm enc : Jwts.ENC.get().values()) { - SecretKey key = enc.keyBuilder().build() + SecretKey key = enc.key().build() // encrypt and compress: String jwe = Jwts.builder() @@ -1587,7 +1587,7 @@ class JwtsTest { static void testHmac(MacAlgorithm alg) { //create random signing key for testing: - SecretKey key = alg.keyBuilder().build() + SecretKey key = alg.key().build() def claims = new DefaultClaims([iss: 'joe', exp: later(), 'https://example.com/is_root': true]) diff --git a/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJweTest.groovy b/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJweTest.groovy index 67a3f2290..d843995a0 100644 --- a/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJweTest.groovy +++ b/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJweTest.groovy @@ -28,7 +28,7 @@ class DefaultJweTest { @Test void testToString() { def alg = Jwts.ENC.A128CBC_HS256 as AeadAlgorithm - def key = alg.keyBuilder().build() + def key = alg.key().build() String compact = Jwts.builder().claim('foo', 'bar').encryptWith(key, alg).compact() def jwe = Jwts.parser().decryptWith(key).build().parseClaimsJwe(compact) String encodedIv = Encoders.BASE64URL.encode(jwe.initializationVector) @@ -40,7 +40,7 @@ class DefaultJweTest { @Test void testEqualsAndHashCode() { def alg = Jwts.ENC.A128CBC_HS256 as AeadAlgorithm - def key = alg.keyBuilder().build() + def key = alg.key().build() String compact = Jwts.builder().claim('foo', 'bar').encryptWith(key, alg).compact() def parser = Jwts.parser().decryptWith(key).build() def jwe1 = parser.parseClaimsJwe(compact) diff --git a/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwsTest.groovy b/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwsTest.groovy index b2393c5f3..179f11d00 100644 --- a/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwsTest.groovy +++ b/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwsTest.groovy @@ -36,7 +36,7 @@ class DefaultJwsTest { void testToString() { //create random signing key for testing: def alg = Jwts.SIG.HS256 - def key = alg.keyBuilder().build() + def key = alg.key().build() String compact = Jwts.builder().claim('foo', 'bar').signWith(key, alg).compact() int i = compact.lastIndexOf('.') String signature = compact.substring(i + 1) @@ -47,7 +47,7 @@ class DefaultJwsTest { @Test void testEqualsAndHashCode() { def alg = Jwts.SIG.HS256 - def key = alg.keyBuilder().build() + def key = alg.key().build() String compact = Jwts.builder().claim('foo', 'bar').signWith(key, alg).compact() def parser = Jwts.parser().verifyWith(key).build() def jws1 = parser.parseClaimsJws(compact) diff --git a/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtBuilderTest.groovy b/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtBuilderTest.groovy index 975e45ad5..acadf4d78 100644 --- a/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtBuilderTest.groovy +++ b/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtBuilderTest.groovy @@ -70,7 +70,7 @@ class DefaultJwtBuilderTest { } @Override - KeyPairBuilder keyPairBuilder() { + KeyPairBuilder keyPair() { throw new IllegalStateException("should not be called during build") } @@ -82,7 +82,7 @@ class DefaultJwtBuilderTest { replay provider def b = new DefaultJwtBuilder().provider(provider) - .setSubject('me').signWith(Jwts.SIG.HS256.keyBuilder().build(), alg) + .setSubject('me').signWith(Jwts.SIG.HS256.key().build(), alg) assertSame provider, b.provider b.compact() verify provider @@ -113,7 +113,7 @@ class DefaultJwtBuilderTest { } @Override - KeyPairBuilder keyPairBuilder() { + KeyPairBuilder keyPair() { throw new IllegalStateException("should not be called during build") } @@ -124,7 +124,7 @@ class DefaultJwtBuilderTest { } def b = new DefaultJwtBuilder().random(random) - .setSubject('me').signWith(Jwts.SIG.HS256.keyBuilder().build(), alg) + .setSubject('me').signWith(Jwts.SIG.HS256.key().build(), alg) assertSame random, b.secureRandom b.compact() assertTrue called[0] @@ -483,7 +483,7 @@ class DefaultJwtBuilderTest { @Test void testCompactSimplestPayload() { def enc = Jwts.ENC.A128GCM - def key = enc.keyBuilder().build() + def key = enc.key().build() def jwe = builder.setPayload("me").encryptWith(key, enc).compact() def jwt = Jwts.parser().decryptWith(key).build().parseContentJwe(jwe) assertEquals 'me', new String(jwt.getPayload(), StandardCharsets.UTF_8) @@ -492,7 +492,7 @@ class DefaultJwtBuilderTest { @Test void testCompactSimplestClaims() { def enc = Jwts.ENC.A128GCM - def key = enc.keyBuilder().build() + def key = enc.key().build() def jwe = builder.setSubject('joe').encryptWith(key, enc).compact() def jwt = Jwts.parser().decryptWith(key).build().parseClaimsJwe(jwe) assertEquals 'joe', jwt.getPayload().getSubject() diff --git a/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtParserBuilderTest.groovy b/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtParserBuilderTest.groovy index 44f620b51..6db65e00c 100644 --- a/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtParserBuilderTest.groovy +++ b/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtParserBuilderTest.groovy @@ -121,7 +121,7 @@ class DefaultJwtParserBuilderTest { assertSame deserializer, p.deserializer def alg = Jwts.SIG.HS256 - def key = alg.keyBuilder().build() + def key = alg.key().build() String jws = Jwts.builder().claim('foo', 'bar').signWith(key, alg).compact() diff --git a/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtParserTest.groovy b/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtParserTest.groovy index 76ce935aa..074eee389 100644 --- a/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtParserTest.groovy +++ b/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtParserTest.groovy @@ -66,7 +66,7 @@ class DefaultJwtParserTest { assertTrue("Expected wrapping deserializer to be instance of JwtDeserializer", p.deserializer instanceof JwtDeserializer ) assertSame deserializer, p.deserializer.deserializer - def key = Jwts.SIG.HS256.keyBuilder().build() + def key = Jwts.SIG.HS256.key().build() String jws = Jwts.builder().claim('foo', 'bar').signWith(key, Jwts.SIG.HS256).compact() diff --git a/impl/src/test/groovy/io/jsonwebtoken/impl/security/AbstractEcJwkFactoryTest.groovy b/impl/src/test/groovy/io/jsonwebtoken/impl/security/AbstractEcJwkFactoryTest.groovy index 835fffd6f..46a1833a6 100644 --- a/impl/src/test/groovy/io/jsonwebtoken/impl/security/AbstractEcJwkFactoryTest.groovy +++ b/impl/src/test/groovy/io/jsonwebtoken/impl/security/AbstractEcJwkFactoryTest.groovy @@ -83,7 +83,7 @@ class AbstractEcJwkFactoryTest { @Test void testAddSamePointDoublesIt() { - def pair = Jwts.SIG.ES256.keyPairBuilder().build() + def pair = Jwts.SIG.ES256.keyPair().build() def pub = pair.getPublic() as ECPublicKey def spec = pub.getParams() @@ -98,7 +98,7 @@ class AbstractEcJwkFactoryTest { @Test void testDerivePublicFails() { - def pair = Jwts.SIG.ES256.keyPairBuilder().build() + def pair = Jwts.SIG.ES256.keyPair().build() def priv = pair.getPrivate() as ECPrivateKey final def context = new DefaultJwkContext(DefaultEcPrivateJwk.FIELDS) diff --git a/impl/src/test/groovy/io/jsonwebtoken/impl/security/AbstractSecureDigestAlgorithmTest.groovy b/impl/src/test/groovy/io/jsonwebtoken/impl/security/AbstractSecureDigestAlgorithmTest.groovy index 4fd0b19df..288297779 100644 --- a/impl/src/test/groovy/io/jsonwebtoken/impl/security/AbstractSecureDigestAlgorithmTest.groovy +++ b/impl/src/test/groovy/io/jsonwebtoken/impl/security/AbstractSecureDigestAlgorithmTest.groovy @@ -32,7 +32,7 @@ class AbstractSecureDigestAlgorithmTest { @Test void testSignAndVerifyWithExplicitProvider() { Provider provider = Security.getProvider('BC') - def pair = Jwts.SIG.RS256.keyPairBuilder().build() + def pair = Jwts.SIG.RS256.keyPair().build() byte[] data = 'foo'.getBytes(StandardCharsets.UTF_8) byte[] signature = Jwts.SIG.RS256.digest(new DefaultSecureRequest@@ -82,33 +82,33 @@ public interface JwtBuilder extends ClaimsMutator{ * .add("aName", aValue) * .add(myHeaderMap) * // ... etc ... - * .{@link JwtBuilder.Header#and() and()}
+ * {@link #header()}.{@link MapMutator#empty() empty()}.{@link MapMutator#add(Map) add(map)}.{@link BuilderHeader#and() and()} * *- * {@link #header()}.{@link MapMutator#empty() empty()}.{@link MapMutator#add(Map) add(map)}.{@link Header#and() and()}
If you do not want to replace the existing header and only want to append to it,
- * call {@link #header()}.{@link io.jsonwebtoken.lang.MapMutator#add(Map) add(map)}.{@link Header#and() and()} instead.
{@link #header()}.{@link io.jsonwebtoken.lang.MapMutator#add(Map) add(map)}.{@link BuilderHeader#and() and()} instead.
*
* @param map the name/value pairs to set as (and potentially replace) the constructed JWT header.
* @return the builder for method chaining.
* @deprecated since JJWT_RELEASE_VERSION in favor of
- * {@link #header()}.{@link MapMutator#empty() empty()}.{@link MapMutator#add(Map) add(map)}.{@link Header#and() and()}
+ * {@link #header()}.{@link MapMutator#empty() empty()}.{@link MapMutator#add(Map) add(map)}.{@link BuilderHeader#and() and()}
* (to replace all header parameters) or
- * {@link #header()}.{@link MapMutator#add(Map) add(map)}.{@link Header#and() and()}
+ * {@link #header()}.{@link MapMutator#add(Map) add(map)}.{@link BuilderHeader#and() and()}
* to only append the {@code map} entries. This method will be removed before the 1.0 release.
*/
@SuppressWarnings("DeprecatedIsStillUsed")
@@ -119,12 +119,12 @@ public interface JwtBuilder extends ClaimsMutator {
* Adds the specified name/value pairs to the header. Any parameter with an empty or null value will remove the
* entry from the header. This is a wrapper method for:
*
- * {@link #header()}.{@link MapMutator#add(Map) add(map)}.{@link Header#and() and()}
+ * {@link #header()}.{@link MapMutator#add(Map) add(map)}.{@link BuilderHeader#and() and()}
*
* @param params the header name/value pairs to append to the header.
* @return the builder for method chaining.
* @deprecated since JJWT_RELEASE_VERSION in favor of
- * {@link #header()}.{@link MapMutator#add(Map) add(map)}.{@link Header#and() and()}
.
+ * {@link #header()}.{@link MapMutator#add(Map) add(map)}.{@link BuilderHeader#and() and()}
.
* This method will be removed before the 1.0 release.
*/
@SuppressWarnings("DeprecatedIsStillUsed")
@@ -135,13 +135,13 @@ public interface JwtBuilder extends ClaimsMutator {
* Adds the specified name/value pair to the header. If the value is {@code null} or empty, the parameter will
* be removed from the header entirely. This is a wrapper method for:
*
- * {@link #header()}.{@link MapMutator#add(Object, Object) add(name, value)}.{@link Header#and() and()}
+ * {@link #header()}.{@link MapMutator#add(Object, Object) add(name, value)}.{@link BuilderHeader#and() and()}
*
* @param name the header parameter name
* @param value the header parameter value
* @return the builder for method chaining.
* @deprecated since JJWT_RELEASE_VERSION in favor of
- * {@link #header()}.{@link MapMutator#add(Object, Object) add(name, value)}.{@link Header#and() and()}
.
+ * {@link #header()}.{@link MapMutator#add(Object, Object) add(name, value)}.{@link BuilderHeader#and() and()}
.
* This method will be removed before the 1.0 release.
*/
@SuppressWarnings("DeprecatedIsStillUsed")
@@ -150,7 +150,7 @@ public interface JwtBuilder extends ClaimsMutatorIf for some reason you do not wish to adhere to the JWT specification recommendation, do not call this * method - instead call {@link #content(byte[])} and set the header's - * {@link Header#contentType(String) contentType} independently. For example:
+ * {@link BuilderHeader#contentType(String) contentType} independently. For example: * ** Jwts.builder() @@ -237,7 +237,7 @@ public interface JwtBuilder extends ClaimsMutator{ /** * Returns the JWT {@code Claims} payload to modify as desired. When finished, callers may - * return to {@code JwtBuilder} configuration via the {@link JwtBuilder.Claims#and() and()} method. + * return to {@code JwtBuilder} configuration via the {@link BuilderClaims#and() and()} method. * For example: * * @@ -250,16 +250,16 @@ public interface JwtBuilder extends ClaimsMutator{ * .add("customClaim", customValue) * .add(myClaimsMap) * // ... etc ... - * .{@link JwtBuilder.Claims#and() and()}
+ * {@link #claims()}.{@link MapMutator#add(Map) add(claims)}.{@link BuilderClaims#and() and()} * *- * {@link #claims()}.{@link MapMutator#add(Map) add(claims)}.{@link Claims#and() and()}
The content and claims properties are mutually exclusive - only one of the two may be used.
* @@ -290,7 +290,7 @@ public interface JwtBuilder extends ClaimsMutator{@link #claims()}.{@link Claims#add(Map) add(Map)}.{@link Claims#and() and()}
.
+ * {@link #claims()}.{@link BuilderClaims#add(Map) add(Map)}.{@link BuilderClaims#and() and()}
.
* This method will be removed before the 1.0 release.
*/
@SuppressWarnings("DeprecatedIsStillUsed")
@@ -301,7 +301,7 @@ public interface JwtBuilder extends ClaimsMutator+ * {@link #claims()}.{@link MapMutator#add(Object, Object) add(name, value)}.{@link BuilderClaims#and() and()} * * @param name the JWT Claims property name * @param value the value to set for the specified Claims property name @@ -315,7 +315,7 @@ public interface JwtBuilder extends ClaimsMutator- * {@link #claims()}.{@link MapMutator#add(Object, Object) add(name, value)}.{@link Claims#and() and()}
+ * {@link #claims()}.{@link MapMutator#add(Map) add(claims)}.{@link BuilderClaims#and() and()} * *- * {@link #claims()}.{@link MapMutator#add(Map) add(claims)}.{@link Claims#and() and()}
The content and claims properties are mutually exclusive - only one of the two may be used.
* @@ -330,7 +330,7 @@ public interface JwtBuilder extends ClaimsMutatoriss
+ * {@link #claims()}.{@link ClaimsMutator#issuer(String) issuer(iss)}.{@link BuilderClaims#and() and()} * * @param iss the JWT {@code iss} value or {@code null} to remove the property from the Claims map. * @return the builder instance for method chaining. @@ -344,7 +344,7 @@ public interface JwtBuilder extends ClaimsMutator- * {@link #claims()}.{@link ClaimsMutator#issuer(String) issuer(iss)}.{@link Claims#and() and()}
sub
+ * {@link #claims()}.{@link ClaimsMutator#subject(String) subject(sub)}.{@link BuilderClaims#and() and()} * * @param sub the JWT {@code sub} value or {@code null} to remove the property from the Claims map. * @return the builder instance for method chaining. @@ -358,7 +358,7 @@ public interface JwtBuilder extends ClaimsMutator- * {@link #claims()}.{@link ClaimsMutator#subject(String) subject(sub)}.{@link Claims#and() and()}
aud
(audience) value. A {@code null} value will remove the property from the Claims.
* This is a convenience wrapper for:
* + * {@link #claims()}.{@link ClaimsMutator#audience(String) audience(aud)}.{@link BuilderClaims#and() and()} * * @param aud the JWT {@code aud} value or {@code null} to remove the property from the Claims map. * @return the builder instance for method chaining. @@ -375,7 +375,7 @@ public interface JwtBuilder extends ClaimsMutator- * {@link #claims()}.{@link ClaimsMutator#audience(String) audience(aud)}.{@link Claims#and() and()}
This is a convenience wrapper for:
*+ * {@link #claims()}.{@link ClaimsMutator#expiration(Date) expiration(exp)}.{@link BuilderClaims#and() and()} * * @param exp the JWT {@code exp} value or {@code null} to remove the property from the Claims map. * @return the builder instance for method chaining. @@ -392,7 +392,7 @@ public interface JwtBuilder extends ClaimsMutator- * {@link #claims()}.{@link ClaimsMutator#expiration(Date) expiration(exp)}.{@link Claims#and() and()}
This is a convenience wrapper for:
*+ * {@link #claims()}.{@link ClaimsMutator#notBefore(Date) notBefore(nbf)}.{@link BuilderClaims#and() and()} * * @param nbf the JWT {@code nbf} value or {@code null} to remove the property from the Claims map. * @return the builder instance for method chaining. @@ -409,7 +409,7 @@ public interface JwtBuilder extends ClaimsMutator- * {@link #claims()}.{@link ClaimsMutator#notBefore(Date) notBefore(nbf)}.{@link Claims#and() and()}
This is a convenience wrapper for:
*+ * {@link #claims()}.{@link ClaimsMutator#issuedAt(Date) issuedAt(iat)}.{@link BuilderClaims#and() and()} * * @param iat the JWT {@code iat} value or {@code null} to remove the property from the Claims map. * @return the builder instance for method chaining. @@ -428,7 +428,7 @@ public interface JwtBuilder extends ClaimsMutator- * {@link #claims()}.{@link ClaimsMutator#issuedAt(Date) issuedAt(iat)}.{@link Claims#and() and()}
This is a convenience wrapper for:
*+ * {@link #claims()}.{@link ClaimsMutator#id(String) id(jti)}.{@link BuilderClaims#and() and()} * * @param jti the JWT {@code jti} (id) value or {@code null} to remove the property from the Claims map. * @return the builder instance for method chaining. @@ -874,13 +874,13 @@ public interface JwtBuilder extends ClaimsMutator- * {@link #claims()}.{@link ClaimsMutator#id(String) id(jti)}.{@link Claims#and() and()}
extends DefaultProtectedJwt extends DefaultProtectedJwt There may be only one registered {@code CompressionAlgorithm} per {@code id}, and the {@code algs}
- * collection is added in iteration order; if a duplicate id is found when iterating the {@code algs}
- * collection, the later element will evict any previously-added algorithm with the same {@code id}. There may be only one registered {@code CompressionAlgorithm} per CaSe-SeNsItIvE {@code id}, and the
+ * {@code algs} collection is added in iteration order; if a duplicate id is found when iterating the {@code algs}
+ * collection, the later algorithm will evict any existing algorithm with the same {@code id}. Standard Algorithms and Overrides Finally, {@link Jwts.ZIP#DEF} and {@link Jwts.ZIP#GZIP} algorithms are added last,
- * after those in the {@code algs} collection, to ensure that JWA standard algorithms cannot be
- * accidentally replaced. The {@link Jwts.ZIP} compression algorithms are supported by default and do not need
+ * to be added via this method, but beware: any algorithm in the {@code algs} collection with a
+ * JWA standard {@link Identifiable#getId() id} will replace the JJWT standard algorithm implementation.
+ * This is to allow application developers to favor their own implementations over JJWT's default implementations
+ * if necessary (for example, to support legacy or custom behavior).
*
- * @param algs collection of compression algorithms to add to the parser's total set of supported compression algorithms.
+ * @param algs collection of compression algorithms to add to the parser's total set of supported compression
+ * algorithms, replacing any existing ones with the same exact (CaSe-SeNsItIvE)
+ * {@link CompressionAlgorithm#getId() id}s.
* @return the builder for method chaining.
+ * @see Jwts.ZIP
* @since JJWT_RELEASE_VERSION
*/
JwtParserBuilder addCompressionAlgorithms(Collection extends CompressionAlgorithm> algs);
/**
* Adds the specified AEAD encryption algorithms to the parser's total set of supported encryption algorithms,
- * overwriting any previously-added algorithms with the same {@link AeadAlgorithm#getId() id}s.
+ * replacing any existing algorithms with the same exact (CaSe-SeNsItIvE) {@link AeadAlgorithm#getId() id}s.
+ * If the parser encounters a JWT {@code enc} header value that equals an encryption algorithm's
+ * {@link Identifiable#getId() id}, that algorithm will be used for decryption.
+ *
+ * There may be only one registered {@code AeadAlgorithm} per algorithm {@code id}, and the {@code algs}
+ * collection is added in iteration order; if a duplicate id is found when iterating the {@code algs}
+ * collection, the later algorithm will evict any existing algorithm with the same {@code id}. There may be only one registered {@code AeadAlgorithm} per algorithm {@code id}, and the {@code encAlgs}
- * collection is added in iteration order; if a duplicate id is found when iterating the {@code encAlgs}
- * collection, the later element will evict any previously-added algorithm with the same {@code id}. Standard Algorithms and Overrides Finally, the {@link Jwts.ENC JWA standard encryption algorithms} are added last,
- * after those in the {@code encAlgs} collection, to ensure that JWA standard algorithms cannot be
- * accidentally replaced. All JWA standard encryption algorithms in {@link Jwts.ENC} are supported by default and do not need
+ * to be added via this method, but beware: any algorithm in the {@code algs} collection with a
+ * JWA standard {@link Identifiable#getId() id} will replace the JJWT standard algorithm implementation.
+ * This is to allow application developers to favor their own implementations over JJWT's default implementations
+ * if necessary (for example, to support legacy or custom behavior).
*
- * @param encAlgs collection of AEAD encryption algorithms to add to the parser's total set of supported
- * encryption algorithms.
+ * @param algs collection of AEAD encryption algorithms to add to the parser's total set of supported
+ * encryption algorithms, replacing any existing algorithms with the same
+ * {@link AeadAlgorithm#getId() id}s.
* @return the builder for method chaining.
* @since JJWT_RELEASE_VERSION
*/
- JwtParserBuilder addEncryptionAlgorithms(Collection extends AeadAlgorithm> encAlgs);
+ JwtParserBuilder addEncryptionAlgorithms(Collection extends AeadAlgorithm> algs);
/**
* Adds the specified signature algorithms to the parser's total set of supported signature algorithms,
- * overwriting any previously-added algorithms with the same
- * {@link Identifiable#getId() id}s.
+ * replacing any existing algorithms with the same exact (CaSe-SeNsItIvE) {@link Identifiable#getId() id}s.
+ * If the parser encounters a JWS {@code alg} header value that equals a signature algorithm's
+ * {@link Identifiable#getId() id}, that algorithm will be used for signature verification.
*
* There may be only one registered {@code SecureDigestAlgorithm} per algorithm {@code id}, and the
- * {@code sigAlgs} collection is added in iteration order; if a duplicate id is found when iterating the
- * {@code sigAlgs} collection, the later element will evict any previously-added algorithm with the same
- * {@code id}. Finally, the {@link Jwts.SIG JWA standard signature and MAC algorithms} are
- * added last, after those in the {@code sigAlgs} collection, to ensure that JWA standard algorithms
- * cannot be accidentally replaced. Standard Algorithms and Overrides All JWA standard signature and MAC algorithms in {@link Jwts.SIG} are supported by default and do not need
+ * to be added via this method, but beware: any algorithm in the {@code algs} collection with a
+ * JWA standard {@link Identifiable#getId() id} will replace the JJWT standard
+ * algorithm implementation. This is to allow application developers to favor their own implementations over
+ * JJWT's default implementations if necessary (for example, to support legacy or custom behavior).
+ *
+ * @param algs collection of signature algorithms to add to the parser's total set of supported signature
+ * algorithms, replacing any existing algorithms with the same exact (CaSe-SeNsItIvE)
+ * {@link Identifiable#getId() id}s.
* @return the builder for method chaining.
+ * @see Algorithm Name (id) requirements
* @since JJWT_RELEASE_VERSION
*/
- JwtParserBuilder addSignatureAlgorithms(Collection extends SecureDigestAlgorithm, ?>> sigAlgs);
+ JwtParserBuilder addSignatureAlgorithms(Collection extends SecureDigestAlgorithm, ?>> algs);
/**
* Adds the specified key management algorithms to the parser's total set of supported key algorithms,
- * overwriting any previously-added algorithms with the same {@link KeyAlgorithm#getId() id}s.
+ * replacing any existing algorithms with the same exact (CaSe-SeNsItIvE) {@link KeyAlgorithm#getId() id}s.
+ * If the parser encounters a JWE {@code alg} header value that equals a key management algorithm's
+ * {@link Identifiable#getId() id}, that algorithm will be used to obtain the content decryption key.
+ *
+ * There may be only one registered {@code KeyAlgorithm} per algorithm {@code id}, and the {@code algs}
+ * collection is added in iteration order; if a duplicate id is found when iterating the {@code algs}
+ * argument, the later algorithm will evict any existing algorithm with the same {@code id}. There may be only one registered {@code KeyAlgorithm} per algorithm {@code id}, and the {@code keyAlgs}
- * collection is added in iteration order; if a duplicate id is found when iterating the {@code keyAlgs}
- * collection, the later element will evict any previously-added algorithm with the same {@code id}. Standard Algorithms and Overrides Finally, the {@link Jwts.KEY#get() JWA standard key management algorithms}
- * are added last, after those in the {@code keyAlgs} collection, to ensure that JWA standard algorithms
- * cannot be accidentally replaced. All JWA standard key management algorithms in {@link Jwts.KEY} are supported by default and do not need
+ * to be added via this method, but beware: any algorithm in the {@code algs} collection with a
+ * JWA standard {@link Identifiable#getId() id} will replace the JJWT standard algorithm implementation.
+ * This is to allow application developers to favor their own implementations over JJWT's default implementations
+ * if necessary (for example, to support legacy or custom behavior).
*
- * @param keyAlgs collection of key management algorithms to add to the parser's total set of supported key
- * management algorithms.
+ * @param algs collection of key management algorithms to add to the parser's total set of supported key
+ * management algorithms, replacing any existing algorithms with the same exact (CaSe-SeNsItIvE)
+ * {@link KeyAlgorithm#getId() id}s.
* @return the builder for method chaining.
* @since JJWT_RELEASE_VERSION
*/
- JwtParserBuilder addKeyAlgorithms(Collection extends KeyAlgorithm, ?>> keyAlgs);
+ JwtParserBuilder addKeyAlgorithms(Collection extends KeyAlgorithm, ?>> algs);
/**
* Deprecated as of JJWT JJWT_RELEASE_VERSION. This method will be removed before the 1.0 release. This method is provided for congruence with the other {@code chain} methods and is expected to be used when
+ * the calling code has a variable {@code PublicKey} reference. Based on the argument type, it will
+ * delegate to one of the following methods if possible:
+ * If the specified {@code chain} argument is not capable of being supported by one of those methods, an
+ * {@link UnsupportedKeyException} will be thrown. Type Parameters In addition to the public key type and automatically set the resulting builder's {@link PrivateJwkBuilder#publicKey(PublicKey) publicKey} with
+ * the pair's {@code PublicKey}. If the specified {@code key} argument is not capable of being supported by one of those methods, an
+ * {@link UnsupportedKeyException} will be thrown. Type Parameters In addition to the private key type If you do not want to replace the existing header and only want to append to it,
- * call
+ *
+ *
+ * A
, the public key's associated private key type
+ * B
is parameterized as well. This ensures that any subsequent call to the builder's
+ * {@link PublicJwkBuilder#privateKey(PrivateKey) privateKey} method will be type-safe. For example:
+ *
+ * @param the type of {@link PublicKey} provided by the created public JWK.
+ * @param the type of {@link PrivateKey} that may be paired with the {@link PublicKey} to produce a
+ * {@link PrivateJwk} if desired.
+ * @param chain the {@link X509Certificate} chain to inspect to find the {@link PublicKey} to represent as a
+ * {@link PublicJwk}.
+ * @return the builder coerced as a {@link PublicJwkBuilder} for continued method chaining.
+ * @throws UnsupportedKeyException if the specified key is not a supported type and cannot be used to delegate to
+ * other {@code key} methods.
+ * @see PublicJwk
+ * @see PrivateJwk
+ */
+ PublicJwkBuilder chain(ListJwks.builder().<EdECPublicKey, EdECPrivateKey>chain(edECPublicKeyX509CertificateChain)
+ * .privateKey(aPrivateKey) // <-- must be an EdECPrivateKey instance
+ * ... etc ...
+ * .build();
+ *
+ * B
, the private key's associated public key type
+ * A
is parameterized as well. This ensures that any subsequent call to the builder's
+ * {@link PrivateJwkBuilder#publicKey(PublicKey) publicKey} method will be type-safe. For example:
+ *
+ * @param the {@code keyPair} argument's {@link PublicKey} type
+ * @param the {@code keyPair} argument's {@link PrivateKey} type
+ * @param keyPair the {@code KeyPair} containing the public and private key
+ * @return the builder coerced as a {@link PrivateJwkBuilder} for continued method chaining.
+ * @throws UnsupportedKeyException if the specified {@code KeyPair}'s keys are not supported and cannot be used to
+ * delegate to other {@code key} methods.
+ * @see PublicJwk
+ * @see PrivateJwk
+ */
+ PrivateJwkBuilder keyPair(KeyPair keyPair)
+ throws UnsupportedKeyException;
+
/**
* Ensures the builder will create an {@link OctetPublicJwk} for the specified Edwards-curve {@code PublicKey}
* argument. The {@code PublicKey} must be an instance of one of the following:
@@ -227,22 +307,6 @@ public interface DynamicJwkBuilderJwks.builder().<EdECPublicKey, EdECPrivateKey>keyPair(anEdECKeyPair)
+ * .publicKey(aPublicKey) // <-- must be an EdECPublicKey instance
+ * ... etc ...
+ * .build();
{@link #header()}.{@link io.jsonwebtoken.lang.MapMutator#add(Map) add(map)}.{@link BuilderHeader#and() and()} instead.
{@link #header()}.{@link io.jsonwebtoken.lang.MapMutator#add(Map) add(map)}.{@link BuilderHeader#and() and()}
instead.
example;part="1/2"
.
*
* @param cty the JWT JOSE {@code cty} header value or {@code null} to remove the property from the JSON map.
- * @return the {@code Header} instance for method chaining.
+ * @return the instance for method chaining.
*/
T contentType(String cty);
+
+ /**
+ * Deprecated since of JJWT_RELEASE_VERSION, delegates to {@link #type(String)}.
+ *
+ * @param typ the JWT JOSE {@code typ} header value or {@code null} to remove the property from the JSON map.
+ * @return the instance for method chaining.
+ * @see #type(String)
+ * @deprecated since JJWT_RELEASE_VERSION in favor of the more modern builder-style {@link #type(String)} method.
+ * This method will be removed before the 1.0 release.
+ */
+ @Deprecated
+ T setType(String typ);
+
+ /**
+ * Deprecated as of JJWT_RELEASE_VERSION, delegates to {@link #contentType(String)}.
+ *
+ * @param cty the JWT JOSE {@code cty} header value or {@code null} to remove the property from the JSON map.
+ * @return the instance for method chaining.
+ * @see #contentType(String)
+ * @deprecated since JJWT_RELEASE_VERSION in favor of the more modern builder-style {@link #contentType(String)}.
+ */
+ @Deprecated
+ T setContentType(String cty);
+
+ /**
+ * Deprecated as of JJWT_RELEASE_VERSION, there is no need to set this any longer as the {@code JwtBuilder} will
+ * always set the {@code zip} header as necessary.
+ *
+ * @param zip the JWT compression algorithm {@code zip} value or {@code null} to remove the property from the JSON map.
+ * @return the instance for method chaining.
+ * @since 0.6.0
+ * @deprecated since JJWT_RELEASE_VERSION and will be removed before the 1.0 release.
+ */
+ @Deprecated
+ T setCompressionAlgorithm(String zip);
}
diff --git a/api/src/main/java/io/jsonwebtoken/ProtectedHeaderMutator.java b/api/src/main/java/io/jsonwebtoken/ProtectedHeaderMutator.java
index 206134b15..5c329a345 100644
--- a/api/src/main/java/io/jsonwebtoken/ProtectedHeaderMutator.java
+++ b/api/src/main/java/io/jsonwebtoken/ProtectedHeaderMutator.java
@@ -87,4 +87,28 @@ public interface ProtectedHeaderMutator"typ"
+ * @deprecated since JJWT_RELEASE_VERSION in favor of {@link #getType()}.
*/
+ @Deprecated
String TYPE = "typ";
/**
* JWT {@code Content Type} header parameter name: "cty"
+ * @deprecated since JJWT_RELEASE_VERSION in favor of {@link #getContentType()}.
*/
+ @Deprecated
String CONTENT_TYPE = "cty";
/**
@@ -67,12 +71,16 @@ public interface Header extends Map"zip"
+ * @deprecated since JJWT_RELEASE_VERSION in favor of {@link #getCompressionAlgorithm()}
*/
+ @Deprecated
String COMPRESSION_ALGORITHM = "zip";
/**
diff --git a/api/src/main/java/io/jsonwebtoken/JwsHeader.java b/api/src/main/java/io/jsonwebtoken/JwsHeader.java
index 676304259..08a2e704f 100644
--- a/api/src/main/java/io/jsonwebtoken/JwsHeader.java
+++ b/api/src/main/java/io/jsonwebtoken/JwsHeader.java
@@ -24,46 +24,73 @@ public interface JwsHeader extends ProtectedHeader {
/**
* JWS Algorithm Header name: the string literal alg
+ *
+ * @deprecated since JJWT_RELEASE_VERSION in favor of {@link #getAlgorithm()}
*/
+ @Deprecated
String ALGORITHM = "alg";
/**
* JWS JWK Set URL Header name: the string literal jku
+ *
+ * @deprecated since JJWT_RELEASE_VERSION in favor of {@link #getJwkSetUrl()}
*/
+ @Deprecated
String JWK_SET_URL = "jku";
/**
* JWS JSON Web Key Header name: the string literal jwk
+ *
+ * @deprecated since JJWT_RELEASE_VERSION in favor of {@link #getJwk()}
*/
+ @Deprecated
String JSON_WEB_KEY = "jwk";
/**
* JWS Key ID Header name: the string literal kid
+ *
+ * @deprecated since JJWT_RELEASE_VERSION in favor of {@link #getKeyId()}
*/
+ @Deprecated
String KEY_ID = "kid";
/**
* JWS X.509 URL Header name: the string literal x5u
+ *
+ * @deprecated since JJWT_RELEASE_VERSION in favor of {@link #getX509Url()}
*/
+ @Deprecated
String X509_URL = "x5u";
/**
* JWS X.509 Certificate Chain Header name: the string literal x5c
+ *
+ * @deprecated since JJWT_RELEASE_VERSION in favor of {@link #getX509CertificateChain()}
*/
+ @Deprecated
String X509_CERT_CHAIN = "x5c";
/**
* JWS X.509 Certificate SHA-1 Thumbprint Header name: the string literal x5t
+ *
+ * @deprecated since JJWT_RELEASE_VERSION in favor of {@link #getX509CertificateSha1Thumbprint()}
*/
+ @Deprecated
String X509_CERT_SHA1_THUMBPRINT = "x5t";
/**
* JWS X.509 Certificate SHA-256 Thumbprint Header name: the string literal x5t#S256
+ *
+ * @deprecated since JJWT_RELEASE_VERSION in favor of {@link #getX509CertificateSha256Thumbprint()}
*/
+ @Deprecated
String X509_CERT_SHA256_THUMBPRINT = "x5t#S256";
/**
* JWS Critical Header name: the string literal crit
+ *
+ * @deprecated since JJWT_RELEASE_VERSION in favor of {@link #getCritical()}
*/
+ @Deprecated
String CRITICAL = "crit";
}