@@ -307,36 +307,43 @@ export class TokenValidationService {
307307
308308 let isValid = false ;
309309
310- if ( ! headerData . hasOwnProperty ( 'kid' ) ) {
311- // exactly 1 key in the jwtkeys and no kid in the Jose header
312- // kty "RSA" or EC use "sig"
313- let amountOfMatchingKeys = 0 ;
314- for ( const key of jwtkeys . keys ) {
315- if ( ( key . kty as string ) === jwtKtyToUse && ( key . use as string ) === 'sig' ) {
316- amountOfMatchingKeys = amountOfMatchingKeys + 1 ;
310+ // No kid in the Jose header
311+ if ( ! kid ) {
312+ let keyToValidate ;
313+
314+ // If only one key, use it
315+ if ( jwtkeys . keys . length === 1 && ( ( jwtkeys . keys [ 0 ] . kty as string ) === jwtKtyToUse ) ) {
316+ keyToValidate = jwtkeys . keys [ 0 ] ;
317+ } else {
318+ // More than one key
319+ // Make sure there's exactly 1 key candidate
320+ // kty "RSA" and "EC" uses "sig"
321+ let amountOfMatchingKeys = 0 ;
322+ for ( const key of jwtkeys . keys ) {
323+ if ( ( key . kty as string ) === jwtKtyToUse && ( key . use as string ) === 'sig' ) {
324+ amountOfMatchingKeys ++ ;
325+ keyToValidate = key ;
326+ }
327+ }
328+
329+ if ( amountOfMatchingKeys > 1 ) {
330+ this . loggerService . logWarning ( 'no ID Token kid claim in JOSE header and multiple supplied in jwks_uri' ) ;
331+ return false ;
317332 }
318333 }
319334
320- if ( amountOfMatchingKeys === 0 ) {
335+ if ( ! keyToValidate ) {
321336 this . loggerService . logWarning ( 'no keys found, incorrect Signature, validation failed for id_token' ) ;
322337 return false ;
323338 }
324339
325- if ( amountOfMatchingKeys > 1 ) {
326- this . loggerService . logWarning ( 'no ID Token kid claim in JOSE header and multiple supplied in jwks_uri' ) ;
327- return false ;
328- }
340+ isValid = KJUR . jws . JWS . verify ( idToken , KEYUTIL . getKey ( keyToValidate ) , [ alg ] ) ;
329341
330- for ( const key of jwtkeys . keys ) {
331- if ( ( key . kty as string ) === jwtKtyToUse && ( key . use as string ) === 'sig' ) {
332- const publickey = KEYUTIL . getKey ( key ) ;
333- isValid = KJUR . jws . JWS . verify ( idToken , publickey , [ alg ] ) ;
334- if ( ! isValid ) {
335- this . loggerService . logWarning ( 'incorrect Signature, validation failed for id_token' ) ;
336- }
337- return isValid ;
338- }
342+ if ( ! isValid ) {
343+ this . loggerService . logWarning ( 'incorrect Signature, validation failed for id_token' ) ;
339344 }
345+
346+ return isValid ;
340347 } else {
341348 // kid in the Jose header of id_token
342349 for ( const key of jwtkeys . keys ) {
0 commit comments