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

Jacek Kowalski
2023-11-24 c8625160e9f918bbf165894379f68c281cfc48de
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.core.MediaType;
5 import jakarta.ws.rs.core.MultivaluedMap;
6 import jakarta.ws.rs.core.Response;
7f7e0c 7 import org.jboss.resteasy.annotations.cache.NoCache;
MP 8 import org.keycloak.events.EventBuilder;
9 import org.keycloak.events.EventType;
ceed8f 10 import org.keycloak.models.KeycloakSession;
74023a 11 import org.keycloak.models.RealmModel;
7f7e0c 12 import org.keycloak.protocol.cas.CASLoginProtocol;
352436 13 import org.keycloak.protocol.cas.utils.CASValidationException;
7f7e0c 14
74023a 15 public class ValidateEndpoint extends AbstractValidateEndpoint {
7f7e0c 16
MP 17     private static final String RESPONSE_OK = "yes\n";
18     private static final String RESPONSE_FAILED = "no\n";
19
ceed8f 20     public ValidateEndpoint(KeycloakSession session, RealmModel realm, EventBuilder event) {
JK 21         super(session, realm, event);
7f7e0c 22     }
MP 23
24     @GET
25     @NoCache
26     public Response build() {
dee145 27         MultivaluedMap<String, String> params = session.getContext().getUri().getQueryParameters();
7f7e0c 28         String service = params.getFirst(CASLoginProtocol.SERVICE_PARAM);
MP 29         String ticket = params.getFirst(CASLoginProtocol.TICKET_PARAM);
7124d2 30         boolean renew = params.containsKey(CASLoginProtocol.RENEW_PARAM);
7f7e0c 31
MP 32         event.event(EventType.CODE_TO_TOKEN);
33
34         try {
35             checkSsl();
36             checkRealm();
37             checkClient(service);
38
39             checkTicket(ticket, renew);
40
41             event.success();
42             return successResponse();
352436 43         } catch (CASValidationException e) {
7f7e0c 44             return errorResponse(e);
MP 45         }
46     }
47
48     protected Response successResponse() {
e3e192 49         String response = RESPONSE_OK + clientSession.getUserSession().getUser().getUsername() + "\n";
JK 50         return Response.ok(response).type(MediaType.TEXT_PLAIN).build();
7f7e0c 51     }
MP 52
352436 53     protected Response errorResponse(CASValidationException e) {
MP 54         return Response.status(e.getStatus()).entity(RESPONSE_FAILED).type(MediaType.TEXT_PLAIN).build();
7f7e0c 55     }
MP 56
57 }