commit | author | age
|
7f7e0c
|
1 |
package org.keycloak.protocol.cas; |
MP |
2 |
|
57a6c1
|
3 |
import org.apache.http.HttpEntity; |
MP |
4 |
import org.jboss.logging.Logger; |
7f7e0c
|
5 |
import org.keycloak.common.util.KeycloakUriBuilder; |
MP |
6 |
import org.keycloak.events.EventBuilder; |
|
7 |
import org.keycloak.events.EventType; |
4a6620
|
8 |
import org.keycloak.forms.login.LoginFormsProvider; |
7f7e0c
|
9 |
import org.keycloak.models.*; |
MP |
10 |
import org.keycloak.protocol.LoginProtocol; |
57a6c1
|
11 |
import org.keycloak.protocol.cas.utils.LogoutHelper; |
7f7e0c
|
12 |
import org.keycloak.services.managers.ClientSessionCode; |
MP |
13 |
import org.keycloak.services.managers.ResourceAdminManager; |
f75caf
|
14 |
import org.keycloak.sessions.AuthenticationSessionModel; |
7f7e0c
|
15 |
|
MP |
16 |
import javax.ws.rs.core.HttpHeaders; |
|
17 |
import javax.ws.rs.core.Response; |
|
18 |
import javax.ws.rs.core.UriInfo; |
57a6c1
|
19 |
import java.io.IOException; |
7f7e0c
|
20 |
import java.net.URI; |
MP |
21 |
|
|
22 |
public class CASLoginProtocol implements LoginProtocol { |
57a6c1
|
23 |
private static final Logger logger = Logger.getLogger(CASLoginProtocol.class); |
MP |
24 |
|
7f7e0c
|
25 |
public static final String LOGIN_PROTOCOL = "cas"; |
MP |
26 |
|
|
27 |
public static final String SERVICE_PARAM = "service"; |
|
28 |
public static final String RENEW_PARAM = "renew"; |
|
29 |
public static final String GATEWAY_PARAM = "gateway"; |
|
30 |
public static final String TICKET_PARAM = "ticket"; |
|
31 |
public static final String FORMAT_PARAM = "format"; |
|
32 |
|
|
33 |
public static final String TICKET_RESPONSE_PARAM = "ticket"; |
|
34 |
|
|
35 |
public static final String SERVICE_TICKET_PREFIX = "ST-"; |
57a6c1
|
36 |
public static final String SESSION_SERVICE_TICKET = "service_ticket"; |
4a6620
|
37 |
|
MP |
38 |
public static final String LOGOUT_REDIRECT_URI = "CAS_LOGOUT_REDIRECT_URI"; |
7f7e0c
|
39 |
|
MP |
40 |
protected KeycloakSession session; |
|
41 |
protected RealmModel realm; |
|
42 |
protected UriInfo uriInfo; |
|
43 |
protected HttpHeaders headers; |
|
44 |
protected EventBuilder event; |
|
45 |
|
7124d2
|
46 |
public CASLoginProtocol(KeycloakSession session, RealmModel realm, UriInfo uriInfo, HttpHeaders headers, EventBuilder event) { |
7f7e0c
|
47 |
this.session = session; |
MP |
48 |
this.realm = realm; |
|
49 |
this.uriInfo = uriInfo; |
|
50 |
this.headers = headers; |
|
51 |
this.event = event; |
|
52 |
} |
|
53 |
|
|
54 |
public CASLoginProtocol() { |
|
55 |
} |
|
56 |
|
|
57 |
@Override |
|
58 |
public CASLoginProtocol setSession(KeycloakSession session) { |
|
59 |
this.session = session; |
|
60 |
return this; |
|
61 |
} |
|
62 |
|
|
63 |
@Override |
|
64 |
public CASLoginProtocol setRealm(RealmModel realm) { |
|
65 |
this.realm = realm; |
|
66 |
return this; |
|
67 |
} |
|
68 |
|
|
69 |
@Override |
|
70 |
public CASLoginProtocol setUriInfo(UriInfo uriInfo) { |
|
71 |
this.uriInfo = uriInfo; |
|
72 |
return this; |
|
73 |
} |
|
74 |
|
|
75 |
@Override |
|
76 |
public CASLoginProtocol setHttpHeaders(HttpHeaders headers) { |
|
77 |
this.headers = headers; |
|
78 |
return this; |
|
79 |
} |
|
80 |
|
|
81 |
@Override |
|
82 |
public CASLoginProtocol setEventBuilder(EventBuilder event) { |
|
83 |
this.event = event; |
|
84 |
return this; |
|
85 |
} |
|
86 |
|
|
87 |
@Override |
f75caf
|
88 |
public Response authenticated(UserSessionModel userSession, AuthenticatedClientSessionModel clientSession) { |
MP |
89 |
ClientSessionCode<AuthenticatedClientSessionModel> accessCode = new ClientSessionCode<>(session, realm, clientSession); |
7f7e0c
|
90 |
|
MP |
91 |
String service = clientSession.getRedirectUri(); |
|
92 |
//TODO validate service |
5570d4
|
93 |
|
6638b8
|
94 |
String code = accessCode.getOrGenerateCode(); |
7f7e0c
|
95 |
KeycloakUriBuilder uriBuilder = KeycloakUriBuilder.fromUri(service); |
5570d4
|
96 |
uriBuilder.queryParam(TICKET_RESPONSE_PARAM, SERVICE_TICKET_PREFIX + code); |
7f7e0c
|
97 |
|
MP |
98 |
URI redirectUri = uriBuilder.build(); |
|
99 |
|
|
100 |
Response.ResponseBuilder location = Response.status(302).location(redirectUri); |
|
101 |
return location.build(); |
|
102 |
} |
|
103 |
|
|
104 |
@Override |
f75caf
|
105 |
public Response sendError(AuthenticationSessionModel authSession, Error error) { |
7f7e0c
|
106 |
return Response.serverError().entity(error).build(); |
MP |
107 |
} |
|
108 |
|
|
109 |
@Override |
f75caf
|
110 |
public void backchannelLogout(UserSessionModel userSession, AuthenticatedClientSessionModel clientSession) { |
57a6c1
|
111 |
String logoutUrl = clientSession.getRedirectUri(); |
MP |
112 |
String serviceTicket = clientSession.getNote(CASLoginProtocol.SESSION_SERVICE_TICKET); |
|
113 |
//check if session is fully authenticated (i.e. serviceValidate has been called) |
|
114 |
if (serviceTicket != null && !serviceTicket.isEmpty()) { |
|
115 |
sendSingleLogoutRequest(logoutUrl, serviceTicket); |
|
116 |
} |
7f7e0c
|
117 |
ClientModel client = clientSession.getClient(); |
MP |
118 |
new ResourceAdminManager(session).logoutClientSession(uriInfo.getRequestUri(), realm, client, clientSession); |
|
119 |
} |
|
120 |
|
57a6c1
|
121 |
private void sendSingleLogoutRequest(String logoutUrl, String serviceTicket) { |
MP |
122 |
HttpEntity requestEntity = LogoutHelper.buildSingleLogoutRequest(serviceTicket); |
|
123 |
try { |
|
124 |
LogoutHelper.postWithRedirect(session, logoutUrl, requestEntity); |
|
125 |
logger.debug("Sent CAS single logout for service " + logoutUrl); |
|
126 |
} catch (IOException e) { |
|
127 |
logger.warn("Failed to call CAS service for logout: " + logoutUrl, e); |
|
128 |
} |
|
129 |
} |
|
130 |
|
7f7e0c
|
131 |
@Override |
f75caf
|
132 |
public Response frontchannelLogout(UserSessionModel userSession, AuthenticatedClientSessionModel clientSession) { |
7f7e0c
|
133 |
// todo oidc redirect support |
MP |
134 |
throw new RuntimeException("NOT IMPLEMENTED"); |
|
135 |
} |
|
136 |
|
|
137 |
@Override |
|
138 |
public Response finishLogout(UserSessionModel userSession) { |
4a6620
|
139 |
String redirectUri = userSession.getNote(CASLoginProtocol.LOGOUT_REDIRECT_URI); |
MP |
140 |
|
7f7e0c
|
141 |
event.event(EventType.LOGOUT); |
MP |
142 |
event.user(userSession.getUser()).session(userSession).success(); |
4a6620
|
143 |
LoginFormsProvider infoPage = session.getProvider(LoginFormsProvider.class).setSuccess("Logout successful"); |
MP |
144 |
if (redirectUri != null) { |
|
145 |
infoPage.setAttribute("pageRedirectUri", redirectUri); |
|
146 |
} else { |
|
147 |
infoPage.setAttribute("skipLink", true); |
|
148 |
} |
|
149 |
return infoPage.createInfoPage(); |
7f7e0c
|
150 |
} |
MP |
151 |
|
|
152 |
@Override |
f75caf
|
153 |
public boolean requireReauthentication(UserSessionModel userSession, AuthenticationSessionModel authSession) { |
MP |
154 |
return "true".equals(authSession.getClientNote(CASLoginProtocol.RENEW_PARAM)); |
7f7e0c
|
155 |
} |
MP |
156 |
|
|
157 |
@Override |
|
158 |
public void close() { |
|
159 |
|
|
160 |
} |
|
161 |
} |