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

Matthias Piepkorn
2018-09-08 9532f1b237bbd5f74f6a4ef137dbdcbd1c58d0ba
commit | author | age
7f7e0c 1 package org.keycloak.protocol.cas.endpoints;
MP 2
3 import org.keycloak.events.EventBuilder;
b8d686 4 import org.keycloak.models.*;
7f7e0c 5 import org.keycloak.protocol.ProtocolMapper;
513246 6 import org.keycloak.protocol.cas.mappers.CASAttributeMapper;
8a5518 7 import org.keycloak.protocol.cas.representations.CASServiceResponse;
352436 8 import org.keycloak.protocol.cas.utils.CASValidationException;
513246 9 import org.keycloak.protocol.cas.utils.ContentTypeHelper;
MP 10 import org.keycloak.protocol.cas.utils.ServiceResponseHelper;
7f7e0c 11 import org.keycloak.services.managers.ClientSessionCode;
b8d686 12 import org.keycloak.services.util.DefaultClientSessionContext;
7f7e0c 13
513246 14 import javax.ws.rs.core.*;
MP 15 import java.util.HashMap;
16 import java.util.Map;
7f7e0c 17 import java.util.Set;
MP 18
19 public class ServiceValidateEndpoint extends ValidateEndpoint {
513246 20     @Context
MP 21     private Request restRequest;
22
7f7e0c 23     public ServiceValidateEndpoint(RealmModel realm, EventBuilder event) {
MP 24         super(realm, event);
25     }
26
27     @Override
28     protected Response successResponse() {
29         UserSessionModel userSession = clientSession.getUserSession();
b8d686 30         // CAS protocol does not support scopes, so pass null scopeParam
MP 31         ClientSessionContext clientSessionCtx = DefaultClientSessionContext.fromClientSessionAndScopeParameter(clientSession, null);
7f7e0c 32
b8d686 33         Set<ProtocolMapperModel> mappings = clientSessionCtx.getProtocolMappers();
7f7e0c 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 }