Skip to content

Commit 45eb34c

Browse files
rh-idjgrandja
authored andcommitted
Improve OAuth2LoginAuthenticationProvider
1. update OAuth2LoginAuthenticationProvider to use OAuth2AuthorizationCodeAuthenticationProvider 2. apply fix gh-5368 for OAuth2AuthorizationCodeAuthenticationProvider to return additionalParameters value from accessTokenResponse Fixes gh-5633
1 parent 4c040e9 commit 45eb34c

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

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

+3-2
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-2020 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.
@@ -73,7 +73,8 @@ public Authentication authenticate(Authentication authentication) throws Authent
7373
authorizationCodeAuthentication.getClientRegistration(),
7474
authorizationCodeAuthentication.getAuthorizationExchange(),
7575
accessTokenResponse.getAccessToken(),
76-
accessTokenResponse.getRefreshToken());
76+
accessTokenResponse.getRefreshToken(),
77+
accessTokenResponse.getAdditionalParameters());
7778
authenticationResult.setDetails(authorizationCodeAuthentication.getDetails());
7879

7980
return authenticationResult;

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/authentication/OAuth2LoginAuthenticationProvider.java

+17-23
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-2020 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.
@@ -28,7 +28,6 @@
2828
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
2929
import org.springframework.security.oauth2.core.OAuth2AuthorizationException;
3030
import org.springframework.security.oauth2.core.OAuth2Error;
31-
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
3231
import org.springframework.security.oauth2.core.user.OAuth2User;
3332
import org.springframework.util.Assert;
3433

@@ -60,7 +59,7 @@
6059
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-4.1.4">Section 4.1.4 Access Token Response</a>
6160
*/
6261
public class OAuth2LoginAuthenticationProvider implements AuthenticationProvider {
63-
private final OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> accessTokenResponseClient;
62+
private final OAuth2AuthorizationCodeAuthenticationProvider authorizationCodeAuthenticationProvider;
6463
private final OAuth2UserService<OAuth2UserRequest, OAuth2User> userService;
6564
private GrantedAuthoritiesMapper authoritiesMapper = (authorities -> authorities);
6665

@@ -74,59 +73,54 @@ public OAuth2LoginAuthenticationProvider(
7473
OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> accessTokenResponseClient,
7574
OAuth2UserService<OAuth2UserRequest, OAuth2User> userService) {
7675

77-
Assert.notNull(accessTokenResponseClient, "accessTokenResponseClient cannot be null");
7876
Assert.notNull(userService, "userService cannot be null");
79-
this.accessTokenResponseClient = accessTokenResponseClient;
77+
this.authorizationCodeAuthenticationProvider = new OAuth2AuthorizationCodeAuthenticationProvider(accessTokenResponseClient);
8078
this.userService = userService;
8179
}
8280

8381
@Override
8482
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
85-
OAuth2LoginAuthenticationToken authorizationCodeAuthentication =
83+
OAuth2LoginAuthenticationToken loginAuthenticationToken =
8684
(OAuth2LoginAuthenticationToken) authentication;
8785

8886
// Section 3.1.2.1 Authentication Request - https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest
8987
// scope
9088
// REQUIRED. OpenID Connect requests MUST contain the "openid" scope value.
91-
if (authorizationCodeAuthentication.getAuthorizationExchange()
89+
if (loginAuthenticationToken.getAuthorizationExchange()
9290
.getAuthorizationRequest().getScopes().contains("openid")) {
9391
// This is an OpenID Connect Authentication Request so return null
9492
// and let OidcAuthorizationCodeAuthenticationProvider handle it instead
9593
return null;
9694
}
9795

98-
OAuth2AccessTokenResponse accessTokenResponse;
96+
OAuth2AuthorizationCodeAuthenticationToken authorizationCodeAuthenticationToken;
9997
try {
100-
OAuth2AuthorizationExchangeValidator.validate(
101-
authorizationCodeAuthentication.getAuthorizationExchange());
102-
103-
accessTokenResponse = this.accessTokenResponseClient.getTokenResponse(
104-
new OAuth2AuthorizationCodeGrantRequest(
105-
authorizationCodeAuthentication.getClientRegistration(),
106-
authorizationCodeAuthentication.getAuthorizationExchange()));
107-
98+
authorizationCodeAuthenticationToken = (OAuth2AuthorizationCodeAuthenticationToken) this.authorizationCodeAuthenticationProvider
99+
.authenticate(new OAuth2AuthorizationCodeAuthenticationToken(
100+
loginAuthenticationToken.getClientRegistration(),
101+
loginAuthenticationToken.getAuthorizationExchange()));
108102
} catch (OAuth2AuthorizationException ex) {
109103
OAuth2Error oauth2Error = ex.getError();
110104
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
111105
}
112106

113-
OAuth2AccessToken accessToken = accessTokenResponse.getAccessToken();
114-
Map<String, Object> additionalParameters = accessTokenResponse.getAdditionalParameters();
107+
OAuth2AccessToken accessToken = authorizationCodeAuthenticationToken.getAccessToken();
108+
Map<String, Object> additionalParameters = authorizationCodeAuthenticationToken.getAdditionalParameters();
115109

116110
OAuth2User oauth2User = this.userService.loadUser(new OAuth2UserRequest(
117-
authorizationCodeAuthentication.getClientRegistration(), accessToken, additionalParameters));
111+
loginAuthenticationToken.getClientRegistration(), accessToken, additionalParameters));
118112

