Skip to content

Commit 73003d5

Browse files
Enkoszjzheaux
authored andcommitted
OAuth 2.0 logout handler resolves uri placeholders
- OidcClientInitiatedLogoutSuccessHandler can automatically resolve placeholders like baseUrl and registrationId inside the postLogoutRedirectUri Issue gh-7900
1 parent fabeabd commit 73003d5

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -18,7 +18,8 @@
1818

1919
import java.net.URI;
2020
import java.nio.charset.StandardCharsets;
21-
import java.util.Collections;
21+
import java.util.HashMap;
22+
import java.util.Map;
2223

2324
import javax.servlet.http.HttpServletRequest;
2425
import javax.servlet.http.HttpServletResponse;
@@ -67,7 +68,7 @@ protected String determineTargetUrl(HttpServletRequest request, HttpServletRespo
6768
URI endSessionEndpoint = this.endSessionEndpoint(clientRegistration);
6869
if (endSessionEndpoint != null) {
6970
String idToken = idToken(authentication);
70-
String postLogoutRedirectUri = postLogoutRedirectUri(request);
71+
String postLogoutRedirectUri = postLogoutRedirectUri(request, clientRegistration);
7172
targetUrl = endpointUri(endSessionEndpoint, idToken, postLogoutRedirectUri);
7273
}
7374
}
@@ -89,7 +90,7 @@ private String idToken(Authentication authentication) {
8990
return ((OidcUser) authentication.getPrincipal()).getIdToken().getTokenValue();
9091
}
9192

92-
private String postLogoutRedirectUri(HttpServletRequest request) {
93+
private String postLogoutRedirectUri(HttpServletRequest request, ClientRegistration clientRegistration) {
9394
if (this.postLogoutRedirectUri == null) {
9495
return null;
9596
}
@@ -100,8 +101,13 @@ private String postLogoutRedirectUri(HttpServletRequest request) {
100101
.replaceQuery(null)
101102
.fragment(null)
102103
.build();
104+
105+
Map<String, String> uriVariables = new HashMap<>();
106+
uriVariables.put("baseUrl", uriComponents.toUriString());
107+
uriVariables.put("registrationId", clientRegistration.getRegistrationId());
108+
103109
return UriComponentsBuilder.fromUriString(this.postLogoutRedirectUri)
104-
.buildAndExpand(Collections.singletonMap("baseUrl", uriComponents.toUriString()))
110+
.buildAndExpand(uriVariables)
105111
.toUriString();
106112
// @formatter:on
107113
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -138,6 +138,22 @@ public void logoutWhenUsingPostLogoutRedirectUriTemplateThenBuildsItForRedirect(
138138
"https://endpoint?" + "id_token_hint=id-token&" + "post_logout_redirect_uri=https://rp.example.org");
139139
}
140140

141+
@Test
142+
public void logoutWhenUsingPostLogoutRedirectUriTemplateThenBuildsItForRedirectExpanded()
143+
throws IOException, ServletException {
144+
OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(TestOidcUsers.create(),
145+
AuthorityUtils.NO_AUTHORITIES, this.registration.getRegistrationId());
146+
this.handler.setPostLogoutRedirectUri("{baseUrl}/{registrationId}");
147+
this.request.setScheme("https");
148+
this.request.setServerPort(443);
149+
this.request.setServerName("rp.example.org");
150+
this.request.setUserPrincipal(token);
151+
this.handler.onLogoutSuccess(this.request, this.response, token);
152+
assertThat(this.response.getRedirectedUrl()).isEqualTo(String.format(
153+
"https://endpoint?" + "id_token_hint=id-token&" + "post_logout_redirect_uri=https://rp.example.org/%s",
154+
this.registration.getRegistrationId()));
155+
}
156+
141157
// gh-9511
142158
@Test
143159
public void logoutWhenUsingPostLogoutRedirectUriWithQueryParametersThenBuildsItForRedirect()

0 commit comments

Comments
 (0)