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.core.authentication;
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.user.population.UserPopulation;
031import org.ametys.runtime.authentication.AuthorizationRequiredException;
032
033/**
034 * This action will authenticate upon a parametrized blocking credential provider
035 */
036public class BlockingCredentialProviderAction extends AuthenticateAction
037{
038    @Override
039    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
040    {
041        Request request = ObjectModelHelper.getRequest(objectModel);
042
043        if (_validateCurrentlyConnectedUser(request, redirector, parameters))
044        {
045            // We passed the authentication, let's mark it now
046            request.setAttribute(REQUEST_ATTRIBUTE_AUTHENTICATED, "true");
047            
048            // We passed the authentication (with a user)
049            return EMPTY_MAP;
050        }
051        
052        // At this point, we already know that the entire process will be executed, whatever the outcome
053        // Set the flag, so that the authentication process won't repeat
054        request.setAttribute(REQUEST_ATTRIBUTE_AUTHENTICATED, "true");
055        
056        List<UserPopulation> chosenUserPopulations = new ArrayList<>();
057        List<CredentialProvider> credentialProviders = new ArrayList<>();
058        if (!_prepareUserPopulationsAndCredentialProviders(request, parameters, null, chosenUserPopulations, credentialProviders))
059        {
060            // The population was not determined (session expired?), so let's finish... that will close the popup and reload to restart the authentication process
061            return EMPTY_MAP;
062        }
063
064        int credentialProviderIndex = Integer.parseInt(source);
065        request.getSession(true).setAttribute(SESSION_CONNECTING_CREDENTIALPROVIDER_INDEX_LASTBLOCKINGKNOWN, credentialProviderIndex);
066        CredentialProvider credentialProvider = credentialProviders.get(credentialProviderIndex);
067        
068        if (_process(request, true, credentialProvider, credentialProviderIndex, redirector, chosenUserPopulations))
069        {
070            // Whatever the user was correctly authenticated or he just required a redirect: let's stop here for the moment
071            return EMPTY_MAP;
072        }
073        
074        throw new AuthorizationRequiredException();
075    }
076    
077    @Override
078    protected List<String> _getContexts(Request request, Parameters parameters)
079    {
080        String contextAsString = request.getParameter("contexts");
081        return Arrays.asList(StringUtils.split(contextAsString, ","));
082    }
083}