Skip to content

Commit 8d54f16

Browse files
author
Steve Riesenberg
committed
Polish additional logging
Issue gh-1245, gh-1246, gh-1247, gh-1248
1 parent 6b6b211 commit 8d54f16

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/ClientSecretAuthenticationProvider.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.commons.logging.Log;
2121
import org.apache.commons.logging.LogFactory;
2222

23+
import org.springframework.core.log.LogMessage;
2324
import org.springframework.security.authentication.AuthenticationProvider;
2425
import org.springframework.security.core.Authentication;
2526
import org.springframework.security.core.AuthenticationException;
@@ -114,8 +115,9 @@ public Authentication authenticate(Authentication authentication) throws Authent
114115

115116
String clientSecret = clientAuthentication.getCredentials().toString();
116117
if (!this.passwordEncoder.matches(clientSecret, registeredClient.getClientSecret())) {
117-
if(this.logger.isDebugEnabled()){
118-
this.logger.debug("Invalid client_secret");
118+
if (this.logger.isDebugEnabled()) {
119+
this.logger.debug(LogMessage.format("Invalid request: client_secret does not match" +
120+
" for registered client '%s'", registeredClient.getId()));
119121
}
120122
throwInvalidClient(OAuth2ParameterNames.CLIENT_SECRET);
121123
}

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/CodeVerifierAuthenticator.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
2626

27+
import org.springframework.core.log.LogMessage;
2728
import org.springframework.security.oauth2.core.AuthorizationGrantType;
2829
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
2930
import org.springframework.security.oauth2.core.OAuth2Error;
@@ -96,7 +97,10 @@ private boolean authenticate(OAuth2ClientAuthenticationToken clientAuthenticatio
9697
.get(PkceParameterNames.CODE_CHALLENGE);
9798
if (!StringUtils.hasText(codeChallenge)) {
9899
if (registeredClient.getClientSettings().isRequireProofKey()) {
99-
logDebugMessage("Missing code_challenge");
100+
if (this.logger.isDebugEnabled()) {
101+
this.logger.debug(LogMessage.format("Invalid request: code_challenge is required" +
102+
" for registered client '%s'", registeredClient.getId()));
103+
}
100104
throwInvalidGrant(PkceParameterNames.CODE_CHALLENGE);
101105
} else {
102106
if (this.logger.isTraceEnabled()) {
@@ -114,6 +118,10 @@ private boolean authenticate(OAuth2ClientAuthenticationToken clientAuthenticatio
114118
.get(PkceParameterNames.CODE_CHALLENGE_METHOD);
115119
String codeVerifier = (String) parameters.get(PkceParameterNames.CODE_VERIFIER);
116120
if (!codeVerifierValid(codeVerifier, codeChallenge, codeChallengeMethod)) {
121+
if (this.logger.isDebugEnabled()) {
122+
this.logger.debug(LogMessage.format("Invalid request: code_verifier is missing or invalid" +
123+
" for registered client '%s'", registeredClient.getId()));
124+
}
117125
throwInvalidGrant(PkceParameterNames.CODE_VERIFIER);
118126
}
119127

@@ -132,7 +140,6 @@ private static boolean authorizationCodeGrant(Map<String, Object> parameters) {
132140

133141
private boolean codeVerifierValid(String codeVerifier, String codeChallenge, String codeChallengeMethod) {
134142
if (!StringUtils.hasText(codeVerifier)) {
135-
logDebugMessage("Missing code_verifier");
136143
return false;
137144
} else if ("S256".equals(codeChallengeMethod)) {
138145
try {
@@ -158,9 +165,4 @@ private static void throwInvalidGrant(String parameterName) {
158165
throw new OAuth2AuthenticationException(error);
159166
}
160167

161-
private void logDebugMessage(String logMessage){
162-
if(this.logger.isDebugEnabled()){
163-
this.logger.debug(logMessage);
164-
}
165-
}
166168
}

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2AuthorizationCodeRequestAuthenticationValidator.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import org.apache.commons.logging.Log;
2222
import org.apache.commons.logging.LogFactory;
23+
24+
import org.springframework.core.log.LogMessage;
2325
import org.springframework.security.core.Authentication;
2426
import org.springframework.security.oauth2.core.OAuth2Error;
2527
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
@@ -49,19 +51,19 @@
4951
*/
5052
public 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

Comments
 (0)