Skip to content

Commit 04f642c

Browse files
committed
Migrated all uses of Jwts.SIG to Jws.alg
1 parent 5a8a2ac commit 04f642c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+461
-235
lines changed

README.adoc

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ import java.security.Key;
738738
739739
// We need a signing key, so we'll create one just for this example. Usually
740740
// the key would be read from your application configuration instead.
741-
SecretKey key = Jwts.SIG.HS256.key().build();
741+
SecretKey key = Jws.alg.HS256.key().build();
742742
743743
String jws = Jwts.builder().subject("Joe").signWith(key).compact();
744744
----
@@ -1611,7 +1611,7 @@ key algorithms:
16111611

16121612
^*2*.{sp}{fn-require-java15-plus}^
16131613

1614-
These are all represented as constants in the `io.jsonwebtoken.Jwts.SIG` registry class.
1614+
These are all represented as constants in the `io.jsonwebtoken.Jws.alg` registry class.
16151615

16161616
+++<a name="jws-key">++++++</a>+++
16171617

@@ -1705,7 +1705,7 @@ algorithm's `key()` builder method:
17051705

17061706
[,java]
17071707
----
1708-
SecretKey key = Jwts.SIG.HS256.key().build(); //or HS384.key() or HS512.key()
1708+
SecretKey key = Jws.alg.HS256.key().build(); //or HS384.key() or HS512.key()
17091709
----
17101710

17111711
Under the hood, JJWT uses the JCA default provider's `KeyGenerator` to create a secure-random key with the correct
@@ -1716,7 +1716,7 @@ as builder arguments. For example:
17161716

17171717
[,java]
17181718
----
1719-
SecretKey key = Jwts.SIG.HS256.key().provider(aProvider).random(aSecureRandom).build();
1719+
SecretKey key = Jws.alg.HS256.key().provider(aProvider).random(aSecureRandom).build();
17201720
----
17211721

17221722
If you need to save this new `SecretKey`, you can Base64 (or Base64URL) encode it:
@@ -1739,7 +1739,7 @@ algorithms, use an algorithm's respective `keyPair()` builder method:
17391739

17401740
[,java]
17411741
----
1742-
KeyPair keyPair = Jwts.SIG.RS256.keyPair().build(); //or RS384, RS512, PS256, etc...
1742+
KeyPair keyPair = Jws.alg.RS256.keyPair().build(); //or RS384, RS512, PS256, etc...
17431743
----
17441744

