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

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