Skip to content

Commit 2064214

Browse files
committed
Add principal name to oauth2Client Test Support
Fixes gh-8054
1 parent 3bc1b7a commit 2064214

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,7 @@ public void beforeServerCreated(WebHttpHandlerBuilder builder) {
783783
mockOAuth2Client()
784784
.accessToken(this.accessToken)
785785
.clientRegistration(this.clientRegistration)
786+
.principalName(token.getPrincipal().getName())
786787
.beforeServerCreated(builder);
787788
mockAuthentication(getToken()).beforeServerCreated(builder);
788789
}
@@ -1028,6 +1029,7 @@ private OidcUser defaultPrincipal() {
10281029
public final static class OAuth2ClientMutator implements WebTestClientConfigurer, MockServerConfigurer {
10291030
private String registrationId = "test";
10301031
private ClientRegistration clientRegistration;
1032+
private String principalName = "user";
10311033
private OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
10321034
"access-token", null, null, Collections.singleton("read"));
10331035

@@ -1068,6 +1070,18 @@ public OAuth2ClientMutator clientRegistration(ClientRegistration clientRegistrat
10681070
return this;
10691071
}
10701072

1073+
/**
1074+
* Use this as the resource owner's principal name
1075+
*
1076+
* @param principalName the resource owner's principal name
1077+
* @return the {@link OAuth2ClientMutator} for further configuration
1078+
*/
1079+
public OAuth2ClientMutator principalName(String principalName) {
1080+
Assert.notNull(principalName, "principalName cannot be null");
1081+
this.principalName = principalName;
1082+
return this;
1083+
}
1084+
10711085
/**
10721086
* Use this {@link OAuth2AccessToken}
10731087
*
@@ -1110,7 +1124,7 @@ private OAuth2AuthorizedClient getClient() {
11101124
throw new IllegalArgumentException("Please specify a ClientRegistration via one " +
11111125
"of the clientRegistration methods");
11121126
}
1113-
return new OAuth2AuthorizedClient(this.clientRegistration, "user", this.accessToken);
1127+
return new OAuth2AuthorizedClient(this.clientRegistration, this.principalName, this.accessToken);
11141128
}
11151129

11161130
private ClientRegistration.Builder clientRegistrationBuilder() {

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,7 @@ public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request)
14011401
request = new AuthenticationRequestPostProcessor(token).postProcessRequest(request);
14021402
return new OAuth2ClientRequestPostProcessor()
14031403
.clientRegistration(this.clientRegistration)
1404+
.principalName(oauth2User.getName())
14041405
.accessToken(this.accessToken)
14051406
.postProcessRequest(request);
14061407
}
@@ -1587,6 +1588,7 @@ private OidcUser defaultPrincipal() {
15871588
public final static class OAuth2ClientRequestPostProcessor implements RequestPostProcessor {
15881589
private String registrationId = "test";
15891590
private ClientRegistration clientRegistration;
1591+
private String principalName = "user";
15901592
private OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
15911593
"access-token", null, null, Collections.singleton("read"));
15921594

@@ -1624,6 +1626,18 @@ public OAuth2ClientRequestPostProcessor clientRegistration(ClientRegistration cl
16241626
return this;
16251627
}
16261628

1629+
/**
1630+
* Use this as the resource owner's principal name
1631+
*
1632+
* @param principalName the resource owner's principal name
1633+
* @return the {@link OAuth2ClientRequestPostProcessor} for further configuration
1634+
*/
1635+
public OAuth2ClientRequestPostProcessor principalName(String principalName) {
1636+
Assert.notNull(principalName, "principalName cannot be null");
1637+
this.principalName = principalName;
1638+
return this;
1639+
}
1640+
16271641
/**
16281642
* Use this {@link OAuth2AccessToken}
16291643
*
@@ -1642,7 +1656,7 @@ public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request)
16421656
"of the clientRegistration methods");
16431657
}
16441658
OAuth2AuthorizedClient client = new OAuth2AuthorizedClient
1645-
(this.clientRegistration, "user", this.accessToken);
1659+
(this.clientRegistration, this.principalName, this.accessToken);
16461660
OAuth2AuthorizedClientRepository authorizedClientRepository =
16471661
new HttpSessionOAuth2AuthorizedClientRepository();
16481662
authorizedClientRepository.saveAuthorizedClient(client, null, request, new MockHttpServletResponse());

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ public void oauth2ClientWhenClientRegistrationConsumerThenUses()
134134
assertThat(client.getRefreshToken()).isNull();
135135
}
136136

137+
@Test
138+
public void oauth2ClientWhenPrincipalNameThenUses() throws Exception {
139+
this.client.mutateWith(mockOAuth2Client("registration-id")
140+
.principalName("test-subject"))
141+
.get().uri("/client")
142+
.exchange()
143+
.expectStatus().isOk()
144+
.expectBody(String.class).isEqualTo("test-subject");
145+
}
146+
137147
@Test
138148
public void oauth2ClientWhenAccessTokenThenUses()
139149
throws Exception {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ public void oauth2ClientWhenClientRegistrationConsumerThenUses()
123123
.andExpect(content().string("client-id"));
124124
}
125125

126+
@Test
127+
public void oauth2ClientWhenPrincipalNameThenUses() throws Exception {
128+
this.mvc.perform(get("/principal-name")
129+
.with(oauth2Client("registration-id").principalName("test-subject")))
130+
.andExpect(content().string("test-subject"));
131+
}
132+
126133
@Test
127134
public void oauth2ClientWhenAccessTokenThenUses() throws Exception {
128135
OAuth2AccessToken accessToken = noScopes();
@@ -161,6 +168,11 @@ String accessToken(@RegisteredOAuth2AuthorizedClient("registration-id") OAuth2Au
161168
return authorizedClient.getAccessToken().getTokenValue();
162169
}
163170

171+
@GetMapping("/principal-name")
172+
String principalName(@RegisteredOAuth2AuthorizedClient("registration-id") OAuth2AuthorizedClient authorizedClient) {
173+
return authorizedClient.getPrincipalName();
174+
}
175+
164176
@GetMapping("/client-id")
165177
String clientId(@RegisteredOAuth2AuthorizedClient("registration-id") OAuth2AuthorizedClient authorizedClient) {
166178
return authorizedClient.getClientRegistration().getClientId();

0 commit comments

Comments
 (0)