17451745
Once you've generated a `KeyPair`, you can use the private key (`keyPair.getPrivate()`) to create a JWS and the
@@ -1874,7 +1874,7 @@ that accepts the `SignatureAlgorithm` as an additional argument:
18741874
[,java]
18751875
----
18761876
1877-
.signWith(privateKey, Jwts.SIG.RS512) // <---
1877+
.signWith(privateKey, Jws.alg.RS512) // <---
18781878
18791879
.compact();
18801880
----
@@ -2062,7 +2062,7 @@ We need to do three things during creation:
20622062
[,java]
20632063
----
20642064
// create a test key for this example:
2065-
SecretKey testKey = Jwts.SIG.HS512.key().build();
2065+
SecretKey testKey = Jws.alg.HS512.key().build();
20662066
20672067
String message = "Hello World. It's a Beautiful Day!";
20682068
byte[] content = message.getBytes(StandardCharsets.UTF_8);
@@ -2105,7 +2105,7 @@ period (`.`) characters_*.
21052105
[,java]
21062106
----
21072107
// create a test key for this example:
2108-
SecretKey testKey = Jwts.SIG.HS512.key().build();
2108+
SecretKey testKey = Jws.alg.HS512.key().build();
21092109
21102110
String claimsString = "{\"sub\":\"joe\",\"iss\":\"me\"}";
21112111
@@ -3740,7 +3740,7 @@ Example:
37403740
[,java]
37413741
----
37423742
// Create a test key suitable for the desired HMAC-SHA algorithm:
3743-
MacAlgorithm alg = Jwts.SIG.HS512; //or HS384 or HS256
3743+
MacAlgorithm alg = Jws.alg.HS512; //or HS384 or HS256
37443744
SecretKey key = alg.key().build();
37453745
37463746
String message = "Hello World!";
@@ -3769,7 +3769,7 @@ public key:
37693769
[,java]
37703770
----
37713771
// Create a test key suitable for the desired RSA signature algorithm:
3772-
SignatureAlgorithm alg = Jwts.SIG.RS512; //or PS512, RS256, etc...
3772+
SignatureAlgorithm alg = Jws.alg.RS512; //or PS512, RS256, etc...
37733773
KeyPair pair = alg.keyPair().build();
37743774
37753775
// Bob creates the compact JWS with his RSA private key:
@@ -3802,7 +3802,7 @@ public key:
38023802
[,java]
38033803
----
38043804
// Create a test key suitable for the desired ECDSA signature algorithm:
3805-
SignatureAlgorithm alg = Jwts.SIG.ES512; //or ES256 or ES384
3805+
SignatureAlgorithm alg = Jws.alg.ES512; //or ES256 or ES384
38063806
KeyPair pair = alg.keyPair().build();
38073807
38083808
// Bob creates the compact JWS with his EC private key:
@@ -3854,7 +3854,7 @@ KeyPair pair = curve.keyPair().build();
38543854
38553855
// Bob creates the compact JWS with his Edwards Curve private key:
38563856
String jws = Jwts.builder().subject("Alice")
3857-
.signWith(pair.getPrivate(), Jwts.SIG.EdDSA) // <-- Bob's Edwards Curve private key w/ EdDSA
3857+
.signWith(pair.getPrivate(), Jws.alg.EdDSA) // <-- Bob's Edwards Curve private key w/ EdDSA
38583858
.compact();
38593859
38603860
// Alice receives and verifies the compact JWS came from Bob:
@@ -3922,7 +3922,7 @@ decrypt the JWT using her RSA private key:
39223922
[,java]
39233923
----
39243924
// Create a test KeyPair suitable for the desired RSA key algorithm:
3925-
KeyPair pair = Jwts.SIG.RS512.keyPair().build();
3925+
KeyPair pair = Jws.alg.RS512.keyPair().build();
39263926
39273927
// Choose the key algorithm used encrypt the payload key:
39283928
KeyAlgorithm<PublicKey, PrivateKey> alg = Jwts.KEY.RSA_OAEP_256; //or RSA_OAEP or RSA1_5
@@ -3994,7 +3994,7 @@ Alice can then decrypt the JWT using her Elliptic Curve private key:
39943994
[,java]
39953995
----
39963996
// Create a test KeyPair suitable for the desired EC key algorithm:
3997-
KeyPair pair = Jwts.SIG.ES512.keyPair().build();
3997+
KeyPair pair = Jws.alg.ES512.keyPair().build();
39983998
39993999
// Choose the key algorithm used encrypt the payload key:
40004000
KeyAlgorithm<PublicKey, PrivateKey> alg = Jwts.KEY.ECDH_ES_A256KW; //ECDH_ES_A192KW, etc...
@@ -4070,7 +4070,7 @@ Example creating and parsing a secret JWK:
40704070

40714071
[,java]
40724072
----
4073-
SecretKey key = Jwts.SIG.HS512.key().build(); // or HS384 or HS256
4073+
SecretKey key = Jws.alg.HS512.key().build(); // or HS384 or HS256
40744074
SecretJwk jwk = Jwks.builder().key(key).idFromThumbprint().build();
40754075
40764076
assert jwk.getId().equals(jwk.thumbprint().toString());
@@ -4092,7 +4092,7 @@ Example creating and parsing an RSA Public JWK:
40924092

40934093
[,java]
40944094
----
4095-
RSAPublicKey key = (RSAPublicKey)Jwts.SIG.RS512.keyPair().build().getPublic();
4095+
RSAPublicKey key = (RSAPublicKey)Jws.alg.RS512.keyPair().build().getPublic();
40964096
RsaPublicJwk jwk = Jwks.builder().key(key).idFromThumbprint().build();
40974097
40984098
assert jwk.getId().equals(jwk.thumbprint().toString());
@@ -4114,7 +4114,7 @@ Example creating and parsing an RSA Private JWK:
41144114

41154115
[,java]
41164116
----
4117-
KeyPair pair = Jwts.SIG.RS512.keyPair().build();
4117+
KeyPair pair = Jws.alg.RS512.keyPair().build();
41184118
RSAPublicKey pubKey = (RSAPublicKey) pair.getPublic();
41194119
RSAPrivateKey privKey = (RSAPrivateKey) pair.getPrivate();
41204120
@@ -4142,7 +4142,7 @@ Example creating and parsing an Elliptic Curve Public JWK:
41424142

