Skip to content

Commit b75d774

Browse files
author
Justin Richer
committed
if there's only one key, return it as the default
1 parent fb2f2f9 commit b75d774

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

openid-connect-common/src/main/java/org/mitre/jwt/encryption/service/impl/DefaultJwtEncryptionAndDecryptionService.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ public DefaultJwtEncryptionAndDecryptionService(JWKSetKeyStore keyStore) throws
102102
throw new IllegalArgumentException("Tried to load a key from a keystore without a 'kid' field: " + key);
103103
}
104104
}
105+
105106
buildEncryptersAndDecrypters();
107+
106108
}
107109

108110

@@ -116,15 +118,29 @@ public void afterPropertiesSet() throws NoSuchAlgorithmException, InvalidKeySpec
116118
}
117119

118120
public String getDefaultEncryptionKeyId() {
119-
return defaultEncryptionKeyId;
121+
if (defaultEncryptionKeyId != null) {
122+
return defaultEncryptionKeyId;
123+
} else if (keys.size() == 1) {
124+
// if there's only one key in the map, it's the default
125+
return keys.keySet().iterator().next();
126+
} else {
127+
return null;
128+
}
120129
}
121130

122131
public void setDefaultEncryptionKeyId(String defaultEncryptionKeyId) {
123132
this.defaultEncryptionKeyId = defaultEncryptionKeyId;
124133
}
125134

126135
public String getDefaultDecryptionKeyId() {
127-
return defaultDecryptionKeyId;
136+
if (defaultDecryptionKeyId != null) {
137+
return defaultDecryptionKeyId;
138+
} else if (keys.size() == 1) {
139+
// if there's only one key in the map, it's the default
140+
return keys.keySet().iterator().next();
141+
} else {
142+
return null;
143+
}
128144
}
129145

130146
public void setDefaultDecryptionKeyId(String defaultDecryptionKeyId) {

openid-connect-common/src/main/java/org/mitre/jwt/signer/service/impl/DefaultJwtSigningAndValidationService.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,14 @@ public DefaultJwtSigningAndValidationService(JWKSetKeyStore keyStore) throws NoS
109109
* @return the defaultSignerKeyId
110110
*/
111111
public String getDefaultSignerKeyId() {
112-
return defaultSignerKeyId;
112+
if (defaultSignerKeyId != null) {
113+
return defaultSignerKeyId;
114+
} else if (keys.size() == 1) {
115+
// if there's only one key, it's the default
116+
return keys.keySet().iterator().next();
117+
} else {
118+
return null;
119+
}
113120
}
114121

115122
/**

0 commit comments

Comments
 (0)