119113
Collection<? extends GrantedAuthority> mappedAuthorities =
120114
this.authoritiesMapper.mapAuthorities(oauth2User.getAuthorities());
121115

122116
OAuth2LoginAuthenticationToken authenticationResult = new OAuth2LoginAuthenticationToken(
123-
authorizationCodeAuthentication.getClientRegistration(),
124-
authorizationCodeAuthentication.getAuthorizationExchange(),
117+
loginAuthenticationToken.getClientRegistration(),
118+
loginAuthenticationToken.getAuthorizationExchange(),
125119
oauth2User,
126120
mappedAuthorities,
127121
accessToken,
128-
accessTokenResponse.getRefreshToken());
129-
authenticationResult.setDetails(authorizationCodeAuthentication.getDetails());
122+
authorizationCodeAuthenticationToken.getRefreshToken());
123+
authenticationResult.setDetails(loginAuthenticationToken.getDetails());
130124

131125
return authenticationResult;
132126
}

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/authentication/OAuth2AuthorizationCodeAuthenticationProviderTests.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -16,6 +16,8 @@
1616
package org.springframework.security.oauth2.client.authentication;
1717

1818
import java.util.Collections;
19+
import java.util.HashMap;
20+
import java.util.Map;
1921

2022
import org.junit.Before;
2123
import org.junit.Test;
@@ -119,4 +121,26 @@ public void authenticateWhenAuthorizationSuccessResponseThenExchangedForAccessTo
119121
assertThat(authenticationResult.getAccessToken()).isEqualTo(accessTokenResponse.getAccessToken());
120122
assertThat(authenticationResult.getRefreshToken()).isEqualTo(accessTokenResponse.getRefreshToken());
121123
}
124+
125+
// gh-5368
126+
@Test
127+
public void authenticateWhenAuthorizationSuccessResponseThenAdditionalParametersIncluded() {
128+
Map<String, Object> additionalParameters = new HashMap<>();
129+
additionalParameters.put("param1", "value1");
130+
additionalParameters.put("param2", "value2");
131+
132+
OAuth2AccessTokenResponse accessTokenResponse = accessTokenResponse().additionalParameters(additionalParameters)
133+
.build();
134+
when(this.accessTokenResponseClient.getTokenResponse(any())).thenReturn(accessTokenResponse);
135+
136+
OAuth2AuthorizationExchange authorizationExchange = new OAuth2AuthorizationExchange(this.authorizationRequest,
137+
success().build());
138+
139+
OAuth2AuthorizationCodeAuthenticationToken authentication = (OAuth2AuthorizationCodeAuthenticationToken) this.authenticationProvider
140+
.authenticate(
141+
new OAuth2AuthorizationCodeAuthenticationToken(this.clientRegistration, authorizationExchange));
142+
143+
assertThat(authentication.getAdditionalParameters())
144+
.containsAllEntriesOf(accessTokenResponse.getAdditionalParameters());
145+
}
122146
}

0 commit comments

Comments
 (0)