2020
2121import org .apache .commons .logging .Log ;
2222import org .apache .commons .logging .LogFactory ;
23+
24+ import org .springframework .core .log .LogMessage ;
2325import org .springframework .security .core .Authentication ;
2426import org .springframework .security .oauth2 .core .OAuth2Error ;
2527import org .springframework .security .oauth2 .core .OAuth2ErrorCodes ;
4951 */
5052public final class OAuth2AuthorizationCodeRequestAuthenticationValidator implements Consumer <OAuth2AuthorizationCodeRequestAuthenticationContext > {
5153 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 );
5255
53- private final Log logger = LogFactory .getLog (getClass ());
5456 /**
5557 * The default validator for {@link OAuth2AuthorizationCodeRequestAuthenticationToken#getScopes()}.
5658 */
57- public final Consumer <OAuth2AuthorizationCodeRequestAuthenticationContext > DEFAULT_SCOPE_VALIDATOR =
58- this ::validateScope ;
59+ public static final Consumer <OAuth2AuthorizationCodeRequestAuthenticationContext > DEFAULT_SCOPE_VALIDATOR =
60+ OAuth2AuthorizationCodeRequestAuthenticationValidator ::validateScope ;
5961
6062 /**
6163 * The default validator for {@link OAuth2AuthorizationCodeRequestAuthenticationToken#getRedirectUri()}.
6264 */
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 ;
6567
6668 private final Consumer <OAuth2AuthorizationCodeRequestAuthenticationContext > authenticationValidator =
6769 DEFAULT_REDIRECT_URI_VALIDATOR .andThen (DEFAULT_SCOPE_VALIDATOR );
@@ -71,21 +73,24 @@ public void accept(OAuth2AuthorizationCodeRequestAuthenticationContext authentic
7173 this .authenticationValidator .accept (authenticationContext );
7274 }
7375
74- private void validateScope (OAuth2AuthorizationCodeRequestAuthenticationContext authenticationContext ) {
76+ private static void validateScope (OAuth2AuthorizationCodeRequestAuthenticationContext authenticationContext ) {
7577 OAuth2AuthorizationCodeRequestAuthenticationToken authorizationCodeRequestAuthentication =
7678 authenticationContext .getAuthentication ();
7779 RegisteredClient registeredClient = authenticationContext .getRegisteredClient ();
7880
7981 Set <String > requestedScopes = authorizationCodeRequestAuthentication .getScopes ();
8082 Set <String > allowedScopes = registeredClient .getScopes ();
8183 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+ }
8388 throwError (OAuth2ErrorCodes .INVALID_SCOPE , OAuth2ParameterNames .SCOPE ,
8489 authorizationCodeRequestAuthentication , registeredClient );
8590 }
8691 }
8792
88- private void validateRedirectUri (OAuth2AuthorizationCodeRequestAuthenticationContext authenticationContext ) {
93+ private static void validateRedirectUri (OAuth2AuthorizationCodeRequestAuthenticationContext authenticationContext ) {
8994 OAuth2AuthorizationCodeRequestAuthenticationToken authorizationCodeRequestAuthentication =
9095 authenticationContext .getAuthentication ();
9196 RegisteredClient registeredClient = authenticationContext .getRegisteredClient ();
@@ -100,6 +105,10 @@ private void validateRedirectUri(OAuth2AuthorizationCodeRequestAuthenticationCon
100105 requestedRedirect = UriComponentsBuilder .fromUriString (requestedRedirectUri ).build ();
101106 } catch (Exception ex ) { }
102107 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+ }
103112 throwError (OAuth2ErrorCodes .INVALID_REQUEST , OAuth2ParameterNames .REDIRECT_URI ,
104113 authorizationCodeRequestAuthentication , registeredClient );
105114 }
@@ -128,7 +137,10 @@ private void validateRedirectUri(OAuth2AuthorizationCodeRequestAuthenticationCon
128137 }
129138 }
130139 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+ }
132144 throwError (OAuth2ErrorCodes .INVALID_REQUEST , OAuth2ParameterNames .REDIRECT_URI ,
133145 authorizationCodeRequestAuthentication , registeredClient );
134146 }
@@ -201,10 +213,4 @@ private static void throwError(OAuth2Error error, String parameterName,
201213 throw new OAuth2AuthorizationCodeRequestAuthenticationException (error , authorizationCodeRequestAuthenticationResult );
202214 }
203215
204- private void logDebugMessage (String logMessage ){
205- if (this .logger .isDebugEnabled ()){
206- this .logger .debug (logMessage );
207- }
208- }
209-
210216}
0 commit comments