Skip to content

Commit 689fc9d

Browse files
committed
Align Test Support Claims
Make all sub claims 'user' and all scopes 'read' to align with existing support for JWT Issue gh-7828 Issue gh-7789 Issue gh-7680 Issue gh-7618
1 parent 30adabb commit 689fc9d

File tree

9 files changed

+32
-32
lines changed

9 files changed

+32
-32
lines changed

samples/boot/oauth2login-webflux/src/test/java/sample/OAuth2LoginControllerTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,6 @@ public void setup() {
7979
public void indexGreetsAuthenticatedUser() {
8080
this.rest.mutateWith(mockOAuth2Login())
8181
.get().uri("/").exchange()
82-
.expectBody(String.class).value(containsString("test-subject"));
82+
.expectBody(String.class).value(containsString("user"));
8383
}
8484
}

samples/boot/oauth2login/src/integration-test/java/sample/OAuth2LoginApplicationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,9 @@ public void requestAuthorizationCodeGrantWhenInvalidStateParamThenDisplayLoginPa
263263
public void requestWhenMockOAuth2LoginThenIndex() throws Exception {
264264
ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId("github");
265265
this.mvc.perform(get("/").with(oauth2Login().clientRegistration(clientRegistration)))
266-
.andExpect(model().attribute("userName", "test-subject"))
266+
.andExpect(model().attribute("userName", "user"))
267267
.andExpect(model().attribute("clientName", "GitHub"))
268-
.andExpect(model().attribute("userAttributes", Collections.singletonMap("sub", "test-subject")));
268+
.andExpect(model().attribute("userAttributes", Collections.singletonMap("sub", "user")));
269269
}
270270

271271
private void assertLoginPage(HtmlPage page) {

samples/boot/oauth2login/src/test/java/sample/web/OAuth2LoginControllerTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public OAuth2AuthorizedClientRepository authorizedClientRepository() {
6464
@Test
6565
public void rootWhenAuthenticatedReturnsUserAndClient() throws Exception {
6666
this.mvc.perform(get("/").with(oauth2Login()))
67-
.andExpect(model().attribute("userName", "test-subject"))
67+
.andExpect(model().attribute("userName", "user"))
6868
.andExpect(model().attribute("clientName", "test"))
69-
.andExpect(model().attribute("userAttributes", Collections.singletonMap("sub", "test-subject")));
69+
.andExpect(model().attribute("userAttributes", Collections.singletonMap("sub", "user")));
7070
}
7171

7272
@Test

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public static OpaqueTokenMutator mockOpaqueToken() {
185185
*/
186186
public static OAuth2LoginMutator mockOAuth2Login() {
187187
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, "access-token",
188-
null, null, Collections.singleton("user"));
188+
null, null, Collections.singleton("read"));
189189
return new OAuth2LoginMutator(accessToken);
190190
}
191191

@@ -200,7 +200,7 @@ public static OAuth2LoginMutator mockOAuth2Login() {
200200
*/
201201
public static OidcLoginMutator mockOidcLogin() {
202202
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, "access-token",
203-
null, null, Collections.singleton("user"));
203+
null, null, Collections.singleton("read"));
204204
return new OidcLoginMutator(accessToken);
205205
}
206206

