Skip to content

Commit d85f96a

Browse files
committed
Polish gh-2182
1 parent 125aeb6 commit d85f96a

File tree

1 file changed

+3
-189
lines changed

1 file changed

+3
-189
lines changed

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/config/annotation/web/configurers/OAuth2AuthorizationCodeGrantTests.java

Lines changed: 3 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2024 the original author or authors.
2+
* Copyright 2020-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -232,9 +232,6 @@ public class OAuth2AuthorizationCodeGrantTests {
232232
@Autowired
233233
private OAuth2AuthorizationService authorizationService;
234234

235-
@Autowired
236-
private OAuth2AuthorizationConsentService authorizationConsentService;
237-
238235
@Autowired
239236
private JwtDecoder jwtDecoder;
240237

@@ -692,38 +689,6 @@ public void requestWhenRequiresConsentThenDisplaysConsentPage() throws Exception
692689
assertThat(consentPage).contains(scopeCheckbox("message.write"));
693690
}
694691

695-
@Test
696-
public void requestWhenRequiresConsentThenDisplaysConsentPageWithOnlyNewScope() throws Exception {
697-
this.spring.register(AuthorizationServerConfiguration.class).autowire();
698-
699-
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().scopes((scopes) -> {
700-
scopes.clear();
701-
scopes.add("message.read");
702-
scopes.add("message.write");
703-
}).clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build()).build();
704-
this.registeredClientRepository.save(registeredClient);
705-
706-
OAuth2AuthorizationConsent authorizationConsent = OAuth2AuthorizationConsent
707-
.withId(registeredClient.getId(), "user")
708-
.scope("message.write")
709-
.build();
710-
711-
this.authorizationConsentService.save(authorizationConsent);
712-
713-
String consentPage = this.mvc
714-
.perform(get(DEFAULT_AUTHORIZATION_ENDPOINT_URI)
715-
.queryParams(getAuthorizationRequestParameters(registeredClient))
716-
.with(user("user")))
717-
.andExpect(status().is2xxSuccessful())
718-
.andReturn()
719-
.getResponse()
720-
.getContentAsString();
721-
722-
assertThat(consentPage).contains("Consent required");
723-
assertThat(consentPage).contains(scopeCheckbox("message.read"));
724-
assertThat(consentPage).contains(disabledScopeCheckbox("message.write"));
725-
}
726-
727692
@Test
728693
public void requestWhenConsentRequestThenReturnAccessTokenResponse() throws Exception {
729694
this.spring.register(AuthorizationServerConfiguration.class).autowire();
@@ -781,47 +746,6 @@ public void requestWhenConsentRequestThenReturnAccessTokenResponse() throws Exce
781746
.andReturn();
782747
}
783748

784-
@Test
785-
public void requestWhenCustomConsentPageConfiguredThenRedirectWithAllScopes() throws Exception {
786-
this.spring.register(AuthorizationServerConfigurationCustomConsentPage.class).autowire();
787-
788-
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().scopes((scopes) -> {
789-
scopes.clear();
790-
scopes.add("message.read");
791-
scopes.add("message.write");
792-
}).clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build()).build();
793-
this.registeredClientRepository.save(registeredClient);
794-
795-
OAuth2AuthorizationConsent authorizationConsent = OAuth2AuthorizationConsent
796-
.withId(registeredClient.getId(), "user")
797-
.scope("message.write")
798-
.build();
799-
800-
this.authorizationConsentService.save(authorizationConsent);
801-
802-
MvcResult mvcResult = this.mvc
803-
.perform(get(DEFAULT_AUTHORIZATION_ENDPOINT_URI)
804-
.queryParams(getAuthorizationRequestParameters(registeredClient))
805-
.with(user("user")))
806-
.andExpect(status().is3xxRedirection())
807-
.andReturn();
808-
String redirectedUrl = mvcResult.getResponse().getRedirectedUrl();
809-
assertThat(redirectedUrl).matches("http://localhost/oauth2/consent\\?scope=.+&client_id=.+&state=.+");
810-
811-
String locationHeader = URLDecoder.decode(redirectedUrl, StandardCharsets.UTF_8);
812-
UriComponents uriComponents = UriComponentsBuilder.fromUriString(locationHeader).build();
813-
MultiValueMap<String, String> redirectQueryParams = uriComponents.getQueryParams();
814-
815-
assertThat(uriComponents.getPath()).isEqualTo(consentPage);
816-
assertThat(redirectQueryParams.getFirst(OAuth2ParameterNames.SCOPE)).isEqualTo("message.read message.write");
817-
assertThat(redirectQueryParams.getFirst(OAuth2ParameterNames.CLIENT_ID))
818-
.isEqualTo(registeredClient.getClientId());
819-
820-
String state = extractParameterFromRedirectUri(redirectedUrl, "state");
821-
OAuth2Authorization authorization = this.authorizationService.findByToken(state, STATE_TOKEN_TYPE);
822-
assertThat(authorization).isNotNull();
823-
}
824-
825749
@Test
826750
public void requestWhenCustomConsentPageConfiguredThenRedirect() throws Exception {
827751
this.spring.register(AuthorizationServerConfigurationCustomConsentPage.class).autowire();
@@ -1152,6 +1076,7 @@ public void requestWhenPushedAuthorizationRequestThenReturnAccessTokenResponse()
11521076
.isEqualTo(true);
11531077
}
11541078

