Skip to content

Commit 08ba07d

Browse files
committed
Extract AuthenticationProvider from OAuth2AuthorizationEndpointFilter
Closes gh-340
1 parent 023e22c commit 08ba07d

File tree

10 files changed

+2342
-1680
lines changed

10 files changed

+2342
-1680
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/authorization/OAuth2AuthorizationServerConfigurer.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.security.crypto.password.PasswordEncoder;
3333
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsentService;
3434
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
35+
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeRequestAuthenticationProvider;
3536
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationProvider;
3637
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenIntrospectionAuthenticationProvider;
3738
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenRevocationAuthenticationProvider;
@@ -221,6 +222,13 @@ public void init(B builder) {
221222
}
222223
builder.authenticationProvider(postProcess(clientAuthenticationProvider));
223224

225+
OAuth2AuthorizationCodeRequestAuthenticationProvider authorizationCodeRequestAuthenticationProvider =
226+
new OAuth2AuthorizationCodeRequestAuthenticationProvider(
227+
OAuth2ConfigurerUtils.getRegisteredClientRepository(builder),
228+
OAuth2ConfigurerUtils.getAuthorizationService(builder),
229+
OAuth2ConfigurerUtils.getAuthorizationConsentService(builder));
230+
builder.authenticationProvider(postProcess(authorizationCodeRequestAuthenticationProvider));
231+
224232
OAuth2TokenIntrospectionAuthenticationProvider tokenIntrospectionAuthenticationProvider =
225233
new OAuth2TokenIntrospectionAuthenticationProvider(
226234
OAuth2ConfigurerUtils.getRegisteredClientRepository(builder),
@@ -285,9 +293,7 @@ public void configure(B builder) {
285293

286294
OAuth2AuthorizationEndpointFilter authorizationEndpointFilter =
287295
new OAuth2AuthorizationEndpointFilter(
288-
OAuth2ConfigurerUtils.getRegisteredClientRepository(builder),
289-
OAuth2ConfigurerUtils.getAuthorizationService(builder),
290-
OAuth2ConfigurerUtils.getAuthorizationConsentService(builder),
296+
authenticationManager,
291297
providerSettings.authorizationEndpoint());
292298
if (StringUtils.hasText(this.consentPage)) {
293299
authorizationEndpointFilter.setUserConsentUri(this.consentPage);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2020-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.oauth2.server.authorization;
17+
18+
import org.springframework.lang.Nullable;
19+
import org.springframework.security.core.Authentication;
20+
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
21+
import org.springframework.security.oauth2.core.OAuth2Error;
22+
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeRequestAuthenticationProvider;
23+
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeRequestAuthenticationToken;
24+
25+
/**
26+
* This exception is thrown by {@link OAuth2AuthorizationCodeRequestAuthenticationProvider}
27+
* when an attempt to authenticate the OAuth 2.0 Authorization Request (or Consent) fails.
28+
*
29+
* @author Joe Grandja
30+
* @since 0.1.2
31+
* @see OAuth2AuthorizationCodeRequestAuthenticationToken
32+
* @see OAuth2AuthorizationCodeRequestAuthenticationProvider
33+
*/
34+
public class OAuth2AuthorizationCodeRequestAuthenticationException extends OAuth2AuthenticationException {
35+
private final OAuth2AuthorizationCodeRequestAuthenticationToken authorizationCodeRequestAuthentication;
36+
37+
/**
38+
* Constructs an {@code OAuth2AuthorizationCodeRequestAuthenticationException} using the provided parameters.
39+
*
40+
* @param error the {@link OAuth2Error OAuth 2.0 Error}
41+
* @param authorizationCodeRequestAuthentication the {@link Authentication} instance of the OAuth 2.0 Authorization Request (or Consent)
42+
*/
43+
public OAuth2AuthorizationCodeRequestAuthenticationException(OAuth2Error error,
44+
@Nullable OAuth2AuthorizationCodeRequestAuthenticationToken authorizationCodeRequestAuthentication) {
45+
super(error);
46+
this.authorizationCodeRequestAuthentication = authorizationCodeRequestAuthentication;
47+
}
48+
49+
/**
50+
* Constructs an {@code OAuth2AuthorizationCodeRequestAuthenticationException} using the provided parameters.
51+
*
52+
* @param error the {@link OAuth2Error OAuth 2.0 Error}
53+
* @param cause the root cause
54+
* @param authorizationCodeRequestAuthentication the {@link Authentication} instance of the OAuth 2.0 Authorization Request (or Consent)
55+
*/
56+
public OAuth2AuthorizationCodeRequestAuthenticationException(OAuth2Error error, Throwable cause,
57+
@Nullable OAuth2AuthorizationCodeRequestAuthenticationToken authorizationCodeRequestAuthentication) {
58+
super(error, cause);
59+
this.authorizationCodeRequestAuthentication = authorizationCodeRequestAuthentication;
60+
}
61+
62+
/**
63+
* Returns the {@link Authentication} instance of the OAuth 2.0 Authorization Request (or Consent), or {@code null} if not available.
64+
*
65+
* @return the {@link OAuth2AuthorizationCodeRequestAuthenticationToken}
66+
*/
67+
@Nullable
68+
public OAuth2AuthorizationCodeRequestAuthenticationToken getAuthorizationCodeRequestAuthentication() {
69+
return this.authorizationCodeRequestAuthentication;
70+
}
71+
72+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
* @since 0.0.1
6262
* @see OAuth2AuthorizationCodeAuthenticationToken
6363
* @see OAuth2AccessTokenAuthenticationToken
64+
* @see OAuth2AuthorizationCodeRequestAuthenticationProvider
6465
* @see OAuth2AuthorizationService
6566
* @see JwtEncoder
6667
* @see OAuth2TokenCustomizer

0 commit comments

Comments
 (0)