@@ -477,7 +477,20 @@ static void handleUserinfoResponse(
477477 if (httpResponse .getStatusLine ().getStatusCode () == 200 ) {
478478 if (ContentType .parse (contentHeader .getValue ()).getMimeType ().equals ("application/json" )) {
479479 final JWTClaimsSet userInfoClaims = JWTClaimsSet .parse (contentAsString );
480- validateUserInfoResponse (userInfoClaims , verifiedIdTokenClaims .getSubject (), claimsListener );
480+ String expectedSub = verifiedIdTokenClaims .getSubject ();
481+ if (userInfoClaims .getSubject ().isEmpty ()) {
482+ claimsListener .onFailure (new ElasticsearchSecurityException ("Userinfo Response did not contain a sub Claim" ));
483+ return ;
484+ } else if (userInfoClaims .getSubject ().equals (expectedSub ) == false ) {
485+ claimsListener .onFailure (
486+ new ElasticsearchSecurityException (
487+ "Userinfo Response is not valid as it is for " + "subject [{}] while the ID Token was for subject [{}]" ,
488+ userInfoClaims .getSubject (),
489+ expectedSub
490+ )
491+ );
492+ return ;
493+ }
481494 if (LOGGER .isTraceEnabled ()) {
482495 LOGGER .trace ("Successfully retrieved user information: [{}]" , userInfoClaims );
483496 }
@@ -527,27 +540,6 @@ static void handleUserinfoResponse(
527540 }
528541 }
529542
530- /**
531- * Validates that the userinfo response contains a sub Claim and that this claim value is the same as the one returned in the ID Token
532- */
533- private static void validateUserInfoResponse (
534- JWTClaimsSet userInfoClaims ,
535- String expectedSub ,
536- ActionListener <JWTClaimsSet > claimsListener
537- ) {
538- if (userInfoClaims .getSubject ().isEmpty ()) {
539- claimsListener .onFailure (new ElasticsearchSecurityException ("Userinfo Response did not contain a sub Claim" ));
540- } else if (userInfoClaims .getSubject ().equals (expectedSub ) == false ) {
541- claimsListener .onFailure (
542- new ElasticsearchSecurityException (
543- "Userinfo Response is not valid as it is for " + "subject [{}] while the ID Token was for subject [{}]" ,
544- userInfoClaims .getSubject (),
545- expectedSub
546- )
547- );
548- }
549- }
550-
551543 /**
552544 * Attempts to make a request to the Token Endpoint of the OpenID Connect provider in order to exchange an
553545 * authorization code for an Id Token (and potentially an Access Token)
0 commit comments