41434143
[,java]
41444144
----
4145-
ECPublicKey key = (ECPublicKey) Jwts.SIG.ES512.keyPair().build().getPublic();
4145+
ECPublicKey key = (ECPublicKey) Jws.alg.ES512.keyPair().build().getPublic();
41464146
EcPublicJwk jwk = Jwks.builder().key(key).idFromThumbprint().build();
41474147
41484148
assert jwk.getId().equals(jwk.thumbprint().toString());
@@ -4164,7 +4164,7 @@ Example creating and parsing an Elliptic Curve Private JWK:
41644164

41654165
[,java]
41664166
----
4167-
KeyPair pair = Jwts.SIG.ES512.keyPair().build();
4167+
KeyPair pair = Jws.alg.ES512.keyPair().build();
41684168
ECPublicKey pubKey = (ECPublicKey) pair.getPublic();
41694169
ECPrivateKey privKey = (ECPrivateKey) pair.getPrivate();
41704170

api/src/main/java/io/jsonwebtoken/Header.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public interface Header extends Map<String, Object> {
130130
* <ul>
131131
* <li>If the JWT is a Signed JWT (a JWS), the <a href="https://tools.ietf.org/html/rfc7515#section-4.1.1">
132132
* <code>alg</code></a> (Algorithm) header parameter identifies the cryptographic algorithm used to secure the
133-
* JWS. Consider using {@link Jwts.SIG}.{@link io.jsonwebtoken.lang.Registry#get(Object) get(id)}
133+
* JWS. Consider using {@link Jws.alg}.{@link io.jsonwebtoken.lang.Registry#get(Object) get(id)}
134134
* to convert this string value to a type-safe {@code SecureDigestAlgorithm} instance.</li>
135135
* <li>If the JWT is an Encrypted JWT (a JWE), the
136136
* <a href="https://tools.ietf.org/html/rfc7516#section-4.1.1"><code>alg</code></a> (Algorithm) header parameter

api/src/main/java/io/jsonwebtoken/Jws.java

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
*/
1616
package io.jsonwebtoken;
1717

18+
import io.jsonwebtoken.lang.Classes;
19+
import io.jsonwebtoken.lang.Registry;
20+
import io.jsonwebtoken.security.KeyPairBuilderSupplier;
21+
import io.jsonwebtoken.security.MacAlgorithm;
22+
import io.jsonwebtoken.security.SecureDigestAlgorithm;
23+
24+
import java.security.Key;
25+
1826
/**
1927
* An expanded (not compact/serialized) Signed JSON Web Token.
2028
*
@@ -23,6 +31,169 @@
2331
*/
2432
public interface Jws<P> extends ProtectedJwt<JwsHeader, P> {
2533

34+
/**
35+
* Constants for all JWA (RFC 7518) standard <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3">
36+
* Cryptographic Algorithms for Digital Signatures and MACs</a> defined in the
37+
* <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-7.1">JSON Web Signature and Encryption Algorithms
38+
* Registry</a>. Each standard algorithm is available as a ({@code public static final}) constant for
39+
* direct type-safe reference in application code. For example:
40+
* <blockquote><pre>
41+
* Jwts.builder()
42+
* // ... etc ...
43+
* .signWith(aKey, <b>Jws.alg.HS512</b>) // or RS512, PS256, EdDSA, etc...
44+
* .build();</pre></blockquote>
45+
* <p>They are also available together as a {@link Registry} instance via the {@link #registry()} method.</p>
46+
*
47+
* @see #registry()
48+
* @since JJWT_RELEASE_VERSION
49+
*/
50+
final class alg {
51+
52+
private static final String IMPL_CLASSNAME = "io.jsonwebtoken.impl.security.StandardSecureDigestAlgorithms";
53+
private static final Registry<String, SecureDigestAlgorithm<?, ?>> REGISTRY = Classes.newInstance(IMPL_CLASSNAME);
54+
55+
//prevent instantiation
56+
private alg() {
57+
}
58+
59+
/**
60+
* Returns all standard JWA <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3">Cryptographic
61+
* Algorithms for Digital Signatures and MACs</a> defined in the
62+
* <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-7.1">JSON Web Signature and Encryption
63+
* Algorithms Registry</a>.
64+
*
65+
* @return all standard JWA digital signature and MAC algorithms.
66+
*/
67+
public static Registry<String, SecureDigestAlgorithm<?, ?>> registry() {
68+
return REGISTRY;
69+
}
70+
71+
/**
72+
* The &quot;none&quot; signature algorithm as defined by
73+
* <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.6">RFC 7518, Section 3.6</a>. This algorithm
74+
* is used only when creating unsecured (not integrity protected) JWSs and is not usable in any other scenario.
75+
* Any attempt to call its methods will result in an exception being thrown.
76+
*/
77+
public static final SecureDigestAlgorithm<Key, Key> NONE = Jwts.get(REGISTRY, "none");
78+
79+
/**
80+
* {@code HMAC using SHA-256} message authentication algorithm as defined by
81+
* <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.2">RFC 7518, Section 3.2</a>. This algorithm
82+
* requires a 256-bit (32 byte) key.
83+
*/
84+
public static final MacAlgorithm HS256 = Jwts.get(REGISTRY, "HS256");
85+
86+
/**
87+
* {@code HMAC using SHA-384} message authentication algorithm as defined by
88+
* <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.2">RFC 7518, Section 3.2</a>. This algorithm
89+
* requires a 384-bit (48 byte) key.
90+
*/
91+
public static final MacAlgorithm HS384 = Jwts.get(REGISTRY, "HS384");
92+
93+
/**
94+
* {@code HMAC using SHA-512} message authentication algorithm as defined by
95+
* <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.2">RFC 7518, Section 3.2</a>. This algorithm
96+
* requires a 512-bit (64 byte) key.
97+
*/
98+
public static final MacAlgorithm HS512 = Jwts.get(REGISTRY, "HS512");
99+
100+
/**
101+
* {@code RSASSA-PKCS1-v1_5 using SHA-256} signature algorithm as defined by
102+
* <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.3">RFC 7518, Section 3.3</a>. This algorithm
103+
* requires a 2048-bit key.
104+
*/
105+
public static final io.jsonwebtoken.security.SignatureAlgorithm RS256 = Jwts.get(REGISTRY, "RS256");
106+
107+
/**
108+
* {@code RSASSA-PKCS1-v1_5 using SHA-384} signature algorithm as defined by
109+
* <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.3">RFC 7518, Section 3.3</a>. This algorithm
110+
* requires a 2048-bit key, but the JJWT team recommends a 3072-bit key.
111+
*/
112+
public static final io.jsonwebtoken.security.SignatureAlgorithm RS384 = Jwts.get(REGISTRY, "RS384");
113+
114+
/**
115+
* {@code RSASSA-PKCS1-v1_5 using SHA-512} signature algorithm as defined by
116+
* <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.3">RFC 7518, Section 3.3</a>. This algorithm
117+
* requires a 2048-bit key, but the JJWT team recommends a 4096-bit key.
118+
*/
119+
public static final io.jsonwebtoken.security.SignatureAlgorithm RS512 = Jwts.get(REGISTRY, "RS512");
120+
121+
/**
122+
* {@code RSASSA-PSS using SHA-256 and MGF1 with SHA-256} signature algorithm as defined by
123+
* <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.5">RFC 7518, Section 3.5</a><b><sup>1</sup></b>.
124+
* This algorithm requires a 2048-bit key.
125+
*
126+
* <p><b><sup>1</sup></b> Requires Java 11 or a compatible JCA Provider (like BouncyCastle) in the runtime
127+
* classpath. If on Java 10 or earlier, BouncyCastle will be used automatically if found in the runtime
128+
* classpath.</p>
129+
*/
130+
public static final io.jsonwebtoken.security.SignatureAlgorithm PS256 = Jwts.get(REGISTRY, "PS256");
131+
132+
/**
133+
* {@code RSASSA-PSS using SHA-384 and MGF1 with SHA-384} signature algorithm as defined by
134+
* <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.5">RFC 7518, Section 3.5</a><b><sup>1</sup></b>.
135+
* This algorithm requires a 2048-bit key, but the JJWT team recommends a 3072-bit key.
136+
*
137+
* <p><b><sup>1</sup></b> Requires Java 11 or a compatible JCA Provider (like BouncyCastle) in the runtime
138+
* classpath. If on Java 10 or earlier, BouncyCastle will be used automatically if found in the runtime
139+
* classpath.</p>
140+
*/
141+
public static final io.jsonwebtoken.security.SignatureAlgorithm PS384 = Jwts.get(REGISTRY, "PS384");
142+
143+
/**
144+
* {@code RSASSA-PSS using SHA-512 and MGF1 with SHA-512} signature algorithm as defined by
145+
* <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.5">RFC 7518, Section 3.5</a><b><sup>1</sup></b>.
146+
* This algorithm requires a 2048-bit key, but the JJWT team recommends a 4096-bit key.
147+
*
148+
* <p><b><sup>1</sup></b> Requires Java 11 or a compatible JCA Provider (like BouncyCastle) in the runtime
149+
* classpath. If on Java 10 or earlier, BouncyCastle will be used automatically if found in the runtime
150+
* classpath.</p>
151+
*/
152+
public static final io.jsonwebtoken.security.SignatureAlgorithm PS512 = Jwts.get(REGISTRY, "PS512");
153+
154+
/**
155+
* {@code ECDSA using P-256 and SHA-256} signature algorithm as defined by
156+
* <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.4">RFC 7518, Section 3.4</a>. This algorithm
157+
* requires a 256-bit key.
158+
*/
159+
public static final io.jsonwebtoken.security.SignatureAlgorithm ES256 = Jwts.get(REGISTRY, "ES256");
160+
161+
/**
162+
* {@code ECDSA using P-384 and SHA-384} signature algorithm as defined by
163+
* <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.4">RFC 7518, Section 3.4</a>. This algorithm
164+
* requires a 384-bit key.
165+
*/
166+
public static final io.jsonwebtoken.security.SignatureAlgorithm ES384 = Jwts.get(REGISTRY, "ES384");
167+
168+
/**
169+
* {@code ECDSA using P-521 and SHA-512} signature algorithm as defined by
170+
* <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.4">RFC 7518, Section 3.4</a>. This algorithm
171+
* requires a 521-bit key.
172+
*/
173+
public static final io.jsonwebtoken.security.SignatureAlgorithm ES512 = Jwts.get(REGISTRY, "ES512");
174+
175+
/**
176+
* {@code EdDSA} signature algorithm defined by
177+
* <a href="https://www.rfc-editor.org/rfc/rfc8037#section-3.1">RFC 8037, Section 3.1</a> that requires
178+
* either {@code Ed25519} or {@code Ed448} Edwards Elliptic Curve<sup><b>1</b></sup> keys.
179+
*
180+
* <p><b>KeyPair Generation</b></p>
181+
*
182+
* <p>This instance's {@link KeyPairBuilderSupplier#keyPair() keyPair()} builder creates {@code Ed448} keys,
183+
* and is essentially an alias for
184+
* <code>{@link io.jsonwebtoken.security.Jwks.CRV Jwks.CRV}.{@link io.jsonwebtoken.security.Jwks.CRV#Ed448 Ed448}.{@link KeyPairBuilderSupplier#keyPair() keyPair()}</code>.</p>
185+
*
186+
* <p>If you would like to generate an {@code Ed25519} {@code KeyPair} for use with the {@code EdDSA} algorithm,
187+
* you may use the
188+
* <code>{@link io.jsonwebtoken.security.Jwks.CRV Jwks.CRV}.{@link io.jsonwebtoken.security.Jwks.CRV#Ed25519 Ed25519}.{@link KeyPairBuilderSupplier#keyPair() keyPair()}</code>
189+
* builder instead.</p>
190+
*
191+
* <p><b><sup>1</sup>This algorithm requires at least JDK 15 or a compatible JCA Provider (like BouncyCastle) in the runtime
192+
* classpath.</b></p>
193+
*/
194+
public static final io.jsonwebtoken.security.SignatureAlgorithm EdDSA = Jwts.get(REGISTRY, "EdDSA");
195+
}
196+
26197
/**
27198
* Visitor implementation that ensures the visited JWT is a JSON Web Signature ('JWS') message with a
28199
* cryptographically authenticated/verified {@code byte[]} array payload, and rejects all others with an

0 commit comments

Comments
 (0)