Skip to content

Commit 594a169

Browse files
committed
Introduce OAuth2AuthorizationRequest.attributes
Fixes gh-5940
1 parent 67fb936 commit 594a169

File tree

13 files changed

+108
-82
lines changed

13 files changed

+108
-82
lines changed

config/src/test/java/org/springframework/security/config/annotation/web/configurers/oauth2/client/OAuth2ClientConfigurerTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -146,14 +146,14 @@ public void configureWhenAuthorizationCodeResponseSuccessThenAuthorizedClientSav
146146
this.spring.register(OAuth2ClientConfig.class).autowire();
147147

148148
// Setup the Authorization Request in the session
149-
Map<String, Object> additionalParameters = new HashMap<>();
150-
additionalParameters.put(OAuth2ParameterNames.REGISTRATION_ID, this.registration1.getRegistrationId());
149+
Map<String, Object> attributes = new HashMap<>();
150+
attributes.put(OAuth2ParameterNames.REGISTRATION_ID, this.registration1.getRegistrationId());
151151
OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode()
152152
.authorizationUri(this.registration1.getProviderDetails().getAuthorizationUri())
153153
.clientId(this.registration1.getClientId())
154154
.redirectUri("http://localhost/client-1")
155155
.state("state")
156-
.additionalParameters(additionalParameters)
156+
.attributes(attributes)
157157
.build();
158158

159159
AuthorizationRequestRepository<OAuth2AuthorizationRequest> authorizationRequestRepository =

