-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
InitialFlowSetupAction.java
137 lines (122 loc) · 7.25 KB
/
InitialFlowSetupAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package org.apereo.cas.web.flow.login;
import org.apereo.cas.authentication.AuthenticationEventExecutionPlan;
import org.apereo.cas.authentication.AuthenticationServiceSelectionPlan;
import org.apereo.cas.authentication.credential.UsernamePasswordCredential;
import org.apereo.cas.configuration.CasConfigurationProperties;
import org.apereo.cas.services.RegisteredServiceAccessStrategyUtils;
import org.apereo.cas.services.ServicesManager;
import org.apereo.cas.services.UnauthorizedServiceException;
import org.apereo.cas.web.support.ArgumentExtractor;
import org.apereo.cas.web.support.CookieRetrievingCookieGenerator;
import org.apereo.cas.web.support.WebUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.springframework.webflow.action.AbstractAction;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;
import org.springframework.webflow.execution.repository.NoSuchFlowExecutionException;
import java.util.List;
import java.util.stream.Collectors;
/**
* Class to automatically set the paths for the CookieGenerators.
* <p>
* Note: This is technically not threadsafe, but because its overriding with a
* constant value it doesn't matter.
* <p>
* Note: As of CAS 3.1, this is a required class that retrieves and exposes the
* values in the two cookies for subclasses to use.
*
* @author Scott Battaglia
* @since 3.1
*/
@Slf4j
@RequiredArgsConstructor
public class InitialFlowSetupAction extends AbstractAction {
private final List<ArgumentExtractor> argumentExtractors;
private final ServicesManager servicesManager;
private final AuthenticationServiceSelectionPlan authenticationRequestServiceSelectionStrategies;
private final CookieRetrievingCookieGenerator ticketGrantingTicketCookieGenerator;
private final CookieRetrievingCookieGenerator warnCookieGenerator;
private final CasConfigurationProperties casProperties;
private final AuthenticationEventExecutionPlan authenticationEventExecutionPlan;
@Override
public Event doExecute(final RequestContext context) {
configureCookieGenerators(context);
configureWebflowContext(context);
configureWebflowContextForService(context);
return success();
}
private void configureWebflowContextForService(final RequestContext context) {
val service = WebUtils.getService(this.argumentExtractors, context);
if (service != null) {
LOGGER.debug("Placing service in context scope: [{}]", service.getId());
val selectedService = authenticationRequestServiceSelectionStrategies.resolveService(service);
val registeredService = this.servicesManager.findServiceBy(selectedService);
RegisteredServiceAccessStrategyUtils.ensureServiceAccessIsAllowed(registeredService);
if (registeredService != null && registeredService.getAccessStrategy().isServiceAccessAllowed()) {
LOGGER.debug("Placing registered service [{}] with id [{}] in context scope",
registeredService.getServiceId(),
registeredService.getId());
WebUtils.putRegisteredService(context, registeredService);
val accessStrategy = registeredService.getAccessStrategy();
if (accessStrategy.getUnauthorizedRedirectUrl() != null) {
LOGGER.debug("Placing registered service's unauthorized redirect url [{}] with id [{}] in context scope",
accessStrategy.getUnauthorizedRedirectUrl(),
registeredService.getServiceId());
WebUtils.putUnauthorizedRedirectUrlIntoFlowScope(context, accessStrategy.getUnauthorizedRedirectUrl());
}
}
} else if (!casProperties.getSso().isAllowMissingServiceParameter()) {
LOGGER.warn("No service authentication request is available at [{}]. CAS is configured to disable the flow.",
WebUtils.getHttpServletRequestFromExternalWebflowContext(context).getRequestURL());
throw new NoSuchFlowExecutionException(context.getFlowExecutionContext().getKey(),
new UnauthorizedServiceException("screen.service.required.message", "Service is required"));
}
WebUtils.putServiceIntoFlowScope(context, service);
}
private void configureWebflowContext(final RequestContext context) {
val request = WebUtils.getHttpServletRequestFromExternalWebflowContext(context);
WebUtils.putTicketGrantingTicketInScopes(context, this.ticketGrantingTicketCookieGenerator.retrieveCookieValue(request));
WebUtils.putWarningCookie(context, Boolean.valueOf(this.warnCookieGenerator.retrieveCookieValue(request)));
WebUtils.putGoogleAnalyticsTrackingIdIntoFlowScope(context, casProperties.getGoogleAnalytics().getGoogleAnalyticsTrackingId());
WebUtils.putGeoLocationTrackingIntoFlowScope(context, casProperties.getEvents().isTrackGeolocation());
WebUtils.putPasswordManagementEnabled(context, casProperties.getAuthn().getPm().isEnabled());
WebUtils.putRememberMeAuthenticationEnabled(context, casProperties.getTicket().getTgt().getRememberMe().isEnabled());
WebUtils.putStaticAuthenticationIntoFlowScope(context,
StringUtils.isNotBlank(casProperties.getAuthn().getAccept().getUsers())
|| StringUtils.isNotBlank(casProperties.getAuthn().getReject().getUsers()));
if (casProperties.getAuthn().getPolicy().isSourceSelectionEnabled()) {
val availableHandlers = authenticationEventExecutionPlan.getAuthenticationHandlers()
.stream()
.filter(h -> h.supports(UsernamePasswordCredential.class))
.map(h -> StringUtils.capitalize(h.getName().trim()))
.distinct()
.sorted()
.collect(Collectors.toList());
WebUtils.putAvailableAuthenticationHandleNames(context, availableHandlers);
}
}
private void configureCookieGenerators(final RequestContext context) {
val contextPath = context.getExternalContext().getContextPath();
val cookiePath = StringUtils.isNotBlank(contextPath) ? contextPath + '/' : "/";
if (StringUtils.isBlank(this.warnCookieGenerator.getCookiePath())) {
LOGGER.info("Setting path for cookies for warn cookie generator to: [{}] ", cookiePath);
this.warnCookieGenerator.setCookiePath(cookiePath);
} else {
LOGGER.trace("Warning cookie path is set to [{}] and path [{}]", this.warnCookieGenerator.getCookieDomain(),
this.warnCookieGenerator.getCookiePath());
}
if (StringUtils.isBlank(this.ticketGrantingTicketCookieGenerator.getCookiePath())) {
LOGGER.debug("Setting path for cookies for TGC cookie generator to: [{}] ", cookiePath);
this.ticketGrantingTicketCookieGenerator.setCookiePath(cookiePath);
} else {
LOGGER.trace("TGC cookie path is set to [{}] and path [{}]", this.ticketGrantingTicketCookieGenerator.getCookieDomain(),
this.ticketGrantingTicketCookieGenerator.getCookiePath());
}
}
public ServicesManager getServicesManager() {
return servicesManager;
}
}