Skip to content

Commit

Permalink
For post logout redirect URI - Make '+' represent existing redirect U…
Browse files Browse the repository at this point in the history
…RIs and merge with existing post logout redirect URIs

Closes #25544

Signed-off-by: Joshua Sorah <jsorah@redhat.com>
  • Loading branch information
jsorah authored and mposolda committed Dec 18, 2023
1 parent 367d0a9 commit a10149b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
Expand Down Expand Up @@ -378,7 +380,7 @@ public void setTosUri(String tosUri) {

public List<String> getPostLogoutRedirectUris() {
List<String> postLogoutRedirectUris = getAttributeMultivalued(OIDCConfigAttributes.POST_LOGOUT_REDIRECT_URIS);
if(postLogoutRedirectUris == null || postLogoutRedirectUris.isEmpty() || postLogoutRedirectUris.get(0).equals("+")) {
if(postLogoutRedirectUris == null || postLogoutRedirectUris.isEmpty()) {
if(clientModel != null) {
return new ArrayList(clientModel.getRedirectUris());
}
Expand All @@ -390,6 +392,18 @@ else if(clientRep != null) {
else if(postLogoutRedirectUris.get(0).equals("-")) {
return new ArrayList<String>();
}
else if (postLogoutRedirectUris.contains("+")) {
Set<String> returnedPostLogoutRedirectUris = postLogoutRedirectUris.stream()
.filter(uri -> !"+".equals(uri)).collect(Collectors.toSet());

if(clientModel != null) {
returnedPostLogoutRedirectUris.addAll(clientModel.getRedirectUris());
}
else if(clientRep != null) {
returnedPostLogoutRedirectUris.addAll(clientRep.getRedirectUris());
}
return new ArrayList<>(returnedPostLogoutRedirectUris);
}
else {
return postLogoutRedirectUris;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
*/
public class RPInitiatedLogoutTest extends AbstractTestRealmKeycloakTest {

public static final String DUMMY_POST_LOGOUT_URI = "http://127.0.0.1:4321/thisisatest";

@Rule
public AssertEvents events = new AssertEvents(this);

Expand Down Expand Up @@ -720,6 +722,50 @@ public void logoutWithClientIdIdTokenHintAndPostLogoutRedirectUri() {
assertCurrentUrlEquals(APP_REDIRECT_URI + "?state=something2");
}

@Test
public void logoutWithClientIdAndPostLogoutRedirectUriWhenUsingPostLogoutRedirectUriAndPlusFirst() throws IOException {
doLogoutTestWithPostLogoutRedirectAttributeAndSpecifiedPostLogoutRedirectUri(
String.join(Constants.CFG_DELIMITER,
"+",
DUMMY_POST_LOGOUT_URI),
DUMMY_POST_LOGOUT_URI);
}

@Test
public void logoutWithClientIdAndPostLogoutRedirectUriWhenUsingPostLogoutRedirectUriAndPlusLast() throws IOException {
doLogoutTestWithPostLogoutRedirectAttributeAndSpecifiedPostLogoutRedirectUri(
String.join(Constants.CFG_DELIMITER,
DUMMY_POST_LOGOUT_URI,
"+"),
DUMMY_POST_LOGOUT_URI);
}

@Test
public void logoutWithClientIdAndPostLogoutRedirectUriWhenUsingAppRedirectUriAndAdditionalPostLogoutUriAndPlusFirstAndLast() throws IOException {
doLogoutTestWithPostLogoutRedirectAttributeAndSpecifiedPostLogoutRedirectUri(
String.join(Constants.CFG_DELIMITER,
"+",
DUMMY_POST_LOGOUT_URI,
"+"),
APP_REDIRECT_URI);
}

@Test
public void logoutWithClientIdAndPostLogoutRedirectUriWhenUsingAppRedirectUriAndAdditionalPostLogoutUriAndPlusLast() throws IOException {
doLogoutTestWithPostLogoutRedirectAttributeAndSpecifiedPostLogoutRedirectUri(
String.join(Constants.CFG_DELIMITER,
DUMMY_POST_LOGOUT_URI,
"+"),
APP_REDIRECT_URI);
}

@Test
public void logoutWithClientIdAndPostLogoutRedirectUriWhenWhenUsingAppRedirectUriAndPlus() throws IOException {
doLogoutTestWithPostLogoutRedirectAttributeAndSpecifiedPostLogoutRedirectUri(
"+",
APP_REDIRECT_URI);
}


@Test
public void logoutWithBadClientId() {
Expand Down Expand Up @@ -1142,4 +1188,23 @@ private boolean isSessionActive(String sessionId) {
return false;
}
}

private void doLogoutTestWithPostLogoutRedirectAttributeAndSpecifiedPostLogoutRedirectUri(String postLogoutRedirectAttr, String postLogoutRedirectUri) throws IOException {
try (Closeable accountClientUpdater = ClientAttributeUpdater.forClient(adminClient, "test", "test-app" )
.setAttribute(OIDCConfigAttributes.POST_LOGOUT_REDIRECT_URIS, postLogoutRedirectAttr).update()) {

OAuthClient.AccessTokenResponse tokenResponse = loginUser();

String logoutUrl = oauth.getLogoutUrl().postLogoutRedirectUri(postLogoutRedirectUri).clientId("test-app").build();
driver.navigate().to(logoutUrl);

// Assert logout confirmation page as id_token_hint was not sent. Session still exists. Assert default language on logout page (English)
logoutConfirmPage.assertCurrent();
Assert.assertEquals("English", logoutConfirmPage.getLanguageDropdownText());
MatcherAssert.assertThat(true, is(isSessionActive(tokenResponse.getSessionState())));
events.assertEmpty();

// We don't need to go further as the intent is that other tests will cover redirection
}
}
}

0 comments on commit a10149b

Please sign in to comment.