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

Doccrazy
2017-11-14 541aa23b71af594da8f576495fabb3c0fa429aae
commit | author | age
7f7e0c 1 package org.keycloak.protocol.cas.endpoints;
MP 2
3 import org.jboss.logging.Logger;
4 import org.keycloak.events.Details;
5 import org.keycloak.events.Errors;
6 import org.keycloak.events.EventBuilder;
7 import org.keycloak.events.EventType;
8 import org.keycloak.models.ClientModel;
9 import org.keycloak.models.RealmModel;
10 import org.keycloak.protocol.AuthorizationEndpointBase;
11 import org.keycloak.protocol.cas.CASLoginProtocol;
12 import org.keycloak.protocol.oidc.utils.RedirectUtils;
13 import org.keycloak.services.ErrorPageException;
14 import org.keycloak.services.messages.Messages;
15 import org.keycloak.services.util.CacheControlUtil;
f75caf 16 import org.keycloak.sessions.AuthenticationSessionModel;
7f7e0c 17
MP 18 import javax.ws.rs.GET;
19 import javax.ws.rs.core.MultivaluedMap;
20 import javax.ws.rs.core.Response;
21
22 public class AuthorizationEndpoint extends AuthorizationEndpointBase {
23     private static final Logger logger = Logger.getLogger(AuthorizationEndpoint.class);
24
25     private ClientModel client;
f75caf 26     private AuthenticationSessionModel authenticationSession;
7f7e0c 27     private String redirectUri;
MP 28
29     public AuthorizationEndpoint(RealmModel realm, EventBuilder event) {
30         super(realm, event);
31         event.event(EventType.LOGIN);
32     }
33
34     @GET
35     public Response build() {
36         MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
37         String service = params.getFirst(CASLoginProtocol.SERVICE_PARAM);
7124d2 38         boolean renew = params.containsKey(CASLoginProtocol.RENEW_PARAM);
MP 39         boolean gateway = params.containsKey(CASLoginProtocol.GATEWAY_PARAM);
7f7e0c 40
MP 41         checkSsl();
42         checkRealm();
43         checkClient(service);
44
f75caf 45         AuthorizationEndpointChecks checks = getOrCreateAuthenticationSession(client, null);
MP 46         if (checks.response != null) {
47             return checks.response;
48         }
49
50         authenticationSession = checks.authSession;
51         updateAuthenticationSession();
52
7f7e0c 53         // So back button doesn't work
MP 54         CacheControlUtil.noBackButtonCacheControlHeader();
55
7124d2 56         if (renew) {
f75caf 57             authenticationSession.setClientNote(CASLoginProtocol.RENEW_PARAM, "true");
7124d2 58         }
MP 59
7f7e0c 60         this.event.event(EventType.LOGIN);
f75caf 61         return handleBrowserAuthenticationRequest(authenticationSession, new CASLoginProtocol(session, realm, uriInfo, headers, event), gateway, false);
7f7e0c 62     }
MP 63
64     private void checkClient(String service) {
65         if (service == null) {
66             event.error(Errors.INVALID_REQUEST);
67             throw new ErrorPageException(session, Messages.MISSING_PARAMETER, CASLoginProtocol.SERVICE_PARAM);
68         }
69
70         client = realm.getClients().stream()
71                 .filter(c -> CASLoginProtocol.LOGIN_PROTOCOL.equals(c.getProtocol()))
72                 .filter(c -> RedirectUtils.verifyRedirectUri(uriInfo, service, realm, c) != null)
73                 .findFirst().orElse(null);
74         if (client == null) {
75             event.error(Errors.CLIENT_NOT_FOUND);
76             throw new ErrorPageException(session, Messages.CLIENT_NOT_FOUND);
77         }
78
79         if (!client.isEnabled()) {
80             event.error(Errors.CLIENT_DISABLED);
81             throw new ErrorPageException(session, Messages.CLIENT_DISABLED);
82         }
83
84         redirectUri = RedirectUtils.verifyRedirectUri(uriInfo, service, realm, client);
85
86         event.client(client.getClientId());
87         event.detail(Details.REDIRECT_URI, redirectUri);
88
89         session.getContext().setClient(client);
90     }
91
f75caf 92     private void updateAuthenticationSession() {
MP 93         authenticationSession.setProtocol(CASLoginProtocol.LOGIN_PROTOCOL);
94         authenticationSession.setRedirectUri(redirectUri);
95         authenticationSession.setAction(AuthenticationSessionModel.Action.AUTHENTICATE.name());
96     }
97
98     @Override
99     protected boolean isNewRequest(AuthenticationSessionModel authSession, ClientModel clientFromRequest, String requestState) {
100         return true;
7f7e0c 101     }
MP 102 }