Skip to content

Commit f3c29bd

Browse files
committed
Use OAuth2AuthenticationException(String errorCode)
Closes gh-402
1 parent ea1f95b commit f3c29bd

12 files changed

+29
-38
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@
1919
import org.springframework.security.core.Authentication;
2020
import org.springframework.security.oauth2.core.AbstractOAuth2Token;
2121
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
22-
import org.springframework.security.oauth2.core.OAuth2Error;
22+
import org.springframework.security.oauth2.core.OAuth2AuthorizationCode;
2323
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
2424
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
2525
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
26-
import org.springframework.security.oauth2.core.OAuth2AuthorizationCode;
2726

2827
/**
2928
* Utility methods for the OAuth 2.0 {@link AuthenticationProvider}'s.
@@ -44,7 +43,7 @@ static OAuth2ClientAuthenticationToken getAuthenticatedClientElseThrowInvalidCli
4443
if (clientPrincipal != null && clientPrincipal.isAuthenticated()) {
4544
return clientPrincipal;
4645
}
47-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_CLIENT));
46+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_CLIENT);
4847
}
4948

5049
static <T extends AbstractOAuth2Token> OAuth2Authorization invalidate(

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.springframework.security.oauth2.core.OAuth2AccessToken;
3838
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
3939
import org.springframework.security.oauth2.core.OAuth2AuthorizationCode;
40-
import org.springframework.security.oauth2.core.OAuth2Error;
4140
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
4241
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
4342
import org.springframework.security.oauth2.core.OAuth2TokenType;
@@ -142,7 +141,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
142141
OAuth2Authorization authorization = this.authorizationService.findByToken(
143142
authorizationCodeAuthentication.getCode(), AUTHORIZATION_CODE_TOKEN_TYPE);
144143
if (authorization == null) {
145-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_GRANT));
144+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_GRANT);
146145
}
147146
OAuth2Authorization.Token<OAuth2AuthorizationCode> authorizationCode =
148147
authorization.getToken(OAuth2AuthorizationCode.class);
@@ -156,16 +155,16 @@ public Authentication authenticate(Authentication authentication) throws Authent
156155
authorization = OAuth2AuthenticationProviderUtils.invalidate(authorization, authorizationCode.getToken());
157156
this.authorizationService.save(authorization);
158157
}
159-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_GRANT));
158+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_GRANT);
160159
}
161160

162161
if (StringUtils.hasText(authorizationRequest.getRedirectUri()) &&
163162
!authorizationRequest.getRedirectUri().equals(authorizationCodeAuthentication.getRedirectUri())) {
164-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_GRANT));
163+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_GRANT);
165164
}
166165

167166
if (!authorizationCode.isActive()) {
168-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_GRANT));
167+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_GRANT);
169168
}
170169

171170
String issuer = this.providerSettings != null ? this.providerSettings.getIssuer() : null;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.springframework.security.crypto.password.PasswordEncoder;
2929
import org.springframework.security.oauth2.core.AuthorizationGrantType;
3030
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
31-
import org.springframework.security.oauth2.core.OAuth2Error;
3231
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
3332
import org.springframework.security.oauth2.core.OAuth2TokenType;
3433
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
@@ -187,10 +186,10 @@ private static boolean codeVerifierValid(String codeVerifier, String codeChallen
187186
// there will likely be bigger issues as well. We default to SERVER_ERROR.
188187
}
189188
}
190-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR));
189+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.SERVER_ERROR);
191190
}
192191

193192
private static void throwInvalidClient() {
194-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_CLIENT));
193+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_CLIENT);
195194
}
196195
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.springframework.security.oauth2.core.AuthorizationGrantType;
2727
import org.springframework.security.oauth2.core.OAuth2AccessToken;
2828
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
29-
import org.springframework.security.oauth2.core.OAuth2Error;
3029
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
3130
import org.springframework.security.oauth2.core.OAuth2TokenType;
3231
import org.springframework.security.oauth2.jwt.JoseHeader;
@@ -106,14 +105,14 @@ public Authentication authenticate(Authentication authentication) throws Authent
106105
RegisteredClient registeredClient = clientPrincipal.getRegisteredClient();
107106

108107
if (!registeredClient.getAuthorizationGrantTypes().contains(AuthorizationGrantType.CLIENT_CREDENTIALS)) {
109-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.UNAUTHORIZED_CLIENT));
108+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.UNAUTHORIZED_CLIENT);
110109
}
111110

112111
Set<String> authorizedScopes = registeredClient.getScopes(); // Default to configured scopes
113112
if (!CollectionUtils.isEmpty(clientCredentialsAuthentication.getScopes())) {
114113
for (String requestedScope : clientCredentialsAuthentication.getScopes()) {
115114
if (!registeredClient.getScopes().contains(requestedScope)) {
116-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_SCOPE));
115+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_SCOPE);
117116
}
118117
}
119118
authorizedScopes = new LinkedHashSet<>(clientCredentialsAuthentication.getScopes());

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.springframework.security.oauth2.core.AuthorizationGrantType;
3636
import org.springframework.security.oauth2.core.OAuth2AccessToken;
3737
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
38-
import org.springframework.security.oauth2.core.OAuth2Error;
3938
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
4039
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
4140
import org.springframework.security.oauth2.core.OAuth2TokenType;
@@ -136,23 +135,23 @@ public Authentication authenticate(Authentication authentication) throws Authent
136135
OAuth2Authorization authorization = this.authorizationService.findByToken(
137136
refreshTokenAuthentication.getRefreshToken(), OAuth2TokenType.REFRESH_TOKEN);
138137
if (authorization == null) {
139-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_GRANT));
138+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_GRANT);
140139
}
141140

142141
if (!registeredClient.getId().equals(authorization.getRegisteredClientId())) {
143-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_CLIENT));
142+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_CLIENT);
144143
}
145144

146145
if (!registeredClient.getAuthorizationGrantTypes().contains(AuthorizationGrantType.REFRESH_TOKEN)) {
147-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.UNAUTHORIZED_CLIENT));
146+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.UNAUTHORIZED_CLIENT);
148147
}
149148

150149
OAuth2Authorization.Token<OAuth2RefreshToken> refreshToken = authorization.getRefreshToken();
151150
if (!refreshToken.isActive()) {
152151
// As per https://tools.ietf.org/html/rfc6749#section-5.2
153152
// invalid_grant: The provided authorization grant (e.g., authorization code,
154153
// resource owner credentials) or refresh token is invalid, expired, revoked [...].
155-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_GRANT));
154+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_GRANT);
156155
}
157156

158157
// As per https://tools.ietf.org/html/rfc6749#section-6
@@ -161,7 +160,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
161160
Set<String> scopes = refreshTokenAuthentication.getScopes();
162161
Set<String> authorizedScopes = authorization.getAttribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME);
163162
if (!authorizedScopes.containsAll(scopes)) {
164-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_SCOPE));
163+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_SCOPE);
165164
}
166165
if (scopes.isEmpty()) {
167166
scopes = authorizedScopes;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.springframework.security.core.AuthenticationException;
2121
import org.springframework.security.oauth2.core.AbstractOAuth2Token;
2222
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
23-
import org.springframework.security.oauth2.core.OAuth2Error;
2423
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
2524
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
2625
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
@@ -69,7 +68,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
6968
}
7069

7170
if (!registeredClient.getId().equals(authorization.getRegisteredClientId())) {
72-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_CLIENT));
71+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_CLIENT);
7372
}
7473

7574
OAuth2Authorization.Token<AbstractOAuth2Token> token = authorization.getToken(tokenRevocationAuthentication.getToken());

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
3333
import org.springframework.security.oauth2.core.OAuth2AccessToken;
3434
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
35-
import org.springframework.security.oauth2.core.OAuth2Error;
3635
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
3736
import org.springframework.security.oauth2.core.OAuth2TokenType;
3837
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponseType;
@@ -93,24 +92,24 @@ public Authentication authenticate(Authentication authentication) throws Authent
9392
accessTokenAuthentication = (AbstractOAuth2TokenAuthenticationToken<?>) clientRegistrationAuthentication.getPrincipal();
9493
}
9594
if (accessTokenAuthentication == null || !accessTokenAuthentication.isAuthenticated()) {
96-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_TOKEN));
95+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_TOKEN);
9796
}
9897

9998
String accessTokenValue = accessTokenAuthentication.getToken().getTokenValue();
10099

101100
OAuth2Authorization authorization = this.authorizationService.findByToken(
102101
accessTokenValue, OAuth2TokenType.ACCESS_TOKEN);
103102
if (authorization == null) {
104-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_TOKEN));
103+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_TOKEN);
105104
}
106105

107106
OAuth2Authorization.Token<OAuth2AccessToken> authorizedAccessToken = authorization.getAccessToken();
108107
if (!authorizedAccessToken.isActive()) {
109-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_TOKEN));
108+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_TOKEN);
110109
}
111110

112111
if (!isAuthorized(authorizedAccessToken)) {
113-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INSUFFICIENT_SCOPE));
112+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INSUFFICIENT_SCOPE);
114113
}
115114

116115
if (!isValidRedirectUris(clientRegistrationAuthentication.getClientRegistration().getRedirectUris())) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public Authentication convert(HttpServletRequest request) {
6363
}
6464

6565
if (parts.length != 2) {
66-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST));
66+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_REQUEST);
6767
}
6868

6969
byte[] decodedCredentials;
@@ -79,7 +79,7 @@ public Authentication convert(HttpServletRequest request) {
7979
if (credentials.length != 2 ||
8080
!StringUtils.hasText(credentials[0]) ||
8181
!StringUtils.hasText(credentials[1])) {
82-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST));
82+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_REQUEST);
8383
}
8484

8585
String clientID;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.springframework.security.core.Authentication;
2626
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
2727
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
28-
import org.springframework.security.oauth2.core.OAuth2Error;
2928
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
3029
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
3130
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken;
@@ -59,7 +58,7 @@ public Authentication convert(HttpServletRequest request) {
5958
}
6059

6160
if (parameters.get(OAuth2ParameterNames.CLIENT_ID).size() != 1) {
62-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST));
61+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_REQUEST);
6362
}
6463

6564
// client_secret (REQUIRED)
@@ -69,7 +68,7 @@ public Authentication convert(HttpServletRequest request) {
6968
}
7069

7170
if (parameters.get(OAuth2ParameterNames.CLIENT_SECRET).size() != 1) {
72-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST));
71+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_REQUEST);
7372
}
7473

7574
return new OAuth2ClientAuthenticationToken(clientId, ClientAuthenticationMethod.CLIENT_SECRET_POST, clientSecret,

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.springframework.security.core.Authentication;
2424
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
2525
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
26-
import org.springframework.security.oauth2.core.OAuth2Error;
2726
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
2827
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
2928
import org.springframework.security.oauth2.core.endpoint.PkceParameterNames;
@@ -59,12 +58,12 @@ public Authentication convert(HttpServletRequest request) {
5958
String clientId = parameters.getFirst(OAuth2ParameterNames.CLIENT_ID);
6059
if (!StringUtils.hasText(clientId) ||
6160
parameters.get(OAuth2ParameterNames.CLIENT_ID).size() != 1) {
62-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST));
61+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_REQUEST);
6362
}
6463

6564
// code_verifier (REQUIRED)
6665
if (parameters.get(PkceParameterNames.CODE_VERIFIER).size() != 1) {
67-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST));
66+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_REQUEST);
6867
}
6968

7069
parameters.remove(OAuth2ParameterNames.CLIENT_ID);

0 commit comments

Comments
 (0)