23
23
import java .util .HashSet ;
24
24
import java .util .Map ;
25
25
import java .util .Set ;
26
+ import java .util .UUID ;
26
27
27
28
import org .mitre .jose .keystore .JWKSetKeyStore ;
28
29
import org .mitre .jwt .signer .service .JwtSigningAndValidationService ;
@@ -95,9 +96,13 @@ public DefaultJwtSigningAndValidationService(JWKSetKeyStore keyStore) throws NoS
95
96
if (keyStore != null && keyStore .getJwkSet () != null ) {
96
97
for (JWK key : keyStore .getKeys ()) {
97
98
if (!Strings .isNullOrEmpty (key .getKeyID ())) {
99
+ // use the key ID that's built into the key itself
100
+ // TODO (#641): deal with JWK thumbprints
98
101
this .keys .put (key .getKeyID (), key );
99
102
} else {
100
- throw new IllegalArgumentException ("Tried to load a key from a keystore without a 'kid' field: " + key );
103
+ // create a random key id
104
+ String fakeKid = UUID .randomUUID ().toString ();
105
+ this .keys .put (fakeKid , key );
101
106
}
102
107
}
103
108
}
@@ -109,14 +114,7 @@ public DefaultJwtSigningAndValidationService(JWKSetKeyStore keyStore) throws NoS
109
114
* @return the defaultSignerKeyId
110
115
*/
111
116
public String getDefaultSignerKeyId () {
112
- if (defaultSignerKeyId != null ) {
113
117
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
- }
120
118
}
121
119
122
120
/**
@@ -189,6 +187,11 @@ private void buildSignersAndVerifiers() throws NoSuchAlgorithmException, Invalid
189
187
logger .warn ("Unknown key type: " + jwk );
190
188
}
191
189
}
190
+
191
+ if (defaultSignerKeyId == null && keys .size () == 1 ) {
192
+ // if there's only one key, it's the default
193
+ setDefaultSignerKeyId (keys .keySet ().iterator ().next ());
194
+ }
192
195
}
193
196
194
197
/**
0 commit comments