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
|
|
|
* Put logout requests into flow scope. |
252
|
|
|
* |
253
|
|
|
* @param context the context |
254
|
|
|
* @param requests the requests |
255
|
|
|
*/ |
256
|
|
|
public static void putLogoutRequests(final RequestContext context, final List<LogoutRequest> requests) { |
257
|
|
|
context.getFlowScope().put(PARAMETER_LOGOUT_REQUESTS, requests); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Gets the logout requests from flow scope. |
262
|
|
|
* |
263
|
|
|
* @param context the context |
264
|
|
|
* @return the logout requests |
265
|
|
|
*/ |
266
|
|
|
public static List<LogoutRequest> getLogoutRequests(final RequestContext context) { |
267
|
|
|
return (List<LogoutRequest>) context.getFlowScope().get(PARAMETER_LOGOUT_REQUESTS); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Put service into flowscope. |
272
|
|
|
* |
273
|
|
|
* @param context the context |
274
|
|
|
* @param service the service |
275
|
|
|
*/ |
276
|
|
|
public static void putService(final RequestContext context, final Service service) { |
277
|
|
|
context.getFlowScope().put(PARAMETER_SERVICE, service); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Put warning cookie value into flowscope. |
282
|
|
|
* |
283
|
|
|
* @param context the context |
284
|
|
|
* @param cookieValue the cookie value |
285
|
|
|
*/ |
286
|
|
|
public static void putWarningCookie(final RequestContext context, final Boolean cookieValue) { |
287
|
|
|
context.getFlowScope().put("warnCookieValue", cookieValue); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Gets warning cookie. |
292
|
|
|
* |
293
|
|
|
* @param context the context |
294
|
|
|
* @return warning cookie value, if present. |
295
|
|
|
*/ |
296
|
|
|
public static boolean getWarningCookie(final RequestContext context) { |
297
|
|
|
final String val = ObjectUtils.defaultIfNull(context.getFlowScope().get("warnCookieValue"), Boolean.FALSE.toString()).toString(); |
298
|
|
|
return Boolean.parseBoolean(val); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Put registered service into flowscope. |
303
|
|
|
* |
304
|
|
|
* @param context the context |
305
|
|
|
* @param registeredService the service |
306
|
|
|
*/ |
307
|
|
|
public static void putRegisteredService(final RequestContext context, final RegisteredService registeredService) { |
308
|
|
|
context.getFlowScope().put(PARAMETER_REGISTERED_SERVICE, registeredService); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Gets credential. |
313
|
|
|
* |
314
|
|
|
* @param <T> the type parameter |
315
|
|
|
* @param context the context |
316
|
|
|
* @param clazz the clazz |
317
|
|
|
* @return the credential |
318
|
|
|
*/ |
319
|
|
|
public static <T extends Credential> T getCredential(final RequestContext context, final Class<T> clazz) { |
320
|
|
|
Assert.notNull(clazz, "clazz cannot be null"); |
321
|
|
|
final Credential credential = getCredential(context); |
322
|
|
|
if (credential == null) { |
323
|
|
|
return null; |
324
|
|
|
} |
325
|
|
|
if (!clazz.isAssignableFrom(credential.getClass())) { |
326
|
|
|
throw new ClassCastException("credential [" + credential.getId() |
327
|
|
|
+ " is of type " + credential.getClass() |
328
|
|
|
+ " when we were expecting " + clazz); |
329
|
|
|
} |
330
|
|
|
return (T) credential; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Gets credential from the context. |
335
|
|
|
* |
336
|
|
|
* @param context the context |
337
|
|
|
* @return the credential, or null if it cant be found in the context or if it has no id. |
338
|
|
|
*/ |
339
|
|
|
public static Credential getCredential(final RequestContext context) { |
340
|
|
|
final Credential cFromRequest = (Credential) context.getRequestScope().get(PARAMETER_CREDENTIAL); |
341
|
|
|
final Credential cFromFlow = (Credential) context.getFlowScope().get(PARAMETER_CREDENTIAL); |
342
|
|
|
final Credential cFromConversation = (Credential) context.getConversationScope().get(PARAMETER_CREDENTIAL); |
343
|
|
|
|
344
|
|
|
Credential credential = cFromRequest; |
345
|
|
|
if (credential == null || StringUtils.isBlank(credential.getId())) { |
346
|
|
|
credential = cFromFlow; |
347
|
|
|
} |
348
|
|
|
if (credential == null || StringUtils.isBlank(credential.getId())) { |
349
|
|
|
credential = cFromConversation; |
350
|
|
|
if (credential != null && !StringUtils.isBlank(credential.getId())) { |
351
|
|
|
//aup and some other modules look only in flow scope via expressions, push down if needed |
352
|
|
|
context.getFlowScope().put(PARAMETER_CREDENTIAL, credential); |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
if (credential == null) { |
357
|
|
|
final FlowSession session = context.getFlowExecutionContext().getActiveSession(); |
358
|
|
|
credential = session.getScope().get(PARAMETER_CREDENTIAL, Credential.class); |
359
|
|
|
} |
360
|
|
|
if (credential != null && StringUtils.isBlank(credential.getId())) { |
361
|
|
|
return null; |
362
|
|
|
} |
363
|
|
|
return credential; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Puts credential into the context. |
368
|
|
|
* |
369
|
|
|
* @param context the context |
370
|
|
|
* @param c the c |
371
|
|
|
*/ |
372
|
|
|
public static void putCredential(final RequestContext context, final Credential c) { |
373
|
|
|
if (c == null) { |
374
|
|
|
context.getRequestScope().remove(PARAMETER_CREDENTIAL); |
375
|
|
|
context.getFlowScope().remove(PARAMETER_CREDENTIAL); |
376
|
|
|
context.getConversationScope().remove(PARAMETER_CREDENTIAL); |
377
|
|
|
} else { |
378
|
|
|
context.getRequestScope().put(PARAMETER_CREDENTIAL, c); |
379
|
|
|
context.getFlowScope().put(PARAMETER_CREDENTIAL, c); |
380
|
|
|
context.getConversationScope().put(PARAMETER_CREDENTIAL, c); |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Is authenticating at a public workstation? |
387
|
|
|
* |
388
|
|
|
* @param ctx the ctx |
389
|
|
|
* @return true if the cookie value is present |
390
|
|
|
*/ |
391
|
|
|
public static boolean isAuthenticatingAtPublicWorkstation(final RequestContext ctx) { |
392
|
|
|
if (ctx.getFlowScope().contains(PUBLIC_WORKSTATION_ATTRIBUTE)) { |
393
|
|
|
LOGGER.debug("Public workstation flag detected. SSO session will be considered renewed."); |
394
|
|
|
return true; |
395
|
|
|
} |
396
|
|
|
return false; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Put public workstation into the flow if request parameter present. |
402
|
|
|
* |
403
|
|
|
* @param context the context |
404
|
|
|
*/ |
405
|
|
|
public static void putPublicWorkstationToFlowIfRequestParameterPresent(final RequestContext context) { |
406
|
|
|
if (StringUtils.isNotBlank(context.getExternalContext().getRequestParameterMap().get(PUBLIC_WORKSTATION_ATTRIBUTE))) { |
407
|
|
|
context.getFlowScope().put(PUBLIC_WORKSTATION_ATTRIBUTE, Boolean.TRUE); |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Put warn cookie if request parameter present. |
413
|
|
|
* |
414
|
|
|
* @param warnCookieGenerator the warn cookie generator |
415
|
|
|
* @param context the context |
416
|
|
|
*/ |
417
|
|
|
public static void putWarnCookieIfRequestParameterPresent(final CookieGenerator warnCookieGenerator, final RequestContext context) { |
418
|
|
|
if (warnCookieGenerator != null) { |
419
|
|
|
LOGGER.debug("Evaluating request to determine if warning cookie should be generated"); |
420
|
|
|
final HttpServletResponse response = WebUtils.getHttpServletResponseFromExternalWebflowContext(context); |
421
|
|
|
if (StringUtils.isNotBlank(context.getExternalContext().getRequestParameterMap().get("warn"))) { |
422
|
|
|
warnCookieGenerator.addCookie(response, "true"); |
423
|
|
|
} |
424
|
|
|
} else { |
425
|
|
|
LOGGER.debug("No warning cookie generator is defined"); |
426
|
|
|
} |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Put authentication into conversation scope. |
431
|
|
|
* |
432
|
|
|
* @param authentication the authentication |
433
|
|
|
* @param ctx the ctx |
434
|
|
|
*/ |
435
|
|
|
public static void putAuthentication(final Authentication authentication, final RequestContext ctx) { |
436
|
|
|
ctx.getConversationScope().put(PARAMETER_AUTHENTICATION, authentication); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Gets authentication from conversation scope. |
441
|
|
|
* |
442
|
|
|
* @param ctx the ctx |
443
|
|
|
* @return the authentication |
444
|
|
|
*/ |
445
|
|
|
public static Authentication getAuthentication(final RequestContext ctx) { |
446
|
|
|
return ctx.getConversationScope().get(PARAMETER_AUTHENTICATION, Authentication.class); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* Put authentication result builder. |
451
|
|
|
* |
452
|
|
|
* @param builder the builder |
453
|
|
|
* @param ctx the ctx |
454
|
|
|
*/ |
455
|
|
|
public static void putAuthenticationResultBuilder(final AuthenticationResultBuilder builder, final RequestContext ctx) { |
456
|
|
|
ctx.getConversationScope().put(PARAMETER_AUTHENTICATION_RESULT_BUILDER, builder); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* Gets the authenticated principal. |
461
|
|
|
* |
462
|
|
|
* @param requestContext the request context |
463
|
|
|
* @param ticketRegistrySupport the ticket registry support |
464
|
|
|
* @return the principal |
465
|
|
|
*/ |
466
|
|
|
public static Principal getPrincipalFromRequestContext(final RequestContext requestContext, |
467
|
|
|
final TicketRegistrySupport ticketRegistrySupport) { |
468
|
|
|
final String tgt = WebUtils.getTicketGrantingTicketId(requestContext); |
469
|
|
|
if (StringUtils.isBlank(tgt)) { |
470
|
|
|
throw new IllegalArgumentException("No ticket-granting ticket could be found in the context"); |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
return ticketRegistrySupport.getAuthenticatedPrincipalFrom(tgt); |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* Gets authentication result builder. |
478
|
|
|
* |
479
|
|
|
* @param ctx the ctx |
480
|
|
|
* @return the authentication result builder |
481
|
|
|
*/ |
482
|
|
|
public static AuthenticationResultBuilder getAuthenticationResultBuilder(final RequestContext ctx) { |
483
|
|
|
return ctx.getConversationScope().get(PARAMETER_AUTHENTICATION_RESULT_BUILDER, AuthenticationResultBuilder.class); |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* Put authentication result. |
488
|
|
|
* |
489
|
|
|
* @param authenticationResult the authentication result |
490
|
|
|
* @param context the context |
491
|
|
|
*/ |
492
|
|
|
public static void putAuthenticationResult(final AuthenticationResult authenticationResult, final RequestContext context) { |
493
|
|
|
context.getConversationScope().put(PARAMETER_AUTHENTICATION_RESULT, authenticationResult); |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* Gets authentication result builder. |
498
|
|
|
* |
499
|
|
|
* @param ctx the ctx |
500
|
|
|
* @return the authentication context builder |
501
|
|
|
*/ |
502
|
|
|
public static AuthenticationResult getAuthenticationResult(final RequestContext ctx) { |
503
|
|
|
return ctx.getConversationScope().get(PARAMETER_AUTHENTICATION_RESULT, AuthenticationResult.class); |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Gets http servlet request user agent. |
508
|
|
|
* |
509
|
|
|
* @return the http servlet request user agent |
510
|
|
|
*/ |
511
|
|
|
public static String getHttpServletRequestUserAgentFromRequestContext() { |
512
|
|
|
final HttpServletRequest httpServletRequestFromExternalWebflowContext = getHttpServletRequestFromExternalWebflowContext(); |
513
|
|
|
return HttpRequestUtils.getHttpServletRequestUserAgent(httpServletRequestFromExternalWebflowContext); |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
/** |
517
|
|
|
* Gets http servlet request geo location. |
518
|
|
|
* |
519
|
|
|
* @return the http servlet request geo location |
520
|
|
|
*/ |
521
|
|
|
public static GeoLocationRequest getHttpServletRequestGeoLocationFromRequestContext() { |
522
|
|
|
final HttpServletRequest servletRequest = getHttpServletRequestFromExternalWebflowContext(); |
523
|
|
|
if (servletRequest != null) { |
524
|
|
|
return HttpRequestUtils.getHttpServletRequestGeoLocation(servletRequest); |
525
|
|
|
} |
526
|
|
|
return null; |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
/** |
530
|
|
|
* Put geo location tracking into flow scope. |
531
|
|
|
* |
532
|
|
|
* @param context the context |
533
|
|
|
* @param value the value |
534
|
|
|
*/ |
535
|
|
|
public static void putGeoLocationTrackingIntoFlowScope(final RequestContext context, final Object value) { |
536
|
|
|
context.getFlowScope().put("trackGeoLocation", value); |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
/** |
540
|
|
|
* Put recaptcha site key into flow scope. |
541
|
|
|
* |
542
|
|
|
* @param context the context |
543
|
|
|
* @param value the value |
544
|
|
|
*/ |
545
|
|
|
public static void putRecaptchaSiteKeyIntoFlowScope(final RequestContext context, final Object value) { |
546
|
|
|
context.getFlowScope().put("recaptchaSiteKey", value); |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
/** |
550
|
|
|
* Put static authentication into flow scope. |
551
|
|
|
* |
552
|
|
|
* @param context the context |
553
|
|
|
* @param value the value |
554
|
|
|
*/ |
555
|
|
|
public static void putStaticAuthenticationIntoFlowScope(final RequestContext context, final Object value) { |
556
|
|
|
context.getFlowScope().put("staticAuthentication", 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 putPasswordManagementEnabled(final RequestContext context, final Boolean value) { |
566
|
|
|
context.getFlowScope().put("passwordManagementEnabled", value); |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
/** |
570
|
|
|
* Put tracking id into flow scope. |
571
|
|
|
* |
572
|
|
|
* @param context the context |
573
|
|
|
* @param value the value |
574
|
|
|
*/ |
575
|
|
|
public static void putGoogleAnalyticsTrackingIdIntoFlowScope(final RequestContext context, final Object value) { |
576
|
|
|
context.getFlowScope().put("googleAnalyticsTrackingId", value); |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
/** |
580
|
|
|
* Put unauthorized redirect url into flowscope. |
581
|
|
|
* |
582
|
|
|
* @param context the context |
583
|
|
|
* @param unauthorizedRedirectUrl the url to redirect to |
584
|
|
|
*/ |
585
|
|
|
public static void putUnauthorizedRedirectUrl(final RequestContext context, final URI unauthorizedRedirectUrl) { |
586
|
|
|
context.getFlowScope().put(PARAMETER_UNAUTHORIZED_REDIRECT_URL, unauthorizedRedirectUrl); |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* Put principal. |
591
|
|
|
* |
592
|
|
|
* @param requestContext the request context |
593
|
|
|
* @param authenticationPrincipal the authentication principal |
594
|
|
|
*/ |
595
|
|
|
public static void putPrincipal(final RequestContext requestContext, final Principal authenticationPrincipal) { |
596
|
|
|
requestContext.getFlowScope().put("principal", authenticationPrincipal); |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
/** |
600
|
|
|
* Put logout redirect url. |
601
|
|
|
* |
602
|
|
|
* @param context the context |
603
|
|
|
* @param service the service |
604
|
|
|
*/ |
605
|
|
|
public static void putLogoutRedirectUrl(final RequestContext context, final String service) { |
606
|
|
|
context.getFlowScope().put("logoutRedirectUrl", service); |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
/** |
610
|
|
|
* Put remember me authentication enabled. |
611
|
|
|
* |
612
|
|
|
* @param context the context |
613
|
|
|
* @param enabled the enabled |
614
|
|
|
*/ |
615
|
|
|
public static void putRememberMeAuthenticationEnabled(final RequestContext context, final Boolean enabled) { |
616
|
|
|
context.getFlowScope().put("rememberMeAuthenticationEnabled", enabled); |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
/** |
620
|
|
|
* Put attribute consent enabled. |
621
|
|
|
* |
622
|
|
|
* @param context the context |
623
|
|
|
* @param enabled the enabled |
624
|
|
|
*/ |
625
|
|
|
public static void putAttributeConsentEnabled(final RequestContext context, final Boolean enabled) { |
626
|
|
|
context.getFlowScope().put("attributeConsentEnabled", enabled); |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
/** |
630
|
|
|
* Is remember me authentication enabled ?. |
631
|
|
|
* |
632
|
|
|
* @param context the context |
633
|
|
|
* @return the boolean |
634
|
|
|
*/ |
635
|
|
|
public static boolean isRememberMeAuthenticationEnabled(final RequestContext context) { |
636
|
|
|
return context.getFlowScope().getBoolean("rememberMeAuthenticationEnabled", false); |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
/** |
640
|
|
|
* Put resolved multifactor authentication providers into scope. |
641
|
|
|
* |
642
|
|
|
* @param context the context |
643
|
|
|
* @param value the value |
644
|
|
|
*/ |
645
|
|
|
public static void putResolvedMultifactorAuthenticationProviders(final RequestContext context, |
646
|
|
|
final Collection<MultifactorAuthenticationProvider> value) { |
647
|
|
|
context.getConversationScope().put("resolvedMultifactorAuthenticationProviders", value); |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
/** |
651
|
|
|
* Gets resolved multifactor authentication providers. |
652
|
|
|
* |
653
|
|
|
* @param context the context |
654
|
|
|
* @return the resolved multifactor authentication providers |
655
|
|
|
*/ |
656
|
|
|
public static Collection<MultifactorAuthenticationProvider> getResolvedMultifactorAuthenticationProviders(final RequestContext context) { |
657
|
|
|
return context.getConversationScope().get("resolvedMultifactorAuthenticationProviders", Collection.class); |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
/** |
661
|
|
|
* Sets service user interface metadata. |
662
|
|
|
* |
663
|
|
|
* @param requestContext the request context |
664
|
|
|
* @param mdui the mdui |
665
|
|
|
*/ |
666
|
|
|
public static void putServiceUserInterfaceMetadata(final RequestContext requestContext, final Serializable mdui) { |
667
|
|
|
if (mdui != null) { |
668
|
|
|
requestContext.getFlowScope().put(PARAMETER_SERVICE_UI_METADATA, mdui); |
669
|
|
|
} |
670
|
|
|
} |
671
|
|
|
|
672
|
|
|
/** |
673
|
|
|
* Gets service user interface metadata. |
674
|
|
|
* |
675
|
|
|
* @param <T> the type parameter |
676
|
|
|
* @param requestContext the request context |
677
|
|
|
* @param clz the clz |
678
|
|
|
* @return the service user interface metadata |
679
|
|
|
*/ |
680
|
|
|
public static <T> T getServiceUserInterfaceMetadata(final RequestContext requestContext, final Class<T> clz) { |
681
|
|
|
if (requestContext.getFlowScope().contains(PARAMETER_SERVICE_UI_METADATA)) { |
682
|
|
|
return requestContext.getFlowScope().get(PARAMETER_SERVICE_UI_METADATA, clz); |
683
|
|
|
} |
684
|
|
|
return null; |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
/** |
688
|
|
|
* Put service response into request scope. |
689
|
|
|
* |
690
|
|
|
* @param requestContext the request context |
691
|
|
|
* @param response the response |
692
|
|
|
*/ |
693
|
|
|
public static void putServiceResponseIntoRequestScope(final RequestContext requestContext, final Response response) { |
694
|
|
|
requestContext.getRequestScope().put("parameters", response.getAttributes()); |
695
|
|
|
requestContext.getRequestScope().put("url", response.getUrl()); |
696
|
|
|
} |
697
|
|
|
|
698
|
|
|
/** |
699
|
|
|
* Put service original url into request scope. |
700
|
|
|
* |
701
|
|
|
* @param requestContext the request context |
702
|
|
|
* @param service the service |
703
|
|
|
*/ |
704
|
|
|
public static void putServiceOriginalUrlIntoRequestScope(final RequestContext requestContext, final WebApplicationService service) { |
705
|
|
|
requestContext.getRequestScope().put("originalUrl", service.getOriginalUrl()); |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
/** |
709
|
|
|
* Produce unauthorized error view model and view. |
710
|
|
|
* |
711
|
|
|
* @return the model and view |
712
|
|
|
*/ |
713
|
|
|
public static ModelAndView produceUnauthorizedErrorView() { |
714
|
|
|
return produceErrorView(new UnauthorizedServiceException(UnauthorizedServiceException.CODE_UNAUTHZ_SERVICE, StringUtils.EMPTY)); |
715
|
|
|
} |
716
|
|
|
|
717
|
|
|
/** |
718
|
|
|
* Produce error view model and view. |
719
|
|
|
* |
720
|
|
|
* @param e the e |
721
|
|
|
* @return the model and view |
722
|
|
|
*/ |
723
|
|
|
public static ModelAndView produceErrorView(final Exception e) { |
724
|
|
|
final Map model = new HashMap<>(); |
725
|
|
|
model.put("rootCauseException", e); |
726
|
|
|
return new ModelAndView(CasWebflowConstants.VIEW_ID_SERVICE_ERROR, model); |
727
|
|
|
} |
728
|
|
|
} |
729
|
|
|
|