|
19 | 19 |
|
20 | 20 | package com.auth0.jwt.algorithms; |
21 | 21 |
|
| 22 | +import com.auth0.jwk.Jwk; |
| 23 | +import com.auth0.jwk.JwkProvider; |
| 24 | +import com.auth0.jwk.UrlJwkProvider; |
22 | 25 | import com.auth0.jwt.creators.EncodeType; |
23 | 26 | import com.auth0.jwt.exceptions.SignatureGenerationException; |
24 | 27 | import com.auth0.jwt.exceptions.SignatureVerificationException; |
25 | 28 | import com.auth0.jwt.interfaces.DecodedJWT; |
26 | 29 | import com.auth0.jwt.interfaces.ECDSAKeyProvider; |
| 30 | +import com.auth0.jwt.interfaces.Payload; |
| 31 | +import com.google.gson.JsonElement; |
| 32 | +import com.google.gson.JsonObject; |
| 33 | +import com.google.gson.JsonParser; |
| 34 | +import com.nimbusds.jose.JWSAlgorithm; |
| 35 | +import com.nimbusds.jose.JWSHeader; |
| 36 | +import com.nimbusds.jose.JWSObject; |
| 37 | +import com.nimbusds.jose.crypto.RSASSASigner; |
| 38 | +import com.nimbusds.jose.crypto.RSASSAVerifier; |
| 39 | +import com.nimbusds.jose.jwk.JWK; |
| 40 | +import net.minidev.json.JSONArray; |
| 41 | +import net.minidev.json.JSONObject; |
| 42 | +import net.minidev.json.parser.JSONParser; |
27 | 43 | import org.apache.commons.codec.binary.Base32; |
28 | 44 | import org.apache.commons.codec.binary.Base64; |
29 | 45 | import org.apache.commons.codec.binary.Hex; |
30 | 46 | import org.apache.commons.codec.binary.StringUtils; |
31 | 47 |
|
| 48 | +import java.io.File; |
| 49 | +import java.io.FileReader; |
| 50 | +import java.net.URL; |
32 | 51 | import java.net.URLDecoder; |
33 | 52 | import java.nio.charset.StandardCharsets; |
34 | | -import java.security.InvalidKeyException; |
35 | | -import java.security.NoSuchAlgorithmException; |
36 | | -import java.security.SignatureException; |
| 53 | +import java.security.*; |
37 | 54 | import java.security.interfaces.ECPrivateKey; |
38 | 55 | import java.security.interfaces.ECPublicKey; |
| 56 | +import java.security.interfaces.RSAPrivateKey; |
| 57 | +import java.security.interfaces.RSAPublicKey; |
| 58 | +import java.util.List; |
39 | 59 |
|
40 | 60 | class ECDSAAlgorithm extends Algorithm { |
41 | 61 |
|
@@ -80,10 +100,48 @@ public void verify(DecodedJWT jwt, EncodeType encodeType) throws Exception { |
80 | 100 | } |
81 | 101 |
|
82 | 102 | try { |
83 | | - ECPublicKey publicKey = keyProvider.getPublicKeyById(jwt.getKeyId()); |
| 103 | + |
| 104 | + //create a http request that gets back a response for the jwks uri and then once you get back the response, |
| 105 | + //parse it to get back the x509 DER string and get the public key from that string |
| 106 | + //from the public key of that string, pass it into verifySignatureFor() |
| 107 | + PublicKey publicKey = null; |
| 108 | + String kid = jwt.getKeyId(); |
| 109 | + String algorithm = jwt.getAlgorithm(); |
| 110 | + if(kid == null) { |
| 111 | + publicKey = keyProvider.getPublicKeyById(kid); |
| 112 | + } else if(algorithm.equals("RSA")){ |
| 113 | + //JwkProvider provider = new UrlJwkProvider("https://sandrino.auth0.com/.well-known/jwks.json"); |
| 114 | + JwkProvider provider = new UrlJwkProvider(new File("/Users/jdahmubed/documents/jwksRSA.json").toURI().toURL());//"file:///); |
| 115 | + Jwk jwk = provider.get(kid); |
| 116 | + publicKey = jwk.getPublicKey(); |
| 117 | + } /*else if(algorithm.contains("ES")) { |
| 118 | + // JSONParser parser = new JSONParser(); |
| 119 | + // JSONArray a = (JSONArray) parser.parse(new FileReader("/Users/jdahmubed/documents/jwks.json")); |
| 120 | +
|
| 121 | + JsonObject gsonObject = new JsonObject(); |
| 122 | +
|
| 123 | +
|
| 124 | + JsonParser parser = new JsonParser(); |
| 125 | + JsonElement jsonElement = parser.parse(new FileReader("/Users/jdahmubed/documents/jwks.json")); |
| 126 | + gsonObject = jsonElement.getAsJsonObject(); |
| 127 | +
|
| 128 | + JSONObject jsonObject = new JSONObject(); |
| 129 | + for(String key : gsonObject.keySet()) { |
| 130 | + jsonObject.put(key, gsonObject.get(key)); |
| 131 | + } |
| 132 | + jsonObject.put("alg", "ES256"); |
| 133 | + JWSHeader jwsHeader = JWSHeader.parse(jsonObject); |
| 134 | +
|
| 135 | + JWSHeader header = new JWSHeader(JWSAlgorithm.ES256); |
| 136 | + header.setJWKURL(new File("/Users/jdahmubed/documents/jwks.json").toURI().toURL()); |
| 137 | + List<com.nimbusds.jose.util.Base64> list = header.getX509CertChain(); |
| 138 | + System.out.print(list); |
| 139 | + }*/ |
| 140 | + |
84 | 141 | if (publicKey == null) { |
85 | 142 | throw new IllegalStateException("The given Public Key is null."); |
86 | 143 | } |
| 144 | + //pass in publicKey from x509 or the current key (look up) |
87 | 145 | boolean valid = crypto.verifySignatureFor(getDescription(), publicKey, contentBytes, JOSEToDER(signatureBytes)); |
88 | 146 |
|
89 | 147 | if (!valid) { |
@@ -241,6 +299,7 @@ static ECDSAKeyProvider providerForKeys(final ECPublicKey publicKey, final ECPri |
241 | 299 | if (publicKey == null && privateKey == null) { |
242 | 300 | throw new IllegalArgumentException("Both provided Keys cannot be null."); |
243 | 301 | } |
| 302 | + |
244 | 303 | return new ECDSAKeyProvider() { |
245 | 304 | @Override |
246 | 305 | public ECPublicKey getPublicKeyById(String keyId) { |
|
0 commit comments