Browse Source

Add principal name to oauth2Client Test Support

Fixes gh-8054
pull/8059/head
Josh Cummings 6 years ago
parent
commit
2064214f39
No known key found for this signature in database
GPG Key ID: 49EF60DD7FF83443
  1. 16
      test/src/main/java/org/springframework/security/test/web/reactive/server/SecurityMockServerConfigurers.java
  2. 16
      test/src/main/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessors.java
  3. 10
      test/src/test/java/org/springframework/security/test/web/reactive/server/SecurityMockServerConfigurersOAuth2ClientTests.java
  4. 12
      test/src/test/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessorsOAuth2ClientTests.java

16
test/src/main/java/org/springframework/security/test/web/reactive/server/SecurityMockServerConfigurers.java

@ -783,6 +783,7 @@ public class SecurityMockServerConfigurers {
mockOAuth2Client() mockOAuth2Client()
.accessToken(this.accessToken) .accessToken(this.accessToken)
.clientRegistration(this.clientRegistration) .clientRegistration(this.clientRegistration)
.principalName(token.getPrincipal().getName())
.beforeServerCreated(builder); .beforeServerCreated(builder);
mockAuthentication(getToken()).beforeServerCreated(builder); mockAuthentication(getToken()).beforeServerCreated(builder);
} }
@ -1028,6 +1029,7 @@ public class SecurityMockServerConfigurers {
public final static class OAuth2ClientMutator implements WebTestClientConfigurer, MockServerConfigurer { public final static class OAuth2ClientMutator implements WebTestClientConfigurer, MockServerConfigurer {
private String registrationId = "test"; private String registrationId = "test";
private ClientRegistration clientRegistration; private ClientRegistration clientRegistration;
private String principalName = "user";
private OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, private OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
"access-token", null, null, Collections.singleton("read")); "access-token", null, null, Collections.singleton("read"));
@ -1068,6 +1070,18 @@ public class SecurityMockServerConfigurers {
return this; return this;
} }
/**
* Use this as the resource owner's principal name
*
* @param principalName the resource owner's principal name
* @return the {@link OAuth2ClientMutator} for further configuration
*/
public OAuth2ClientMutator principalName(String principalName) {
Assert.notNull(principalName, "principalName cannot be null");
this.principalName = principalName;
return this;
}
/** /**
* Use this {@link OAuth2AccessToken} * Use this {@link OAuth2AccessToken}
* *
@ -1110,7 +1124,7 @@ public class SecurityMockServerConfigurers {
throw new IllegalArgumentException("Please specify a ClientRegistration via one " + throw new IllegalArgumentException("Please specify a ClientRegistration via one " +
"of the clientRegistration methods"); "of the clientRegistration methods");
} }
return new OAuth2AuthorizedClient(this.clientRegistration, "user", this.accessToken); return new OAuth2AuthorizedClient(this.clientRegistration, this.principalName, this.accessToken);
} }
private ClientRegistration.Builder clientRegistrationBuilder() { private ClientRegistration.Builder clientRegistrationBuilder() {

16
test/src/main/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessors.java

@ -1401,6 +1401,7 @@ public final class SecurityMockMvcRequestPostProcessors {
request = new AuthenticationRequestPostProcessor(token).postProcessRequest(request); request = new AuthenticationRequestPostProcessor(token).postProcessRequest(request);
return new OAuth2ClientRequestPostProcessor() return new OAuth2ClientRequestPostProcessor()
.clientRegistration(this.clientRegistration) .clientRegistration(this.clientRegistration)
.principalName(oauth2User.getName())
.accessToken(this.accessToken) .accessToken(this.accessToken)
.postProcessRequest(request); .postProcessRequest(request);
} }
@ -1587,6 +1588,7 @@ public final class SecurityMockMvcRequestPostProcessors {
public final static class OAuth2ClientRequestPostProcessor implements RequestPostProcessor { public final static class OAuth2ClientRequestPostProcessor implements RequestPostProcessor {
private String registrationId = "test"; private String registrationId = "test";
private ClientRegistration clientRegistration; private ClientRegistration clientRegistration;
private String principalName = "user";
private OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, private OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
"access-token", null, null, Collections.singleton("read")); "access-token", null, null, Collections.singleton("read"));
@ -1624,6 +1626,18 @@ public final class SecurityMockMvcRequestPostProcessors {
return this; return this;
} }
/**
* Use this as the resource owner's principal name
*
* @param principalName the resource owner's principal name
* @return the {@link OAuth2ClientRequestPostProcessor} for further configuration
*/
public OAuth2ClientRequestPostProcessor principalName(String principalName) {
Assert.notNull(principalName, "principalName cannot be null");
this.principalName = principalName;
return this;
}
/** /**
* Use this {@link OAuth2AccessToken} * Use this {@link OAuth2AccessToken}
* *
@ -1642,7 +1656,7 @@ public final class SecurityMockMvcRequestPostProcessors {
"of the clientRegistration methods"); "of the clientRegistration methods");
} }
OAuth2AuthorizedClient client = new OAuth2AuthorizedClient OAuth2AuthorizedClient client = new OAuth2AuthorizedClient
(this.clientRegistration, "user", this.accessToken); (this.clientRegistration, this.principalName, this.accessToken);
OAuth2AuthorizedClientRepository authorizedClientRepository = OAuth2AuthorizedClientRepository authorizedClientRepository =
new HttpSessionOAuth2AuthorizedClientRepository(); new HttpSessionOAuth2AuthorizedClientRepository();
authorizedClientRepository.saveAuthorizedClient(client, null, request, new MockHttpServletResponse()); authorizedClientRepository.saveAuthorizedClient(client, null, request, new MockHttpServletResponse());

10
test/src/test/java/org/springframework/security/test/web/reactive/server/SecurityMockServerConfigurersOAuth2ClientTests.java

@ -134,6 +134,16 @@ public class SecurityMockServerConfigurersOAuth2ClientTests extends AbstractMock
assertThat(client.getRefreshToken()).isNull(); assertThat(client.getRefreshToken()).isNull();
} }
@Test
public void oauth2ClientWhenPrincipalNameThenUses() throws Exception {
this.client.mutateWith(mockOAuth2Client("registration-id")
.principalName("test-subject"))
.get().uri("/client")
.exchange()
.expectStatus().isOk()
.expectBody(String.class).isEqualTo("test-subject");
}
@Test @Test
public void oauth2ClientWhenAccessTokenThenUses() public void oauth2ClientWhenAccessTokenThenUses()
throws Exception { throws Exception {

12
test/src/test/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessorsOAuth2ClientTests.java

@ -123,6 +123,13 @@ public class SecurityMockMvcRequestPostProcessorsOAuth2ClientTests {
.andExpect(content().string("client-id")); .andExpect(content().string("client-id"));
} }
@Test
public void oauth2ClientWhenPrincipalNameThenUses() throws Exception {
this.mvc.perform(get("/principal-name")
.with(oauth2Client("registration-id").principalName("test-subject")))
.andExpect(content().string("test-subject"));
}
@Test @Test
public void oauth2ClientWhenAccessTokenThenUses() throws Exception { public void oauth2ClientWhenAccessTokenThenUses() throws Exception {
OAuth2AccessToken accessToken = noScopes(); OAuth2AccessToken accessToken = noScopes();
@ -161,6 +168,11 @@ public class SecurityMockMvcRequestPostProcessorsOAuth2ClientTests {
return authorizedClient.getAccessToken().getTokenValue(); return authorizedClient.getAccessToken().getTokenValue();
} }
@GetMapping("/principal-name")
String principalName(@RegisteredOAuth2AuthorizedClient("registration-id") OAuth2AuthorizedClient authorizedClient) {
return authorizedClient.getPrincipalName();
}
@GetMapping("/client-id") @GetMapping("/client-id")
String clientId(@RegisteredOAuth2AuthorizedClient("registration-id") OAuth2AuthorizedClient authorizedClient) { String clientId(@RegisteredOAuth2AuthorizedClient("registration-id") OAuth2AuthorizedClient authorizedClient) {
return authorizedClient.getClientRegistration().getClientId(); return authorizedClient.getClientRegistration().getClientId();

Loading…
Cancel
Save