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

Jacek Kowalski
2023-11-24 ceed8fb052685c0105d225a61d8574d36e171166
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;
513246 10
MP 11 import java.util.List;
12 import java.util.Map;
13
14 public final class ServiceResponseHelper {
15     private ServiceResponseHelper() {
16     }
17
8a5518 18     public static CASServiceResponse createSuccess(String username, Map<String, Object> attributes) {
513246 19         return createSuccess(username, attributes, null, null);
MP 20     }
21
8a5518 22     public static CASServiceResponse createSuccess(String username, Map<String, Object> attributes,
513246 23                                                    String proxyGrantingTicket, List<String> proxies) {
8a5518 24         CASServiceResponse response = new CASServiceResponse();
MP 25         CASServiceResponseAuthenticationSuccess success = new CASServiceResponseAuthenticationSuccess();
513246 26         success.setUser(username);
MP 27         success.setProxies(proxies);
28         success.setProxyGrantingTicket(proxyGrantingTicket);
29         success.setAttributes(attributes);
30
31         response.setAuthenticationSuccess(success);
32
33         return response;
34     }
35
8a5518 36     public static CASServiceResponse createFailure(CASErrorCode errorCode, String errorDescription) {
MP 37         CASServiceResponse response = new CASServiceResponse();
38         CASServiceResponseAuthenticationFailure failure = new CASServiceResponseAuthenticationFailure();
352436 39         failure.setCode(errorCode == null ? CASErrorCode.INTERNAL_ERROR.name() : errorCode.name());
513246 40         failure.setDescription(errorDescription);
MP 41         response.setAuthenticationFailure(failure);
42
43         return response;
44     }
45
8a5518 46     public static Response createResponse(Response.Status status, MediaType mediaType, CASServiceResponse serviceResponse) {
513246 47         Response.ResponseBuilder builder = Response.status(status)
MP 48                 .header(HttpHeaders.CONTENT_TYPE, mediaType.withCharset("utf-8"));
49         if (MediaType.APPLICATION_JSON_TYPE.equals(mediaType)) {
50             return builder.entity(ServiceResponseMarshaller.marshalJson(serviceResponse)).build();
51         } else {
52             return builder.entity(ServiceResponseMarshaller.marshalXml(serviceResponse)).build();
53         }
54     }
55 }