Skip to content

Commit

Permalink
README.md fix (and associated JweReadmeTest) to ensure docs accuratel…
Browse files Browse the repository at this point in the history
…y reflected new Jwks.CRV references
  • Loading branch information
lhazlewood committed Aug 17, 2023
1 parent 620cc5d commit 7bb97fa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1560,8 +1560,7 @@ public key (`keyPair.getPublic()`) to parse/verify a JWS.
>
> **The `PS256`, `PS384`, and `PS512` algorithms require JDK 11 or a compatible JCA Provider
> (like BouncyCastle) in the runtime classpath.**
> **The `EdDSA`, `Ed25519` and `Ed448` algorithms require JDK 15 or a compatible JCA Provider
> (like BouncyCastle) in the runtime classpath.**
> **The `EdDSA` algorithms requires JDK 15 or a compatible JCA Provider (like BouncyCastle) in the runtime classpath.**
> If you want to use either set of algorithms, and you are on an earlier JDK that does not support them,
> see the [Installation](#Installation) section to see how to enable BouncyCastle. All other algorithms are
> natively supported by the JDK.
Expand Down Expand Up @@ -3154,12 +3153,12 @@ using Bob's Edwards Curve public key:
```java
// Create a test key suitable for the EdDSA signature algorithm using Ed25519 or Ed448 keys:
SignatureAlgorithm alg = Jwts.SIG.Ed25519; //or Ed448
KeyPair pair = alg.keyPair().build();
Curve curve = Jwks.CRV.Ed25519; //or Ed448
KeyPair pair = curve.keyPair().build();
// Bob creates the compact JWS with his Edwards Curve private key:
String jws = Jwts.builder().subject("Alice")
.signWith(pair.getPrivate(), alg) // <-- Bob's Edwards Curve private key
.signWith(pair.getPrivate(), Jwts.SIG.EdDSA) // <-- Bob's Edwards Curve private key w/ EdDSA
.compact();

// Alice receives and verifies the compact JWS came from Bob:
Expand Down Expand Up @@ -3477,7 +3476,7 @@ Example creating and parsing an Edwards Elliptic Curve (Ed25519, Ed448, X25519,
`OctetPublicJwk` interface names):

```java
PublicKey key = Jwks.CRV.Ed25519.keyPair().build().getPublic();
PublicKey key = Jwks.CRV.Ed25519.keyPair().build().getPublic(); // or Ed448, X25519, X448
OctetPublicJwk<PublicKey> jwk = builder().octetKey(key).idFromThumbprint().build();

assert jwk.getId().equals(jwk.thumbprint().toString());
Expand All @@ -3499,7 +3498,7 @@ Example creating and parsing an Edwards Elliptic Curve (Ed25519, Ed448, X25519,
`OctetPrivateJwk` and `OctetPublicJwk` interface names):

```java
KeyPair pair = Jwks.CRV.Ed448.keyPair().build();
KeyPair pair = Jwks.CRV.Ed448.keyPair().build(); // or Ed25519, X25519, X448
PublicKey pubKey = pair.getPublic();
PrivateKey privKey = pair.getPrivate();

Expand Down
39 changes: 31 additions & 8 deletions tdjar/src/test/java/io/jsonwebtoken/all/JavaReadmeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.jackson.io.JacksonSerializer;
import io.jsonwebtoken.security.AeadAlgorithm;
import io.jsonwebtoken.security.Curve;
import io.jsonwebtoken.security.EcPrivateJwk;
import io.jsonwebtoken.security.EcPublicJwk;
import io.jsonwebtoken.security.Jwk;
Expand Down Expand Up @@ -85,7 +86,7 @@ public void testExampleJwsRSA() {
KeyPair pair = alg.keyPair().build();

// Bob creates the compact JWS with his RSA private key:
String jws = Jwts.builder().setSubject("Alice")
String jws = Jwts.builder().subject("Alice")
.signWith(pair.getPrivate(), alg) // <-- Bob's RSA private key
.compact();

Expand All @@ -107,7 +108,7 @@ public void testExampleJwsECDSA() {
KeyPair pair = alg.keyPair().build();

// Bob creates the compact JWS with his EC private key:
String jws = Jwts.builder().setSubject("Alice")
String jws = Jwts.builder().subject("Alice")
.signWith(pair.getPrivate(), alg) // <-- Bob's EC private key
.compact();

Expand All @@ -119,6 +120,28 @@ public void testExampleJwsECDSA() {
assert "Alice".equals(subject);
}

/**
* {@code README.md#example-jws-eddsa}
*/
@Test
public void testExampleJwsEdDSA() {
// Create a test key suitable for the EdDSA signature algorithm using Ed25519 or Ed448 keys:
Curve curve = Jwks.CRV.Ed25519; //or Ed448
KeyPair pair = curve.keyPair().build();

// Bob creates the compact JWS with his Edwards Curve private key:
String jws = Jwts.builder().subject("Alice")
.signWith(pair.getPrivate(), Jwts.SIG.EdDSA) // <-- Bob's Edwards Curve private key w/ EdDSA
.compact();

// Alice receives and verifies the compact JWS came from Bob:
String subject = Jwts.parser()
.verifyWith(pair.getPublic()) // <-- Bob's Edwards Curve public key
.build().parseClaimsJws(jws).getPayload().getSubject();

assert "Alice".equals(subject);
}

/**
* {@code README.md#example-jwe-dir}
*/
Expand Down Expand Up @@ -155,7 +178,7 @@ public void testExampleJweRSA() {
AeadAlgorithm enc = Jwts.ENC.A256GCM; //or A192GCM, A128GCM, A256CBC-HS512, etc...

// Bob creates the compact JWE with Alice's RSA public key so only she may read it:
String jwe = Jwts.builder().setAudience("Alice")
String jwe = Jwts.builder().audience("Alice")
.encryptWith(pair.getPublic(), alg, enc) // <-- Alice's RSA public key
.compact();

Expand All @@ -180,7 +203,7 @@ public void testExampleJweAESKW() {
AeadAlgorithm enc = Jwts.ENC.A256GCM; //or A192GCM, A128GCM, A256CBC-HS512, etc...

// Create the compact JWE:
String jwe = Jwts.builder().setIssuer("me").encryptWith(key, alg, enc).compact();
String jwe = Jwts.builder().issuer("me").encryptWith(key, alg, enc).compact();

// Parse the compact JWE:
String issuer = Jwts.parser().decryptWith(key).build()
Expand All @@ -203,7 +226,7 @@ public void testExampleJweECDHES() {
AeadAlgorithm enc = Jwts.ENC.A256GCM; //or A192GCM, A128GCM, A256CBC-HS512, etc...

// Bob creates the compact JWE with Alice's EC public key so only she may read it:
String jwe = Jwts.builder().setAudience("Alice")
String jwe = Jwts.builder().audience("Alice")
.encryptWith(pair.getPublic(), alg, enc) // <-- Alice's EC public key
.compact();

Expand Down Expand Up @@ -239,7 +262,7 @@ public void testExampleJwePassword() {
AeadAlgorithm enc = Jwts.ENC.A256GCM; //or A192GCM, A128GCM, A256CBC-HS512, etc...

// Create the compact JWE:
String jwe = Jwts.builder().setIssuer("me")
String jwe = Jwts.builder().issuer("me")
// Optional work factor is specified in the header:
//.header().pbes2Count(pbkdf2Iterations)).and()
.encryptWith(password, alg, enc)
Expand Down Expand Up @@ -352,7 +375,7 @@ public void testExampleEcPrivateJwk() {
@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void testExampleEdEcPublicJwk() {
PublicKey key = Jwks.CRV.Ed25519.keyPair().build().getPublic();
PublicKey key = Jwks.CRV.Ed25519.keyPair().build().getPublic(); // or Ed448, X25519, X448
OctetPublicJwk<PublicKey> jwk = builder().octetKey(key).idFromThumbprint().build();

assert jwk.getId().equals(jwk.thumbprint().toString());
Expand All @@ -369,7 +392,7 @@ public void testExampleEdEcPublicJwk() {
@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void testExampleEdEcPrivateJwk() {
KeyPair pair = Jwks.CRV.Ed448.keyPair().build();
KeyPair pair = Jwks.CRV.Ed448.keyPair().build(); // or Ed25519, X25519, X448
PublicKey pubKey = pair.getPublic();
PrivateKey privKey = pair.getPrivate();

Expand Down

0 comments on commit 7bb97fa

Please sign in to comment.