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

Jacek Kowalski
8 days ago 29db863fcc9683b82cfdd9a50a55fe21dc982008
commit | author | age
7f7e0c 1 package org.keycloak.protocol.cas.endpoints;
MP 2
fdb9f6 3 import jakarta.ws.rs.GET;
JK 4 import jakarta.ws.rs.QueryParam;
5 import jakarta.ws.rs.core.Response;
7f7e0c 6 import org.jboss.logging.Logger;
MP 7 import org.jboss.resteasy.annotations.cache.NoCache;
4a6620 8 import org.keycloak.models.ClientModel;
7f7e0c 9 import org.keycloak.models.KeycloakSession;
MP 10 import org.keycloak.models.RealmModel;
11 import org.keycloak.models.UserSessionModel;
12 import org.keycloak.protocol.cas.CASLoginProtocol;
4a6620 13 import org.keycloak.protocol.oidc.utils.RedirectUtils;
MP 14 import org.keycloak.services.ErrorPage;
7f7e0c 15 import org.keycloak.services.managers.AuthenticationManager;
4a6620 16 import org.keycloak.services.messages.Messages;
7f7e0c 17
74f9bf 18 import java.net.URI;
7f7e0c 19
MP 20 public class LogoutEndpoint {
57a6c1 21     private static final Logger logger = Logger.getLogger(LogoutEndpoint.class);
7f7e0c 22
MP 23     private KeycloakSession session;
24
25     private RealmModel realm;
4a6620 26     private String redirectUri;
7f7e0c 27
ceed8f 28     public LogoutEndpoint(KeycloakSession session, RealmModel realm) {
JK 29         this.session = session;
7f7e0c 30         this.realm = realm;
MP 31     }
32
33     @GET
34     @NoCache
4a6620 35     public Response logout(@QueryParam(CASLoginProtocol.SERVICE_PARAM) String service) {
MP 36         checkClient(service);
7f7e0c 37
MP 38         AuthenticationManager.AuthResult authResult = AuthenticationManager.authenticateIdentityCookie(session, realm, false);
39         if (authResult != null) {
40             UserSessionModel userSession = authResult.getSession();
41             userSession.setNote(AuthenticationManager.KEYCLOAK_LOGOUT_PROTOCOL, CASLoginProtocol.LOGIN_PROTOCOL);
b88dc3 42
AP 43             if (redirectUri != null) {
44                 userSession.setNote(CASLoginProtocol.LOGOUT_REDIRECT_URI, redirectUri);
45             }
7f7e0c 46
MP 47             logger.debug("Initiating CAS browser logout");
ceed8f 48             Response response = AuthenticationManager.browserLogout(session, realm, authResult.getSession(), session.getContext().getUri(), session.getContext().getConnection(), session.getContext().getRequestHeaders());
7f7e0c 49             logger.debug("finishing CAS browser logout");
MP 50             return response;
51         }
74f9bf 52
V 53         if (redirectUri != null) {
54             logger.debugv("no active session, redirecting to {0}", redirectUri);
55             return Response.status(302).location(URI.create(redirectUri)).build();
56         }
57
6638b8 58         return ErrorPage.error(session, null, Response.Status.BAD_REQUEST, Messages.FAILED_LOGOUT);
4a6620 59     }
MP 60
61     private void checkClient(String service) {
62         if (service == null) {
63             return;
64         }
65
7a34c2 66         ClientModel client = realm.getClientsStream()
4a6620 67                 .filter(c -> CASLoginProtocol.LOGIN_PROTOCOL.equals(c.getProtocol()))
019db5 68                 .filter(c -> RedirectUtils.verifyRedirectUri(session, service, c) != null)
4a6620 69                 .findFirst().orElse(null);
MP 70         if (client != null) {
019db5 71             redirectUri = RedirectUtils.verifyRedirectUri(session, service, client);
4a6620 72
MP 73             session.getContext().setClient(client);
74         }
7f7e0c 75     }
MP 76 }