|
1
|
|
|
package org.apereo.cas.web.support; |
|
2
|
|
|
|
|
3
|
|
|
import org.apache.commons.lang3.ObjectUtils; |
|
4
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
5
|
|
|
import org.apereo.cas.authentication.Authentication; |
|
6
|
|
|
import org.apereo.cas.authentication.AuthenticationResult; |
|
7
|
|
|
import org.apereo.cas.authentication.AuthenticationResultBuilder; |
|
8
|
|
|
import org.apereo.cas.authentication.Credential; |
|
9
|
|
|
import org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest; |
|
10
|
|
|
import org.apereo.cas.authentication.principal.Principal; |
|
11
|
|
|
import org.apereo.cas.authentication.principal.Response; |
|
12
|
|
|
import org.apereo.cas.authentication.principal.Service; |
|
13
|
|
|
import org.apereo.cas.authentication.principal.WebApplicationService; |
|
14
|
|
|
import org.apereo.cas.logout.LogoutRequest; |
|
15
|
|
|
import org.apereo.cas.services.MultifactorAuthenticationProvider; |
|
16
|
|
|
import org.apereo.cas.services.RegisteredService; |
|
17
|
|
|
import org.apereo.cas.services.UnauthorizedServiceException; |
|
18
|
|
|
import org.apereo.cas.ticket.ServiceTicket; |
|
19
|
|
|
import org.apereo.cas.ticket.TicketGrantingTicket; |
|
20
|
|
|
import org.apereo.cas.ticket.registry.TicketRegistrySupport; |
|
21
|
|
|
import org.apereo.cas.util.HttpRequestUtils; |
|
22
|
|
|
import org.apereo.cas.web.flow.CasWebflowConstants; |
|
23
|
|
|
import org.slf4j.Logger; |
|
24
|
|
|
import org.slf4j.LoggerFactory; |
|
25
|
|
|
import org.springframework.util.Assert; |
|
26
|
|
|
import org.springframework.web.servlet.ModelAndView; |
|
27
|
|
|
import org.springframework.web.util.CookieGenerator; |
|
28
|
|
|
import org.springframework.webflow.context.ExternalContextHolder; |
|
29
|
|
|
import org.springframework.webflow.context.servlet.ServletExternalContext; |
|
30
|
|
|
import org.springframework.webflow.core.collection.MutableAttributeMap; |
|
31
|
|
|
import org.springframework.webflow.execution.FlowSession; |
|
32
|
|
|
import org.springframework.webflow.execution.RequestContext; |
|
33
|
|
|
|
|
34
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
35
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
36
|
|
|
import java.io.Serializable; |
|
37
|
|
|
import java.net.URI; |
|
38
|
|
|
import java.util.Collection; |
|
39
|
|
|
import java.util.HashMap; |
|
40
|
|
|
import java.util.List; |
|
41
|
|
|
import java.util.Map; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Common utilities for the web tier. |
|
45
|
|
|
* |
|
46
|
|
|
* @author Scott Battaglia |
|
47
|
|
|
* @since 3.1 |
|
48
|
|
|
*/ |
|
49
|
|
|
public final class WebUtils { |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Request attribute that contains message key describing details of authorization failure. |
|
53
|
|
|
*/ |
|
54
|
|
|
public static final String CAS_ACCESS_DENIED_REASON = "CAS_ACCESS_DENIED_REASON"; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Ticket-granting ticket id parameter used in various flow scopes. |
|
58
|
|
|
*/ |
|
59
|
|
|
public static final String PARAMETER_TICKET_GRANTING_TICKET_ID = "ticketGrantingTicketId"; |
|
60
|
|
|
|
|
61
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(WebUtils.class); |
|
62
|
|
|
|
|
63
|
|
|
private static final String PUBLIC_WORKSTATION_ATTRIBUTE = "publicWorkstation"; |
|
64
|
|
|
private static final String PARAMETER_AUTHENTICATION = "authentication"; |
|
65
|
|
|
private static final String PARAMETER_AUTHENTICATION_RESULT_BUILDER = "authenticationResultBuilder"; |
|
66
|
|
|
private static final String PARAMETER_AUTHENTICATION_RESULT = "authenticationResult"; |
|
67
|
|
|
private static final String PARAMETER_CREDENTIAL = "credential"; |
|
68
|
|
|
private static final String PARAMETER_UNAUTHORIZED_REDIRECT_URL = "unauthorizedRedirectUrl"; |
|
69
|
|
|
private static final String PARAMETER_REGISTERED_SERVICE = "registeredService"; |
|
70
|
|
|
private static final String PARAMETER_SERVICE = "service"; |
|
71
|
|
|
private static final String PARAMETER_SERVICE_TICKET_ID = "serviceTicketId"; |
|
72
|
|
|
private static final String PARAMETER_LOGOUT_REQUESTS = "logoutRequests"; |
|
73
|
|
|
private static final String PARAMETER_SERVICE_UI_METADATA = "serviceUIMetadata"; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Instantiates a new web utils instance. |
|
77
|
|
|
*/ |
|
78
|
|
|
private WebUtils() { |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Gets the http servlet request from the context. |
|
83
|
|
|
* |
|
84
|
|
|
* @param context the context |
|
85
|
|
|
* @return the http servlet request |
|
86
|
|
|
*/ |
|
87
|
|
|
public static HttpServletRequest getHttpServletRequestFromExternalWebflowContext(final RequestContext context) { |
|
88
|
|
|
Assert.isInstanceOf(ServletExternalContext.class, context.getExternalContext(), |
|
89
|
|
|
"Cannot obtain HttpServletRequest from event of type: " |
|
90
|
|
|
+ context.getExternalContext().getClass().getName()); |
|
91
|
|
|
|
|
92
|
|
|
return (HttpServletRequest) context.getExternalContext().getNativeRequest(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Gets the http servlet request from the current servlet context. |
|
97
|
|
|
* |
|
98
|
|
|
* @return the http servlet request |
|
99
|
|
|
*/ |
|
100
|
|
|
public static HttpServletRequest getHttpServletRequestFromExternalWebflowContext() { |
|
101
|
|
|
final ServletExternalContext servletExternalContext = (ServletExternalContext) ExternalContextHolder.getExternalContext(); |
|
102
|
|
|
if (servletExternalContext != null) { |
|
103
|
|
|
return (HttpServletRequest) servletExternalContext.getNativeRequest(); |
|
104
|
|
|
} |
|
105
|
|
|
return null; |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Gets the http servlet response from the context. |
|
111
|
|
|
* |
|
112
|
|
|
* @param context the context |
|
113
|
|
|
* @return the http servlet response |
|
114
|
|
|
*/ |
|
115
|
|
|
public static HttpServletResponse getHttpServletResponseFromExternalWebflowContext(final RequestContext context) { |
|
116
|
|
|
Assert.isInstanceOf(ServletExternalContext.class, context.getExternalContext(), |
|
117
|
|
|
"Cannot obtain HttpServletResponse from event of type: " + context.getExternalContext().getClass().getName()); |
|
118
|
|
|
return (HttpServletResponse) context.getExternalContext().getNativeResponse(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Gets the http servlet response from the current servlet context. |
|
123
|
|
|
* |
|
124
|
|
|
* @return the http servlet response |
|
125
|
|
|
*/ |
|
126
|
|
|
public static HttpServletResponse getHttpServletResponseFromExternalWebflowContext() { |
|
127
|
|
|
final ServletExternalContext servletExternalContext = (ServletExternalContext) ExternalContextHolder.getExternalContext(); |
|
128
|
|
|
if (servletExternalContext != null) { |
|
129
|
|
|
return (HttpServletResponse) servletExternalContext.getNativeResponse(); |
|
130
|
|
|
} |
|
131
|
|
|
return null; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Gets the service. |
|
137
|
|
|
* |
|
138
|
|
|
* @param argumentExtractors the argument extractors |
|
139
|
|
|
* @param context the context |
|
140
|
|
|
* @return the service |
|
141
|
|
|
*/ |
|
142
|
|
|
public static WebApplicationService getService(final List<ArgumentExtractor> argumentExtractors, final RequestContext context) { |
|
143
|
|
|
final HttpServletRequest request = WebUtils.getHttpServletRequestFromExternalWebflowContext(context); |
|
144
|
|
|
return HttpRequestUtils.getService(argumentExtractors, request); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Gets the service from the flow scope. |
|
149
|
|
|
* |
|
150
|
|
|
* @param context the context |
|
151
|
|
|
* @return the service |
|
152
|
|
|
*/ |
|
153
|
|
|
public static WebApplicationService getService(final RequestContext context) { |
|
154
|
|
|
return context != null ? (WebApplicationService) context.getFlowScope().get(PARAMETER_SERVICE) : null; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Gets the registered service from the flow scope. |
|
159
|
|
|
* |
|
160
|
|
|
* @param context the context |
|
161
|
|
|
* @return the service |
|
162
|
|
|
*/ |
|
163
|
|
|
public static RegisteredService getRegisteredService(final RequestContext context) { |
|
164
|
|
|
return context != null ? (RegisteredService) context.getFlowScope().get(PARAMETER_REGISTERED_SERVICE) : null; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Put ticket granting ticket in request and flow scopes. |
|
169
|
|
|
* |
|
170
|
|
|
* @param context the context |
|
171
|
|
|
* @param ticket the ticket value |
|
172
|
|
|
*/ |
|
173
|
|
|
public static void putTicketGrantingTicketInScopes(final RequestContext context, final TicketGrantingTicket ticket) { |
|
174
|
|
|
final String ticketValue = ticket != null ? ticket.getId() : null; |
|
175
|
|
|
putTicketGrantingTicketInScopes(context, ticketValue); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Put ticket granting ticket in request and flow scopes. |
|
180
|
|
|
* |
|
181
|
|
|
* @param context the context |
|
182
|
|
|
* @param ticketValue the ticket value |
|
183
|
|
|
*/ |
|
184
|
|
|
public static void putTicketGrantingTicketInScopes(final RequestContext context, final String ticketValue) { |
|
185
|
|
|
putTicketGrantingTicketIntoMap(context.getRequestScope(), ticketValue); |
|
186
|
|
|
putTicketGrantingTicketIntoMap(context.getFlowScope(), ticketValue); |
|
187
|
|
|
|
|
188
|
|
|
FlowSession session = context.getFlowExecutionContext().getActiveSession().getParent(); |
|
189
|
|
|
while (session != null) { |
|
190
|
|
|
putTicketGrantingTicketIntoMap(session.getScope(), ticketValue); |
|
191
|
|
|
session = session.getParent(); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Put ticket granting ticket into map that is either backed by the flow/request scope. |
|
197
|
|
|
* Will override the previous value and blank out the setting if value is null or empty. |
|
198
|
|
|
* |
|
199
|
|
|
* @param map the map |
|
200
|
|
|
* @param ticketValue the ticket value |
|
201
|
|
|
*/ |
|
202
|
|
|
public static void putTicketGrantingTicketIntoMap(final MutableAttributeMap map, final String ticketValue) { |
|
203
|
|
|
map.put(PARAMETER_TICKET_GRANTING_TICKET_ID, ticketValue); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Gets the ticket granting ticket id from the request and flow scopes. |
|
208
|
|
|
* |
|
209
|
|
|
* @param context the context |
|
210
|
|
|
* @return the ticket granting ticket id |
|
211
|
|
|
*/ |
|
212
|
|
|
public static String getTicketGrantingTicketId(final RequestContext context) { |
|
213
|
|
|
final String tgtFromRequest = (String) context.getRequestScope().get(PARAMETER_TICKET_GRANTING_TICKET_ID); |
|
214
|
|
|
final String tgtFromFlow = (String) context.getFlowScope().get(PARAMETER_TICKET_GRANTING_TICKET_ID); |
|
215
|
|
|
|
|
216
|
|
|
return tgtFromRequest != null ? tgtFromRequest : tgtFromFlow; |
|
217
|
|
|
|
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Put service ticket in request scope. |
|
222
|
|
|
* |
|
223
|
|
|
* @param context the context |
|
224
|
|
|
* @param ticketValue the ticket value |
|
225
|
|
|
*/ |
|
226
|
|
|
public static void putServiceTicketInRequestScope(final RequestContext context, final ServiceTicket ticketValue) { |
|
227
|
|
|
context.getRequestScope().put(PARAMETER_SERVICE_TICKET_ID, ticketValue.getId()); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Gets the service ticket from request scope. |
|
232
|
|
|
* |
|
233
|
|
|
* @param context the context |
|
234
|
|
|
* @return the service ticket from request scope |
|
235
|
|
|
*/ |
|
236
|
|
|
public static String getServiceTicketFromRequestScope(final RequestContext context) { |
|
237
|
|
|
return context.getRequestScope().getString(PARAMETER_SERVICE_TICKET_ID); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Adds the unauthorized redirect url to the flow scope. |
|
242
|
|
|
* |
|
243
|
|
|
* @param context the request context |
|
244
|
|
|
* @param url the uri to redirect the flow |
|
245
|
|
|
*/ |
|
246
|
|
|
public static void putUnauthorizedRedirectUrlIntoFlowScope(final RequestContext context, final URI url) { |
|
247
|
|
|
context.getFlowScope().put(PARAMETER_UNAUTHORIZED_REDIRECT_URL, url); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Gets unauthorized redirect url into flow scope. |
|
252
|
|
|
* |
|
253
|
|
|
* @param context the context |
|
254
|
|
|
* @return the unauthorized redirect url into flow scope |
|
255
|
|
|
*/ |
|
256
|
|
|
public static URI getUnauthorizedRedirectUrlIntoFlowScope(final RequestContext context) { |
|
257
|
|
|
return context.getFlowScope().get(PARAMETER_UNAUTHORIZED_REDIRECT_URL, URI.class); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Put logout requests into flow scope. |
|
262
|
|
|
* |
|
263
|
|
|
* @param context the context |
|
264
|
|
|
* @param requests the requests |
|
265
|
|
|
*/ |
|
266
|
|
|
public static void putLogoutRequests(final RequestContext context, final List<LogoutRequest> requests) { |
|
267
|
|
|
context.getFlowScope().put(PARAMETER_LOGOUT_REQUESTS, requests); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Gets the logout requests from flow scope. |
|
272
|
|
|
* |
|
273
|
|
|
* @param context the context |
|
274
|
|
|
* @return the logout requests |
|
275
|
|
|
*/ |
|
276
|
|
|
public static List<LogoutRequest> getLogoutRequests(final RequestContext context) { |
|
277
|
|
|
return (List<LogoutRequest>) context.getFlowScope().get(PARAMETER_LOGOUT_REQUESTS); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Put service into flowscope. |
|
282
|
|
|
* |
|
283
|
|
|
* @param context the context |
|
284
|
|
|
* @param service the service |
|
285
|
|
|
*/ |
|
286
|
|
|
public static void putService(final RequestContext context, final Service service) { |
|
287
|
|
|
context.getFlowScope().put(PARAMETER_SERVICE, service); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Put warning cookie value into flowscope. |
|
292
|
|
|
* |
|
293
|
|
|
* @param context the context |
|
294
|
|
|
* @param cookieValue the cookie value |
|
295
|
|
|
*/ |
|
296
|
|
|
public static void putWarningCookie(final RequestContext context, final Boolean cookieValue) { |
|
297
|
|
|
context.getFlowScope().put("warnCookieValue", cookieValue); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Gets warning cookie. |
|
302
|
|
|
* |
|
303
|
|
|
* @param context the context |
|
304
|
|
|
* @return warning cookie value, if present. |
|
305
|
|
|
*/ |
|
306
|
|
|
public static boolean getWarningCookie(final RequestContext context) { |
|
307
|
|
|
final String val = ObjectUtils.defaultIfNull(context.getFlowScope().get("warnCookieValue"), Boolean.FALSE.toString()).toString(); |
|
308
|
|
|
return Boolean.parseBoolean(val); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* Put registered service into flowscope. |
|
313
|
|
|
* |
|
314
|
|
|
* @param context the context |
|
315
|
|
|
* @param registeredService the service |
|
316
|
|
|
*/ |
|
317
|
|
|
public static void putRegisteredService(final RequestContext context, final RegisteredService registeredService) { |
|
318
|
|
|
context.getFlowScope().put(PARAMETER_REGISTERED_SERVICE, registeredService); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Gets credential. |
|
323
|
|
|
* |
|
324
|
|
|
* @param <T> the type parameter |
|
325
|
|
|
* @param context the context |
|
326
|
|
|
* @param clazz the clazz |
|
327
|
|
|
* @return the credential |
|
328
|
|
|
*/ |
|
329
|
|
|
public static <T extends Credential> T getCredential(final RequestContext context, final Class<T> clazz) { |
|
330
|
|
|
Assert.notNull(clazz, "clazz cannot be null"); |
|
331
|
|
|
final Credential credential = getCredential(context); |
|
332
|
|
|
if (credential == null) { |
|
333
|
|
|
return null; |
|
334
|
|
|
} |
|
335
|
|
|
if (!clazz.isAssignableFrom(credential.getClass())) { |
|
336
|
|
|
throw new ClassCastException("credential [" + credential.getId() |
|
337
|
|
|
+ " is of type " + credential.getClass() |
|
338
|
|
|
+ " when we were expecting " + clazz); |
|
339
|
|
|
} |
|
340
|
|
|
return (T) credential; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* Gets credential from the context. |
|
345
|
|
|
* |
|
346
|
|
|
* @param context the context |
|
347
|
|
|
* @return the credential, or null if it cant be found in the context or if it has no id. |
|
348
|
|
|
*/ |
|
349
|
|
|
public static Credential getCredential(final RequestContext context) { |
|
350
|
|
|
final Credential cFromRequest = (Credential) context.getRequestScope().get(PARAMETER_CREDENTIAL); |
|
351
|
|
|
final Credential cFromFlow = (Credential) context.getFlowScope().get(PARAMETER_CREDENTIAL); |
|
352
|
|
|
final Credential cFromConversation = (Credential) context.getConversationScope().get(PARAMETER_CREDENTIAL); |
|
353
|
|
|
|
|
354
|
|
|
Credential credential = cFromRequest; |
|
355
|
|
|
if (credential == null || StringUtils.isBlank(credential.getId())) { |
|
356
|
|
|
credential = cFromFlow; |
|
357
|
|
|
} |
|
358
|
|
|
if (credential == null || StringUtils.isBlank(credential.getId())) { |
|
359
|
|
|
credential = cFromConversation; |
|
360
|
|
|
if (credential != null && !StringUtils.isBlank(credential.getId())) { |
|
361
|
|
|
//aup and some other modules look only in flow scope via expressions, push down if needed |
|
362
|
|
|
context.getFlowScope().put(PARAMETER_CREDENTIAL, credential); |
|
363
|
|
|
} |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
if (credential == null) { |
|
367
|
|
|
final FlowSession session = context.getFlowExecutionContext().getActiveSession(); |
|
368
|
|
|
credential = session.getScope().get(PARAMETER_CREDENTIAL, Credential.class); |
|
369
|
|
|
} |
|
370
|
|
|
if (credential != null && StringUtils.isBlank(credential.getId())) { |
|
371
|
|
|
return null; |
|
372
|
|
|
} |
|
373
|
|
|
return credential; |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
/** |
|
377
|
|
|
* Puts credential into the context. |
|
378
|
|
|
* |
|
379
|
|
|
* @param context the context |
|
380
|
|
|
* @param c the c |
|
381
|
|
|
*/ |
|
382
|
|
|
public static void putCredential(final RequestContext context, final Credential c) { |
|
383
|
|
|
if (c == null) { |
|
384
|
|
|
context.getRequestScope().remove(PARAMETER_CREDENTIAL); |
|
385
|
|
|
context.getFlowScope().remove(PARAMETER_CREDENTIAL); |
|
386
|
|
|
context.getConversationScope().remove(PARAMETER_CREDENTIAL); |
|
387
|
|
|
} else { |
|
388
|
|
|
context.getRequestScope().put(PARAMETER_CREDENTIAL, c); |
|
389
|
|
|
context.getFlowScope().put(PARAMETER_CREDENTIAL, c); |
|
390
|
|
|
context.getConversationScope().put(PARAMETER_CREDENTIAL, c); |
|
391
|
|
|
} |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
|
|
395
|
|
|
/** |
|
396
|
|
|
* Is authenticating at a public workstation? |
|
397
|
|
|
* |
|
398
|
|
|
* @param ctx the ctx |
|
399
|
|
|
* @return true if the cookie value is present |
|
400
|
|
|
*/ |
|
401
|
|
|
public static boolean isAuthenticatingAtPublicWorkstation(final RequestContext ctx) { |
|
402
|
|
|
if (ctx.getFlowScope().contains(PUBLIC_WORKSTATION_ATTRIBUTE)) { |
|
403
|
|
|
LOGGER.debug("Public workstation flag detected. SSO session will be considered renewed."); |
|
404
|
|
|
return true; |
|
405
|
|
|
} |
|
406
|
|
|
return false; |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
|
|
410
|
|
|
/** |
|
411
|
|
|
* Put public workstation into the flow if request parameter present. |
|
412
|
|
|
* |
|
413
|
|
|
* @param context the context |
|
414
|
|
|
*/ |
|
415
|
|
|
public static void putPublicWorkstationToFlowIfRequestParameterPresent(final RequestContext context) { |
|
416
|
|
|
if (StringUtils.isNotBlank(context.getExternalContext().getRequestParameterMap().get(PUBLIC_WORKSTATION_ATTRIBUTE))) { |
|
417
|
|
|
context.getFlowScope().put(PUBLIC_WORKSTATION_ATTRIBUTE, Boolean.TRUE); |
|
418
|
|
|
} |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
/** |
|
422
|
|
|
* Put warn cookie if request parameter present. |
|
423
|
|
|
* |
|
424
|
|
|
* @param warnCookieGenerator the warn cookie generator |
|
425
|
|
|
* @param context the context |
|
426
|
|
|
*/ |
|
427
|
|
|
public static void putWarnCookieIfRequestParameterPresent(final CookieGenerator warnCookieGenerator, final RequestContext context) { |
|
428
|
|
|
if (warnCookieGenerator != null) { |
|
429
|
|
|
LOGGER.debug("Evaluating request to determine if warning cookie should be generated"); |
|
430
|
|
|
final HttpServletResponse response = WebUtils.getHttpServletResponseFromExternalWebflowContext(context); |
|
431
|
|
|
if (StringUtils.isNotBlank(context.getExternalContext().getRequestParameterMap().get("warn"))) { |
|
432
|
|
|
warnCookieGenerator.addCookie(response, "true"); |
|
433
|
|
|
} |
|
434
|
|
|
} else { |
|
435
|
|
|
LOGGER.debug("No warning cookie generator is defined"); |
|
436
|
|
|
} |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
/** |
|
440
|
|
|
* Put authentication into conversation scope. |
|
441
|
|
|
* |
|
442
|
|
|
* @param authentication the authentication |
|
443
|
|
|
* @param ctx the ctx |
|
444
|
|
|
*/ |
|
445
|
|
|
public static void putAuthentication(final Authentication authentication, final RequestContext ctx) { |
|
446
|
|
|
ctx.getConversationScope().put(PARAMETER_AUTHENTICATION, authentication); |
|
447
|
|
|
} |
|
448
|
|
|
|
|
449
|
|
|
/** |
|
450
|
|
|
* Gets authentication from conversation scope. |
|
451
|
|
|
* |
|
452
|
|
|
* @param ctx the ctx |
|
453
|
|
|
* @return the authentication |
|
454
|
|
|
*/ |
|
455
|
|
|
public static Authentication getAuthentication(final RequestContext ctx) { |
|
456
|
|
|
return ctx.getConversationScope().get(PARAMETER_AUTHENTICATION, Authentication.class); |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
/** |
|
460
|
|
|
* Put authentication result builder. |
|
461
|
|
|
* |
|
462
|
|
|
* @param builder the builder |
|
463
|
|
|
* @param ctx the ctx |
|
464
|
|
|
*/ |
|
465
|
|
|
public static void putAuthenticationResultBuilder(final AuthenticationResultBuilder builder, final RequestContext ctx) { |
|
466
|
|
|
ctx.getConversationScope().put(PARAMETER_AUTHENTICATION_RESULT_BUILDER, builder); |
|
467
|
|
|
} |
|
468
|
|
|
|
|
469
|
|
|
/** |
|
470
|
|
|
* Gets the authenticated principal. |
|
471
|
|
|
* |
|
472
|
|
|
* @param requestContext the request context |
|
473
|
|
|
* @param ticketRegistrySupport the ticket registry support |
|
474
|
|
|
* @return the principal |
|
475
|
|
|
*/ |
|
476
|
|
|
public static Principal getPrincipalFromRequestContext(final RequestContext requestContext, |
|
477
|
|
|
final TicketRegistrySupport ticketRegistrySupport) { |
|
478
|
|
|
final String tgt = WebUtils.getTicketGrantingTicketId(requestContext); |
|
479
|
|
|
if (StringUtils.isBlank(tgt)) { |
|
480
|
|
|
throw new IllegalArgumentException("No ticket-granting ticket could be found in the context"); |
|
481
|
|
|
} |
|
482
|
|
|
|
|
483
|
|
|
return ticketRegistrySupport.getAuthenticatedPrincipalFrom(tgt); |
|
484
|
|
|
} |
|
485
|
|
|
|
|
486
|
|
|
/** |
|
487
|
|
|
* Gets authentication result builder. |
|
488
|
|
|
* |
|
489
|
|
|
* @param ctx the ctx |
|
490
|
|
|
* @return the authentication result builder |
|
491
|
|
|
*/ |
|
492
|
|
|
public static AuthenticationResultBuilder getAuthenticationResultBuilder(final RequestContext ctx) { |
|
493
|
|
|
return ctx.getConversationScope().get(PARAMETER_AUTHENTICATION_RESULT_BUILDER, AuthenticationResultBuilder.class); |
|
494
|
|
|
} |
|
495
|
|
|
|
|
496
|
|
|
/** |
|
497
|
|
|
* Put authentication result. |
|
498
|
|
|
* |
|
499
|
|
|
* @param authenticationResult the authentication result |
|
500
|
|
|
* @param context the context |
|
501
|
|
|
*/ |
|
502
|
|
|
public static void putAuthenticationResult(final AuthenticationResult authenticationResult, final RequestContext context) { |
|
503
|
|
|
context.getConversationScope().put(PARAMETER_AUTHENTICATION_RESULT, authenticationResult); |
|
504
|
|
|
} |
|
505
|
|
|
|
|
506
|
|
|
/** |
|
507
|
|
|
* Gets authentication result builder. |
|
508
|
|
|
* |
|
509
|
|
|
* @param ctx the ctx |
|
510
|
|
|
* @return the authentication context builder |
|
511
|
|
|
*/ |
|
512
|
|
|
public static AuthenticationResult getAuthenticationResult(final RequestContext ctx) { |
|
513
|
|
|
return ctx.getConversationScope().get(PARAMETER_AUTHENTICATION_RESULT, AuthenticationResult.class); |
|
514
|
|
|
} |
|
515
|
|
|
|
|
516
|
|
|
/** |
|
517
|
|
|
* Gets http servlet request user agent. |
|
518
|
|
|
* |
|
519
|
|
|
* @return the http servlet request user agent |
|
520
|
|
|
*/ |
|
521
|
|
|
public static String getHttpServletRequestUserAgentFromRequestContext() { |
|
522
|
|
|
final HttpServletRequest httpServletRequestFromExternalWebflowContext = getHttpServletRequestFromExternalWebflowContext(); |
|
523
|
|
|
return HttpRequestUtils.getHttpServletRequestUserAgent(httpServletRequestFromExternalWebflowContext); |
|
524
|
|
|
} |
|
525
|
|
|
|
|
526
|
|
|
/** |
|
527
|
|
|
* Gets http servlet request geo location. |
|
528
|
|
|
* |
|
529
|
|
|
* @return the http servlet request geo location |
|
530
|
|
|
*/ |
|
531
|
|
|
public static GeoLocationRequest getHttpServletRequestGeoLocationFromRequestContext() { |
|
532
|
|
|
final HttpServletRequest servletRequest = getHttpServletRequestFromExternalWebflowContext(); |
|
533
|
|
|
if (servletRequest != null) { |
|
534
|
|
|
return HttpRequestUtils.getHttpServletRequestGeoLocation(servletRequest); |
|
535
|
|
|
} |
|
536
|
|
|
return null; |
|
537
|
|
|
} |
|
538
|
|
|
|
|
539
|
|
|
/** |
|
540
|
|
|
* Put geo location tracking into flow scope. |
|
541
|
|
|
* |
|
542
|
|
|
* @param context the context |
|
543
|
|
|
* @param value the value |
|
544
|
|
|
*/ |
|
545
|
|
|
public static void putGeoLocationTrackingIntoFlowScope(final RequestContext context, final Object value) { |
|
546
|
|
|
context.getFlowScope().put("trackGeoLocation", value); |
|
547
|
|
|
} |
|
548
|
|
|
|
|
549
|
|
|
/** |
|
550
|
|
|
* Put recaptcha site key into flow scope. |
|
551
|
|
|
* |
|
552
|
|
|
* @param context the context |
|
553
|
|
|
* @param value the value |
|
554
|
|
|
*/ |
|
555
|
|
|
public static void putRecaptchaSiteKeyIntoFlowScope(final RequestContext context, final Object value) { |
|
556
|
|
|
context.getFlowScope().put("recaptchaSiteKey", value); |
|
557
|
|
|
} |
|
558
|
|
|
|
|
559
|
|
|
/** |
|
560
|
|
|
* Put static authentication into flow scope. |
|
561
|
|
|
* |
|
562
|
|
|
* @param context the context |
|
563
|
|
|
* @param value the value |
|
564
|
|
|
*/ |
|
565
|
|
|
public static void putStaticAuthenticationIntoFlowScope(final RequestContext context, final Object value) { |
|
566
|
|
|
context.getFlowScope().put("staticAuthentication", value); |
|
567
|
|
|
} |
|
568
|
|
|
|
|
569
|
|
|
/** |
|
570
|
|
|
* Put static authentication into flow scope. |
|
571
|
|
|
* |
|
572
|
|
|
* @param context the context |
|
573
|
|
|
* @param value the value |
|
574
|
|
|
*/ |
|
575
|
|
|
public static void putPasswordManagementEnabled(final RequestContext context, final Boolean value) { |
|
576
|
|
|
context.getFlowScope().put("passwordManagementEnabled", value); |
|
577
|
|
|
} |
|
578
|
|
|
|
|
579
|
|
|
/** |
|
580
|
|
|
* Put tracking id into flow scope. |
|
581
|
|
|
* |
|
582
|
|
|
* @param context the context |
|
583
|
|
|
* @param value the value |
|
584
|
|
|
*/ |
|
585
|
|
|
public static void putGoogleAnalyticsTrackingIdIntoFlowScope(final RequestContext context, final Object value) { |
|
586
|
|
|
context.getFlowScope().put("googleAnalyticsTrackingId", value); |
|
587
|
|
|
} |
|
588
|
|
|
|
|
589
|
|
|
/** |
|
590
|
|
|
* Put unauthorized redirect url into flowscope. |
|
591
|
|
|
* |
|
592
|
|
|
* @param context the context |
|
593
|
|
|
* @param unauthorizedRedirectUrl the url to redirect to |
|
594
|
|
|
*/ |
|
595
|
|
|
public static void putUnauthorizedRedirectUrl(final RequestContext context, final URI unauthorizedRedirectUrl) { |
|
596
|
|
|
context.getFlowScope().put(PARAMETER_UNAUTHORIZED_REDIRECT_URL, unauthorizedRedirectUrl); |
|
597
|
|
|
} |
|
598
|
|
|
|
|
599
|
|
|
/** |
|
600
|
|
|
* Put principal. |
|
601
|
|
|
* |
|
602
|
|
|
* @param requestContext the request context |
|
603
|
|
|
* @param authenticationPrincipal the authentication principal |
|
604
|
|
|
*/ |
|
605
|
|
|
public static void putPrincipal(final RequestContext requestContext, final Principal authenticationPrincipal) { |
|
606
|
|
|
requestContext.getFlowScope().put("principal", authenticationPrincipal); |
|
607
|
|
|
} |
|
608
|
|
|
|
|
609
|
|
|
/** |
|
610
|
|
|
* Put logout redirect url. |
|
611
|
|
|
* |
|
612
|
|
|
* @param context the context |
|
613
|
|
|
* @param service the service |
|
614
|
|
|
*/ |
|
615
|
|
|
public static void putLogoutRedirectUrl(final RequestContext context, final String service) { |
|
616
|
|
|
context.getFlowScope().put("logoutRedirectUrl", service); |
|
617
|
|
|
} |
|
618
|
|
|
|
|
619
|
|
|
/** |
|
620
|
|
|
* Put remember me authentication enabled. |
|
621
|
|
|
* |
|
622
|
|
|
* @param context the context |
|
623
|
|
|
* @param enabled the enabled |
|
624
|
|
|
*/ |
|
625
|
|
|
public static void putRememberMeAuthenticationEnabled(final RequestContext context, final Boolean enabled) { |
|
626
|
|
|
context.getFlowScope().put("rememberMeAuthenticationEnabled", enabled); |
|
627
|
|
|
} |
|
628
|
|
|
|
|
629
|
|
|
/** |
|
630
|
|
|
* Put attribute consent enabled. |
|
631
|
|
|
* |
|
632
|
|
|
* @param context the context |
|
633
|
|
|
* @param enabled the enabled |
|
634
|
|
|
*/ |
|
635
|
|
|
public static void putAttributeConsentEnabled(final RequestContext context, final Boolean enabled) { |
|
636
|
|
|
context.getFlowScope().put("attributeConsentEnabled", enabled); |
|
637
|
|
|
} |
|
638
|
|
|
|
|
639
|
|
|
/** |
|
640
|
|
|
* Is remember me authentication enabled ?. |
|
641
|
|
|
* |
|
642
|
|
|
* @param context the context |
|
643
|
|
|
* @return the boolean |
|
644
|
|
|
*/ |
|
645
|
|
|
public static boolean isRememberMeAuthenticationEnabled(final RequestContext context) { |
|
646
|
|
|
return context.getFlowScope().getBoolean("rememberMeAuthenticationEnabled", false); |
|
647
|
|
|
} |
|
648
|
|
|
|
|
649
|
|
|
/** |
|
650
|
|
|
* Put resolved multifactor authentication providers into scope. |
|
651
|
|
|
* |
|
652
|
|
|
* @param context the context |
|
653
|
|
|
* @param value the value |
|
654
|
|
|
*/ |
|
655
|
|
|
public static void putResolvedMultifactorAuthenticationProviders(final RequestContext context, |
|
656
|
|
|
final Collection<MultifactorAuthenticationProvider> value) { |
|
657
|
|
|
context.getConversationScope().put("resolvedMultifactorAuthenticationProviders", value); |
|
658
|
|
|
} |
|
659
|
|
|
|
|
660
|
|
|
/** |
|
661
|
|
|
* Gets resolved multifactor authentication providers. |
|
662
|
|
|
* |
|
663
|
|
|
* @param context the context |
|
664
|
|
|
* @return the resolved multifactor authentication providers |
|
665
|
|
|
*/ |
|
666
|
|
|
public static Collection<MultifactorAuthenticationProvider> getResolvedMultifactorAuthenticationProviders(final RequestContext context) { |
|
667
|
|
|
return context.getConversationScope().get("resolvedMultifactorAuthenticationProviders", Collection.class); |
|
668
|
|
|
} |
|
669
|
|
|
|
|
670
|
|
|
/** |
|
671
|
|
|
* Sets service user interface metadata. |
|
672
|
|
|
* |
|
673
|
|
|
* @param requestContext the request context |
|
674
|
|
|
* @param mdui the mdui |
|
675
|
|
|
*/ |
|
676
|
|
|
public static void putServiceUserInterfaceMetadata(final RequestContext requestContext, final Serializable mdui) { |
|
677
|
|
|
if (mdui != null) { |
|
678
|
|
|
requestContext.getFlowScope().put(PARAMETER_SERVICE_UI_METADATA, mdui); |
|
679
|
|
|
} |
|
680
|
|
|
} |
|
681
|
|
|
|
|
682
|
|
|
/** |
|
683
|
|
|
* Gets service user interface metadata. |
|
684
|
|
|
* |
|
685
|
|
|
* @param <T> the type parameter |
|
686
|
|
|
* @param requestContext the request context |
|
687
|
|
|
* @param clz the clz |
|
688
|
|
|
* @return the service user interface metadata |
|
689
|
|
|
*/ |
|
690
|
|
|
public static <T> T getServiceUserInterfaceMetadata(final RequestContext requestContext, final Class<T> clz) { |
|
691
|
|
|
if (requestContext.getFlowScope().contains(PARAMETER_SERVICE_UI_METADATA)) { |
|
692
|
|
|
return requestContext.getFlowScope().get(PARAMETER_SERVICE_UI_METADATA, clz); |
|
693
|
|
|
} |
|
694
|
|
|
return null; |
|
695
|
|
|
} |
|
696
|
|
|
|
|
697
|
|
|
/** |
|
698
|
|
|
* Put service response into request scope. |
|
699
|
|
|
* |
|
700
|
|
|
* @param requestContext the request context |
|
701
|
|
|
* @param response the response |
|
702
|
|
|
*/ |
|
703
|
|
|
public static void putServiceResponseIntoRequestScope(final RequestContext requestContext, final Response response) { |
|
704
|
|
|
requestContext.getRequestScope().put("parameters", response.getAttributes()); |
|
705
|
|
|
requestContext.getRequestScope().put("url", response.getUrl()); |
|
706
|
|
|
} |
|
707
|
|
|
|
|
708
|
|
|
/** |
|
709
|
|
|
* Put service original url into request scope. |
|
710
|
|
|
* |
|
711
|
|
|
* @param requestContext the request context |
|
712
|
|
|
* @param service the service |
|
713
|
|
|
*/ |
|
714
|
|
|
public static void putServiceOriginalUrlIntoRequestScope(final RequestContext requestContext, final WebApplicationService service) { |
|
715
|
|
|
requestContext.getRequestScope().put("originalUrl", service.getOriginalUrl()); |
|
716
|
|
|
} |
|
717
|
|
|
|
|
718
|
|
|
/** |
|
719
|
|
|
* Produce unauthorized error view model and view. |
|
720
|
|
|
* |
|
721
|
|
|
* @return the model and view |
|
722
|
|
|
*/ |
|
723
|
|
|
public static ModelAndView produceUnauthorizedErrorView() { |
|
724
|
|
|
return produceErrorView(new UnauthorizedServiceException(UnauthorizedServiceException.CODE_UNAUTHZ_SERVICE, StringUtils.EMPTY)); |
|
725
|
|
|
} |
|
726
|
|
|
|
|
727
|
|
|
/** |
|
728
|
|
|
* Produce error view model and view. |
|
729
|
|
|
* |
|
730
|
|
|
* @param e the e |
|
731
|
|
|
* @return the model and view |
|
732
|
|
|
*/ |
|
733
|
|
|
public static ModelAndView produceErrorView(final Exception e) { |
|
734
|
|
|
final Map model = new HashMap<>(); |
|
735
|
|
|
model.put("rootCauseException", e); |
|
736
|
|
|
return new ModelAndView(CasWebflowConstants.VIEW_ID_SERVICE_ERROR, model); |
|
737
|
|
|
} |
|
738
|
|
|
} |
|
739
|
|
|
|