Skip to content

Commit 6b6b211

Browse files
fndejanSteve Riesenberg
authored and
Steve Riesenberg
committed
Add debug log entries
Closes gh-1245 Closes gh-1246 Closes gh-1247 Closes gh-1248
1 parent 9c1ec34 commit 6b6b211

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ public Authentication authenticate(Authentication authentication) throws Authent
114114

115115
String clientSecret = clientAuthentication.getCredentials().toString();
116116
if (!this.passwordEncoder.matches(clientSecret, registeredClient.getClientSecret())) {
117+
if(this.logger.isDebugEnabled()){
118+
this.logger.debug("Invalid client_secret");
119+
}
117120
throwInvalidClient(OAuth2ParameterNames.CLIENT_SECRET);
118121
}
119122

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ private boolean authenticate(OAuth2ClientAuthenticationToken clientAuthenticatio
9696
.get(PkceParameterNames.CODE_CHALLENGE);
9797
if (!StringUtils.hasText(codeChallenge)) {
9898
if (registeredClient.getClientSettings().isRequireProofKey()) {
99+
logDebugMessage("Missing code_challenge");
99100
throwInvalidGrant(PkceParameterNames.CODE_CHALLENGE);
100101
} else {
101102
if (this.logger.isTraceEnabled()) {
@@ -129,8 +130,9 @@ private static boolean authorizationCodeGrant(Map<String, Object> parameters) {
129130
parameters.get(OAuth2ParameterNames.CODE) != null;
130131
}
131132

132-
private static boolean codeVerifierValid(String codeVerifier, String codeChallenge, String codeChallengeMethod) {
133+
private boolean codeVerifierValid(String codeVerifier, String codeChallenge, String codeChallengeMethod) {
133134
if (!StringUtils.hasText(codeVerifier)) {
135+
logDebugMessage("Missing code_verifier");
134136
return false;
135137
} else if ("S256".equals(codeChallengeMethod)) {
136138
try {
@@ -156,4 +158,9 @@ private static void throwInvalidGrant(String parameterName) {
156158
throw new OAuth2AuthenticationException(error);
157159
}
158160

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

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.Set;
1919
import java.util.function.Consumer;
2020

21+
import org.apache.commons.logging.Log;
22+
import org.apache.commons.logging.LogFactory;
2123
import org.springframework.security.core.Authentication;
2224
import org.springframework.security.oauth2.core.OAuth2Error;
2325
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
@@ -48,17 +50,18 @@
4850
public final class OAuth2AuthorizationCodeRequestAuthenticationValidator implements Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> {
4951
private static final String ERROR_URI = "https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.2.1";
5052

53+
private final Log logger = LogFactory.getLog(getClass());
5154
/**
5255
* The default validator for {@link OAuth2AuthorizationCodeRequestAuthenticationToken#getScopes()}.
5356
*/
54-
public static final Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> DEFAULT_SCOPE_VALIDATOR =
55-
OAuth2AuthorizationCodeRequestAuthenticationValidator::validateScope;
57+
public final Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> DEFAULT_SCOPE_VALIDATOR =
58+
this::validateScope;
5659

5760
/**
5861
* The default validator for {@link OAuth2AuthorizationCodeRequestAuthenticationToken#getRedirectUri()}.
5962
*/
60-
public static final Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> DEFAULT_REDIRECT_URI_VALIDATOR =
61-
OAuth2AuthorizationCodeRequestAuthenticationValidator::validateRedirectUri;
63+
public final Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> DEFAULT_REDIRECT_URI_VALIDATOR =
64+
this::validateRedirectUri;
6265

6366
private final Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> authenticationValidator =
6467
DEFAULT_REDIRECT_URI_VALIDATOR.andThen(DEFAULT_SCOPE_VALIDATOR);
@@ -68,20 +71,21 @@ public void accept(OAuth2AuthorizationCodeRequestAuthenticationContext authentic
6871
this.authenticationValidator.accept(authenticationContext);
6972
}
7073

71-
private static void validateScope(OAuth2AuthorizationCodeRequestAuthenticationContext authenticationContext) {
74+
private void validateScope(OAuth2AuthorizationCodeRequestAuthenticationContext authenticationContext) {
7275
OAuth2AuthorizationCodeRequestAuthenticationToken authorizationCodeRequestAuthentication =
7376
authenticationContext.getAuthentication();
7477
RegisteredClient registeredClient = authenticationContext.getRegisteredClient();
7578

7679
Set<String> requestedScopes = authorizationCodeRequestAuthentication.getScopes();
7780
Set<String> allowedScopes = registeredClient.getScopes();
7881
if (!requestedScopes.isEmpty() && !allowedScopes.containsAll(requestedScopes)) {
82+
logDebugMessage("Invalid scope");
7983
throwError(OAuth2ErrorCodes.INVALID_SCOPE, OAuth2ParameterNames.SCOPE,
8084
authorizationCodeRequestAuthentication, registeredClient);
8185
}
8286
}
8387

84-
private static void validateRedirectUri(OAuth2AuthorizationCodeRequestAuthenticationContext authenticationContext) {
88+
private void validateRedirectUri(OAuth2AuthorizationCodeRequestAuthenticationContext authenticationContext) {
8589
OAuth2AuthorizationCodeRequestAuthenticationToken authorizationCodeRequestAuthentication =
8690
authenticationContext.getAuthentication();
8791
RegisteredClient registeredClient = authenticationContext.getRegisteredClient();
@@ -124,6 +128,7 @@ private static void validateRedirectUri(OAuth2AuthorizationCodeRequestAuthentica
124128
}
125129
}
126130
if (!validRedirectUri) {
131+
logDebugMessage("Invalid redirect_uri");
127132
throwError(OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.REDIRECT_URI,
128133
authorizationCodeRequestAuthentication, registeredClient);
129134
}
@@ -196,4 +201,10 @@ private static void throwError(OAuth2Error error, String parameterName,
196201
throw new OAuth2AuthorizationCodeRequestAuthenticationException(error, authorizationCodeRequestAuthenticationResult);
197202
}
198203

204+
private void logDebugMessage(String logMessage){
205+
if(this.logger.isDebugEnabled()){
206+
this.logger.debug(logMessage);
207+
}
208+
}
209+
199210
}

0 commit comments

Comments
 (0)