1079+
// gh-2182
11551080
@Test
11561081
public void requestWhenPushedAuthorizationRequestAndRequiresConsentThenDisplaysConsentPage() throws Exception {
11571082
this.spring.register(AuthorizationServerConfigurationWithPushedAuthorizationRequests.class).autowire();
@@ -1192,54 +1117,7 @@ public void requestWhenPushedAuthorizationRequestAndRequiresConsentThenDisplaysC
11921117
assertThat(consentPage).contains(scopeCheckbox("message.write"));
11931118
}
11941119

1195-
@Test
1196-
public void requestWhenPushedAuthorizationRequestAndRequiresConsentThenDisplaysConsentPageWithOnlyNewScope()
1197-
throws Exception {
1198-
this.spring.register(AuthorizationServerConfigurationWithPushedAuthorizationRequests.class).autowire();
1199-
1200-
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().scopes((scopes) -> {
1201-
scopes.clear();
1202-
scopes.add("message.read");
1203-
scopes.add("message.write");
1204-
}).clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build()).build();
1205-
this.registeredClientRepository.save(registeredClient);
1206-
1207-
OAuth2AuthorizationConsent authorizationConsent = OAuth2AuthorizationConsent
1208-
.withId(registeredClient.getId(), "user")
1209-
.scope("message.write")
1210-
.build();
1211-
1212-
this.authorizationConsentService.save(authorizationConsent);
1213-
1214-
MvcResult mvcResult = this.mvc
1215-
.perform(post("/oauth2/par").params(getAuthorizationRequestParameters(registeredClient))
1216-
.param(PkceParameterNames.CODE_CHALLENGE, S256_CODE_CHALLENGE)
1217-
.param(PkceParameterNames.CODE_CHALLENGE_METHOD, "S256")
1218-
.header(HttpHeaders.AUTHORIZATION, getAuthorizationHeader(registeredClient)))
1219-
.andExpect(header().string(HttpHeaders.CACHE_CONTROL, containsString("no-store")))
1220-
.andExpect(header().string(HttpHeaders.PRAGMA, containsString("no-cache")))
1221-
.andExpect(status().isCreated())
1222-
.andExpect(jsonPath("$.request_uri").isNotEmpty())
1223-
.andExpect(jsonPath("$.expires_in").isNotEmpty())
1224-
.andReturn();
1225-
1226-
String requestUri = JsonPath.read(mvcResult.getResponse().getContentAsString(), "$.request_uri");
1227-
1228-
String consentPage = this.mvc
1229-
.perform(get(DEFAULT_AUTHORIZATION_ENDPOINT_URI)
1230-
.queryParam(OAuth2ParameterNames.CLIENT_ID, registeredClient.getClientId())
1231-
.queryParam(OAuth2ParameterNames.REQUEST_URI, requestUri)
1232-
.with(user("user")))
1233-
.andExpect(status().is2xxSuccessful())
1234-
.andReturn()
1235-
.getResponse()
1236-
.getContentAsString();
1237-
1238-
assertThat(consentPage).contains("Consent required");
1239-
assertThat(consentPage).contains(scopeCheckbox("message.read"));
1240-
assertThat(consentPage).contains(disabledScopeCheckbox("message.write"));
1241-
}
1242-
1120+
// gh-2182
12431121
@Test
12441122
public void requestWhenPushedAuthorizationRequestAndCustomConsentPageConfiguredThenRedirect() throws Exception {
12451123
this.spring.register(AuthorizationServerConfigurationWithPushedAuthorizationRequestsAndCustomConsentPage.class)
@@ -1290,64 +1168,6 @@ public void requestWhenPushedAuthorizationRequestAndCustomConsentPageConfiguredT
12901168
assertThat(authorization).isNotNull();
12911169
}
12921170

