Skip to content

Commit 6d816fb

Browse files
committed
Polish postLogoutRedirectUri encoding
Issue gh-9511
1 parent e52b104 commit 6d816fb

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/oidc/web/logout/OidcClientInitiatedLogoutSuccessHandler.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected String determineTargetUrl(HttpServletRequest request,
6363
endSessionEndpoint = this.endSessionEndpoint(clientRegistration);
6464
if (endSessionEndpoint != null) {
6565
String idToken = idToken(authentication);
66-
URI postLogoutRedirectUri = postLogoutRedirectUri(request);
66+
String postLogoutRedirectUri = postLogoutRedirectUri(request);
6767
targetUrl = endpointUri(endSessionEndpoint, idToken, postLogoutRedirectUri);
6868
}
6969
}
@@ -91,7 +91,7 @@ private String idToken(Authentication authentication) {
9191
return ((OidcUser) authentication.getPrincipal()).getIdToken().getTokenValue();
9292
}
9393

94-
private URI postLogoutRedirectUri(HttpServletRequest request) {
94+
private String postLogoutRedirectUri(HttpServletRequest request) {
9595
if (this.postLogoutRedirectUri == null) {
9696
return null;
9797
}
@@ -100,13 +100,12 @@ private URI postLogoutRedirectUri(HttpServletRequest request) {
100100
.replaceQuery(null)
101101
.fragment(null)
102102
.build();
103-
return URI.create (UriComponentsBuilder.fromUriString(this.postLogoutRedirectUri)
103+
return UriComponentsBuilder.fromUriString(this.postLogoutRedirectUri)
104104
.buildAndExpand(Collections.singletonMap("baseUrl", uriComponents.toUriString()))
105-
.toUriString());
105+
.toUriString();
106106
}
107107

108-
109-
private String endpointUri(URI endSessionEndpoint, String idToken, URI postLogoutRedirectUri) {
108+
private String endpointUri(URI endSessionEndpoint, String idToken, String postLogoutRedirectUri) {
110109
UriComponentsBuilder builder = UriComponentsBuilder.fromUri(endSessionEndpoint);
111110
builder.queryParam("id_token_hint", idToken);
112111
if (postLogoutRedirectUri != null) {

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/oidc/web/logout/OidcClientInitiatedLogoutSuccessHandlerTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,17 @@ public void logoutWhenUsingPostLogoutRedirectUriTemplateThenBuildsItForRedirect(
165165
"post_logout_redirect_uri=https://rp.example.org");
166166
}
167167

168+
// gh-9511
168169
@Test
169-
public void logoutWhenUsingPostLogoutRedirectUriWithQueryParametersThenBuildItForRedirectWithEncodedQueryParameters() throws IOException, ServletException {
170+
public void logoutWhenUsingPostLogoutRedirectUriWithQueryParametersThenBuildsItForRedirect()
171+
throws IOException, ServletException {
170172
OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(TestOidcUsers.create(),
171173
AuthorityUtils.NO_AUTHORITIES, this.registration.getRegistrationId());
172174
this.handler.setPostLogoutRedirectUri("https://rp.example.org/context?forwardUrl=secured%3Fparam%3Dtrue");
173175
this.request.setUserPrincipal(token);
174176
this.handler.onLogoutSuccess(this.request, this.response, token);
175-
assertThat(this.response.getRedirectedUrl()).isEqualTo(
176-
"https://endpoint?" + "id_token_hint=id-token&" + "post_logout_redirect_uri=https://rp.example.org/context?forwardUrl%3Dsecured%253Fparam%253Dtrue");
177+
assertThat(this.response.getRedirectedUrl()).isEqualTo("https://endpoint?id_token_hint=id-token&"
178+
+ "post_logout_redirect_uri=https://rp.example.org/context?forwardUrl%3Dsecured%253Fparam%253Dtrue");
177179
}
178180

179181
@Test

0 commit comments

Comments
 (0)