Skip to content

Commit 6715dfb

Browse files
committed
security-openid example add integration test for notifyProvider=true
1 parent c7753cf commit 6715dfb

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.superbiz.openid;
18+
19+
import jakarta.servlet.ServletException;
20+
import jakarta.servlet.annotation.WebServlet;
21+
import jakarta.servlet.http.HttpServlet;
22+
import jakarta.servlet.http.HttpServletRequest;
23+
import jakarta.servlet.http.HttpServletResponse;
24+
25+
import java.io.IOException;
26+
27+
@WebServlet(name = "Logout Servlet", urlPatterns = "/logout")
28+
public class LogoutServlet extends HttpServlet {
29+
@Override
30+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
31+
if (req.getUserPrincipal() != null) {
32+
req.logout();
33+
return;
34+
}
35+
36+
resp.getWriter().write("logged out");
37+
}
38+
}

examples/security-openid/src/main/java/org/superbiz/openid/SecuredServlet.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.superbiz.openid;
1818

1919
import jakarta.security.enterprise.authentication.mechanism.http.OpenIdAuthenticationMechanismDefinition;
20+
import jakarta.security.enterprise.authentication.mechanism.http.openid.LogoutDefinition;
2021
import jakarta.servlet.ServletException;
2122
import jakarta.servlet.annotation.HttpConstraint;
2223
import jakarta.servlet.annotation.ServletSecurity;
@@ -33,7 +34,8 @@
3334
clientId = "#{openIdConfig.clientId}",
3435
clientSecret = "#{openIdConfig.clientSecret}",
3536
useSession = false,
36-
redirectToOriginalResource = true)
37+
redirectToOriginalResource = true,
38+
logout = @LogoutDefinition(notifyProvider = true, redirectURI = "#{baseURL}/logout"))
3739
@ServletSecurity(@HttpConstraint(rolesAllowed = "user"))
3840
@WebServlet(name = "Secured Servlet", urlPatterns = "/secured")
3941
public class SecuredServlet extends HttpServlet {

examples/security-openid/src/test/java/org/superbiz/openid/SecuredServletTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static WebArchive createDeployment() {
5252
+ "openid.client-secret = tomee-client-secret\n";
5353

5454
return ShrinkWrap.create(WebArchive.class, "ROOT.war")
55-
.addClasses(SecuredServlet.class, OpenIdConfig.class)
55+
.addClasses(SecuredServlet.class, LogoutServlet.class, OpenIdConfig.class)
5656
.addAsResource("META-INF/beans.xml")
5757
.addAsResource(new StringAsset(mpConfig), "META-INF/microprofile-config.properties");
5858
}
@@ -91,4 +91,25 @@ public void adminRoleMapped() throws Exception {
9191
assertEquals("Hello, tomee-admin\nYou're an admin!\nRequest parameters: ", securedServletPage.getContent());
9292
}
9393
}
94+
95+
@Test
96+
@RunAsClient
97+
public void testLogoutNotifyProvider() throws Exception {
98+
try (WebClient webClient = new WebClient()) {
99+
// Login and logout again
100+
HtmlPage htmlPage = webClient.getPage(url + "/secured");
101+
assertTrue(htmlPage.getUrl().toString().startsWith(KEYCLOAK_CONTAINER.getAuthServerUrl() + "/realms/tomee/protocol/openid-connect/auth"));
102+
103+
HtmlForm loginForm = htmlPage.getForms().get(0);
104+
loginForm.getInputByName("username").setValue("tomee-user");
105+
loginForm.getInputByName("password").setValue("tomee");
106+
loginForm.getButtonByName("login").click();
107+
108+
webClient.getPage(url + "/logout");
109+
110+
// Try to log in again, assert that the keycloak login is showing (= provider has been notified of logout)
111+
HtmlPage securedAgain = webClient.getPage(url + "/secured");
112+
assertTrue(securedAgain.getUrl().toString().startsWith(KEYCLOAK_CONTAINER.getAuthServerUrl() + "/realms/tomee/protocol/openid-connect/auth"));
113+
}
114+
}
94115
}

0 commit comments

Comments
 (0)