2525import java .time .temporal .ChronoUnit ;
2626import java .util .concurrent .ExecutionException ;
2727import java .util .concurrent .TimeoutException ;
28+ import java .util .logging .Level ;
2829
2930public class OAuth2Authentication implements Capability {
31+ private static final java .util .logging .Logger JAVA_LOGGER =
32+ java .util .logging .Logger .getLogger (OAuth2Authentication .class .getName ());
33+
3034 protected ClientRegistration clientRegistration ;
3135 protected OAuth2IDPClient idpClient ;
3236
3337 protected AsyncClient <Object > httpClient ;
3438 protected Request .Options httpOptions ;
3539 protected Decoder jsonDecoder ;
40+ protected Logger logger ;
3641
3742 private OAuth2TokenResponse oAuth2TokenResponse = null ;
3843 private Instant expiresAt = null ;
@@ -65,6 +70,12 @@ public Decoder enrich(final Decoder decoder) {
6570 return decoder ;
6671 }
6772
73+ @ Override
74+ public Logger enrich (final Logger logger ) {
75+ this .logger = new ConfidentialLogger (logger );
76+ return this .logger ;
77+ }
78+
6879 @ Override
6980 public <B extends BaseBuilder <B , T >, T > B beforeBuild (final B baseBuilder ) {
7081 if (httpClient == null ) {
@@ -84,12 +95,14 @@ public <B extends BaseBuilder<B, T>, T> B beforeBuild(final B baseBuilder) {
8495 return baseBuilder
8596 .requestInterceptor (new AuthenticationInterceptor ())
8697 .retryer (new UnauthorizedRetryer ())
87- .errorDecoder (UnauthorizedErrorDecoder .INSTANCE );
98+ .errorDecoder (UnauthorizedErrorDecoder .INSTANCE )
99+ .logger (this .logger );
88100 }
89101
90102 private synchronized String getAccessToken () {
91103 if (expiresAt != null && expiresAt .minus (10 , ChronoUnit .SECONDS ).isBefore (Instant .now ())) {
92104 // Access token is expired or about to expire
105+ JAVA_LOGGER .log (Level .INFO , "Access token is about to be expired. Refreshing token." );
93106 expiresAt = null ;
94107 oAuth2TokenResponse = null ;
95108 }
@@ -102,6 +115,8 @@ private synchronized String getAccessToken() {
102115 }
103116
104117 private synchronized String forceAuthentication () {
118+ JAVA_LOGGER .log (Level .INFO , "Perform authentication against IDP." );
119+
105120 try {
106121 oAuth2TokenResponse =
107122 idpClient
@@ -136,9 +151,14 @@ public void continueOrPropagate(final RetryableException unauthorizedException)
136151 }
137152
138153 if (reauthenticated ) {
154+ JAVA_LOGGER .log (
155+ Level .WARNING ,
156+ "Client still unauthorized event after access token was updated. Fail request." );
139157 throw unauthorizedException ;
140158 }
141159
160+ JAVA_LOGGER .log (
161+ Level .INFO , "Request was unauthorized by Resource Server. Refresh access token." );
142162 final String accessToken = forceAuthentication ();
143163
144164 final RequestTemplate requestTemplate = unauthorizedException .request ().requestTemplate ();
0 commit comments