89 log.debug(
"Retrieving JWK...");
91 JSONObject jsonKeyValue =
getJsonKey(jwksUri, jwks, keyId);
93 if (jsonKeyValue == null) {
100 String resultKeyId = jsonKeyValue.getString(KEY_ID);
101 if (signatureAlgorithm == null) {
102 signatureAlgorithm = SignatureAlgorithm.fromString(jsonKeyValue.getString(ALGORITHM));
103 if (signatureAlgorithm == null) {
104 log.error(String.format(
"Failed to determine key '%s' signature algorithm", resultKeyId));
109 JSONObject jsonPublicKey = jsonKeyValue;
110 if (jsonKeyValue.has(PUBLIC_KEY)) {
112 jsonPublicKey = jsonKeyValue.getJSONObject(PUBLIC_KEY);
115 if (signatureAlgorithm == SignatureAlgorithm.RS256 || signatureAlgorithm == SignatureAlgorithm.RS384 || signatureAlgorithm == SignatureAlgorithm.RS512) {
118 String exp = jsonPublicKey.getString(EXPONENT);
119 String mod = jsonPublicKey.getString(MODULUS);
121 BigInteger publicExponent =
new BigInteger(1, Base64Util.base64urldecode(exp));
122 BigInteger modulus =
new BigInteger(1, Base64Util.base64urldecode(mod));
124 publicKey =
new RSAPublicKey(modulus, publicExponent);
125 }
else if (signatureAlgorithm == SignatureAlgorithm.ES256 || signatureAlgorithm == SignatureAlgorithm.ES384 || signatureAlgorithm == SignatureAlgorithm.ES512) {
129 String xx = jsonPublicKey.getString(X);
130 String yy = jsonPublicKey.getString(Y);
132 BigInteger x =
new BigInteger(1, Base64Util.base64urldecode(xx));
133 BigInteger y =
new BigInteger(1, Base64Util.base64urldecode(yy));
135 publicKey =
new ECDSAPublicKey(signatureAlgorithm, x, y);
138 if (publicKey != null && jsonKeyValue.has(CERTIFICATE_CHAIN)) {
139 final String BEGIN =
"-----BEGIN CERTIFICATE-----";
140 final String END =
"-----END CERTIFICATE-----";
142 JSONArray certChain = jsonKeyValue.getJSONArray(CERTIFICATE_CHAIN);
143 String certificateString = BEGIN +
"\n" + certChain.getString(0) +
"\n" + END;
144 StringReader sr =
new StringReader(certificateString);
145 PEMParser pemReader =
new PEMParser(sr);
146 X509Certificate cert = (X509CertificateObject) pemReader.readObject();
147 Certificate certificate =
new Certificate(signatureAlgorithm, cert);
148 publicKey.setCertificate(certificate);
150 if (publicKey != null) {
151 publicKey.setKeyId(resultKeyId);
152 publicKey.setSignatureAlgorithm(signatureAlgorithm);
154 }
catch (Exception ex) {
155 log.error(ex.getMessage(), ex);
static final Logger log
Definition: JwtUtil.java:40
static JSONObject getJsonKey(String jwksUri, String jwks, String keyId)
Definition: JwtUtil.java:161
Definition: AbstractCryptoProvider.java:6
Definition: PublicKey.java:18