|
@@ -47,9 +47,8 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
|
|
|
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
|
|
|
import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
|
|
+import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
|
|
|
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
|
|
|
-import org.springframework.security.oauth2.core.AuthorizationGrantType;
|
|
|
-import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
|
|
|
import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
|
|
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
|
|
|
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
|
|
@@ -105,18 +104,7 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
private Authentication authentication;
|
|
|
|
|
|
- private ClientRegistration github = ClientRegistration.withRegistrationId("github")
|
|
|
- .redirectUriTemplate("{baseUrl}/{action}/oauth2/code/{registrationId}")
|
|
|
- .clientAuthenticationMethod(ClientAuthenticationMethod.BASIC)
|
|
|
- .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
|
|
|
- .scope("read:user")
|
|
|
- .authorizationUri("https://github.com/login/oauth/authorize")
|
|
|
- .tokenUri("https://github.com/login/oauth/access_token")
|
|
|
- .userInfoUri("https://api.github.com/user")
|
|
|
- .userNameAttributeName("id")
|
|
|
- .clientName("GitHub")
|
|
|
- .clientId("clientId")
|
|
|
- .clientSecret("clientSecret")
|
|
|
+ private ClientRegistration registration = TestClientRegistrations.clientRegistration()
|
|
|
.build();
|
|
|
|
|
|
private OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
|
|
@@ -170,7 +158,7 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
@Test
|
|
|
public void defaultRequestOAuth2AuthorizedClientWhenOAuth2AuthorizationClientAndClientIdThenNotOverride() {
|
|
|
this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.authorizedClientRepository);
|
|
|
- OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.github,
|
|
|
+ OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
|
"principalName", this.accessToken);
|
|
|
oauth2AuthorizedClient(authorizedClient).accept(this.result);
|
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
@@ -213,7 +201,7 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
OAuth2User user = mock(OAuth2User.class);
|
|
|
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
|
|
|
OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(user, authorities, "id");
|
|
|
- OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.github,
|
|
|
+ OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
|
"principalName", this.accessToken);
|
|
|
when(this.authorizedClientRepository.loadAuthorizedClient(any(), any(), any())).thenReturn(authorizedClient);
|
|
|
authentication(token).accept(this.result);
|
|
@@ -243,7 +231,7 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
OAuth2User user = mock(OAuth2User.class);
|
|
|
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
|
|
|
OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(user, authorities, "id");
|
|
|
- OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.github,
|
|
|
+ OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
|
"principalName", this.accessToken);
|
|
|
when(this.authorizedClientRepository.loadAuthorizedClient(any(), any(), any())).thenReturn(authorizedClient);
|
|
|
authentication(token).accept(this.result);
|
|
@@ -260,7 +248,7 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.authorizedClientRepository);
|
|
|
OAuth2User user = mock(OAuth2User.class);
|
|
|
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
|
|
|
- OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.github,
|
|
|
+ OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
|
"principalName", this.accessToken);
|
|
|
when(this.authorizedClientRepository.loadAuthorizedClient(any(), any(), any())).thenReturn(authorizedClient);
|
|
|
clientRegistrationId("id").accept(this.result);
|
|
@@ -292,7 +280,7 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
@Test
|
|
|
public void filterWhenAuthorizedClientThenAuthorizationHeader() {
|
|
|
- OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.github,
|
|
|
+ OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
|
"principalName", this.accessToken);
|
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
@@ -305,7 +293,7 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
@Test
|
|
|
public void filterWhenExistingAuthorizationThenSingleAuthorizationHeader() {
|
|
|
- OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.github,
|
|
|
+ OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
|
"principalName", this.accessToken);
|
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
|
.header(HttpHeaders.AUTHORIZATION, "Existing")
|
|
@@ -337,7 +325,7 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.authorizedClientRepository);
|
|
|
|
|
|
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token", issuedAt, refreshTokenExpiresAt);
|
|
|
- OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.github,
|
|
|
+ OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
|
"principalName", this.accessToken, refreshToken);
|
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
@@ -352,8 +340,8 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
assertThat(requests).hasSize(2);
|
|
|
|
|
|
ClientRequest request0 = requests.get(0);
|
|
|
- assertThat(request0.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Basic Y2xpZW50SWQ6Y2xpZW50U2VjcmV0");
|
|
|
- assertThat(request0.url().toASCIIString()).isEqualTo("https://github.com/login/oauth/access_token");
|
|
|
+ assertThat(request0.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ=");
|
|
|
+ assertThat(request0.url().toASCIIString()).isEqualTo("https://example.com/login/oauth/access_token");
|
|
|
assertThat(request0.method()).isEqualTo(HttpMethod.POST);
|
|
|
assertThat(getBody(request0)).isEqualTo("grant_type=refresh_token&refresh_token=refresh-token");
|
|
|
|
|
@@ -383,7 +371,7 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.authorizedClientRepository);
|
|
|
|
|
|
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token", issuedAt, refreshTokenExpiresAt);
|
|
|
- OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.github,
|
|
|
+ OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
|
"principalName", this.accessToken, refreshToken);
|
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
@@ -398,8 +386,8 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
assertThat(requests).hasSize(2);
|
|
|
|
|
|
ClientRequest request0 = requests.get(0);
|
|
|
- assertThat(request0.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Basic Y2xpZW50SWQ6Y2xpZW50U2VjcmV0");
|
|
|
- assertThat(request0.url().toASCIIString()).isEqualTo("https://github.com/login/oauth/access_token");
|
|
|
+ assertThat(request0.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ=");
|
|
|
+ assertThat(request0.url().toASCIIString()).isEqualTo("https://example.com/login/oauth/access_token");
|
|
|
assertThat(request0.method()).isEqualTo(HttpMethod.POST);
|
|
|
assertThat(getBody(request0)).isEqualTo("grant_type=refresh_token&refresh_token=refresh-token");
|
|
|
|
|
@@ -414,7 +402,7 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
public void filterWhenRefreshTokenNullThenShouldRefreshFalse() {
|
|
|
this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.authorizedClientRepository);
|
|
|
|
|
|
- OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.github,
|
|
|
+ OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
|
"principalName", this.accessToken);
|
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
@@ -437,7 +425,7 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.authorizedClientRepository);
|
|
|
|
|
|
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token", this.accessToken.getIssuedAt(), this.accessToken.getExpiresAt());
|
|
|
- OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.github,
|
|
|
+ OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
|
"principalName", this.accessToken, refreshToken);
|
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|