@Test public void testWriteJwsSignedByMacSpecExample() throws Exception { JwtHeaders headers = new JwtHeaders(Algorithm.HmacSHA256.getJwtName()); JwsCompactProducer jws = initSpecJwtTokenWriter(headers); jws.signWith(new HmacJwsSignatureProvider(ENCODED_MAC_KEY)); assertEquals(ENCODED_TOKEN_SIGNED_BY_MAC, jws.getSignedEncodedJws()); }
@Test public void testWriteJwsSignedByPrivateKey() throws Exception { JwtHeaders headers = new JwtHeaders(); headers.setAlgorithm(Algorithm.SHA256withRSA.getJwtName()); JwsCompactProducer jws = initSpecJwtTokenWriter(headers); PrivateKey key = CryptoUtils.getRSAPrivateKey(RSA_MODULUS_ENCODED, RSA_PRIVATE_EXPONENT_ENCODED); jws.signWith(new PrivateKeyJwsSignatureProvider(key)); assertEquals(ENCODED_TOKEN_SIGNED_BY_PRIVATE_KEY, jws.getSignedEncodedJws()); }
@Test public void testWriteReadJwsSignedByESPrivateKey() throws Exception { JwtHeaders headers = new JwtHeaders(); headers.setAlgorithm(Algorithm.SHA256withECDSA.getJwtName()); JwsCompactProducer jws = initSpecJwtTokenWriter(headers); ECPrivateKey privateKey = CryptoUtils.getECPrivateKey(EC_PRIVATE_KEY_ENCODED); jws.signWith(new EcDsaJwsSignatureProvider(privateKey)); String signedJws = jws.getSignedEncodedJws(); ECPublicKey publicKey = CryptoUtils.getECPublicKey(EC_X_POINT_ENCODED, EC_Y_POINT_ENCODED); JwsJwtCompactConsumer jwsConsumer = new JwsJwtCompactConsumer(signedJws); assertTrue(jwsConsumer.verifySignatureWith(new PublicKeyJwsSignatureVerifier(publicKey))); JwtToken token = jwsConsumer.getJwtToken(); JwtHeaders headersReceived = token.getHeaders(); assertEquals(Algorithm.SHA256withECDSA.getJwtName(), headersReceived.getAlgorithm()); validateSpecClaim(token.getClaims()); }
private void doTestWriteJwsWithJwkSignedByMac(Object jsonWebKey) throws Exception { JwtHeaders headers = new JwtHeaders(Algorithm.HmacSHA256.getJwtName()); headers.setHeader(JwtConstants.HEADER_JSON_WEB_KEY, jsonWebKey); JwtClaims claims = new JwtClaims(); claims.setIssuer("joe"); claims.setExpiryTime(1300819380L); claims.setClaim("http://example.com/is_root", Boolean.TRUE); JwtToken token = new JwtToken(headers, claims); JwsCompactProducer jws = new JwsJwtCompactProducer(token, getWriter()); jws.signWith(new HmacJwsSignatureProvider(ENCODED_MAC_KEY)); assertEquals(ENCODED_TOKEN_WITH_JSON_KEY_SIGNED_BY_MAC, jws.getSignedEncodedJws()); }