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

Doccrazy
2017-01-27 1482f2bf59b0e26b74f1e34631d5366e1abaff96
commit | author | age
7f7e0c 1 package org.keycloak.protocol.cas.endpoints;
MP 2
3 import org.keycloak.events.EventBuilder;
4 import org.keycloak.models.KeycloakSessionFactory;
5 import org.keycloak.models.ProtocolMapperModel;
6 import org.keycloak.models.RealmModel;
7 import org.keycloak.models.UserSessionModel;
8 import org.keycloak.protocol.ProtocolMapper;
513246 9 import org.keycloak.protocol.cas.mappers.CASAttributeMapper;
MP 10 import org.keycloak.protocol.cas.representations.CasServiceResponse;
11 import org.keycloak.protocol.cas.utils.ContentTypeHelper;
12 import org.keycloak.protocol.cas.utils.ServiceResponseHelper;
7f7e0c 13 import org.keycloak.services.ErrorResponseException;
MP 14 import org.keycloak.services.managers.ClientSessionCode;
15
513246 16 import javax.ws.rs.core.*;
MP 17 import java.util.HashMap;
18 import java.util.Map;
7f7e0c 19 import java.util.Set;
MP 20
21 public class ServiceValidateEndpoint extends ValidateEndpoint {
513246 22     @Context
MP 23     private Request restRequest;
24
7f7e0c 25     public ServiceValidateEndpoint(RealmModel realm, EventBuilder event) {
MP 26         super(realm, event);
27     }
28
29     @Override
30     protected Response successResponse() {
31         UserSessionModel userSession = clientSession.getUserSession();
32
33         Set<ProtocolMapperModel> mappings = new ClientSessionCode(session, realm, clientSession).getRequestedProtocolMappers();
34         KeycloakSessionFactory sessionFactory = session.getKeycloakSessionFactory();
513246 35         Map<String, Object> attributes = new HashMap<>();
7f7e0c 36         for (ProtocolMapperModel mapping : mappings) {
MP 37             ProtocolMapper mapper = (ProtocolMapper) sessionFactory.getProviderFactory(ProtocolMapper.class, mapping.getProtocolMapper());
513246 38             if (mapper instanceof CASAttributeMapper) {
MP 39                 ((CASAttributeMapper) mapper).setAttribute(attributes, mapping, userSession);
40             }
7f7e0c 41         }
MP 42
513246 43         CasServiceResponse serviceResponse = ServiceResponseHelper.createSuccess(userSession.getUser().getUsername(), attributes);
MP 44         return prepare(Response.Status.OK, serviceResponse);
7f7e0c 45     }
MP 46
47     @Override
48     protected Response errorResponse(ErrorResponseException e) {
513246 49         CasServiceResponse serviceResponse = ServiceResponseHelper.createFailure("CODE", "Description");
MP 50         return prepare(Response.Status.FORBIDDEN, serviceResponse);
7f7e0c 51     }
MP 52
513246 53     private Response prepare(Response.Status status, CasServiceResponse serviceResponse) {
MP 54         MediaType responseMediaType = new ContentTypeHelper(request, restRequest, uriInfo).selectResponseType();
55         return ServiceResponseHelper.createResponse(status, responseMediaType, serviceResponse);
7f7e0c 56     }
MP 57 }