2 * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package edu.internet2.middleware.shibboleth.idp.profile;
19 import java.util.HashMap;
20 import java.util.List;
22 import java.util.Timer;
23 import java.util.concurrent.locks.Lock;
25 import javax.servlet.ServletRequest;
26 import javax.servlet.http.HttpServletRequest;
28 import org.apache.log4j.Logger;
29 import org.opensaml.util.resource.Resource;
30 import org.opensaml.xml.util.Pair;
31 import org.springframework.context.ApplicationContext;
33 import edu.internet2.middleware.shibboleth.common.config.BaseReloadableService;
34 import edu.internet2.middleware.shibboleth.common.profile.AbstractErrorHandler;
35 import edu.internet2.middleware.shibboleth.common.profile.ProfileHandler;
36 import edu.internet2.middleware.shibboleth.common.profile.ProfileHandlerManager;
37 import edu.internet2.middleware.shibboleth.common.profile.provider.AbstractRequestURIMappedProfileHandler;
38 import edu.internet2.middleware.shibboleth.idp.authn.AuthenticationHandler;
39 import edu.internet2.middleware.shibboleth.idp.authn.LoginContext;
42 * Implementation of a {@link ProfileHandlerManager} that maps the request path, without the servlet context, to a
43 * profile handler and adds support for authentication handlers.
45 public class IdPProfileHandlerManager extends BaseReloadableService implements ProfileHandlerManager {
48 private final Logger log = Logger.getLogger(IdPProfileHandlerManager.class);
50 /** Handler used for errors. */
51 private AbstractErrorHandler errorHandler;
53 /** Map of request paths to profile handlers. */
54 private Map<String, AbstractRequestURIMappedProfileHandler> profileHandlers;
56 /** Map of authentication methods to authentication handlers. */
57 private Map<String, AuthenticationHandler> authenticationHandlers;
60 * Constructor. Configuration resources are not monitored for changes.
62 * @param configurations configuration resources for this service
64 public IdPProfileHandlerManager(List<Resource> configurations) {
65 super(configurations);
66 profileHandlers = new HashMap<String, AbstractRequestURIMappedProfileHandler>();
67 authenticationHandlers = new HashMap<String, AuthenticationHandler>();
73 * @param timer timer resource polling tasks are scheduled with
74 * @param configurations configuration resources for this service
75 * @param pollingFrequency the frequency, in milliseconds, to poll the policy resources for changes, must be greater
78 public IdPProfileHandlerManager(List<Resource> configurations, Timer timer, long pollingFrequency) {
79 super(timer, configurations, pollingFrequency);
80 profileHandlers = new HashMap<String, AbstractRequestURIMappedProfileHandler>();
81 authenticationHandlers = new HashMap<String, AuthenticationHandler>();
85 public AbstractErrorHandler getErrorHandler() {
90 * Sets the error handler.
92 * @param handler error handler
94 public void setErrorHandler(AbstractErrorHandler handler) {
95 if (handler == null) {
96 throw new IllegalArgumentException("Error handler may not be null");
98 errorHandler = handler;
102 public ProfileHandler getProfileHandler(ServletRequest request) {
103 ProfileHandler handler;
105 String requestPath = ((HttpServletRequest) request).getPathInfo();
106 if (log.isDebugEnabled()) {
107 log.debug(getId() + ": Looking up profile handler for request path: " + requestPath);
109 Lock readLock = getReadWriteLock().readLock();
111 handler = profileHandlers.get(requestPath);
114 if (handler != null) {
115 if (log.isDebugEnabled()) {
116 log.debug(getId() + ": Located profile handler of the following type for request path "
117 + requestPath + ": " + handler.getClass().getName());
120 if (log.isDebugEnabled()) {
121 log.debug(getId() + ": No profile handler registered for request path " + requestPath);
128 * Gets the registered profile handlers.
130 * @return registered profile handlers
132 public Map<String, AbstractRequestURIMappedProfileHandler> getProfileHandlers() {
133 return profileHandlers;
137 * Gets the authentication handler appropriate for the given loging context. The mechanism used to determine the
138 * "appropriate" handler is implementation specific.
140 * @param loginContext current login context
142 * @return authentication method URI and handler appropriate for given login context
144 public Pair<String, AuthenticationHandler> getAuthenticationHandler(LoginContext loginContext) {
145 if (log.isDebugEnabled()) {
146 log.debug(getId() + ": Looking up authentication method for relying party "
147 + loginContext.getRelyingPartyId());
149 List<String> requestedMethods = loginContext.getRequestedAuthenticationMethods();
150 if (requestedMethods != null) {
151 AuthenticationHandler candidateHandler;
152 for (String requestedMethod : requestedMethods) {
153 if (log.isDebugEnabled()) {
154 log.debug(getId() + ": Checking for authentication handler for method " + requestedMethod
155 + " which was requested for relying party " + loginContext.getRelyingPartyId());
157 candidateHandler = authenticationHandlers.get(requestedMethod);
158 if (candidateHandler != null) {
159 if (log.isDebugEnabled()) {
160 log.debug(getId() + ": Authentication handler for method " + requestedMethod
161 + " for relying party " + loginContext.getRelyingPartyId()
162 + " found. Checking if it meets othe criteria.");
164 if(loginContext.getPassiveAuth() && !candidateHandler.supportsPassive()){
165 if (log.isDebugEnabled()) {
166 log.debug(getId() + ": Authentication handler for method " + requestedMethod
167 + " for relying party " + loginContext.getRelyingPartyId()
168 + " does not meet required support for passive auth. Skipping it");
173 if (log.isDebugEnabled()) {
174 log.debug(getId() + ": Authentication handler for method " + requestedMethod
175 + " for relying party " + loginContext.getRelyingPartyId()
176 + " meets all requirements, using it.");
178 return new Pair<String, AuthenticationHandler>(requestedMethod, candidateHandler);
182 log.error(getId() + ": No requested authentication methods for relying party "
183 + loginContext.getRelyingPartyId());
190 * Gets the registered authentication handlers.
192 * @return registered authentication handlers
194 public Map<String, AuthenticationHandler> getAuthenticationHandlers() {
195 return authenticationHandlers;
199 protected void newContextCreated(ApplicationContext newServiceContext) {
200 if (log.isDebugEnabled()) {
201 log.debug(getId() + ": Loading new configuration into service");
203 Lock writeLock = getReadWriteLock().writeLock();
205 loadNewErrorHandler(newServiceContext);
206 loadNewProfileHandlers(newServiceContext);
207 loadNewAuthenticationHandlers(newServiceContext);
212 * Reads the new error handler from the newly created application context and loads it into this manager.
214 * @param newServiceContext newly created application context
216 protected void loadNewErrorHandler(ApplicationContext newServiceContext) {
217 String[] errorBeanNames = newServiceContext.getBeanNamesForType(AbstractErrorHandler.class);
218 if (log.isDebugEnabled()) {
219 log.debug(getId() + ": Loading " + errorBeanNames.length + " new error handler.");
222 errorHandler = (AbstractErrorHandler) newServiceContext.getBean(errorBeanNames[0]);
223 if (log.isDebugEnabled()) {
224 log.debug(getId() + ": Loaded new error handler of type: " + errorHandler.getClass().getName());
229 * Reads the new profile handlers from the newly created application context and loads it into this manager.
231 * @param newServiceContext newly created application context
233 protected void loadNewProfileHandlers(ApplicationContext newServiceContext) {
234 String[] profileBeanNames = newServiceContext.getBeanNamesForType(AbstractRequestURIMappedProfileHandler.class);
235 if (log.isDebugEnabled()) {
236 log.debug(getId() + ": Loading " + profileBeanNames.length + " new profile handlers.");
239 profileHandlers.clear();
240 AbstractRequestURIMappedProfileHandler<?,?> profileHandler;
241 for (String profileBeanName : profileBeanNames) {
242 profileHandler = (AbstractRequestURIMappedProfileHandler) newServiceContext.getBean(profileBeanName);
243 for (String requestPath : profileHandler.getRequestPaths()) {
244 profileHandlers.put(requestPath, profileHandler);
245 if (log.isDebugEnabled()) {
246 log.debug(getId() + ": Loaded profile handler of type "
247 + profileHandler.getClass().getName() + " handling requests to request path "
255 * Reads the new authentication handlers from the newly created application context and loads it into this manager.
257 * @param newServiceContext newly created application context
259 protected void loadNewAuthenticationHandlers(ApplicationContext newServiceContext) {
260 String[] authnBeanNames = newServiceContext.getBeanNamesForType(AuthenticationHandler.class);
261 if (log.isDebugEnabled()) {
262 log.debug(getId() + ": Loading " + authnBeanNames.length + " new authentication handlers.");
265 authenticationHandlers.clear();
266 AuthenticationHandler authnHandler;
267 for (String authnBeanName : authnBeanNames) {
268 authnHandler = (AuthenticationHandler) newServiceContext.getBean(authnBeanName);
269 if (log.isDebugEnabled()) {
270 log.debug(getId() + ": Loading authentication handler of type "
271 + authnHandler.getClass().getName() + " supporting authentication methods: "
272 + authnHandler.getSupportedAuthenticationMethods());
274 for (String authnMethod : authnHandler.getSupportedAuthenticationMethods()) {
275 authenticationHandlers.put(authnMethod, authnHandler);