@@ -844,7 +844,7 @@ private Collection<GrantedAuthority> defaultAuthorities() {
844844

845845
private Map<String, Object> defaultAttributes() {
846846
Map<String, Object> attributes = new HashMap<>();
847-
attributes.put(this.nameAttributeKey, "test-subject");
847+
attributes.put(this.nameAttributeKey, "user");
848848
return attributes;
849849
}
850850

@@ -907,7 +907,7 @@ public OidcLoginMutator authorities(GrantedAuthority... authorities) {
907907
*/
908908
public OidcLoginMutator idToken(Consumer<OidcIdToken.Builder> idTokenBuilderConsumer) {
909909
OidcIdToken.Builder builder = OidcIdToken.withTokenValue("id-token");
910-
builder.subject("test-subject");
910+
builder.subject("user");
911911
idTokenBuilderConsumer.accept(builder);
912912
this.idToken = builder.build();
913913
this.oidcUser = this::defaultPrincipal;
@@ -1018,7 +1018,7 @@ private Collection<GrantedAuthority> getAuthorities() {
10181018

10191019
private OidcIdToken getOidcIdToken() {
10201020
if (this.idToken == null) {
1021-
return new OidcIdToken("id-token", null, null, Collections.singletonMap(IdTokenClaimNames.SUB, "test-subject"));
1021+
return new OidcIdToken("id-token", null, null, Collections.singletonMap(IdTokenClaimNames.SUB, "user"));
10221022
} else {
10231023
return this.idToken;
10241024
}
@@ -1041,7 +1041,7 @@ public final static class OAuth2ClientMutator implements WebTestClientConfigurer
10411041
private String registrationId = "test";
10421042
private ClientRegistration clientRegistration;
10431043
private OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
1044-
"access-token", null, null, Collections.singleton("user"));
1044+
"access-token", null, null, Collections.singleton("read"));
10451045

10461046
private ServerOAuth2AuthorizedClientRepository authorizedClientRepository =
10471047
new WebSessionServerOAuth2AuthorizedClientRepository();
@@ -1122,7 +1122,7 @@ private OAuth2AuthorizedClient getClient() {
11221122
throw new IllegalArgumentException("Please specify a ClientRegistration via one " +
11231123
"of the clientRegistration methods");
11241124
}
1125-
return new OAuth2AuthorizedClient(this.clientRegistration, "test-subject", this.accessToken);
1125+
return new OAuth2AuthorizedClient(this.clientRegistration, "user", this.accessToken);
11261126
}
11271127

11281128
private ClientRegistration.Builder clientRegistrationBuilder() {

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ public static RequestPostProcessor httpBasic(String username, String password) {
398398
*/
399399
public static OAuth2LoginRequestPostProcessor oauth2Login() {
400400
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, "access-token",
401-
null, null, Collections.singleton("user"));
401+
null, null, Collections.singleton("read"));
402402
return new OAuth2LoginRequestPostProcessor(accessToken);
403403
}
404404

@@ -428,7 +428,7 @@ public static OAuth2LoginRequestPostProcessor oauth2Login() {
428428
*/
429429
public static OidcLoginRequestPostProcessor oidcLogin() {
430430
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, "access-token",
431-
null, null, Collections.singleton("user"));
431+
null, null, Collections.singleton("read"));
432432
return new OidcLoginRequestPostProcessor(accessToken);
433433
}
434434

@@ -1435,7 +1435,7 @@ private Collection<GrantedAuthority> defaultAuthorities() {
14351435

14361436
private Map<String, Object> defaultAttributes() {
14371437
Map<String, Object> attributes = new HashMap<>();
1438-
attributes.put(this.nameAttributeKey, "test-subject");
1438+
attributes.put(this.nameAttributeKey, "user");
14391439
return attributes;
14401440
}
14411441

@@ -1495,7 +1495,7 @@ public OidcLoginRequestPostProcessor authorities(GrantedAuthority... authorities
14951495
*/
14961496
public OidcLoginRequestPostProcessor idToken(Consumer<OidcIdToken.Builder> idTokenBuilderConsumer) {
14971497
OidcIdToken.Builder builder = OidcIdToken.withTokenValue("id-token");
1498-
builder.subject("test-subject");
1498+
builder.subject("user");
14991499
idTokenBuilderConsumer.accept(builder);
15001500
this.idToken = builder.build();
15011501
this.oidcUser = this::defaultPrincipal;
@@ -1577,7 +1577,7 @@ private Collection<GrantedAuthority> getAuthorities() {
15771577
private OidcIdToken getOidcIdToken() {
15781578
if (this.idToken == null) {
15791579
return new OidcIdToken("id-token", null, null,
1580-
Collections.singletonMap(IdTokenClaimNames.SUB, "test-subject"));
1580+
Collections.singletonMap(IdTokenClaimNames.SUB, "user"));
15811581
} else {
15821582
return this.idToken;
15831583
}
@@ -1600,7 +1600,7 @@ public final static class OAuth2ClientRequestPostProcessor implements RequestPos
16001600
private String registrationId = "test";
16011601
private ClientRegistration clientRegistration;
16021602
private OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
1603-
"access-token", null, null, Collections.singleton("user"));
1603+
"access-token", null, null, Collections.singleton("read"));
16041604

16051605
private OAuth2ClientRequestPostProcessor() {
16061606
}
@@ -1654,7 +1654,7 @@ public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request)
16541654
"of the clientRegistration methods");
16551655
}
16561656
OAuth2AuthorizedClient client = new OAuth2AuthorizedClient
1657-
(this.clientRegistration, "test-subject", this.accessToken);
1657+
(this.clientRegistration, "user", this.accessToken);
16581658
OAuth2AuthorizedClientRepository authorizedClientRepository =
16591659
new HttpSessionOAuth2AuthorizedClientRepository();
16601660
authorizedClientRepository.saveAuthorizedClient(client, null, request, new MockHttpServletResponse());

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ public void oauth2LoginWhenUsingDefaultsThenProducesDefaultAuthentication() {
8686
assertThat(token.getAuthorizedClientRegistrationId()).isEqualTo("test");
8787
assertThat(token.getPrincipal()).isInstanceOf(OAuth2User.class);
8888
assertThat(token.getPrincipal().getAttributes())
89-
.containsEntry("sub", "test-subject");
89+
.containsEntry("sub", "user");
9090
assertThat((Collection<GrantedAuthority>) token.getPrincipal().getAuthorities())
91-
.contains(new SimpleGrantedAuthority("SCOPE_user"));
91+
.contains(new SimpleGrantedAuthority("SCOPE_read"));
9292
}
9393

