mirror of
https://github.com/jwtk/jjwt.git
synced 2025-02-21 17:14:49 +00:00
README.md fix (and associated JweReadmeTest) to ensure docs accurately reflected new Jwks.CRV references
This commit is contained in:
parent
620cc5d97f
commit
7bb97fac72
13
README.md
13
README.md
@ -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
|
> **The `PS256`, `PS384`, and `PS512` algorithms require JDK 11 or a compatible JCA Provider
|
||||||
> (like BouncyCastle) in the runtime classpath.**
|
> (like BouncyCastle) in the runtime classpath.**
|
||||||
> **The `EdDSA`, `Ed25519` and `Ed448` algorithms require JDK 15 or a compatible JCA Provider
|
> **The `EdDSA` algorithms requires JDK 15 or a compatible JCA Provider (like BouncyCastle) in the runtime classpath.**
|
||||||
> (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,
|
> 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
|
> see the [Installation](#Installation) section to see how to enable BouncyCastle. All other algorithms are
|
||||||
> natively supported by the JDK.
|
> natively supported by the JDK.
|
||||||
@ -3154,12 +3153,12 @@ using Bob's Edwards Curve public key:
|
|||||||
|
|
||||||
```java
|
```java
|
||||||
// Create a test key suitable for the EdDSA signature algorithm using Ed25519 or Ed448 keys:
|
// Create a test key suitable for the EdDSA signature algorithm using Ed25519 or Ed448 keys:
|
||||||
SignatureAlgorithm alg = Jwts.SIG.Ed25519; //or Ed448
|
Curve curve = Jwks.CRV.Ed25519; //or Ed448
|
||||||
KeyPair pair = alg.keyPair().build();
|
KeyPair pair = curve.keyPair().build();
|
||||||
|
|
||||||
// Bob creates the compact JWS with his Edwards Curve private key:
|
// Bob creates the compact JWS with his Edwards Curve private key:
|
||||||
String jws = Jwts.builder().subject("Alice")
|
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();
|
.compact();
|
||||||
|
|
||||||
// Alice receives and verifies the compact JWS came from Bob:
|
// Alice receives and verifies the compact JWS came from Bob:
|
||||||
@ -3477,7 +3476,7 @@ Example creating and parsing an Edwards Elliptic Curve (Ed25519, Ed448, X25519,
|
|||||||
`OctetPublicJwk` interface names):
|
`OctetPublicJwk` interface names):
|
||||||
|
|
||||||
```java
|
```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();
|
OctetPublicJwk<PublicKey> jwk = builder().octetKey(key).idFromThumbprint().build();
|
||||||
|
|
||||||
assert jwk.getId().equals(jwk.thumbprint().toString());
|
assert jwk.getId().equals(jwk.thumbprint().toString());
|
||||||
@ -3499,7 +3498,7 @@ Example creating and parsing an Edwards Elliptic Curve (Ed25519, Ed448, X25519,
|
|||||||
`OctetPrivateJwk` and `OctetPublicJwk` interface names):
|
`OctetPrivateJwk` and `OctetPublicJwk` interface names):
|
||||||
|
|
||||||
```java
|
```java
|
||||||
KeyPair pair = Jwks.CRV.Ed448.keyPair().build();
|
KeyPair pair = Jwks.CRV.Ed448.keyPair().build(); // or Ed25519, X25519, X448
|
||||||
PublicKey pubKey = pair.getPublic();
|
PublicKey pubKey = pair.getPublic();
|
||||||
PrivateKey privKey = pair.getPrivate();
|
PrivateKey privKey = pair.getPrivate();
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ package io.jsonwebtoken.all;
|
|||||||
import io.jsonwebtoken.Jwts;
|
import io.jsonwebtoken.Jwts;
|
||||||
import io.jsonwebtoken.jackson.io.JacksonSerializer;
|
import io.jsonwebtoken.jackson.io.JacksonSerializer;
|
||||||
import io.jsonwebtoken.security.AeadAlgorithm;
|
import io.jsonwebtoken.security.AeadAlgorithm;
|
||||||
|
import io.jsonwebtoken.security.Curve;
|
||||||
import io.jsonwebtoken.security.EcPrivateJwk;
|
import io.jsonwebtoken.security.EcPrivateJwk;
|
||||||
import io.jsonwebtoken.security.EcPublicJwk;
|
import io.jsonwebtoken.security.EcPublicJwk;
|
||||||
import io.jsonwebtoken.security.Jwk;
|
import io.jsonwebtoken.security.Jwk;
|
||||||
@ -85,7 +86,7 @@ public class JavaReadmeTest {
|
|||||||
KeyPair pair = alg.keyPair().build();
|
KeyPair pair = alg.keyPair().build();
|
||||||
|
|
||||||
// Bob creates the compact JWS with his RSA private key:
|
// 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
|
.signWith(pair.getPrivate(), alg) // <-- Bob's RSA private key
|
||||||
.compact();
|
.compact();
|
||||||
|
|
||||||
@ -107,7 +108,7 @@ public class JavaReadmeTest {
|
|||||||
KeyPair pair = alg.keyPair().build();
|
KeyPair pair = alg.keyPair().build();
|
||||||
|
|
||||||
// Bob creates the compact JWS with his EC private key:
|
// 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
|
.signWith(pair.getPrivate(), alg) // <-- Bob's EC private key
|
||||||
.compact();
|
.compact();
|
||||||
|
|
||||||
@ -119,6 +120,28 @@ public class JavaReadmeTest {
|
|||||||
assert "Alice".equals(subject);
|
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}
|
* {@code README.md#example-jwe-dir}
|
||||||
*/
|
*/
|
||||||
@ -155,7 +178,7 @@ public class JavaReadmeTest {
|
|||||||
AeadAlgorithm enc = Jwts.ENC.A256GCM; //or A192GCM, A128GCM, A256CBC-HS512, etc...
|
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:
|
// 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
|
.encryptWith(pair.getPublic(), alg, enc) // <-- Alice's RSA public key
|
||||||
.compact();
|
.compact();
|
||||||
|
|
||||||
@ -180,7 +203,7 @@ public class JavaReadmeTest {
|
|||||||
AeadAlgorithm enc = Jwts.ENC.A256GCM; //or A192GCM, A128GCM, A256CBC-HS512, etc...
|
AeadAlgorithm enc = Jwts.ENC.A256GCM; //or A192GCM, A128GCM, A256CBC-HS512, etc...
|
||||||
|
|
||||||
// Create the compact JWE:
|
// 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:
|
// Parse the compact JWE:
|
||||||
String issuer = Jwts.parser().decryptWith(key).build()
|
String issuer = Jwts.parser().decryptWith(key).build()
|
||||||
@ -203,7 +226,7 @@ public class JavaReadmeTest {
|
|||||||
AeadAlgorithm enc = Jwts.ENC.A256GCM; //or A192GCM, A128GCM, A256CBC-HS512, etc...
|
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:
|
// 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
|
.encryptWith(pair.getPublic(), alg, enc) // <-- Alice's EC public key
|
||||||
.compact();
|
.compact();
|
||||||
|
|
||||||
@ -239,7 +262,7 @@ public class JavaReadmeTest {
|
|||||||
AeadAlgorithm enc = Jwts.ENC.A256GCM; //or A192GCM, A128GCM, A256CBC-HS512, etc...
|
AeadAlgorithm enc = Jwts.ENC.A256GCM; //or A192GCM, A128GCM, A256CBC-HS512, etc...
|
||||||
|
|
||||||
// Create the compact JWE:
|
// Create the compact JWE:
|
||||||
String jwe = Jwts.builder().setIssuer("me")
|
String jwe = Jwts.builder().issuer("me")
|
||||||
// Optional work factor is specified in the header:
|
// Optional work factor is specified in the header:
|
||||||
//.header().pbes2Count(pbkdf2Iterations)).and()
|
//.header().pbes2Count(pbkdf2Iterations)).and()
|
||||||
.encryptWith(password, alg, enc)
|
.encryptWith(password, alg, enc)
|
||||||
@ -352,7 +375,7 @@ public class JavaReadmeTest {
|
|||||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
@Test
|
@Test
|
||||||
public void testExampleEdEcPublicJwk() {
|
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();
|
OctetPublicJwk<PublicKey> jwk = builder().octetKey(key).idFromThumbprint().build();
|
||||||
|
|
||||||
assert jwk.getId().equals(jwk.thumbprint().toString());
|
assert jwk.getId().equals(jwk.thumbprint().toString());
|
||||||
@ -369,7 +392,7 @@ public class JavaReadmeTest {
|
|||||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
@Test
|
@Test
|
||||||
public void testExampleEdEcPrivateJwk() {
|
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();
|
PublicKey pubKey = pair.getPublic();
|
||||||
PrivateKey privKey = pair.getPrivate();
|
PrivateKey privKey = pair.getPrivate();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user