Created
November 19, 2019 14:51
-
-
Save NeilMadden/685ea66fb79d37a50c2310f853bd9496 to your computer and use it in GitHub Desktop.
Example ECDH DPoP using existing JWE ECDH-ES implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Shared context of the request: | |
String accessToken = "an_access_token"; | |
String origin = "https://api.example.com:443"; | |
JweHeader header = new JweHeader(); | |
header.setAgreementPartyUInfo(Base64url.encode(accessToken)); | |
header.setAgreementPartyVInfo(Base64url.encode(origin)); | |
// Server - generate challenge | |
OkpJWK ephemeral = OkpJWK.generateKeyPair(X25519); | |
String challenge = Base64url.encode(ephemeral.toPublicJwk().get().toJsonValue().toString()); | |
System.out.println("WWW-Authenticate: DPoP " + challenge); | |
// Client - derive HMAC key | |
OkpJWK staticKeys = OkpJWK.generateKeyPair(X25519); | |
ECDHDerivedKey ecdh = new ECDHDerivedKey(staticKeys.toPrivateKey(), "HmacSHA256", 256); | |
ecdh.setTheirPublicKey(ephemeral.toPublicKey()); | |
ecdh.setOtherInfo(ECDHEncryptionHandler.generateOtherInfo(header, "HS256", 256)); | |
SecretKey hmacKey = ecdh.getDerivedKey(); | |
// Client - generate response | |
String jwt = JOSE.jws(new HmacSigningHandler(hmacKey)) | |
.headers().alg(JwsAlgorithm.HS256).headerIfNotNull("kid", ephemeral.getKeyId()).done() | |
.claims(JOSE.claims().claim("htm", "POST").claim("htu", "https://api.example.com/foo").build()) | |
.build(); | |
System.out.println("Authorization: DPoP at=" + accessToken + ", jwt=" + jwt); | |
// Server - validate response | |
PublicKey confirmationKey = staticKeys.toPublicKey(); // Received from token introspection | |
ecdh = new ECDHDerivedKey(ephemeral.toPrivateKey(), "HmacSHA256", 256); | |
ecdh.setTheirPublicKey(confirmationKey); | |
ecdh.setOtherInfo(ECDHEncryptionHandler.generateOtherInfo(header, "HS256", 256)); | |
hmacKey = ecdh.getDerivedKey(); | |
SignedJwt response = JOSE.reconstruct(jwt, SignedJwt.class); | |
assertThat(response.verify(new HmacSigningHandler(hmacKey))).isTrue(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: