20
20
21
21
import org .apache .commons .logging .Log ;
22
22
import org .apache .commons .logging .LogFactory ;
23
+
24
+ import org .springframework .core .log .LogMessage ;
23
25
import org .springframework .security .core .Authentication ;
24
26
import org .springframework .security .oauth2 .core .OAuth2Error ;
25
27
import org .springframework .security .oauth2 .core .OAuth2ErrorCodes ;
49
51
*/
50
52
public final class OAuth2AuthorizationCodeRequestAuthenticationValidator implements Consumer <OAuth2AuthorizationCodeRequestAuthenticationContext > {
51
53
private static final String ERROR_URI = "https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.2.1" ;
54
+ private static final Log LOGGER = LogFactory .getLog (OAuth2AuthorizationCodeRequestAuthenticationValidator .class );
52
55
53
- private final Log logger = LogFactory .getLog (getClass ());
54
56
/**
55
57
* The default validator for {@link OAuth2AuthorizationCodeRequestAuthenticationToken#getScopes()}.
56
58
*/
57
- public final Consumer <OAuth2AuthorizationCodeRequestAuthenticationContext > DEFAULT_SCOPE_VALIDATOR =
58
- this ::validateScope ;
59
+ public static final Consumer <OAuth2AuthorizationCodeRequestAuthenticationContext > DEFAULT_SCOPE_VALIDATOR =
60
+ OAuth2AuthorizationCodeRequestAuthenticationValidator ::validateScope ;
59
61
60
62
/**
61
63
* The default validator for {@link OAuth2AuthorizationCodeRequestAuthenticationToken#getRedirectUri()}.
62
64
*/
63
- public final Consumer <OAuth2AuthorizationCodeRequestAuthenticationContext > DEFAULT_REDIRECT_URI_VALIDATOR =
64
- this ::validateRedirectUri ;
65
+ public static final Consumer <OAuth2AuthorizationCodeRequestAuthenticationContext > DEFAULT_REDIRECT_URI_VALIDATOR =
66
+ OAuth2AuthorizationCodeRequestAuthenticationValidator ::validateRedirectUri ;
65
67
66
68
private final Consumer <OAuth2AuthorizationCodeRequestAuthenticationContext > authenticationValidator =
67
69
DEFAULT_REDIRECT_URI_VALIDATOR .andThen (DEFAULT_SCOPE_VALIDATOR );
@@ -71,21 +73,24 @@ public void accept(OAuth2AuthorizationCodeRequestAuthenticationContext authentic
71
73
this .authenticationValidator .accept (authenticationContext );
72
74
}
73
75
74
- private void validateScope (OAuth2AuthorizationCodeRequestAuthenticationContext authenticationContext ) {
76
+ private static void validateScope (OAuth2AuthorizationCodeRequestAuthenticationContext authenticationContext ) {
75
77
OAuth2AuthorizationCodeRequestAuthenticationToken authorizationCodeRequestAuthentication =
76
78
authenticationContext .getAuthentication ();
77
79
RegisteredClient registeredClient = authenticationContext .getRegisteredClient ();
78
80
79
81
Set <String > requestedScopes = authorizationCodeRequestAuthentication .getScopes ();
80
82
Set <String > allowedScopes = registeredClient .getScopes ();
81
83
if (!requestedScopes .isEmpty () && !allowedScopes .containsAll (requestedScopes )) {
82
- logDebugMessage ("Invalid scope" );
84
+ if (LOGGER .isDebugEnabled ()) {
85
+ LOGGER .debug (LogMessage .format ("Invalid request: requested scope is not allowed" +
86
+ " for registered client '%s'" , registeredClient .getId ()));
87
+ }
83
88
throwError (OAuth2ErrorCodes .INVALID_SCOPE , OAuth2ParameterNames .SCOPE ,
84
89
authorizationCodeRequestAuthentication , registeredClient );
85
90
}
86
91
}
87
92
88
- private void validateRedirectUri (OAuth2AuthorizationCodeRequestAuthenticationContext authenticationContext ) {
93
+ private static void validateRedirectUri (OAuth2AuthorizationCodeRequestAuthenticationContext authenticationContext ) {
89
94
OAuth2AuthorizationCodeRequestAuthenticationToken authorizationCodeRequestAuthentication =
90
95
authenticationContext .getAuthentication ();
91
96
RegisteredClient registeredClient = authenticationContext .getRegisteredClient ();
@@ -100,6 +105,10 @@ private void validateRedirectUri(OAuth2AuthorizationCodeRequestAuthenticationCon
100
105
requestedRedirect = UriComponentsBuilder .fromUriString (requestedRedirectUri ).build ();
101
106
} catch (Exception ex ) { }
102
107
if (requestedRedirect == null || requestedRedirect .getFragment () != null ) {
108
+ if (LOGGER .isDebugEnabled ()) {
109
+ LOGGER .debug (LogMessage .format ("Invalid request: redirect_uri is missing or contains a fragment" +
110
+ " for registered client '%s'" , registeredClient .getId ()));
111
+ }
103
112
throwError (OAuth2ErrorCodes .INVALID_REQUEST , OAuth2ParameterNames .REDIRECT_URI ,
104
113
authorizationCodeRequestAuthentication , registeredClient );
105
114
}
@@ -128,7 +137,10 @@ private void validateRedirectUri(OAuth2AuthorizationCodeRequestAuthenticationCon
128
137
}
129
138
}
130
139
if (!validRedirectUri ) {
131
- logDebugMessage ("Invalid redirect_uri" );
140
+ if (LOGGER .isDebugEnabled ()) {
141
+ LOGGER .debug (LogMessage .format ("Invalid request: redirect_uri does not match" +
142
+ " for registered client '%s'" , registeredClient .getId ()));
143
+ }
132
144
throwError (OAuth2ErrorCodes .INVALID_REQUEST , OAuth2ParameterNames .REDIRECT_URI ,
133
145
authorizationCodeRequestAuthentication , registeredClient );
134
146
}
@@ -201,10 +213,4 @@ private static void throwError(OAuth2Error error, String parameterName,
201
213
throw new OAuth2AuthorizationCodeRequestAuthenticationException (error , authorizationCodeRequestAuthenticationResult );
202
214
}
203
215
204
- private void logDebugMessage (String logMessage ){
205
- if (this .logger .isDebugEnabled ()){
206
- this .logger .debug (logMessage );
207
- }
208
- }
209
-
210
216
}
0 commit comments