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

github-actions
2024-10-18 7e38937aa0589baa9451fef751e659323c611380
commit | author | age
755fd7 1 package org.keycloak.protocol.cas.endpoints;
ARW 2
3 import jakarta.ws.rs.GET;
4 import jakarta.ws.rs.core.MediaType;
5 import jakarta.ws.rs.core.MultivaluedMap;
6 import jakarta.ws.rs.core.Response;
7 import org.jboss.resteasy.annotations.cache.NoCache;
8 import org.keycloak.events.EventBuilder;
9 import org.keycloak.events.EventType;
10 import org.keycloak.models.*;
11 import org.keycloak.protocol.cas.CASLoginProtocol;
12 import org.keycloak.protocol.cas.representations.CASServiceResponse;
13 import org.keycloak.protocol.cas.utils.CASValidationException;
14 import org.keycloak.protocol.cas.utils.ContentTypeHelper;
15 import org.keycloak.protocol.cas.utils.ServiceResponseHelper;
16
17 public class ProxyEndpoint extends AbstractValidateEndpoint {
18
19     public ProxyEndpoint(KeycloakSession session, RealmModel realm, EventBuilder event) {
20         super(session, realm, event);
21     }
22
23     @GET
24     @NoCache
25     public Response build() {
26         MultivaluedMap<String, String> params = session.getContext().getUri().getQueryParameters();
27         String targetService = params.getFirst(CASLoginProtocol.TARGET_SERVICE_PARAM);
28         String pgt = params.getFirst(CASLoginProtocol.PGT_PARAM);
29
30         event.event(EventType.CODE_TO_TOKEN);
31
32         try {
33             checkSsl();
34             checkRealm();
35             checkTicket(pgt, CASLoginProtocol.PROXY_GRANTING_TICKET_PREFIX, false);
36             event.success();
37             return successResponse(getPT(this.session, clientSession, targetService));
38         } catch (CASValidationException e) {
39             return errorResponse(e);
40         }
41     }
42
43     protected Response successResponse(String pt) {
44         CASServiceResponse serviceResponse = ServiceResponseHelper.createProxySuccess(pt);
45         return prepare(Response.Status.OK, serviceResponse);
46     }
47
48     protected Response errorResponse(CASValidationException e) {
49         CASServiceResponse serviceResponse = ServiceResponseHelper.createProxyFailure(e.getError(), e.getErrorDescription());
50         return prepare(e.getStatus(), serviceResponse);
51     }
52
53     private Response prepare(Response.Status status, CASServiceResponse serviceResponse) {
54         MediaType responseMediaType = new ContentTypeHelper(session.getContext().getUri()).selectResponseType();
55         return ServiceResponseHelper.createResponse(status, responseMediaType, serviceResponse);
56     }
57 }