com.nimbusds.jose.jwk.JWK类的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(12.7k)|赞(0)|评价(0)|浏览(571)

本文整理了Java中com.nimbusds.jose.jwk.JWK类的一些代码示例,展示了JWK类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JWK类的具体详情如下:
包路径:com.nimbusds.jose.jwk.JWK
类名称:JWK

JWK介绍

[英]The base abstract class for JSON Web Keys (JWKs). It serialises to a JSON object.

The following JSON object members are common to all JWK types:

  • #getKeyType (required)
  • #getKeyUse (optional)
  • #getKeyOperations (optional)
  • #getKeyID (optional)
  • #getX509CertURL() (optional)
  • #getX509CertThumbprint() (optional)
  • #getX509CertSHA256Thumbprint() (optional)
  • #getX509CertChain() (optional)
  • #getKeyStore()

Example JWK (of the Elliptic Curve type):

  1. {
  2. "kty" : "EC",
  3. "crv" : "P-256",
  4. "x" : "MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4",
  5. "y" : "4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM",
  6. "use" : "enc",
  7. "kid" : "1"
  8. }

[中]JSON Web键(JWKs)的基本抽象类。它序列化为一个JSON对象。
以下JSON对象成员对于所有JWK类型都是通用的:
*#getKeyType(必需)
*#getKeyUse(可选)
*#getKeyOperations(可选)
*#getKeyID(可选)
*#getX509CertURL()(可选)
*#getX509CertThumbprint()(可选)
*#getX509CertSHA256Thumbprint()(可选)
*#getX509CertChain()(可选)
*#getKeyStore()
示例JWK(椭圆曲线类型):

  1. {
  2. "kty" : "EC",
  3. "crv" : "P-256",
  4. "x" : "MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4",
  5. "y" : "4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM",
  6. "use" : "enc",
  7. "kid" : "1"
  8. }

代码示例

代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server

  1. /**
  2. * Build this service based on the given keystore. All keys must have a key
  3. * id ({@code kid}) field in order to be used.
  4. *
  5. * @param keyStore
  6. * @throws NoSuchAlgorithmException
  7. * @throws InvalidKeySpecException
  8. * @throws JOSEException
  9. */
  10. public DefaultJWTEncryptionAndDecryptionService(JWKSetKeyStore keyStore) throws NoSuchAlgorithmException, InvalidKeySpecException, JOSEException {
  11. // convert all keys in the keystore to a map based on key id
  12. for (JWK key : keyStore.getKeys()) {
  13. if (!Strings.isNullOrEmpty(key.getKeyID())) {
  14. this.keys.put(key.getKeyID(), key);
  15. } else {
  16. throw new IllegalArgumentException("Tried to load a key from a keystore without a 'kid' field: " + key);
  17. }
  18. }
  19. buildEncryptersAndDecrypters();
  20. }

代码示例来源:origin: spring-projects/spring-security

  1. @Test
  2. public void getWhenMatchThenCreatesKeys() {
  3. when(this.matcher.matches(any())).thenReturn(true);
  4. List<JWK> keys = this.source.get(this.selector).block();
  5. assertThat(keys).hasSize(2);
  6. JWK key1 = keys.get(0);
  7. assertThat(key1.getKeyID()).isEqualTo("1923397381d9574bb873202a90c32b7ceeaed027");
  8. assertThat(key1.getAlgorithm().getName()).isEqualTo("RS256");
  9. assertThat(key1.getKeyType()).isEqualTo(KeyType.RSA);
  10. assertThat(key1.getKeyUse()).isEqualTo(KeyUse.SIGNATURE);
  11. JWK key2 = keys.get(1);
  12. assertThat(key2.getKeyID()).isEqualTo("7ddf54d3032d1f0d48c3618892ca74c1ac30ad77");
  13. assertThat(key2.getAlgorithm().getName()).isEqualTo("RS256");
  14. assertThat(key2.getKeyType()).isEqualTo(KeyType.RSA);
  15. assertThat(key2.getKeyUse()).isEqualTo(KeyUse.SIGNATURE);
  16. }