1293-
@Test
1294-
public void requestWhenPushedAuthorizationRequestAndCustomConsentPageConfiguredThenRedirectWithAllScopes()
1295-
throws Exception {
1296-
this.spring.register(AuthorizationServerConfigurationWithPushedAuthorizationRequestsAndCustomConsentPage.class)
1297-
.autowire();
1298-
1299-
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().scopes((scopes) -> {
1300-
scopes.clear();
1301-
scopes.add("message.read");
1302-
scopes.add("message.write");
1303-
}).clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build()).build();
1304-
this.registeredClientRepository.save(registeredClient);
1305-
1306-
OAuth2AuthorizationConsent authorizationConsent = OAuth2AuthorizationConsent
1307-
.withId(registeredClient.getId(), "user")
1308-
.scope("message.write")
1309-
.build();
1310-
1311-
this.authorizationConsentService.save(authorizationConsent);
1312-
1313-
MvcResult mvcResult = this.mvc
1314-
.perform(post("/oauth2/par").params(getAuthorizationRequestParameters(registeredClient))
1315-
.param(PkceParameterNames.CODE_CHALLENGE, S256_CODE_CHALLENGE)
1316-
.param(PkceParameterNames.CODE_CHALLENGE_METHOD, "S256")
1317-
.header(HttpHeaders.AUTHORIZATION, getAuthorizationHeader(registeredClient)))
1318-
.andExpect(header().string(HttpHeaders.CACHE_CONTROL, containsString("no-store")))
1319-
.andExpect(header().string(HttpHeaders.PRAGMA, containsString("no-cache")))
1320-
.andExpect(status().isCreated())
1321-
.andExpect(jsonPath("$.request_uri").isNotEmpty())
1322-
.andExpect(jsonPath("$.expires_in").isNotEmpty())
1323-
.andReturn();
1324-
1325-
String requestUri = JsonPath.read(mvcResult.getResponse().getContentAsString(), "$.request_uri");
1326-
1327-
mvcResult = this.mvc
1328-
.perform(get(DEFAULT_AUTHORIZATION_ENDPOINT_URI)
1329-
.queryParam(OAuth2ParameterNames.CLIENT_ID, registeredClient.getClientId())
1330-
.queryParam(OAuth2ParameterNames.REQUEST_URI, requestUri)
1331-
.with(user("user")))
1332-
.andExpect(status().is3xxRedirection())
1333-
.andReturn();
1334-
String redirectedUrl = mvcResult.getResponse().getRedirectedUrl();
1335-
assertThat(redirectedUrl).matches("http://localhost/oauth2/consent\\?scope=.+&client_id=.+&state=.+");
1336-
1337-
String locationHeader = URLDecoder.decode(redirectedUrl, StandardCharsets.UTF_8);
1338-
UriComponents uriComponents = UriComponentsBuilder.fromUriString(locationHeader).build();
1339-
MultiValueMap<String, String> redirectQueryParams = uriComponents.getQueryParams();
1340-
1341-
assertThat(uriComponents.getPath()).isEqualTo(consentPage);
1342-
assertThat(redirectQueryParams.getFirst(OAuth2ParameterNames.SCOPE)).isEqualTo("message.read message.write");
1343-
assertThat(redirectQueryParams.getFirst(OAuth2ParameterNames.CLIENT_ID))
1344-
.isEqualTo(registeredClient.getClientId());
1345-
1346-
String state = extractParameterFromRedirectUri(redirectedUrl, "state");
1347-
OAuth2Authorization authorization = this.authorizationService.findByToken(state, STATE_TOKEN_TYPE);
1348-
assertThat(authorization).isNotNull();
1349-
}
1350-
13511171
private static String generateDPoPProof(String tokenEndpointUri) {
13521172
// @formatter:off
13531173
Map<String, Object> publicJwk = TestJwks.DEFAULT_EC_JWK
@@ -1404,12 +1224,6 @@ private static String scopeCheckbox(String scope) {
14041224
"<input class=\"form-check-input\" type=\"checkbox\" name=\"scope\" value=\"{0}\" id=\"{0}\">", scope);
14051225
}
14061226

1407-
private static String disabledScopeCheckbox(String scope) {
1408-
return MessageFormat.format(
1409-
"<input class=\"form-check-input\" type=\"checkbox\" name=\"scope\" id=\"{0}\" checked disabled>",
1410-
scope);
1411-
}
1412-
14131227
private String extractParameterFromRedirectUri(String redirectUri, String param)
14141228
throws UnsupportedEncodingException {
14151229
String locationHeader = URLDecoder.decode(redirectUri, StandardCharsets.UTF_8.name());

0 commit comments

Comments
 (0)