mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-05-30 00:32:14 +00:00
Add principal name to oauth2Client Test Support
Fixes gh-8054
This commit is contained in:
parent
3bc1b7a933
commit
2064214f39
@ -783,6 +783,7 @@ public class SecurityMockServerConfigurers {
|
||||
mockOAuth2Client()
|
||||
.accessToken(this.accessToken)
|
||||
.clientRegistration(this.clientRegistration)
|
||||
.principalName(token.getPrincipal().getName())
|
||||
.beforeServerCreated(builder);
|
||||
mockAuthentication(getToken()).beforeServerCreated(builder);
|
||||
}
|
||||
@ -1028,6 +1029,7 @@ public class SecurityMockServerConfigurers {
|
||||
public final static class OAuth2ClientMutator implements WebTestClientConfigurer, MockServerConfigurer {
|
||||
private String registrationId = "test";
|
||||
private ClientRegistration clientRegistration;
|
||||
private String principalName = "user";
|
||||
private OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
|
||||
"access-token", null, null, Collections.singleton("read"));
|
||||
|
||||
@ -1068,6 +1070,18 @@ public class SecurityMockServerConfigurers {
|
||||
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}
|
||||
*
|
||||
@ -1110,7 +1124,7 @@ public class SecurityMockServerConfigurers {
|
||||
throw new IllegalArgumentException("Please specify a ClientRegistration via one " +
|
||||
"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() {
|
||||
|
@ -1401,6 +1401,7 @@ public final class SecurityMockMvcRequestPostProcessors {
|
||||
request = new AuthenticationRequestPostProcessor(token).postProcessRequest(request);
|
||||
return new OAuth2ClientRequestPostProcessor()
|
||||
.clientRegistration(this.clientRegistration)
|
||||
.principalName(oauth2User.getName())
|
||||
.accessToken(this.accessToken)
|
||||
.postProcessRequest(request);
|
||||
}
|
||||
@ -1587,6 +1588,7 @@ public final class SecurityMockMvcRequestPostProcessors {
|
||||
public final static class OAuth2ClientRequestPostProcessor implements RequestPostProcessor {
|
||||
private String registrationId = "test";
|
||||
private ClientRegistration clientRegistration;
|
||||
private String principalName = "user";
|
||||
private OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
|
||||
"access-token", null, null, Collections.singleton("read"));
|
||||
|
||||
@ -1624,6 +1626,18 @@ public final class SecurityMockMvcRequestPostProcessors {
|
||||
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}
|
||||
*
|
||||
@ -1642,7 +1656,7 @@ public final class SecurityMockMvcRequestPostProcessors {
|
||||
"of the clientRegistration methods");
|
||||
}
|
||||
OAuth2AuthorizedClient client = new OAuth2AuthorizedClient
|
||||
(this.clientRegistration, "user", this.accessToken);
|
||||
(this.clientRegistration, this.principalName, this.accessToken);
|
||||
OAuth2AuthorizedClientRepository authorizedClientRepository =
|
||||
new HttpSessionOAuth2AuthorizedClientRepository();
|
||||
authorizedClientRepository.saveAuthorizedClient(client, null, request, new MockHttpServletResponse());
|
||||
|
@ -134,6 +134,16 @@ public class SecurityMockServerConfigurersOAuth2ClientTests extends AbstractMock
|
||||
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
|
||||
public void oauth2ClientWhenAccessTokenThenUses()
|
||||
throws Exception {
|
||||
|
@ -123,6 +123,13 @@ public class SecurityMockMvcRequestPostProcessorsOAuth2ClientTests {
|
||||
.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
|
||||
public void oauth2ClientWhenAccessTokenThenUses() throws Exception {
|
||||
OAuth2AccessToken accessToken = noScopes();
|
||||
@ -161,6 +168,11 @@ public class SecurityMockMvcRequestPostProcessorsOAuth2ClientTests {
|
||||
return authorizedClient.getAccessToken().getTokenValue();
|
||||
}
|
||||
|
||||
@GetMapping("/principal-name")
|
||||
String principalName(@RegisteredOAuth2AuthorizedClient("registration-id") OAuth2AuthorizedClient authorizedClient) {
|
||||
return authorizedClient.getPrincipalName();
|
||||
}
|
||||
|
||||
@GetMapping("/client-id")
|
||||
String clientId(@RegisteredOAuth2AuthorizedClient("registration-id") OAuth2AuthorizedClient authorizedClient) {
|
||||
return authorizedClient.getClientRegistration().getClientId();
|
||||
|
Loading…
x
Reference in New Issue
Block a user