Skip to content

Commit 52888d6

Browse files
msosaSteve Riesenberg
authored andcommitted
Warn when AuthorizationGrantType does not match
Log a warning when AuthorizationGrantType does not exactly match a pre-defined constant. Closes gh-11905
1 parent a61fffc commit 52888d6

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/registration/ClientRegistration.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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,9 +24,14 @@
2424
import java.util.HashSet;
2525
import java.util.LinkedHashMap;
2626
import java.util.LinkedHashSet;
27+
import java.util.List;
2728
import java.util.Map;
2829
import java.util.Set;
2930

31+
import org.apache.commons.logging.Log;
32+
import org.apache.commons.logging.LogFactory;
33+
34+
import org.springframework.core.log.LogMessage;
3035
import org.springframework.security.core.SpringSecurityCoreVersion;
3136
import org.springframework.security.oauth2.core.AuthenticationMethod;
3237
import org.springframework.security.oauth2.core.AuthorizationGrantType;
@@ -39,6 +44,7 @@
3944
* Provider.
4045
*
4146
* @author Joe Grandja
47+
* @author Michael Sosa
4248
* @since 5.0
4349
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-2">Section 2
4450
* Client Registration</a>
@@ -333,6 +339,12 @@ public static final class Builder implements Serializable {
333339

334340
private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
335341

342+
private static final Log logger = LogFactory.getLog(Builder.class);
343+
344+
private static final List<AuthorizationGrantType> AUTHORIZATION_GRANT_TYPES = Arrays.asList(
345+
AuthorizationGrantType.AUTHORIZATION_CODE, AuthorizationGrantType.CLIENT_CREDENTIALS,
346+
AuthorizationGrantType.REFRESH_TOKEN, AuthorizationGrantType.IMPLICIT, AuthorizationGrantType.PASSWORD);
347+
336348
private String registrationId;
337349

338350
private String clientId;
@@ -622,6 +634,7 @@ else if (AuthorizationGrantType.IMPLICIT.equals(this.authorizationGrantType)) {
622634
else if (AuthorizationGrantType.AUTHORIZATION_CODE.equals(this.authorizationGrantType)) {
623635
this.validateAuthorizationCodeGrantType();
624636
}
637+
this.validateAuthorizationGrantTypes();
625638
this.validateScopes();
626639
return this.create();
627640
}
@@ -698,6 +711,17 @@ private void validatePasswordGrantType() {
698711
Assert.hasText(this.tokenUri, "tokenUri cannot be empty");
699712
}
700713

714+
private void validateAuthorizationGrantTypes() {
715+
for (AuthorizationGrantType authorizationGrantType : AUTHORIZATION_GRANT_TYPES) {
716+
if (authorizationGrantType.getValue().equalsIgnoreCase(this.authorizationGrantType.getValue())
717+
&& !authorizationGrantType.equals(this.authorizationGrantType)) {
718+
logger.warn(LogMessage.format(
719+
"AuthorizationGrantType: %s does not match the pre-defined constant %s and won't match a valid OAuth2AuthorizedClientProvider",
720+
this.authorizationGrantType, authorizationGrantType));
721+
}
722+
}
723+
}
724+
701725
private void validateScopes() {
702726
if (this.scopes == null) {
703727
return;

0 commit comments

Comments
 (0)