JoseUtils { public static JSONBuilder createJoseRequest(URL url, KeyPair keypair, @Nullable JSONBuilder payload, @Nullable String nonce, @Nullable String kid) { try { PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(keypair.getPublic()); JsonWebSignature jws = new JsonWebSignature(); jws.getHeaders().setObjectHeaderValue("url", url); if (kid != null) { jws.getHeaders().setObjectHeaderValue("kid", kid); } else { jws.getHeaders().setJwkHeaderValue("jwk", jwk); } if (nonce != null) { jws.getHeaders().setObjectHeaderValue("nonce", nonce); } jws.setPayload(payload != null ? payload.toString() : ""); jws.setAlgorithmHeaderValue(keyAlgorithm(jwk)); jws.setKey(keypair.getPrivate()); jws.sign(); if (LOG.isDebugEnabled()) { LOG.debug("{} {}", payload != null ? "POST" : "POST-as-GET", url); if (payload != null) { LOG.debug(" Payload: {}", payload); } LOG.debug(" JWS Header: {}", jws.getHeaders().getFullHeaderAsJsonString()); } JSONBuilder jb = new JSONBuilder(); jb.put("protected", jws.getHeaders().getEncodedHeader()); jb.put("payload", jws.getEncodedPayload()); jb.put("signature", jws.getEncodedSignature()); return jb; } catch (JoseException ex) { throw new IllegalArgumentException("Could not create a JOSE request", ex); } } private  JoseUtils(); static JSONBuilder createJoseRequest(URL url, KeyPair keypair,
                @Nullable JSONBuilder payload, @Nullable String nonce, @Nullable String kid); static Map<String, Object> createExternalAccountBinding(String kid,
            PublicKey accountKey, SecretKey macKey, URL resource); static Map<String, Object> publicKeyToJWK(PublicKey key); static PublicKey jwkToPublicKey(Map<String, Object> jwk); static byte[] thumbprint(PublicKey key); static String keyAlgorithm(JsonWebKey jwk); static String macKeyAlgorithm(SecretKey macKey);  }
@Test public void testCreateJosePostRequest() throws Exception { URL resourceUrl = url("http: KeyPair accountKey = TestUtils.createKeyPair(); String nonce = URL_ENCODER.encodeToString("foo-nonce-1-foo".getBytes()); JSONBuilder payload = new JSONBuilder(); payload.put("foo", 123); payload.put("bar", "a-string"); Map<String, Object> jose = JoseUtils .createJoseRequest(resourceUrl, accountKey, payload, nonce, TestUtils.ACCOUNT_URL) .toMap(); String encodedHeader = jose.get("protected").toString(); String encodedSignature = jose.get("signature").toString(); String encodedPayload = jose.get("payload").toString(); StringBuilder expectedHeader = new StringBuilder(); expectedHeader.append('{'); expectedHeader.append("\"nonce\":\"").append(nonce).append("\","); expectedHeader.append("\"url\":\"").append(resourceUrl).append("\","); expectedHeader.append("\"alg\":\"RS256\","); expectedHeader.append("\"kid\":\"").append(TestUtils.ACCOUNT_URL).append('"'); expectedHeader.append('}'); assertThat(new String(URL_DECODER.decode(encodedHeader), UTF_8), sameJSONAs(expectedHeader.toString())); assertThat(new String(URL_DECODER.decode(encodedPayload), UTF_8), sameJSONAs("{\"foo\":123,\"bar\":\"a-string\"}")); assertThat(encodedSignature, not(emptyOrNullString())); JsonWebSignature jws = new JsonWebSignature(); jws.setCompactSerialization(CompactSerializer.serialize(encodedHeader, encodedPayload, encodedSignature)); jws.setKey(accountKey.getPublic()); assertThat(jws.verifySignature(), is(true)); }
@Test public void testCreateJosePostAsGetRequest() throws Exception { URL resourceUrl = url("http: KeyPair accountKey = TestUtils.createKeyPair(); String nonce = URL_ENCODER.encodeToString("foo-nonce-1-foo".getBytes()); Map<String, Object> jose = JoseUtils .createJoseRequest(resourceUrl, accountKey, null, nonce, TestUtils.ACCOUNT_URL) .toMap(); String encodedHeader = jose.get("protected").toString(); String encodedSignature = jose.get("signature").toString(); String encodedPayload = jose.get("payload").toString(); StringBuilder expectedHeader = new StringBuilder(); expectedHeader.append('{'); expectedHeader.append("\"nonce\":\"").append(nonce).append("\","); expectedHeader.append("\"url\":\"").append(resourceUrl).append("\","); expectedHeader.append("\"alg\":\"RS256\","); expectedHeader.append("\"kid\":\"").append(TestUtils.ACCOUNT_URL).append('"'); expectedHeader.append('}'); assertThat(new String(URL_DECODER.decode(encodedHeader), UTF_8), sameJSONAs(expectedHeader.toString())); assertThat(new String(URL_DECODER.decode(encodedPayload), UTF_8), is("")); assertThat(encodedSignature, not(emptyOrNullString())); JsonWebSignature jws = new JsonWebSignature(); jws.setCompactSerialization(CompactSerializer.serialize(encodedHeader, encodedPayload, encodedSignature)); jws.setKey(accountKey.getPublic()); assertThat(jws.verifySignature(), is(true)); }
@Test public void testCreateJoseKeyChangeRequest() throws Exception { URL resourceUrl = url("http: KeyPair accountKey = TestUtils.createKeyPair(); JSONBuilder payload = new JSONBuilder(); payload.put("foo", 123); payload.put("bar", "a-string"); Map<String, Object> jose = JoseUtils .createJoseRequest(resourceUrl, accountKey, payload, null, null) .toMap(); String encodedHeader = jose.get("protected").toString(); String encodedSignature = jose.get("signature").toString(); String encodedPayload = jose.get("payload").toString(); StringBuilder expectedHeader = new StringBuilder(); expectedHeader.append('{'); expectedHeader.append("\"url\":\"").append(resourceUrl).append("\","); expectedHeader.append("\"alg\":\"RS256\","); expectedHeader.append("\"jwk\": {"); expectedHeader.append("\"kty\": \"").append(TestUtils.KTY).append("\","); expectedHeader.append("\"e\": \"").append(TestUtils.E).append("\","); expectedHeader.append("\"n\": \"").append(TestUtils.N).append("\"}"); expectedHeader.append("}"); assertThat(new String(URL_DECODER.decode(encodedHeader), UTF_8), sameJSONAs(expectedHeader.toString())); assertThat(new String(URL_DECODER.decode(encodedPayload), UTF_8), sameJSONAs("{\"foo\":123,\"bar\":\"a-string\"}")); assertThat(encodedSignature, not(emptyOrNullString())); JsonWebSignature jws = new JsonWebSignature(); jws.setCompactSerialization(CompactSerializer.serialize(encodedHeader, encodedPayload, encodedSignature)); jws.setKey(accountKey.getPublic()); assertThat(jws.verifySignature(), is(true)); }