79
79
import org .elasticsearch .common .collect .Tuple ;
80
80
import org .elasticsearch .common .util .concurrent .EsExecutors ;
81
81
import org .elasticsearch .common .util .concurrent .ListenableFuture ;
82
+ import org .elasticsearch .rest .RestStatus ;
82
83
import org .elasticsearch .watcher .FileChangesListener ;
83
84
import org .elasticsearch .watcher .FileWatcher ;
84
85
import org .elasticsearch .watcher .ResourceWatcherService ;
@@ -514,29 +515,31 @@ private void handleTokenResponse(HttpResponse httpResponse, ActionListener<Tuple
514
515
return ;
515
516
}
516
517
final Charset encoding = encodingHeader == null ? StandardCharsets .UTF_8 : Charsets .toCharset (encodingHeader .getValue ());
517
- final String json = EntityUtils .toString (entity , encoding );
518
- if (LOGGER .isTraceEnabled ()) {
519
- LOGGER .trace ("Received Token Response from OP with status [{}] and content [{}] " ,
520
- httpResponse .getStatusLine ().getStatusCode (), json );
521
- }
522
- final OIDCTokenResponse oidcTokenResponse = OIDCTokenResponse .parse (JSONObjectUtils .parse (json ));
523
- if (oidcTokenResponse .indicatesSuccess () == false ) {
524
- TokenErrorResponse errorResponse = oidcTokenResponse .toErrorResponse ();
525
- tokensListener .onFailure (
526
- new ElasticsearchSecurityException ("Failed to exchange code for Id Token. Code=[{}], Description=[{}]" ,
527
- errorResponse .getErrorObject ().getCode (), errorResponse .getErrorObject ().getDescription ()));
518
+ final RestStatus responseStatus = RestStatus .fromCode (httpResponse .getStatusLine ().getStatusCode ());
519
+ if (RestStatus .OK != responseStatus ) {
520
+ final String json = EntityUtils .toString (entity , encoding );
521
+ LOGGER .warn ("Received Token Response from OP with status [{}] and content [{}]" , responseStatus , json );
522
+ if (RestStatus .BAD_REQUEST == responseStatus ) {
523
+ final TokenErrorResponse tokenErrorResponse = TokenErrorResponse .parse (JSONObjectUtils .parse (json ));
524
+ tokensListener .onFailure (
525
+ new ElasticsearchSecurityException ("Failed to exchange code for Id Token. Code=[{}], Description=[{}]" ,
526
+ tokenErrorResponse .getErrorObject ().getCode (), tokenErrorResponse .getErrorObject ().getDescription ()));
527
+ } else {
528
+ tokensListener .onFailure (new ElasticsearchSecurityException ("Failed to exchange code for Id Token" ));
529
+ }
528
530
} else {
529
- OIDCTokenResponse successResponse = oidcTokenResponse .toSuccessResponse ();
530
- final OIDCTokens oidcTokens = successResponse .getOIDCTokens ();
531
+ final OIDCTokenResponse oidcTokenResponse = OIDCTokenResponse .parse (
532
+ JSONObjectUtils .parse (EntityUtils .toString (entity , encoding )));
533
+ final OIDCTokens oidcTokens = oidcTokenResponse .getOIDCTokens ();
531
534
final AccessToken accessToken = oidcTokens .getAccessToken ();
532
535
final JWT idToken = oidcTokens .getIDToken ();
533
536
if (LOGGER .isTraceEnabled ()) {
534
- LOGGER .trace ("Successfully exchanged code for ID Token: [{}] and Access Token [{}]" ,
535
- idToken , accessToken );
537
+ LOGGER .trace ("Successfully exchanged code for ID Token [{}] and Access Token [{}]" , idToken ,
538
+ truncateToken ( accessToken . toString ()) );
536
539
}
537
540
if (idToken == null ) {
538
- tokensListener .onFailure (new ElasticsearchSecurityException ( "Token Response did not contain an ID Token or parsing of" +
539
- " the JWT failed." ));
541
+ tokensListener .onFailure (
542
+ new ElasticsearchSecurityException ( "Token Response did not contain an ID Token or parsing of the JWT failed." ));
540
543
return ;
541
544
}
542
545
tokensListener .onResponse (new Tuple <>(accessToken , idToken ));
@@ -548,6 +551,13 @@ private void handleTokenResponse(HttpResponse httpResponse, ActionListener<Tuple
548
551
}
549
552
}
550
553
554
+ private static String truncateToken (String input ) {
555
+ if (Strings .hasText (input ) == false || input .length () <= 4 ) {
556
+ return input ;
557
+ }
558
+ return input .substring (0 , 2 ) + "***" + input .substring (input .length () - 2 );
559
+ }
560
+
551
561
/**
552
562
* Creates a {@link CloseableHttpAsyncClient} that uses a {@link PoolingNHttpClientConnectionManager}
553
563
*/
0 commit comments