mirror of https://github.com/jacekkow/keycloak-protocol-cas

github-actions
2023-02-24 70cbe305478ffbf7644ddde461ce1eec71cb4a27
commit | author | age
7f7e0c 1 package org.keycloak.protocol.cas.endpoints;
MP 2
3 import org.jboss.logging.Logger;
4 import org.jboss.resteasy.annotations.cache.NoCache;
5 import org.jboss.resteasy.spi.HttpRequest;
6 import org.keycloak.common.ClientConnection;
b88dc3 7 import org.keycloak.events.Details;
AP 8 import org.keycloak.events.Errors;
7f7e0c 9 import org.keycloak.events.EventBuilder;
b88dc3 10 import org.keycloak.events.EventType;
4a6620 11 import org.keycloak.models.ClientModel;
7f7e0c 12 import org.keycloak.models.KeycloakSession;
MP 13 import org.keycloak.models.RealmModel;
14 import org.keycloak.models.UserSessionModel;
15 import org.keycloak.protocol.cas.CASLoginProtocol;
4a6620 16 import org.keycloak.protocol.oidc.utils.RedirectUtils;
MP 17 import org.keycloak.services.ErrorPage;
7f7e0c 18 import org.keycloak.services.managers.AuthenticationManager;
4a6620 19 import org.keycloak.services.messages.Messages;
7f7e0c 20
MP 21 import javax.ws.rs.GET;
4a6620 22 import javax.ws.rs.QueryParam;
7f7e0c 23 import javax.ws.rs.core.Context;
MP 24 import javax.ws.rs.core.HttpHeaders;
25 import javax.ws.rs.core.Response;
26
27 public class LogoutEndpoint {
57a6c1 28     private static final Logger logger = Logger.getLogger(LogoutEndpoint.class);
7f7e0c 29
MP 30     @Context
31     private KeycloakSession session;
32
33     @Context
34     private ClientConnection clientConnection;
35
36     @Context
37     private HttpRequest request;
38
39     @Context
40     private HttpHeaders headers;
41
42     private RealmModel realm;
4a6620 43     private ClientModel client;
MP 44     private String redirectUri;
7f7e0c 45
b88dc3 46     public LogoutEndpoint(RealmModel realm) {
7f7e0c 47         this.realm = realm;
MP 48     }
49
50     @GET
51     @NoCache
4a6620 52     public Response logout(@QueryParam(CASLoginProtocol.SERVICE_PARAM) String service) {
MP 53         checkClient(service);
7f7e0c 54
MP 55         AuthenticationManager.AuthResult authResult = AuthenticationManager.authenticateIdentityCookie(session, realm, false);
56         if (authResult != null) {
57             UserSessionModel userSession = authResult.getSession();
58             userSession.setNote(AuthenticationManager.KEYCLOAK_LOGOUT_PROTOCOL, CASLoginProtocol.LOGIN_PROTOCOL);
b88dc3 59
AP 60             if (redirectUri != null) {
61                 userSession.setNote(CASLoginProtocol.LOGOUT_REDIRECT_URI, redirectUri);
62             }
7f7e0c 63
MP 64             logger.debug("Initiating CAS browser logout");
d5f868 65             Response response =  AuthenticationManager.browserLogout(session, realm, authResult.getSession(), session.getContext().getUri(), clientConnection, headers);
7f7e0c 66             logger.debug("finishing CAS browser logout");
MP 67             return response;
68         }
6638b8 69         return ErrorPage.error(session, null, Response.Status.BAD_REQUEST, Messages.FAILED_LOGOUT);
4a6620 70     }
MP 71
72     private void checkClient(String service) {
73         if (service == null) {
74             return;
75         }
76
ea9555 77         client = realm.getClientsStream()
4a6620 78                 .filter(c -> CASLoginProtocol.LOGIN_PROTOCOL.equals(c.getProtocol()))
019db5 79                 .filter(c -> RedirectUtils.verifyRedirectUri(session, service, c) != null)
4a6620 80                 .findFirst().orElse(null);
MP 81         if (client != null) {
019db5 82             redirectUri = RedirectUtils.verifyRedirectUri(session, service, client);
4a6620 83
MP 84             session.getContext().setClient(client);
85         }
7f7e0c 86     }
MP 87 }