001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.webengine.jaxrs.login;
013
014import java.util.Map;
015
016import javax.security.auth.login.LoginContext;
017import javax.security.auth.login.LoginException;
018import javax.servlet.http.HttpServletRequest;
019import javax.servlet.http.HttpServletResponse;
020
021import org.nuxeo.ecm.webengine.jaxrs.BundleNotFoundException;
022import org.nuxeo.ecm.webengine.jaxrs.Utils;
023
024/**
025 * An authentication handlers that delegate the authentication to the first registered handler that knows how to
026 * authenticate.
027 *
028 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
029 */
030public class CompositeAuthenticationHandler implements AuthenticationHandler {
031
032    protected AuthenticationHandler[] handlers;
033
034    public CompositeAuthenticationHandler(String classRefs) throws ReflectiveOperationException,
035            BundleNotFoundException {
036        handlers = Utils.newInstances(AuthenticationHandler.class, classRefs);
037    }
038
039    public CompositeAuthenticationHandler(AuthenticationHandler[] handlers) {
040        this.handlers = handlers;
041    }
042
043    @Override
044    public void init(Map<String, String> properties) {
045        // do nothing
046    }
047
048    @Override
049    public LoginContext handleAuthentication(HttpServletRequest request, HttpServletResponse response)
050            throws LoginException {
051        for (AuthenticationHandler handler : handlers) {
052            LoginContext lc = handler.handleAuthentication(request, response);
053            if (lc != null) {
054                return lc;
055            }
056        }
057        return null;
058    }
059
060}