jwks parsing
This commit is contained in:
1 parent
4a650e5417
commit
2d7e861086
9 files changed
+176
-51
No files matched your search
Regular → Executable
+22
@@ -1,7 +1,12 @@
|
||||
package org.keycloak.jose.jwk;
|
||||
|
||||
import org.codehaus.jackson.annotate.JsonAnyGetter;
|
||||
import org.codehaus.jackson.annotate.JsonAnySetter;
|
||||
import org.codehaus.jackson.annotate.JsonProperty;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
|
||||
*/
|
||||
@@ -15,6 +20,9 @@ public class JWK {
|
||||
|
||||
public static final String PUBLIC_KEY_USE = "use";
|
||||
|
||||
public static final String SIG_USE = "sig";
|
||||
public static final String ENCRYPTION_USE = "enc";
|
||||
|
||||
@JsonProperty(KEY_ID)
|
||||
private String keyId;
|
||||
|
||||
@@ -27,6 +35,9 @@ public class JWK {
|
||||
@JsonProperty(PUBLIC_KEY_USE)
|
||||
private String publicKeyUse;
|
||||
|
||||
protected Map<String, Object> otherClaims = new HashMap<String, Object>();
|
||||
|
||||
|
||||
public String getKeyId() {
|
||||
return keyId;
|
||||
}
|
||||
@@ -59,4 +70,15 @@ public class JWK {
|
||||
this.publicKeyUse = publicKeyUse;
|
||||
}
|
||||
|
||||
@JsonAnyGetter
|
||||
public Map<String, Object> getOtherClaims() {
|
||||
return otherClaims;
|
||||
}
|
||||
|
||||
@JsonAnySetter
|
||||
public void setOtherClaims(String name, Object value) {
|
||||
otherClaims.put(name, value);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Regular → Executable
+17
-5
@@ -17,29 +17,41 @@ public class JWKParser {
|
||||
|
||||
private static TypeReference<Map<String,String>> typeRef = new TypeReference<Map<String,String>>() {};
|
||||
|
||||
private Map<String, String> values;
|
||||
private JWK jwk;
|
||||
|
||||
private JWKParser() {
|
||||
}
|
||||
|
||||
public JWKParser(JWK jwk) {
|
||||
this.jwk = jwk;
|
||||
}
|
||||
|
||||
public static JWKParser create() {
|
||||
return new JWKParser();
|
||||
}
|
||||
|
||||
public static JWKParser create(JWK jwk) {
|
||||
return new JWKParser(jwk);
|
||||
}
|
||||
|
||||
public JWKParser parse(String jwk) {
|
||||
try {
|
||||
this.values = JsonSerialization.mapper.readValue(jwk, typeRef);
|
||||
this.jwk = JsonSerialization.mapper.readValue(jwk, JWK.class);
|
||||
return this;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public JWK getJwk() {
|
||||
return jwk;
|
||||
}
|
||||
|
||||
public PublicKey toPublicKey() {
|
||||
String algorithm = values.get(JWK.KEY_TYPE);
|
||||
String algorithm = jwk.getKeyType();
|
||||
if (RSAPublicJWK.RSA.equals(algorithm)) {
|
||||
BigInteger modulus = new BigInteger(1, Base64Url.decode(values.get(RSAPublicJWK.MODULUS)));
|
||||
BigInteger publicExponent = new BigInteger(1, Base64Url.decode(values.get(RSAPublicJWK.PUBLIC_EXPONENT)));
|
||||
BigInteger modulus = new BigInteger(1, Base64Url.decode(jwk.getOtherClaims().get(RSAPublicJWK.MODULUS).toString()));
|
||||
BigInteger publicExponent = new BigInteger(1, Base64Url.decode(jwk.getOtherClaims().get(RSAPublicJWK.PUBLIC_EXPONENT).toString()));
|
||||
|
||||
try {
|
||||
return KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(modulus, publicExponent));
|
||||
|
||||
Reference in new issue
Block a user