代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server

  1. @Override
  2. public Map<String, JWK> getAllPublicKeys() {
  3. Map<String, JWK> pubKeys = new HashMap<>();
  4. // pull all keys out of the verifiers if we know how
  5. for (String keyId : keys.keySet()) {
  6. JWK key = keys.get(keyId);
  7. JWK pub = key.toPublicJWK();
  8. if (pub != null) {
  9. pubKeys.put(keyId, pub);
  10. }
  11. }
  12. return pubKeys;
  13. }

代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server

  1. encrypters.put(id, encrypter);
  2. if (jwk.isPrivate()) { // we can decrypt!
  3. RSADecrypter decrypter = new RSADecrypter((RSAKey) jwk);
  4. decrypter.getJCAContext().setProvider(BouncyCastleProviderSingleton.getInstance());
  5. decrypters.put(id, decrypter);
  6. } else {
  7. logger.warn("No private key for key #" + jwk.getKeyID());
  8. encrypters.put(id, encrypter);
  9. if (jwk.isPrivate()) { // we can decrypt too
  10. ECDHDecrypter decrypter = new ECDHDecrypter((ECKey) jwk);
  11. decrypter.getJCAContext().setProvider(BouncyCastleProviderSingleton.getInstance());
  12. decrypters.put(id, decrypter);
  13. } else {
  14. logger.warn("No private key for key # " + jwk.getKeyID());

代码示例来源:origin: com.nimbusds/nimbus-jose-jwt

  1. if (hasUse && key.getKeyUse() == null)
  2. return false;
  3. if (hasID && (key.getKeyID() == null || key.getKeyID().trim().isEmpty()))
  4. return false;
  5. if (privateOnly && ! key.isPrivate())
  6. return false;
  7. if (publicOnly && key.isPrivate())
  8. return false;
  9. if (types != null && ! types.contains(key.getKeyType()))
  10. return false;
  11. if (uses != null && ! uses.contains(key.getKeyUse()))
  12. return false;
  13. if (ops.contains(null) && key.getKeyOperations() == null) {
  14. } else if (key.getKeyOperations() != null && ops.containsAll(key.getKeyOperations())) {
  15. if (algs != null && ! algs.contains(key.getAlgorithm()))
  16. return false;
  17. if (ids != null && ! ids.contains(key.getKeyID()))
  18. return false;
  19. if (key.size() < minSizeBits)

代码示例来源:origin: gravitee-io/graviteeio-access-management

  1. private JWK convert(com.nimbusds.jose.jwk.JWK nimbusJwk) {
  2. RSAKey jwk = new RSAKey();
  3. if (nimbusJwk.getKeyType() != null) {
  4. jwk.setKty(nimbusJwk.getKeyType().getValue());
  5. if (nimbusJwk.getKeyUse() != null) {
  6. jwk.setUse(nimbusJwk.getKeyUse().identifier());
  7. if (nimbusJwk.getKeyOperations() != null) {
  8. jwk.setKeyOps(nimbusJwk.getKeyOperations().stream().map(keyOperation -> keyOperation.identifier()).collect(Collectors.toSet()));
  9. if (nimbusJwk.getAlgorithm() != null) {
  10. jwk.setAlg(nimbusJwk.getAlgorithm().getName());
  11. if (nimbusJwk.getKeyID() != null) {
  12. jwk.setKid(nimbusJwk.getKeyID());
  13. if (nimbusJwk.getX509CertURL() != null) {
  14. jwk.setX5u(nimbusJwk.getX509CertURL().toString());
  15. if (nimbusJwk.getX509CertChain() != null) {
  16. jwk.setX5c(nimbusJwk.getX509CertChain().stream().map(cert -> cert.toString()).collect(Collectors.toSet()));
  17. if (nimbusJwk.getX509CertThumbprint() != null) {
  18. jwk.setX5t(nimbusJwk.getX509CertThumbprint().toString());
  19. if (nimbusJwk.getX509CertSHA256Thumbprint() != null) {
  20. jwk.setX5tS256(nimbusJwk.getX509CertSHA256Thumbprint().toString());

代码示例来源:origin: de.adorsys.sts/sts-crypto-utils

  1. public StsServerKeyMap(JWKSet jwkSet) {
  2. List<JWK> keys = jwkSet.getKeys();
  3. for (JWK jwk : keys) {
  4. if (jwk instanceof RSAKey) {
  5. Key key = KeyConverter.toPrivateOrSecret(jwk);
  6. if(key!=null && jwk.getKeyID()!=null){
  7. KeyAndJwk keyAndJwk = new KeyAndJwk(key, jwk);
  8. keyMap.put(jwk.getKeyID(), keyAndJwk);
  9. if(KeyUse.SIGNATURE.equals(jwk.getKeyUse())){
  10. signKeyList.add(keyAndJwk);
  11. } else if (KeyUse.ENCRYPTION.equals(jwk.getKeyUse())){
  12. encKeyList.add(keyAndJwk);
  13. }
  14. }
  15. } else if (jwk instanceof SecretJWK) {
  16. Key key = KeyConverter.toPrivateOrSecret(jwk);
  17. if(key!=null && jwk.getKeyID()!=null){
  18. KeyAndJwk keyAndJwk = new KeyAndJwk(key, jwk);
  19. keyMap.put(jwk.getKeyID(), keyAndJwk);
  20. secretKeyList.add(keyAndJwk);
  21. }
  22. }
  23. }
  24. }

代码示例来源:origin: GoogleCloudPlatform/java-docs-samples

  1. private ECPublicKey getKey(String kid, String alg) throws Exception {
  2. JWK jwk = keyCache.get(kid);
  3. if (jwk == null) {
  4. // update cache loading jwk public key data from url
  5. JWKSet jwkSet = JWKSet.load(new URL(PUBLIC_KEY_VERIFICATION_URL));
  6. for (JWK key : jwkSet.getKeys()) {
  7. keyCache.put(key.getKeyID(), key);
  8. }
  9. jwk = keyCache.get(kid);
  10. }
  11. // confirm that algorithm matches
  12. if (jwk != null && jwk.getAlgorithm().getName().equals(alg)) {
  13. return ECKey.parse(jwk.toJSONString()).toECPublicKey();
  14. }
  15. return null;
  16. }

代码示例来源:origin: de.adorsys.cryptoutils/jjwk

  1. boolean change = false;
  2. for (JWK jwk : keys) {
  3. String keyID = jwk.getKeyID();
  4. Base64URL thumbprint = jwk.computeThumbprint();
  5. String expectedKeyId = thumbprint.toString().toLowerCase();
  6. if(!StringUtils.equals(keyID, expectedKeyId)){

代码示例来源:origin: de.adorsys.sts/sts-simple-encryption

  1. private JWK tryToParseJwk(String key) {
  2. JWK parsedKey;
  3. try {
  4. parsedKey = JWK.parse(key);
  5. } catch (ParseException e) {
  6. throw new IllegalArgumentException(e);
  7. }
  8. return parsedKey;
  9. }
  10. }

代码示例来源:origin: de.adorsys.cryptoutils/jjwk

  1. public static JWSAlgorithm getJWSAlgo(KeyAndJwk randomKey) {
  2. Algorithm algorithm = randomKey.jwk.getAlgorithm();
  3. if(algorithm!=null && (algorithm instanceof JWSAlgorithm)) return (JWSAlgorithm) algorithm;
  4. KeyType keyType = randomKey.jwk.getKeyType();
  5. if(keyType!=null){
  6. if(KeyType.RSA.equals(keyType)){
  7. return JWSAlgorithm.RS256;
  8. } else if(KeyType.EC.equals(keyType)){
  9. return JWSAlgorithm.ES256;
  10. } else if(KeyType.OCT.equals(keyType)){
  11. return JWSAlgorithm.HS256;
  12. } else {
  13. throw new IllegalStateException("Unknown key type: " + keyType);
  14. }
  15. } else {
  16. if(randomKey.jwk instanceof RSAKey){
  17. return JWSAlgorithm.RS256;
  18. } else if (randomKey.jwk instanceof ECKey){
  19. return JWSAlgorithm.ES256;
  20. } else if (randomKey.jwk instanceof OctetSequenceKey){
  21. return JWSAlgorithm.HS256;
  22. } else {
  23. throw new IllegalStateException("Unknown key type: " + randomKey.jwk.getClass().getName());
  24. }
  25. }
  26. }

代码示例来源:origin: io.gravitee.am.gateway.handlers/gravitee-am-gateway-handler

  1. private JWK convert(com.nimbusds.jose.jwk.JWK jwk) {
  2. if (jwk == null) {
  3. return null;
  4. }
  5. switch (KeyType.valueOf(jwk.getKeyType().getValue())) {
  6. case EC:
  7. return fromEC((com.nimbusds.jose.jwk.ECKey) jwk);
  8. case RSA:
  9. return fromRSA((com.nimbusds.jose.jwk.RSAKey) jwk);
  10. case OCT:
  11. throw new NotImplementedException("JWK Key Type:" + KeyType.OCT.getKeyType());
  12. case OKP:
  13. throw new NotImplementedException("JWK Key Type:" + KeyType.OKP.getKeyType());
  14. default:
  15. throw new InvalidClientMetadataException("Unknown JWK Key Type (kty)");
  16. }
  17. }

代码示例来源:origin: de.adorsys.cryptoutils/jjwk

  1. try {
  2. Algorithm alg = jwk.getAlgorithm();
  3. if(alg!=null){
  4. if(alg instanceof JWSAlgorithm) {

代码示例来源:origin: com.nimbusds/nimbus-jose-jwt

  1. /**
  2. * Returns the JSON object representation of this JSON Web Key (JWK)
  3. * set.
  4. *
  5. * @param publicKeysOnly Controls the inclusion of private keys and
  6. * parameters into the output JWK members. If
  7. * {@code true} private keys and parameters will
  8. * be omitted. If {@code false} all available key
  9. * parameters will be included.
  10. *
  11. * @return The JSON object representation.
  12. */
  13. public JSONObject toJSONObject(final boolean publicKeysOnly) {
  14. JSONObject o = new JSONObject(customMembers);
  15. JSONArray a = new JSONArray();
  16. for (JWK key: keys) {
  17. if (publicKeysOnly) {
  18. // Try to get public key, then serialise
  19. JWK publicKey = key.toPublicJWK();
  20. if (publicKey != null) {
  21. a.add(publicKey.toJSONObject());
  22. }
  23. } else {
  24. a.add(key.toJSONObject());
  25. }
  26. }
  27. o.put("keys", a);
  28. return o;
  29. }

代码示例来源:origin: de.adorsys.cryptoutils/jjwk

  1. public ServerKeyMap(JWKSet jwkSet){
  2. List<JWK> keys = jwkSet.getKeys();
  3. for (JWK jwk : keys) {
  4. if (jwk instanceof AssymetricJWK) {
  5. Key key = KeyConverter.toPrivateOrSecret(jwk);
  6. if(key!=null && jwk.getKeyID()!=null){
  7. KeyAndJwk keyAndJwk = new KeyAndJwk(key, jwk);
  8. keyMap.put(jwk.getKeyID(), keyAndJwk);
  9. if(KeyUse.SIGNATURE.equals(jwk.getKeyUse())){
  10. signKeyList.add(keyAndJwk);
  11. } else if (KeyUse.ENCRYPTION.equals(jwk.getKeyUse())){
  12. encKeyList.add(keyAndJwk);
  13. }
  14. }
  15. } else if (jwk instanceof SecretJWK) {
  16. Key key = KeyConverter.toPrivateOrSecret(jwk);
  17. if(key!=null && jwk.getKeyID()!=null){
  18. KeyAndJwk keyAndJwk = new KeyAndJwk(key, jwk);
  19. keyMap.put(jwk.getKeyID(), keyAndJwk);
  20. secretKeyList.add(keyAndJwk);
  21. }
  22. }
  23. }
  24. }

代码示例来源:origin: de.adorsys.sts/sts-simple-encryption

  1. private static Key extractSecretKey(String jwkAsString) {
  2. Key key;
  3. try {
  4. JWK parsedKey = JWK.parse(jwkAsString);
  5. key = KeyConverter.toPrivateOrSecret(parsedKey, "AES");
  6. } catch (ParseException e) {
  7. throw new IllegalArgumentException(e);
  8. }
  9. return key;
  10. }
  11. }

代码示例来源:origin: gravitee-io/graviteeio-access-management

  1. private JWK convert(com.nimbusds.jose.jwk.JWK jwk) {
  2. if (jwk == null) {
  3. return null;
  4. }
  5. switch (KeyType.valueOf(jwk.getKeyType().getValue())) {
  6. case EC:
  7. return fromEC((com.nimbusds.jose.jwk.ECKey) jwk);
  8. case RSA:
  9. return fromRSA((com.nimbusds.jose.jwk.RSAKey) jwk);
  10. case OCT:
  11. throw new NotImplementedException("JWK Key Type:" + KeyType.OCT.getKeyType());
  12. case OKP:
  13. throw new NotImplementedException("JWK Key Type:" + KeyType.OKP.getKeyType());
  14. default:
  15. throw new InvalidClientMetadataException("Unknown JWK Key Type (kty)");
  16. }
  17. }

代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server

  1. /**
  2. * Build this service based on the given keystore. All keys must have a key
  3. * id ({@code kid}) field in order to be used.
  4. *
  5. * @param keyStore
  6. * the keystore to load all keys from
  7. *
  8. * @throws InvalidKeySpecException
  9. * If the keys in the JWKs are not valid
  10. * @throws NoSuchAlgorithmException
  11. * If there is no appropriate algorithm to tie the keys to.
  12. */
  13. public DefaultJWTSigningAndValidationService(JWKSetKeyStore keyStore) throws NoSuchAlgorithmException, InvalidKeySpecException {
  14. // convert all keys in the keystore to a map based on key id
  15. if (keyStore!= null && keyStore.getJwkSet() != null) {
  16. for (JWK key : keyStore.getKeys()) {
  17. if (!Strings.isNullOrEmpty(key.getKeyID())) {
  18. // use the key ID that's built into the key itself
  19. this.keys.put(key.getKeyID(), key);
  20. } else {
  21. // create a random key id
  22. String fakeKid = UUID.randomUUID().toString();
  23. this.keys.put(fakeKid, key);
  24. }
  25. }
  26. }
  27. buildSignersAndVerifiers();
  28. }

代码示例来源:origin: com.nimbusds/nimbus-jose-jwt

  1. /**
  2. * Parses a JWK from the specified JSON object string representation.
  3. * The JWK must be an {@link ECKey}, an {@link RSAKey}, or a
  4. * {@link OctetSequenceKey}.
  5. *
  6. * @param s The JSON object string to parse. Must not be {@code null}.
  7. *
  8. * @return The JWK.
  9. *
  10. * @throws ParseException If the string couldn't be parsed to a
  11. * supported JWK.
  12. */
  13. public static JWK parse(final String s)
  14. throws ParseException {
  15. return parse(JSONObjectUtils.parse(s));
  16. }

代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server

  1. @Override
  2. public Map<String, JWK> getAllPublicKeys() {
  3. Map<String, JWK> pubKeys = new HashMap<>();
  4. // pull out all public keys
  5. for (String keyId : keys.keySet()) {
  6. JWK key = keys.get(keyId);
  7. JWK pub = key.toPublicJWK();
  8. if (pub != null) {
  9. pubKeys.put(keyId, pub);
  10. }
  11. }
  12. return pubKeys;
  13. }

相关文章