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

github-actions
2024-12-03 0d71e01978a7871823e81ba1430777c3e6eca853
commit | author | age
513246 1 package org.keycloak.protocol.cas.utils;
MP 2
fdb9f6 3 import jakarta.ws.rs.core.HttpHeaders;
JK 4 import jakarta.ws.rs.core.MediaType;
5 import jakarta.ws.rs.core.Response;
352436 6 import org.keycloak.protocol.cas.representations.CASErrorCode;
8a5518 7 import org.keycloak.protocol.cas.representations.CASServiceResponse;
MP 8 import org.keycloak.protocol.cas.representations.CASServiceResponseAuthenticationFailure;
9 import org.keycloak.protocol.cas.representations.CASServiceResponseAuthenticationSuccess;
755fd7 10 import org.keycloak.protocol.cas.representations.CASServiceResponseProxySuccess;
ARW 11 import org.keycloak.protocol.cas.representations.CASServiceResponseProxyFailure;
513246 12
MP 13 import java.util.List;
14 import java.util.Map;
15
16 public final class ServiceResponseHelper {
17     private ServiceResponseHelper() {
18     }
19
8a5518 20     public static CASServiceResponse createSuccess(String username, Map<String, Object> attributes) {
513246 21         return createSuccess(username, attributes, null, null);
MP 22     }
23
8a5518 24     public static CASServiceResponse createSuccess(String username, Map<String, Object> attributes,
513246 25                                                    String proxyGrantingTicket, List<String> proxies) {
8a5518 26         CASServiceResponse response = new CASServiceResponse();
MP 27         CASServiceResponseAuthenticationSuccess success = new CASServiceResponseAuthenticationSuccess();
513246 28         success.setUser(username);
MP 29         success.setProxies(proxies);
30         success.setProxyGrantingTicket(proxyGrantingTicket);
31         success.setAttributes(attributes);
32
33         response.setAuthenticationSuccess(success);
34
35         return response;
36     }
37
8a5518 38     public static CASServiceResponse createFailure(CASErrorCode errorCode, String errorDescription) {
MP 39         CASServiceResponse response = new CASServiceResponse();
40         CASServiceResponseAuthenticationFailure failure = new CASServiceResponseAuthenticationFailure();
352436 41         failure.setCode(errorCode == null ? CASErrorCode.INTERNAL_ERROR.name() : errorCode.name());
513246 42         failure.setDescription(errorDescription);
MP 43         response.setAuthenticationFailure(failure);
44
45         return response;
46     }
47
755fd7 48     public static CASServiceResponse createProxySuccess(String pt) {
ARW 49         CASServiceResponse response = new CASServiceResponse();
50         CASServiceResponseProxySuccess success = new CASServiceResponseProxySuccess();
51         success.setProxyTicket(pt);
52         response.setProxySuccess(success);
53         return response;
54     }
55
56     public static CASServiceResponse createProxyFailure(CASErrorCode errorCode, String errorDescription) {
57         CASServiceResponse response = new CASServiceResponse();
58         CASServiceResponseProxyFailure failure = new CASServiceResponseProxyFailure();
59         failure.setCode(errorCode == null ? CASErrorCode.INTERNAL_ERROR.name() : errorCode.name());
60         failure.setDescription(errorDescription);
61         response.setProxyFailure(failure);
62
63         return response;
64     }
65
8a5518 66     public static Response createResponse(Response.Status status, MediaType mediaType, CASServiceResponse serviceResponse) {
513246 67         Response.ResponseBuilder builder = Response.status(status)
MP 68                 .header(HttpHeaders.CONTENT_TYPE, mediaType.withCharset("utf-8"));
69         if (MediaType.APPLICATION_JSON_TYPE.equals(mediaType)) {
70             return builder.entity(ServiceResponseMarshaller.marshalJson(serviceResponse)).build();
71         } else {
72             return builder.entity(ServiceResponseMarshaller.marshalXml(serviceResponse)).build();
73         }
74     }
75 }