25
25
import java .time .temporal .ChronoUnit ;
26
26
import java .util .concurrent .ExecutionException ;
27
27
import java .util .concurrent .TimeoutException ;
28
+ import java .util .logging .Level ;
28
29
29
30
public class OAuth2Authentication implements Capability {
31
+ private static final java .util .logging .Logger JAVA_LOGGER =
32
+ java .util .logging .Logger .getLogger (OAuth2Authentication .class .getName ());
33
+
30
34
protected ClientRegistration clientRegistration ;
31
35
protected OAuth2IDPClient idpClient ;
32
36
33
37
protected AsyncClient <Object > httpClient ;
34
38
protected Request .Options httpOptions ;
35
39
protected Decoder jsonDecoder ;
40
+ protected Logger logger ;
36
41
37
42
private OAuth2TokenResponse oAuth2TokenResponse = null ;
38
43
private Instant expiresAt = null ;
@@ -65,6 +70,12 @@ public Decoder enrich(final Decoder decoder) {
65
70
return decoder ;
66
71
}
67
72
73
+ @ Override
74
+ public Logger enrich (final Logger logger ) {
75
+ this .logger = new ConfidentialLogger (logger );
76
+ return this .logger ;
77
+ }
78
+
68
79
@ Override
69
80
public <B extends BaseBuilder <B , T >, T > B beforeBuild (final B baseBuilder ) {
70
81
if (httpClient == null ) {
@@ -84,12 +95,14 @@ public <B extends BaseBuilder<B, T>, T> B beforeBuild(final B baseBuilder) {
84
95
return baseBuilder
85
96
.requestInterceptor (new AuthenticationInterceptor ())
86
97
.retryer (new UnauthorizedRetryer ())
87
- .errorDecoder (UnauthorizedErrorDecoder .INSTANCE );
98
+ .errorDecoder (UnauthorizedErrorDecoder .INSTANCE )
99
+ .logger (this .logger );
88
100
}
89
101
90
102
private synchronized String getAccessToken () {
91
103
if (expiresAt != null && expiresAt .minus (10 , ChronoUnit .SECONDS ).isBefore (Instant .now ())) {
92
104
// Access token is expired or about to expire
105
+ JAVA_LOGGER .log (Level .INFO , "Access token is about to be expired. Refreshing token." );
93
106
expiresAt = null ;
94
107
oAuth2TokenResponse = null ;
95
108
}
@@ -102,6 +115,8 @@ private synchronized String getAccessToken() {
102
115
}
103
116
104
117
private synchronized String forceAuthentication () {
118
+ JAVA_LOGGER .log (Level .INFO , "Perform authentication against IDP." );
119
+
105
120
try {
106
121
oAuth2TokenResponse =
107
122
idpClient
@@ -136,9 +151,14 @@ public void continueOrPropagate(final RetryableException unauthorizedException)
136
151
}
137
152
138
153
if (reauthenticated ) {
154
+ JAVA_LOGGER .log (
155
+ Level .WARNING ,
156
+ "Client still unauthorized event after access token was updated. Fail request." );
139
157
throw unauthorizedException ;
140
158
}
141
159
160
+ JAVA_LOGGER .log (
161
+ Level .INFO , "Request was unauthorized by Resource Server. Refresh access token." );
142
162
final String accessToken = forceAuthentication ();
143
163
144
164
final RequestTemplate requestTemplate = unauthorizedException .request ().requestTemplate ();
0 commit comments