@Test public void testValidate3() throws InvalidKeySpecException, NoSuchAlgorithmException { final byte[] n = TokenDecoder.base64DecodeToByte("n4EPtAOCc9AlkeQHPzHStgAbgs7bTZLwUBZdR8_KuKPEHLd4rHVTeT-O-XV2jRojdNhxJWTDvNd7nqQ0VEiZQHz_AJmSCpMaJMRBSFKrKb2wqVwGU_NsYOYL-QtiWN2lbzcEe6XC0dApr5ydQLrHqkHHig3RBordaZ6Aj-oBHqFEHYpPe7Tpe-OfVfHd1E6cS6M1FZcD1NNLYD5lFHpPI9bTwJlsde3uhGqC0ZCuEHg8lhzwOHrtIQbS0FVbb9k3-tVTU4fg_3L_vniUFAKwuCLqKnS2BYwdq_mzSnbLY7h_qixoR7jig3__kRhuaxwUkRz5iaiQkqgc5gHdrNP5zw"); final byte[] e =TokenDecoder.base64DecodeToByte("AQAB"); final byte[] d = TokenDecoder.base64DecodeToByte("bWUC9B-EFRIo8kpGfh0ZuyGPvMNKvYWNtB_ikiH9k20eT-O1q_I78eiZkpXxXQ0UTEs2LsNRS-8uJbvQ-A1irkwMSMkK1J3XTGgdrhCku9gRldY7sNA_AKZGh-Q661_42rINLRCe8W-nZ34ui_qOfkLnK9QWDDqpaIsA-bMwWWSDFu2MUBYwkHTMEzLYGqOe04noqeq1hExBTHBOBdkMXiuFhUq1BU6l-DqEiWxqg82sXt2h-LMnT3046AOYJoRioz75tSUQfGCshWTBnP5uDjd18kKhyv07lhfSJdrPdM5Plyl21hsFf4L_mHCuoFau7gdsPfHPxxjVOcOpBrQzwQ"); BigInteger N = new BigInteger(1, n); BigInteger E = new BigInteger(1, e); BigInteger D = new BigInteger(1, d); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(N, E); RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(N, D); String rsa256 = "{\"kid\":\"bilbo.baggins@hobbiton.example\", \"alg\":\"RS256\"}"; String accessToken = TokenDecoder.base64Encode(rsa256)+ "."+ "SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IH"+ "lvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdSBk"+ "b24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcm"+ "UgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4"+ "."+ "CQKOXffDcqJ490YwuiHW7JfsLNxrRXKzCYFIDZtvznxtfxUMkcvriV4y-2_UGPqqzLNF67ps3VfI_J_OYSIfeoNfawa9bDNKtoqflRyGlDSaNIJYVjvNqVSvTFwcPcUqyVACcABosJMuAd9UWPAvNkGwRuXQEU8dD4_5KTilLbogLB4-rkQnQUq29vfA3VTOw8btMimSrsx0OrUaaOB3U9b3EpWilBEpPqndHRmgL_BPktn9gfk9xSoeGybmQGXOMZrvzH3DOAb4Ga6gzZeZImcDw5O48GiO78ARk_PJ7JXj0ebYn7m0svK-meFQRUVIfcnYxOopde9QwG6rit3Nmg"; JWS jws = new JWSReader().read(accessToken); SignatureMethod signatureMethod = new SignatureMethodRSAImpl("RS256"); RSAPublicKey rsaPublicKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec); Assert.assertTrue(jws.validate(signatureMethod, new PublicKey(rsaPublicKey))); }
public <SK extends SigningKey, VK extends VerifyingKey> boolean validate(SignatureMethod<SK, VK> method, VK verifyingKey) { if (!acceptAlgorithm(method)) { throw new IllegalArgumentException("Impossible to verify current JWS signature with algorithm '" + method.getAlgorithm() + "', JWS header specifies message has been signed with '" + header.getAlgorithm() + "' algorithm."); } if (verifyingKey == null) { throw new IllegalArgumentException("A verifying key is required in order to verify the signature."); } if (payload == null) { throw new IllegalStateException("JWS token must have a payload."); } if (signature == null) { throw new IllegalStateException("JWS token must have a signature to be verified."); } if (rawString == null) { return method.verify(signature, TokenDecoder.base64Encode(new JWSHeaderWriter().write(header)), TokenDecoder.base64Encode(payload), verifyingKey); } else { String jwt[] = rawString.split("\\."); return method.verify(jwt[2], jwt[0], jwt[1], verifyingKey); } }
JWS { public <SK extends SigningKey, VK extends VerifyingKey> boolean validate(SignatureMethod<SK, VK> method, VK verifyingKey) { if (!acceptAlgorithm(method)) { throw new IllegalArgumentException("Impossible to verify current JWS signature with algorithm '" + method.getAlgorithm() + "', JWS header specifies message has been signed with '" + header.getAlgorithm() + "' algorithm."); } if (verifyingKey == null) { throw new IllegalArgumentException("A verifying key is required in order to verify the signature."); } if (payload == null) { throw new IllegalStateException("JWS token must have a payload."); } if (signature == null) { throw new IllegalStateException("JWS token must have a signature to be verified."); } if (rawString == null) { return method.verify(signature, TokenDecoder.base64Encode(new JWSHeaderWriter().write(header)), TokenDecoder.base64Encode(payload), verifyingKey); } else { String jwt[] = rawString.split("\\."); return method.verify(jwt[2], jwt[0], jwt[1], verifyingKey); } } }
JWS { public <SK extends SigningKey, VK extends VerifyingKey> boolean validate(SignatureMethod<SK, VK> method, VK verifyingKey) { if (!acceptAlgorithm(method)) { throw new IllegalArgumentException("Impossible to verify current JWS signature with algorithm '" + method.getAlgorithm() + "', JWS header specifies message has been signed with '" + header.getAlgorithm() + "' algorithm."); } if (verifyingKey == null) { throw new IllegalArgumentException("A verifying key is required in order to verify the signature."); } if (payload == null) { throw new IllegalStateException("JWS token must have a payload."); } if (signature == null) { throw new IllegalStateException("JWS token must have a signature to be verified."); } if (rawString == null) { return method.verify(signature, TokenDecoder.base64Encode(new JWSHeaderWriter().write(header)), TokenDecoder.base64Encode(payload), verifyingKey); } else { String jwt[] = rawString.split("\\."); return method.verify(jwt[2], jwt[0], jwt[1], verifyingKey); } } JWS(Header header, String payload, String signature);   JWS(String rawString, Header header, String payload, String signature); }
JWS { public <SK extends SigningKey, VK extends VerifyingKey> boolean validate(SignatureMethod<SK, VK> method, VK verifyingKey) { if (!acceptAlgorithm(method)) { throw new IllegalArgumentException("Impossible to verify current JWS signature with algorithm '" + method.getAlgorithm() + "', JWS header specifies message has been signed with '" + header.getAlgorithm() + "' algorithm."); } if (verifyingKey == null) { throw new IllegalArgumentException("A verifying key is required in order to verify the signature."); } if (payload == null) { throw new IllegalStateException("JWS token must have a payload."); } if (signature == null) { throw new IllegalStateException("JWS token must have a signature to be verified."); } if (rawString == null) { return method.verify(signature, TokenDecoder.base64Encode(new JWSHeaderWriter().write(header)), TokenDecoder.base64Encode(payload), verifyingKey); } else { String jwt[] = rawString.split("\\."); return method.verify(jwt[2], jwt[0], jwt[1], verifyingKey); } } JWS(Header header, String payload, String signature);   JWS(String rawString, Header header, String payload, String signature); Header getHeader(); String getPayload(); String getSignature(); boolean acceptAlgorithm(SignatureMethod<SK, VK> method); boolean validate(SignatureMethod<SK, VK> method,
                                                                             VK verifyingKey); }
