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"; |
74023a
|
28 |
public static final String TARGET_PARAM = "TARGET"; |
7f7e0c
|
29 |
public static final String RENEW_PARAM = "renew"; |
MP |
30 |
public static final String GATEWAY_PARAM = "gateway"; |
|
31 |
public static final String TICKET_PARAM = "ticket"; |
|
32 |
public static final String FORMAT_PARAM = "format"; |
|
33 |
|
|
34 |
public static final String TICKET_RESPONSE_PARAM = "ticket"; |
|
35 |
|
|
36 |
public static final String SERVICE_TICKET_PREFIX = "ST-"; |
57a6c1
|
37 |
public static final String SESSION_SERVICE_TICKET = "service_ticket"; |
4a6620
|
38 |
|
MP |
39 |
public static final String LOGOUT_REDIRECT_URI = "CAS_LOGOUT_REDIRECT_URI"; |
7f7e0c
|
40 |
|
MP |
41 |
protected KeycloakSession session; |
|
42 |
protected RealmModel realm; |
|
43 |
protected UriInfo uriInfo; |
|
44 |
protected HttpHeaders headers; |
|
45 |
protected EventBuilder event; |
|
46 |
|
7124d2
|
47 |
public CASLoginProtocol(KeycloakSession session, RealmModel realm, UriInfo uriInfo, HttpHeaders headers, EventBuilder event) { |
7f7e0c
|
48 |
this.session = session; |
MP |
49 |
this.realm = realm; |
|
50 |
this.uriInfo = uriInfo; |
|
51 |
this.headers = headers; |
|
52 |
this.event = event; |
|
53 |
} |
|
54 |
|
|
55 |
public CASLoginProtocol() { |
|
56 |
} |
|
57 |
|
|
58 |
@Override |
|
59 |
public CASLoginProtocol setSession(KeycloakSession session) { |
|
60 |
this.session = session; |
|
61 |
return this; |
|
62 |
} |
|
63 |
|
|
64 |
@Override |
|
65 |
public CASLoginProtocol setRealm(RealmModel realm) { |
|
66 |
this.realm = realm; |
|
67 |
return this; |
|
68 |
} |
|
69 |
|
|
70 |
@Override |
|
71 |
public CASLoginProtocol setUriInfo(UriInfo uriInfo) { |
|
72 |
this.uriInfo = uriInfo; |
|
73 |
return this; |
|
74 |
} |
|
75 |
|
|
76 |
@Override |
|
77 |
public CASLoginProtocol setHttpHeaders(HttpHeaders headers) { |
|
78 |
this.headers = headers; |
|
79 |
return this; |
|
80 |
} |
|
81 |
|
|
82 |
@Override |
|
83 |
public CASLoginProtocol setEventBuilder(EventBuilder event) { |
|
84 |
this.event = event; |
|
85 |
return this; |
|
86 |
} |
|
87 |
|
|
88 |
@Override |
b8d686
|
89 |
public Response authenticated(UserSessionModel userSession, ClientSessionContext clientSessionCtx) { |
MP |
90 |
AuthenticatedClientSessionModel clientSession = clientSessionCtx.getClientSession(); |
f75caf
|
91 |
ClientSessionCode<AuthenticatedClientSessionModel> accessCode = new ClientSessionCode<>(session, realm, clientSession); |
7f7e0c
|
92 |
|
MP |
93 |
String service = clientSession.getRedirectUri(); |
|
94 |
//TODO validate service |
5570d4
|
95 |
|
6638b8
|
96 |
String code = accessCode.getOrGenerateCode(); |
7f7e0c
|
97 |
KeycloakUriBuilder uriBuilder = KeycloakUriBuilder.fromUri(service); |
5570d4
|
98 |
uriBuilder.queryParam(TICKET_RESPONSE_PARAM, SERVICE_TICKET_PREFIX + code); |
7f7e0c
|
99 |
|
MP |
100 |
URI redirectUri = uriBuilder.build(); |
|
101 |
|
|
102 |
Response.ResponseBuilder location = Response.status(302).location(redirectUri); |
|
103 |
return location.build(); |
|
104 |
} |
|
105 |
|
|
106 |
@Override |
f75caf
|
107 |
public Response sendError(AuthenticationSessionModel authSession, Error error) { |
7f7e0c
|
108 |
return Response.serverError().entity(error).build(); |
MP |
109 |
} |
|
110 |
|
|
111 |
@Override |
f75caf
|
112 |
public void backchannelLogout(UserSessionModel userSession, AuthenticatedClientSessionModel clientSession) { |
57a6c1
|
113 |
String logoutUrl = clientSession.getRedirectUri(); |
MP |
114 |
String serviceTicket = clientSession.getNote(CASLoginProtocol.SESSION_SERVICE_TICKET); |
|
115 |
//check if session is fully authenticated (i.e. serviceValidate has been called) |
|
116 |
if (serviceTicket != null && !serviceTicket.isEmpty()) { |
|
117 |
sendSingleLogoutRequest(logoutUrl, serviceTicket); |
|
118 |
} |
7f7e0c
|
119 |
ClientModel client = clientSession.getClient(); |
MP |
120 |
new ResourceAdminManager(session).logoutClientSession(uriInfo.getRequestUri(), realm, client, clientSession); |
|
121 |
} |
|
122 |
|
57a6c1
|
123 |
private void sendSingleLogoutRequest(String logoutUrl, String serviceTicket) { |
MP |
124 |
HttpEntity requestEntity = LogoutHelper.buildSingleLogoutRequest(serviceTicket); |
|
125 |
try { |
|
126 |
LogoutHelper.postWithRedirect(session, logoutUrl, requestEntity); |
|
127 |
logger.debug("Sent CAS single logout for service " + logoutUrl); |
|
128 |
} catch (IOException e) { |
|
129 |
logger.warn("Failed to call CAS service for logout: " + logoutUrl, e); |
|
130 |
} |
|
131 |
} |
|
132 |
|
7f7e0c
|
133 |
@Override |
f75caf
|
134 |
public Response frontchannelLogout(UserSessionModel userSession, AuthenticatedClientSessionModel clientSession) { |
7f7e0c
|
135 |
// todo oidc redirect support |
MP |
136 |
throw new RuntimeException("NOT IMPLEMENTED"); |
|
137 |
} |
|
138 |
|
|
139 |
@Override |
|
140 |
public Response finishLogout(UserSessionModel userSession) { |
4a6620
|
141 |
String redirectUri = userSession.getNote(CASLoginProtocol.LOGOUT_REDIRECT_URI); |
MP |
142 |
|
7f7e0c
|
143 |
event.event(EventType.LOGOUT); |
MP |
144 |
event.user(userSession.getUser()).session(userSession).success(); |
cbb2f2
|
145 |
|
4a6620
|
146 |
if (redirectUri != null) { |
cbb2f2
|
147 |
return Response.status(302).location(URI.create(redirectUri)).build(); |
4a6620
|
148 |
} else { |
cbb2f2
|
149 |
LoginFormsProvider infoPage = session.getProvider(LoginFormsProvider.class).setSuccess("Logout successful"); |
4a6620
|
150 |
infoPage.setAttribute("skipLink", true); |
cbb2f2
|
151 |
return infoPage.createInfoPage(); |
4a6620
|
152 |
} |
7f7e0c
|
153 |
} |
MP |
154 |
|
|
155 |
@Override |
f75caf
|
156 |
public boolean requireReauthentication(UserSessionModel userSession, AuthenticationSessionModel authSession) { |
MP |
157 |
return "true".equals(authSession.getClientNote(CASLoginProtocol.RENEW_PARAM)); |
7f7e0c
|
158 |
} |
MP |
159 |
|
|
160 |
@Override |
|
161 |
public void close() { |
|
162 |
|
|
163 |
} |
|
164 |
} |