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

github-actions
2023-02-21 0dd6a08579adb4f5a92fc6bc338935a348528e6f
commit | author | age
7f7e0c 1 package org.keycloak.protocol.cas.endpoints;
MP 2
3 import org.jboss.resteasy.annotations.cache.NoCache;
4 import org.keycloak.events.EventBuilder;
5 import org.keycloak.events.EventType;
74023a 6 import org.keycloak.models.RealmModel;
7f7e0c 7 import org.keycloak.protocol.cas.CASLoginProtocol;
352436 8 import org.keycloak.protocol.cas.utils.CASValidationException;
7f7e0c 9
MP 10 import javax.ws.rs.GET;
74023a 11 import javax.ws.rs.core.MediaType;
EH 12 import javax.ws.rs.core.MultivaluedMap;
13 import javax.ws.rs.core.Response;
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
20     public ValidateEndpoint(RealmModel realm, EventBuilder event) {
74023a 21         super(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() {
aa6e6a 49         StringBuilder sb = new StringBuilder(RESPONSE_OK);
DR 50         sb.append(clientSession.getUserSession().getUser().getUsername());
51         sb.append("\n");
52         return Response.ok(sb.toString()).type(MediaType.TEXT_PLAIN).build();
7f7e0c 53     }
MP 54
352436 55     protected Response errorResponse(CASValidationException e) {
MP 56         return Response.status(e.getStatus()).entity(RESPONSE_FAILED).type(MediaType.TEXT_PLAIN).build();
7f7e0c 57     }
MP 58
59 }