9494
@Test
@@ -134,7 +134,7 @@ public void oauth2LoginWhenAttributeSpecifiedThenUserHasAttribute() {
134134
@Test
135135
public void oauth2LoginWhenOAuth2UserSpecifiedThenLastCalledTakesPrecedence() throws Exception {
136136
OAuth2User oauth2User = new DefaultOAuth2User(
137-
AuthorityUtils.createAuthorityList("SCOPE_user"),
137+
AuthorityUtils.createAuthorityList("SCOPE_read"),
138138
Collections.singletonMap("sub", "subject"),
139139
"sub");
140140

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ public void oidcLoginWhenUsingDefaultsThenProducesDefaultAuthentication() {
8686
assertThat(token.getAuthorizedClientRegistrationId()).isEqualTo("test");
8787
assertThat(token.getPrincipal()).isInstanceOf(OidcUser.class);
8888
assertThat(token.getPrincipal().getAttributes())
89-
.containsEntry("sub", "test-subject");
89+
.containsEntry("sub", "user");
9090
assertThat((Collection<GrantedAuthority>) token.getPrincipal().getAuthorities())
91-
.contains(new SimpleGrantedAuthority("SCOPE_user"));
91+
.contains(new SimpleGrantedAuthority("SCOPE_read"));
9292
assertThat(((OidcUser) token.getPrincipal()).getIdToken().getTokenValue())
9393
.isEqualTo("id-token");
9494
}
@@ -150,7 +150,7 @@ public void oidcLoginWhenUserInfoSpecifiedThenUserHasClaims() throws Exception {
150150
@Test
151151
public void oidcLoginWhenOidcUserSpecifiedThenLastCalledTakesPrecedence() throws Exception {
152152
OidcUser oidcUser = new DefaultOidcUser(
153-
AuthorityUtils.createAuthorityList("SCOPE_user"), idToken().build());
153+
AuthorityUtils.createAuthorityList("SCOPE_read"), idToken().build());
154154

155155
this.client.mutateWith(mockOidcLogin()
156156
.idToken(i -> i.subject("foo"))

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void oauth2LoginWhenUsingDefaultsThenProducesDefaultAuthentication()
9090
throws Exception {
9191

9292
this.mvc.perform(get("/name").with(oauth2Login()))
93-
.andExpect(content().string("test-subject"));
93+
.andExpect(content().string("user"));
9494
this.mvc.perform(get("/admin/id-token/name").with(oauth2Login()))
9595
.andExpect(status().isForbidden());
9696
}
@@ -120,7 +120,7 @@ public void oauth2LoginWhenAttributeSpecifiedThenUserHasAttribute() throws Excep
120120
@Test
121121
public void oauth2LoginWhenNameSpecifiedThenUserHasName() throws Exception {
122122
OAuth2User oauth2User = new DefaultOAuth2User(
123-
AuthorityUtils.commaSeparatedStringToAuthorityList("SCOPE_user"),
123+
AuthorityUtils.commaSeparatedStringToAuthorityList("SCOPE_read"),
124124
Collections.singletonMap("custom-attribute", "test-subject"),
125125
"custom-attribute");
126126
this.mvc.perform(get("/attributes/custom-attribute")
@@ -142,7 +142,7 @@ public void oauth2LoginWhenClientRegistrationSpecifiedThenUses() throws Exceptio
142142
@Test
143143
public void oauth2LoginWhenOAuth2UserSpecifiedThenLastCalledTakesPrecedence() throws Exception {
144144
OAuth2User oauth2User = new DefaultOAuth2User(
145-
AuthorityUtils.createAuthorityList("SCOPE_user"),
145+
AuthorityUtils.createAuthorityList("SCOPE_read"),
146146
Collections.singletonMap("username", "user"),
147147
"username");
148148

@@ -167,7 +167,7 @@ protected void configure(HttpSecurity http) throws Exception {
167167
http
168168
.authorizeRequests(authorize -> authorize
169169
.mvcMatchers("/admin/**").hasAuthority("SCOPE_admin")
170-
.anyRequest().hasAuthority("SCOPE_user")
170+
.anyRequest().hasAuthority("SCOPE_read")
171171
).oauth2Login();
172172
}
173173

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void oidcLoginWhenUsingDefaultsThenProducesDefaultAuthentication()
9595
throws Exception {
9696

9797
this.mvc.perform(get("/name").with(oidcLogin()))
98-
.andExpect(content().string("test-subject"));
98+
.andExpect(content().string("user"));
9999
this.mvc.perform(get("/admin/id-token/name").with(oidcLogin()))
100100
.andExpect(status().isForbidden());
101101
}
@@ -133,7 +133,7 @@ public void oidcLoginWhenUserInfoSpecifiedThenUserHasClaims() throws Exception {
133133
@Test
134134
public void oidcLoginWhenOidcUserSpecifiedThenLastCalledTakesPrecedence() throws Exception {
135135
OidcUser oidcUser = new DefaultOidcUser(
136-
AuthorityUtils.createAuthorityList("SCOPE_user"), idToken().build());
136+
AuthorityUtils.createAuthorityList("SCOPE_read"), idToken().build());
137137

138138
this.mvc.perform(get("/id-token/sub")
139139
.with(oidcLogin()
@@ -156,7 +156,7 @@ protected void configure(HttpSecurity http) throws Exception {
156156
http
157157
.authorizeRequests()
158158
.mvcMatchers("/admin/**").hasAuthority("SCOPE_admin")
159-
.anyRequest().hasAuthority("SCOPE_user")
159+
.anyRequest().hasAuthority("SCOPE_read")
160160
.and()
161161
.oauth2Login();
162162
}

0 commit comments

Comments
 (0)