Skip to content

Commit 53c4095

Browse files
aanganesDave Syer
authored andcommitted
Refactored request lifecycle
Refactored AuthorizationRequest into three distinct classes, AuthorizationRequest, StoredRequest, and TokenRequest. Removed AuthorizationRequestHolder, now storing OAuth2Authentication everywhere. (Squashed pull request spring-attic#79 by aanganes)
1 parent e901836 commit 53c4095

File tree

69 files changed

+1138
-795
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1138
-795
lines changed

samples/oauth2/sparklr/src/main/java/org/springframework/security/oauth/examples/sparklr/mvc/AccessConfirmationController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.security.oauth2.provider.ClientDetails;
77
import org.springframework.security.oauth2.provider.ClientDetailsService;
8-
import org.springframework.security.oauth2.provider.OAuth2Request;
8+
import org.springframework.security.oauth2.provider.AuthorizationRequest;
99
import org.springframework.stereotype.Controller;
1010
import org.springframework.web.bind.annotation.RequestMapping;
1111
import org.springframework.web.bind.annotation.SessionAttributes;
@@ -24,7 +24,7 @@ public class AccessConfirmationController {
2424

2525
@RequestMapping("/oauth/confirm_access")
2626
public ModelAndView getAccessConfirmation(Map<String, Object> model) throws Exception {
27-
OAuth2Request clientAuth = (OAuth2Request) model.remove("authorizationRequest");
27+
AuthorizationRequest clientAuth = (AuthorizationRequest) model.remove("authorizationRequest");
2828
ClientDetails client = clientDetailsService.loadClientByClientId(clientAuth.getClientId());
2929
model.put("auth_request", clientAuth);
3030
model.put("client", client);

samples/oauth2/sparklr/src/main/java/org/springframework/security/oauth/examples/sparklr/oauth/SparklrUserApprovalHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
import java.util.HashSet;
2121

2222
import org.springframework.security.core.Authentication;
23-
import org.springframework.security.oauth2.provider.OAuth2Request;
23+
import org.springframework.security.oauth2.common.util.OAuth2Utils;
24+
import org.springframework.security.oauth2.provider.AuthorizationRequest;
2425
import org.springframework.security.oauth2.provider.approval.TokenServicesUserApprovalHandler;
2526

2627
/**
@@ -56,7 +57,7 @@ public void setAutoApproveClients(Collection<String> autoApproveClients) {
5657
* @return Whether the specified request has been approved by the current user.
5758
*/
5859
@Override
59-
public boolean isApproved(OAuth2Request authorizationRequest, Authentication userAuthentication) {
60+
public boolean isApproved(AuthorizationRequest authorizationRequest, Authentication userAuthentication) {
6061

6162
// If we are allowed to check existing approvals this will short circuit the decision
6263
if (useTokenServices && super.isApproved(authorizationRequest, userAuthentication)) {
@@ -67,7 +68,7 @@ public boolean isApproved(OAuth2Request authorizationRequest, Authentication use
6768
return false;
6869
}
6970

70-
String flag = authorizationRequest.getApprovalParameters().get(OAuth2Request.USER_OAUTH_APPROVAL);
71+
String flag = authorizationRequest.getApprovalParameters().get(OAuth2Utils.USER_OAUTH_APPROVAL);
7172
boolean approved = flag != null && flag.toLowerCase().equals("true");
7273

7374
return approved

samples/oauth2/sparklr/src/main/webapp/WEB-INF/spring-servlet.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,18 @@
124124
<property name="clientDetailsService" ref="clientDetails" />
125125
</bean>
126126

127+
<bean id="requestFactory" class="org.springframework.security.oauth2.provider.DefaultOAuth2RequestFactory">
128+
<constructor-arg name="clientDetailsService" ref="clientDetails" />
129+
</bean>
130+
127131
<bean id="userApprovalHandler" class="org.springframework.security.oauth.examples.sparklr.oauth.SparklrUserApprovalHandler">
128132
<property name="autoApproveClients">
129133
<set>
130134
<value>my-less-trusted-autoapprove-client</value>
131135
</set>
132136
</property>
133137
<property name="tokenServices" ref="tokenServices" />
138+
<property name="requestFactory" ref="requestFactory" />
134139
</bean>
135140

136141
<oauth:authorization-server client-details-service-ref="clientDetails" token-services-ref="tokenServices"

samples/oauth2/sparklr/src/test/java/org/springframework/security/oauth2/provider/TestAuthorizationCodeProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails;
4040
import org.springframework.security.oauth2.common.OAuth2AccessToken;
4141
import org.springframework.security.oauth2.common.exceptions.RedirectMismatchException;
42+
import org.springframework.security.oauth2.common.util.OAuth2Utils;
4243
import org.springframework.security.oauth2.provider.ServerRunning.UriBuilder;
4344
import org.springframework.util.LinkedMultiValueMap;
4445
import org.springframework.util.MultiValueMap;
@@ -146,7 +147,7 @@ public void testUnauthenticatedAuthorizationRequestRedirectsToLogin() throws Exc
146147

147148
AccessTokenRequest request = context.getAccessTokenRequest();
148149
request.setCurrentUri("http://anywhere");
149-
request.add(OAuth2Request.USER_OAUTH_APPROVAL, "true");
150+
request.add(OAuth2Utils.USER_OAUTH_APPROVAL, "true");
150151

151152
String location = null;
152153

@@ -446,7 +447,7 @@ private void approveAccessTokenGrant(String currentUri, boolean approved) {
446447
assertNull(request.getAuthorizationCode());
447448

448449
// The approval (will be processed on the next attempt to obtain an access token)...
449-
request.set(OAuth2Request.USER_OAUTH_APPROVAL, "" + approved);
450+
request.set(OAuth2Utils.USER_OAUTH_APPROVAL, "" + approved);
450451

451452
}
452453

samples/oauth2/sparklr/src/test/java/org/springframework/security/oauth2/provider/TestImplicitProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.security.oauth2.client.test.OAuth2ContextSetup;
2424
import org.springframework.security.oauth2.client.token.grant.implicit.ImplicitAccessTokenProvider;
2525
import org.springframework.security.oauth2.client.token.grant.implicit.ImplicitResourceDetails;
26+
import org.springframework.security.oauth2.common.util.OAuth2Utils;
2627
import org.springframework.util.LinkedMultiValueMap;
2728
import org.springframework.util.MultiValueMap;
2829

@@ -98,7 +99,7 @@ public void testPostForNonAutomaticApprovalToken() throws Exception {
9899
// ignore
99100
}
100101
// add user approval parameter for the second request
101-
context.getAccessTokenRequest().add(OAuth2Request.USER_OAUTH_APPROVAL, "true");
102+
context.getAccessTokenRequest().add(OAuth2Utils.USER_OAUTH_APPROVAL, "true");
102103
assertNotNull(context.getAccessToken());
103104
}
104105

spring-security-oauth2/src/main/java/org/springframework/security/oauth2/client/token/grant/code/AuthorizationCodeAccessTokenProvider.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
5858
import org.springframework.security.oauth2.common.exceptions.InvalidRequestException;
5959
import org.springframework.security.oauth2.common.util.OAuth2Utils;
60-
import org.springframework.security.oauth2.provider.OAuth2Request;
6160
import org.springframework.util.LinkedMultiValueMap;
6261
import org.springframework.util.MultiValueMap;
6362
import org.springframework.web.client.ResponseExtractor;
@@ -96,9 +95,9 @@ public String obtainAuthorizationCode(OAuth2ProtectedResourceDetails details, Ac
9695

9796
HttpHeaders headers = getHeadersForTokenRequest(request);
9897
MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
99-
if (request.containsKey(OAuth2Request.USER_OAUTH_APPROVAL)) {
100-
form.set(OAuth2Request.USER_OAUTH_APPROVAL,
101-
request.getFirst(OAuth2Request.USER_OAUTH_APPROVAL));
98+
if (request.containsKey(OAuth2Utils.USER_OAUTH_APPROVAL)) {
99+
form.set(OAuth2Utils.USER_OAUTH_APPROVAL,
100+
request.getFirst(OAuth2Utils.USER_OAUTH_APPROVAL));
102101
}
103102
else {
104103
form.putAll(getParametersForAuthorizeRequest(resource, request));
@@ -320,7 +319,7 @@ protected UserApprovalRequiredException getUserApprovalSignal(AuthorizationCodeR
320319
String message = String.format("Do you approve the client '%s' to access your resources with scope=%s",
321320
resource.getClientId(), resource.getScope());
322321
return new UserApprovalRequiredException(resource.getUserAuthorizationUri(), Collections.singletonMap(
323-
OAuth2Request.USER_OAUTH_APPROVAL, message), resource.getClientId(), resource.getScope());
322+
OAuth2Utils.USER_OAUTH_APPROVAL, message), resource.getClientId(), resource.getScope());
324323
}
325324

326325
}

spring-security-oauth2/src/main/java/org/springframework/security/oauth2/common/util/OAuth2Utils.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@
3131
*/
3232
public abstract class OAuth2Utils {
3333

34+
/**
35+
* Constants to use while parsing parameter maps for OAuth2 requests
36+
*/
37+
public static final String CLIENT_ID = "client_id";
38+
39+
public static final String STATE = "state";
40+
41+
public static final String SCOPE = "scope";
42+
43+
public static final String REDIRECT_URI = "redirect_uri";
44+
45+
public static final String RESPONSE_TYPE = "response_type";
46+
47+
public static final String USER_OAUTH_APPROVAL = "user_oauth_approval";
48+
49+
public static final String GRANT_TYPE = "grant_type";
50+
3451
/**
3552
* Parses a string parameter value into a set of strings.
3653
*

spring-security-oauth2/src/main/java/org/springframework/security/oauth2/config/AuthorizationServerBeanDefinitionParser.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
import java.util.List;
1717

1818
import org.springframework.beans.BeanMetadataElement;
19+
import org.springframework.beans.factory.config.RuntimeBeanReference;
1920
import org.springframework.beans.factory.config.TypedStringValue;
2021
import org.springframework.beans.factory.support.AbstractBeanDefinition;
2122
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
2223
import org.springframework.beans.factory.support.ManagedList;
2324
import org.springframework.beans.factory.support.ManagedMap;
2425
import org.springframework.beans.factory.xml.ParserContext;
25-
import org.springframework.beans.factory.config.RuntimeBeanReference;
2626
import org.springframework.security.config.BeanIds;
2727
import org.springframework.security.oauth2.provider.CompositeTokenGranter;
2828
import org.springframework.security.oauth2.provider.DefaultOAuth2RequestFactory;
@@ -70,6 +70,15 @@ protected AbstractBeanDefinition parseEndpointAndReturnFilter(Element element, P
7070
BeanDefinitionBuilder authorizationEndpointBean = BeanDefinitionBuilder
7171
.rootBeanDefinition(AuthorizationEndpoint.class);
7272

73+
if (!StringUtils.hasText(oAuth2RequestFactoryRef)) {
74+
oAuth2RequestFactoryRef = "oAuth2AuthorizationRequestManager";
75+
BeanDefinitionBuilder oAuth2RequestManager = BeanDefinitionBuilder
76+
.rootBeanDefinition(DefaultOAuth2RequestFactory.class);
77+
oAuth2RequestManager.addConstructorArgReference(clientDetailsRef);
78+
parserContext.getRegistry().registerBeanDefinition(oAuth2RequestFactoryRef,
79+
oAuth2RequestManager.getBeanDefinition());
80+
}
81+
7382
ManagedList<BeanMetadataElement> tokenGranters = null;
7483
if (!StringUtils.hasText(tokenGranterRef)) {
7584
tokenGranterRef = "oauth2TokenGranter";
@@ -84,6 +93,7 @@ protected AbstractBeanDefinition parseEndpointAndReturnFilter(Element element, P
8493
boolean registerAuthorizationEndpoint = false;
8594

8695
Element authorizationCodeElement = DomUtils.getChildElementByTagName(element, "authorization-code");
96+
8797
if (authorizationCodeElement != null
8898
&& !"true".equalsIgnoreCase(authorizationCodeElement.getAttribute("disabled"))) {
8999
// authorization code grant configuration.
@@ -108,6 +118,7 @@ protected AbstractBeanDefinition parseEndpointAndReturnFilter(Element element, P
108118
authorizationEndpointBean.addPropertyReference("authorizationCodeServices", authorizationCodeServices);
109119
authorizationCodeTokenGranterBean.addConstructorArgReference(authorizationCodeServices);
110120
authorizationCodeTokenGranterBean.addConstructorArgReference(clientDetailsRef);
121+
authorizationCodeTokenGranterBean.addConstructorArgReference(oAuth2RequestFactoryRef);
111122

112123
if (StringUtils.hasText(clientTokenCacheRef)) {
113124
authorizationEndpointBean.addPropertyReference("clientTokenCache", clientTokenCacheRef);
@@ -124,22 +135,15 @@ protected AbstractBeanDefinition parseEndpointAndReturnFilter(Element element, P
124135
registerAuthorizationEndpoint = true;
125136
}
126137

127-
if (!StringUtils.hasText(oAuth2RequestFactoryRef)) {
128-
oAuth2RequestFactoryRef = "oAuth2AuthorizationRequestManager";
129-
BeanDefinitionBuilder oAuth2RequestManager = BeanDefinitionBuilder
130-
.rootBeanDefinition(DefaultOAuth2RequestFactory.class);
131-
oAuth2RequestManager.addConstructorArgReference(clientDetailsRef);
132-
parserContext.getRegistry().registerBeanDefinition(oAuth2RequestFactoryRef,
133-
oAuth2RequestManager.getBeanDefinition());
134-
}
135-
136138
if (tokenGranters != null) {
137139
Element refreshTokenElement = DomUtils.getChildElementByTagName(element, "refresh-token");
140+
138141
if (refreshTokenElement != null && !"true".equalsIgnoreCase(refreshTokenElement.getAttribute("disabled"))) {
139142
BeanDefinitionBuilder refreshTokenGranterBean = BeanDefinitionBuilder
140143
.rootBeanDefinition(RefreshTokenGranter.class);
141144
refreshTokenGranterBean.addConstructorArgReference(tokenServicesRef);
142145
refreshTokenGranterBean.addConstructorArgReference(clientDetailsRef);
146+
refreshTokenGranterBean.addConstructorArgReference(oAuth2RequestFactoryRef);
143147
tokenGranters.add(refreshTokenGranterBean.getBeanDefinition());
144148
}
145149
Element implicitElement = DomUtils.getChildElementByTagName(element, "implicit");
@@ -148,6 +152,7 @@ protected AbstractBeanDefinition parseEndpointAndReturnFilter(Element element, P
148152
.rootBeanDefinition(ImplicitTokenGranter.class);
149153
implicitGranterBean.addConstructorArgReference(tokenServicesRef);
150154
implicitGranterBean.addConstructorArgReference(clientDetailsRef);
155+
implicitGranterBean.addConstructorArgReference(oAuth2RequestFactoryRef);
151156
tokenGranters.add(implicitGranterBean.getBeanDefinition());
152157
registerAuthorizationEndpoint = true;
153158
}
@@ -158,6 +163,7 @@ protected AbstractBeanDefinition parseEndpointAndReturnFilter(Element element, P
158163
.rootBeanDefinition(ClientCredentialsTokenGranter.class);
159164
clientCredentialsGranterBean.addConstructorArgReference(tokenServicesRef);
160165
clientCredentialsGranterBean.addConstructorArgReference(clientDetailsRef);
166+
clientCredentialsGranterBean.addConstructorArgReference(oAuth2RequestFactoryRef);
161167
tokenGranters.add(clientCredentialsGranterBean.getBeanDefinition());
162168
}
163169
Element clientPasswordElement = DomUtils.getChildElementByTagName(element, "password");
@@ -172,6 +178,7 @@ protected AbstractBeanDefinition parseEndpointAndReturnFilter(Element element, P
172178
clientPasswordTokenGranter.addConstructorArgReference(authenticationManagerRef);
173179
clientPasswordTokenGranter.addConstructorArgReference(tokenServicesRef);
174180
clientPasswordTokenGranter.addConstructorArgReference(clientDetailsRef);
181+
clientPasswordTokenGranter.addConstructorArgReference(oAuth2RequestFactoryRef);
175182
tokenGranters.add(clientPasswordTokenGranter.getBeanDefinition());
176183
}
177184
List<Element> customGrantElements = DomUtils.getChildElementsByTagName(element, "custom-grant");

spring-security-oauth2/src/main/java/org/springframework/security/oauth2/provider/OAuth2Request.java renamed to spring-security-oauth2/src/main/java/org/springframework/security/oauth2/provider/AuthorizationRequest.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import org.springframework.security.oauth2.common.util.OAuth2Utils;
1414

1515
/**
16-
* Base class representing an OAuth2 authorization or token request. HTTP request parameters are stored in
16+
* Base class representing an OAuth2 Authorization Request. HTTP request parameters are stored in
1717
* the parameters map, and any processing the server makes throughout the lifecycle of a request are stored
1818
* on individual properties. The original request parameters will remain available through the parameters
1919
* map. For convenience, constants are defined in order to get at those original values. However, the
@@ -23,24 +23,12 @@
2323
* @author Dave Syer
2424
* @author Amanda Anganes
2525
*/
26-
public class OAuth2Request implements Serializable {
26+
public class AuthorizationRequest implements Serializable {
2727

2828
private static final long serialVersionUID = 1L;
29-
30-
public static final String CLIENT_ID = "client_id";
31-
32-
public static final String STATE = "state";
33-
34-
public static final String SCOPE = "scope";
35-
36-
public static final String REDIRECT_URI = "redirect_uri";
37-
38-
public static final String RESPONSE_TYPE = "response_type";
39-
40-
public static final String USER_OAUTH_APPROVAL = "user_oauth_approval";
4129

4230
/**
43-
* Map of parameters passed in to the Authorizatoin Endpoint or Token
31+
* Map of parameters passed in to the Authorization Endpoint or Token
4432
* Endpoint, preserved unchanged from the original request. This map should
4533
* not be modified after initialization. In general, classes should not
4634
* retrieve values from this map directly, and should instead use the
@@ -123,7 +111,7 @@ public class OAuth2Request implements Serializable {
123111
/**
124112
* Default constructor.
125113
*/
126-
public OAuth2Request() {
114+
public AuthorizationRequest() {
127115

128116
}
129117

@@ -141,12 +129,11 @@ public OAuth2Request() {
141129
* @param redirectUri
142130
* @param responseTypes
143131
*/
144-
public OAuth2Request(Map<String, String> authorizationParameters, Map<String, String> approvalParameters,
132+
public AuthorizationRequest(Map<String, String> authorizationParameters, Map<String, String> approvalParameters,
145133
String clientId, Set<String> scope, Set<String> resourceIds,
146134
Collection<? extends GrantedAuthority> authorities, boolean approved, String state,
147135
String redirectUri, Set<String> responseTypes){
148136
if (authorizationParameters != null) {
149-
//this.authorizationParameters.putAll(authorizationParameters);
150137
this.requestParameters = Collections.unmodifiableMap(authorizationParameters);
151138
}
152139
if (approvalParameters != null) {
@@ -177,7 +164,7 @@ public OAuth2Request(Map<String, String> authorizationParameters, Map<String, St
177164
* @param clientId
178165
* @param scopes
179166
*/
180-
public OAuth2Request(String clientId, Collection<String> scopes) {
167+
public AuthorizationRequest(String clientId, Collection<String> scopes) {
181168
this.clientId = clientId;
182169
if (scopes!= null) {
183170
this.scope.addAll(scopes);
@@ -356,10 +343,10 @@ public boolean equals(Object obj) {
356343
if (obj == null) {
357344
return false;
358345
}
359-
if (!(obj instanceof OAuth2Request)) {
346+
if (!(obj instanceof AuthorizationRequest)) {
360347
return false;
361348
}
362-
OAuth2Request other = (OAuth2Request) obj;
349+
AuthorizationRequest other = (AuthorizationRequest) obj;
363350
if (approvalParameters == null) {
364351
if (other.approvalParameters != null) {
365352
return false;

spring-security-oauth2/src/main/java/org/springframework/security/oauth2/provider/CompositeTokenGranter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public CompositeTokenGranter(List<TokenGranter> tokenGranters) {
3333
this.tokenGranters = new ArrayList<TokenGranter>(tokenGranters);
3434
}
3535

36-
public OAuth2AccessToken grant(String grantType, OAuth2Request tokenRequest) {
36+
public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) {
3737
for (TokenGranter granter : tokenGranters) {
3838
OAuth2AccessToken grant = granter.grant(grantType, tokenRequest);
3939
if (grant!=null) {

0 commit comments

Comments
 (0)