JoseUtils { public static Map<String, Object> createExternalAccountBinding(String kid, PublicKey accountKey, SecretKey macKey, URL resource) { try { PublicJsonWebKey keyJwk = PublicJsonWebKey.Factory.newPublicJwk(accountKey); JsonWebSignature innerJws = new JsonWebSignature(); innerJws.setPayload(keyJwk.toJson()); innerJws.getHeaders().setObjectHeaderValue("url", resource); innerJws.getHeaders().setObjectHeaderValue("kid", kid); innerJws.setAlgorithmHeaderValue(macKeyAlgorithm(macKey)); innerJws.setKey(macKey); innerJws.setDoKeyValidation(false); innerJws.sign(); JSONBuilder outerClaim = new JSONBuilder(); outerClaim.put("protected", innerJws.getHeaders().getEncodedHeader()); outerClaim.put("signature", innerJws.getEncodedSignature()); outerClaim.put("payload", innerJws.getEncodedPayload()); return outerClaim.toMap(); } catch (JoseException ex) { throw new IllegalArgumentException("Could not create external account binding", 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 testCreateExternalAccountBinding() throws Exception { KeyPair accountKey = TestUtils.createKeyPair(); String keyIdentifier = "NCC-1701"; SecretKey macKey = TestUtils.createSecretKey("SHA-256"); URL resourceUrl = url("http: Map<String, Object> binding = JoseUtils.createExternalAccountBinding( keyIdentifier, accountKey.getPublic(), macKey, resourceUrl); String encodedHeader = binding.get("protected").toString(); String encodedSignature = binding.get("signature").toString(); String encodedPayload = binding.get("payload").toString(); String serialized = CompactSerializer.serialize(encodedHeader, encodedPayload, encodedSignature); assertExternalAccountBinding(serialized, resourceUrl, keyIdentifier, macKey); }