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

Matthias Piepkorn
2017-02-14 de7d254562ee04401894b82e3ee18f46d79ea357
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;
8a5518 10 import org.keycloak.protocol.cas.representations.CASServiceResponse;
352436 11 import org.keycloak.protocol.cas.utils.CASValidationException;
513246 12 import org.keycloak.protocol.cas.utils.ContentTypeHelper;
MP 13 import org.keycloak.protocol.cas.utils.ServiceResponseHelper;
7f7e0c 14 import org.keycloak.services.managers.ClientSessionCode;
MP 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
8a5518 43         CASServiceResponse serviceResponse = ServiceResponseHelper.createSuccess(userSession.getUser().getUsername(), attributes);
513246 44         return prepare(Response.Status.OK, serviceResponse);
7f7e0c 45     }
MP 46
47     @Override
352436 48     protected Response errorResponse(CASValidationException e) {
8a5518 49         CASServiceResponse serviceResponse = ServiceResponseHelper.createFailure(e.getError(), e.getErrorDescription());
352436 50         return prepare(e.getStatus(), serviceResponse);
7f7e0c 51     }
MP 52
8a5518 53     private Response prepare(Response.Status status, CASServiceResponse serviceResponse) {
513246 54         MediaType responseMediaType = new ContentTypeHelper(request, restRequest, uriInfo).selectResponseType();
MP 55         return ServiceResponseHelper.createResponse(status, responseMediaType, serviceResponse);
7f7e0c 56     }
MP 57 }