20
20
import java .util .HashMap ;
21
21
import java .util .Map ;
22
22
23
+ import org .apache .commons .logging .Log ;
24
+ import org .apache .commons .logging .LogFactory ;
25
+
26
+ import org .springframework .core .log .LogMessage ;
23
27
import org .springframework .security .authentication .AuthenticationProvider ;
24
28
import org .springframework .security .core .Authentication ;
25
29
import org .springframework .security .core .AuthenticationException ;
@@ -72,6 +76,7 @@ public final class OAuth2AuthorizationCodeAuthenticationProvider implements Auth
72
76
new OAuth2TokenType (OAuth2ParameterNames .CODE );
73
77
private static final OAuth2TokenType ID_TOKEN_TOKEN_TYPE =
74
78
new OAuth2TokenType (OidcParameterNames .ID_TOKEN );
79
+ private final Log logger = LogFactory .getLog (getClass ());
75
80
private final OAuth2AuthorizationService authorizationService ;
76
81
private final OAuth2TokenGenerator <? extends OAuth2Token > tokenGenerator ;
77
82
@@ -99,11 +104,20 @@ public Authentication authenticate(Authentication authentication) throws Authent
99
104
getAuthenticatedClientElseThrowInvalidClient (authorizationCodeAuthentication );
100
105
RegisteredClient registeredClient = clientPrincipal .getRegisteredClient ();
101
106
107
+ if (this .logger .isTraceEnabled ()) {
108
+ this .logger .trace ("Retrieved registered client" );
109
+ }
110
+
102
111
OAuth2Authorization authorization = this .authorizationService .findByToken (
103
112
authorizationCodeAuthentication .getCode (), AUTHORIZATION_CODE_TOKEN_TYPE );
104
113
if (authorization == null ) {
105
114
throw new OAuth2AuthenticationException (OAuth2ErrorCodes .INVALID_GRANT );
106
115
}
116
+
117
+ if (this .logger .isTraceEnabled ()) {
118
+ this .logger .trace ("Retrieved authorization with authorization code" );
119
+ }
120
+
107
121
OAuth2Authorization .Token <OAuth2AuthorizationCode > authorizationCode =
108
122
authorization .getToken (OAuth2AuthorizationCode .class );
109
123
@@ -115,6 +129,9 @@ public Authentication authenticate(Authentication authentication) throws Authent
115
129
// Invalidate the authorization code given that a different client is attempting to use it
116
130
authorization = OAuth2AuthenticationProviderUtils .invalidate (authorization , authorizationCode .getToken ());
117
131
this .authorizationService .save (authorization );
132
+ if (this .logger .isWarnEnabled ()) {
133
+ this .logger .warn (LogMessage .format ("Invalidated authorization code used by registered client '%s'" , registeredClient .getId ()));
134
+ }
118
135
}
119
136
throw new OAuth2AuthenticationException (OAuth2ErrorCodes .INVALID_GRANT );
120
137
}
@@ -128,6 +145,10 @@ public Authentication authenticate(Authentication authentication) throws Authent
128
145
throw new OAuth2AuthenticationException (OAuth2ErrorCodes .INVALID_GRANT );
129
146
}
130
147
148
+ if (this .logger .isTraceEnabled ()) {
149
+ this .logger .trace ("Validated token request parameters" );
150
+ }
151
+
131
152
// @formatter:off
132
153
DefaultOAuth2TokenContext .Builder tokenContextBuilder = DefaultOAuth2TokenContext .builder ()
133
154
.registeredClient (registeredClient )
@@ -149,6 +170,11 @@ public Authentication authenticate(Authentication authentication) throws Authent
149
170
"The token generator failed to generate the access token." , ERROR_URI );
150
171
throw new OAuth2AuthenticationException (error );
151
172
}
173
+
174
+ if (this .logger .isTraceEnabled ()) {
175
+ this .logger .trace ("Generated access token" );
176
+ }
177
+
152
178
OAuth2AccessToken accessToken = new OAuth2AccessToken (OAuth2AccessToken .TokenType .BEARER ,
153
179
generatedAccessToken .getTokenValue (), generatedAccessToken .getIssuedAt (),
154
180
generatedAccessToken .getExpiresAt (), tokenContext .getAuthorizedScopes ());
@@ -172,6 +198,11 @@ public Authentication authenticate(Authentication authentication) throws Authent
172
198
"The token generator failed to generate the refresh token." , ERROR_URI );
173
199
throw new OAuth2AuthenticationException (error );
174
200
}
201
+
202
+ if (this .logger .isTraceEnabled ()) {
203
+ this .logger .trace ("Generated refresh token" );
204
+ }
205
+
175
206
refreshToken = (OAuth2RefreshToken ) generatedRefreshToken ;
176
207
authorizationBuilder .refreshToken (refreshToken );
177
208
}
@@ -191,6 +222,11 @@ public Authentication authenticate(Authentication authentication) throws Authent
191
222
"The token generator failed to generate the ID token." , ERROR_URI );
192
223
throw new OAuth2AuthenticationException (error );
193
224
}
225
+
226
+ if (this .logger .isTraceEnabled ()) {
227
+ this .logger .trace ("Generated id token" );
228
+ }
229
+
194
230
idToken = new OidcIdToken (generatedIdToken .getTokenValue (), generatedIdToken .getIssuedAt (),
195
231
generatedIdToken .getExpiresAt (), ((Jwt ) generatedIdToken ).getClaims ());
196
232
authorizationBuilder .token (idToken , (metadata ) ->
@@ -206,12 +242,20 @@ public Authentication authenticate(Authentication authentication) throws Authent
206
242
207
243
this .authorizationService .save (authorization );
208
244
245
+ if (this .logger .isTraceEnabled ()) {
246
+ this .logger .trace ("Saved authorization" );
247
+ }
248
+
209
249
Map <String , Object > additionalParameters = Collections .emptyMap ();
210
250
if (idToken != null ) {
211
251
additionalParameters = new HashMap <>();
212
252
additionalParameters .put (OidcParameterNames .ID_TOKEN , idToken .getTokenValue ());
213
253
}
214
254
255
+ if (this .logger .isTraceEnabled ()) {
256
+ this .logger .trace ("Authenticated token request" );
257
+ }
258
+
215
259
return new OAuth2AccessTokenAuthenticationToken (
216
260
registeredClient , clientPrincipal , accessToken , refreshToken , additionalParameters );
217
261
}
0 commit comments