config/src/test/java/org/springframework/security/config/annotation/web/configurers/oauth2/client/OAuth2LoginConfigurerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -473,7 +473,7 @@ private OAuth2AuthorizationRequest createOAuth2AuthorizationRequest(ClientRegist
473473
.clientId(registration.getClientId())
474474
.state("state123")
475475
.redirectUri("http://localhost")
476-
.additionalParameters(
476+
.attributes(
477477
Collections.singletonMap(
478478
OAuth2ParameterNames.REGISTRATION_ID,
479479
registration.getRegistrationId()))

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/DefaultOAuth2AuthorizationRequestResolver.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -115,16 +115,16 @@ private OAuth2AuthorizationRequest resolve(HttpServletRequest request, String re
115115

116116
String redirectUriStr = this.expandRedirectUri(request, clientRegistration, redirectUriAction);
117117

118-
Map<String, Object> additionalParameters = new HashMap<>();
119-
additionalParameters.put(OAuth2ParameterNames.REGISTRATION_ID, clientRegistration.getRegistrationId());
118+
Map<String, Object> attributes = new HashMap<>();
119+
attributes.put(OAuth2ParameterNames.REGISTRATION_ID, clientRegistration.getRegistrationId());
120120

121121
OAuth2AuthorizationRequest authorizationRequest = builder
122122
.clientId(clientRegistration.getClientId())
123123
.authorizationUri(clientRegistration.getProviderDetails().getAuthorizationUri())
124124
.redirectUri(redirectUriStr)
125125
.scopes(clientRegistration.getScopes())
126126
.state(this.stateGenerator.generateKey())
127-
.additionalParameters(additionalParameters)
127+
.attributes(attributes)
128128
.build();
129129

130130
return authorizationRequest;

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/OAuth2AuthorizationCodeGrantFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -161,7 +161,7 @@ private void processAuthorizationResponse(HttpServletRequest request, HttpServle
161161
OAuth2AuthorizationRequest authorizationRequest =
162162
this.authorizationRequestRepository.removeAuthorizationRequest(request, response);
163163

164-
String registrationId = (String) authorizationRequest.getAdditionalParameters().get(OAuth2ParameterNames.REGISTRATION_ID);
164+
String registrationId = authorizationRequest.getAttribute(OAuth2ParameterNames.REGISTRATION_ID);
165165
ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId(registrationId);
166166

167167
MultiValueMap<String, String> params = OAuth2AuthorizationResponseUtils.toMultiMap(request.getParameterMap());

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/OAuth2LoginAuthenticationFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -165,7 +165,7 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
165165
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
166166
}
167167

168-
String registrationId = (String) authorizationRequest.getAdditionalParameters().get(OAuth2ParameterNames.REGISTRATION_ID);
168+
String registrationId = authorizationRequest.getAttribute(OAuth2ParameterNames.REGISTRATION_ID);
169169
ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId(registrationId);
170170
if (clientRegistration == null) {
171171
OAuth2Error oauth2Error = new OAuth2Error(CLIENT_REGISTRATION_NOT_FOUND_ERROR_CODE,

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/server/DefaultServerOAuth2AuthorizationRequestResolver.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -118,9 +118,8 @@ private OAuth2AuthorizationRequest authorizationRequest(ServerWebExchange exchan
118118
String redirectUriStr = this
119119
.expandRedirectUri(exchange.getRequest(), clientRegistration);
120120

121-
Map<String, Object> additionalParameters = new HashMap<>();
122-
additionalParameters.put(OAuth2ParameterNames.REGISTRATION_ID,
123-
clientRegistration.getRegistrationId());
121+
Map<String, Object> attributes = new HashMap<>();
122+
attributes.put(OAuth2ParameterNames.REGISTRATION_ID, clientRegistration.getRegistrationId());
124123

125124
OAuth2AuthorizationRequest.Builder builder;
126125
if (AuthorizationGrantType.AUTHORIZATION_CODE.equals(clientRegistration.getAuthorizationGrantType())) {
@@ -139,7 +138,7 @@ else if (AuthorizationGrantType.IMPLICIT.equals(clientRegistration.getAuthorizat
139138
.authorizationUri(clientRegistration.getProviderDetails().getAuthorizationUri())
140139
.redirectUri(redirectUriStr).scopes(clientRegistration.getScopes())
141140
.state(this.stateGenerator.generateKey())
142-
.additionalParameters(additionalParameters)
141+
.attributes(attributes)
143142
.build();
144143
}
145144

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/server/ServerOAuth2AuthorizationCodeAuthenticationTokenConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -85,9 +85,9 @@ private <T> Mono<T> oauth2AuthorizationException(String errorCode) {
8585

8686
private Mono<OAuth2AuthorizationCodeAuthenticationToken> authenticationRequest(ServerWebExchange exchange, OAuth2AuthorizationRequest authorizationRequest) {
8787
return Mono.just(authorizationRequest)
88-
.map(OAuth2AuthorizationRequest::getAdditionalParameters)
89-
.flatMap(additionalParams -> {
90-
String id = (String) additionalParams.get(OAuth2ParameterNames.REGISTRATION_ID);
88+
.map(OAuth2AuthorizationRequest::getAttributes)
89+
.flatMap(attributes -> {
90+
String id = (String) attributes.get(OAuth2ParameterNames.REGISTRATION_ID);
9191
if (id == null) {
9292
return oauth2AuthorizationException(CLIENT_REGISTRATION_NOT_FOUND_ERROR_CODE);
9393
}

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/web/DefaultOAuth2AuthorizationRequestResolverTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -105,7 +105,8 @@ public void resolveWhenAuthorizationRequestWithValidClientThenResolves() {
105105
.isEqualTo("http://localhost/login/oauth2/code/" + clientRegistration.getRegistrationId());
106106
assertThat(authorizationRequest.getScopes()).isEqualTo(clientRegistration.getScopes());
107107
assertThat(authorizationRequest.getState()).isNotNull();
108-
assertThat(authorizationRequest.getAdditionalParameters())
108+
assertThat(authorizationRequest.getAdditionalParameters()).doesNotContainKey(OAuth2ParameterNames.REGISTRATION_ID);
109+
assertThat(authorizationRequest.getAttributes())
109110
.containsExactly(entry(OAuth2ParameterNames.REGISTRATION_ID, clientRegistration.getRegistrationId()));
110111
assertThat(authorizationRequest.getAuthorizationRequestUri())
111112
.matches("https://example.com/login/oauth/authorize\\?" +
@@ -123,7 +124,7 @@ public void resolveWhenClientAuthorizationRequiredExceptionAvailableThenResolves
123124

124125
OAuth2AuthorizationRequest authorizationRequest = this.resolver.resolve(request, clientRegistration.getRegistrationId());
125126
assertThat(authorizationRequest).isNotNull();
126-
assertThat(authorizationRequest.getAdditionalParameters())
127+
assertThat(authorizationRequest.getAttributes())
127128
.containsExactly(entry(OAuth2ParameterNames.REGISTRATION_ID, clientRegistration.getRegistrationId()));
128129
}
129130

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/web/OAuth2LoginAuthenticationFilterTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -402,15 +402,15 @@ public void doFilterWhenAuthorizationResponseHasNonDefaultPortThenRedirectUriMat
402402

403403
private void setUpAuthorizationRequest(HttpServletRequest request, HttpServletResponse response,
404404
ClientRegistration registration, String state) {
405-
Map<String, Object> additionalParameters = new HashMap<>();
406-
additionalParameters.put(OAuth2ParameterNames.REGISTRATION_ID, registration.getRegistrationId());
405+
Map<String, Object> attributes = new HashMap<>();
406+
attributes.put(OAuth2ParameterNames.REGISTRATION_ID, registration.getRegistrationId());
407407
OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode()
408408
.authorizationUri(registration.getProviderDetails().getAuthorizationUri())
409409
.clientId(registration.getClientId())
410410
.redirectUri(expandRedirectUri(request, registration))
411411
.scopes(registration.getScopes())
412412
.state(state)
413-
.additionalParameters(additionalParameters)
413+
.attributes(attributes)
414414
.build();
415415
this.authorizationRequestRepository.saveAuthorizationRequest(authorizationRequest, request, response);
416416
}

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/web/server/ServerOAuth2AuthorizationCodeAuthenticationTokenConverterTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -74,7 +74,7 @@ public class ServerOAuth2AuthorizationCodeAuthenticationTokenConverterTest {
7474
.clientId("client-id")
7575
.redirectUri("http://localhost/client-1")
7676
.state("state")
77-
.additionalParameters(Collections.singletonMap(OAuth2ParameterNames.REGISTRATION_ID, this.clientRegistrationId));
77+
.attributes(Collections.singletonMap(OAuth2ParameterNames.REGISTRATION_ID, this.clientRegistrationId));
7878

7979
private final MockServerHttpRequest.BaseBuilder<?> request = MockServerHttpRequest.get("/");
8080

@@ -95,8 +95,8 @@ public void applyWhenAuthorizationRequestEmptyThenOAuth2AuthorizationException()
9595
}
9696

9797
@Test
98-
public void applyWhenAdditionalParametersMissingThenOAuth2AuthorizationException() {
99-
this.authorizationRequest.additionalParameters(Collections.emptyMap());
98+
public void applyWhenAttributesMissingThenOAuth2AuthorizationException() {
99+
this.authorizationRequest.attributes(Collections.emptyMap());
100100
when(this.authorizationRequestRepository.removeAuthorizationRequest(any())).thenReturn(Mono.just(this.authorizationRequest.build()));
101101

102102
assertThatThrownBy(() -> applyConverter())

0 commit comments

Comments
 (0)