001/*
002 *  Copyright 2016 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.site;
017
018import java.util.ArrayList;
019import java.util.Arrays;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.parameters.Parameters;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Redirector;
026import org.apache.cocoon.environment.Request;
027import org.apache.cocoon.environment.SourceResolver;
028import org.apache.commons.lang3.StringUtils;
029
030import org.ametys.core.authentication.CredentialProvider;
031import org.ametys.core.user.population.UserPopulation;
032import org.ametys.runtime.authentication.AccessDeniedException;
033
034/**
035 * This action will authenticate upon a parametrized blocking credential provider
036 */
037public class FrontBlockingCredentialProviderAction extends FrontAuthenticateAction
038{
039    @Override
040    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
041    {
042        Request request = ObjectModelHelper.getRequest(objectModel);
043
044        if (_validateCurrentlyConnectedUser(request, redirector, parameters))
045        {
046            // We passed the authentication, let's mark it now
047            request.setAttribute(REQUEST_ATTRIBUTE_AUTHENTICATED, "true");
048            
049            // We passed the authentication (with a user)
050            return EMPTY_MAP;
051        }
052        
053        // At this point, we already know that the entire process will be executed, whatever the outcome
054        // Set the flag, so that the authentication process won't repeat
055        request.setAttribute(REQUEST_ATTRIBUTE_AUTHENTICATED, "true");
056        
057        List<UserPopulation> chosenUserPopulations = new ArrayList<>();
058        List<CredentialProvider> credentialProviders = new ArrayList<>();
059        if (!_prepareUserPopulationsAndCredentialProviders(request, parameters, null, chosenUserPopulations, credentialProviders))
060        {
061            // The population was not determined (session expired?), so let's finish... that will close the popup and reload to restart the authentication process
062            return EMPTY_MAP;
063        }
064
065        int credentialProviderIndex = Integer.parseInt(source);
066        request.getSession(true).setAttribute(SESSION_CONNECTING_CREDENTIALPROVIDER_INDEX_LASTBLOCKINGKNOWN, credentialProviderIndex);
067        CredentialProvider credentialProvider = credentialProviders.get(credentialProviderIndex);
068        
069        if (_process(request, true, credentialProvider, credentialProviderIndex, redirector, chosenUserPopulations))
070        {
071            // Whatever the user was correctly authenticated or he just required a redirect: let's stop here for the moment
072            return EMPTY_MAP;
073        }
074        
075        throw new AccessDeniedException();
076    }
077    
078    @Override
079    protected List<String> _getContexts(Request request, Parameters parameters)
080    {
081        String contextAsString = request.getParameter("contexts");
082        return Arrays.asList(StringUtils.split(contextAsString, ","));
083    }
084}