JWS { public <SK extends SigningKey, VK extends VerifyingKey> boolean validate(SignatureMethod<SK, VK> method, VK verifyingKey) { if (!acceptAlgorithm(method)) { throw new IllegalArgumentException("Impossible to verify current JWS signature with algorithm '" + method.getAlgorithm() + "', JWS header specifies message has been signed with '" + header.getAlgorithm() + "' algorithm."); } if (verifyingKey == null) { throw new IllegalArgumentException("A verifying key is required in order to verify the signature."); } if (payload == null) { throw new IllegalStateException("JWS token must have a payload."); } if (signature == null) { throw new IllegalStateException("JWS token must have a signature to be verified."); } if (rawString == null) { return method.verify(signature, TokenDecoder.base64Encode(new JWSHeaderWriter().write(header)), TokenDecoder.base64Encode(payload), verifyingKey); } else { String jwt[] = rawString.split("\\."); return method.verify(jwt[2], jwt[0], jwt[1], verifyingKey); } } JWS(Header header, String payload, String signature);   JWS(String rawString, Header header, String payload, String signature); Header getHeader(); String getPayload(); String getSignature(); boolean acceptAlgorithm(SignatureMethod<SK, VK> method); boolean validate(SignatureMethod<SK, VK> method,
                                                                             VK verifyingKey);  }