001/*
002 *  Copyright 2020 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.plugins.mobileapp.action;
017
018import java.io.IOException;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.environment.Request;
024import org.apache.excalibur.source.SourceResolver;
025
026import org.ametys.core.authentication.AuthenticateAction;
027import org.ametys.core.authentication.CredentialProvider;
028import org.ametys.core.user.UserIdentity;
029import org.ametys.core.user.population.UserPopulation;
030import org.ametys.core.util.URIUtils;
031import org.ametys.plugins.core.impl.authentication.FormCredentialProvider;
032
033/**
034 * Authenticates a user based on login and password form parameters.
035 */
036public class GetTokenFromFormAction extends AbstractGetTokenAction
037{
038    private SourceResolver _sourceResolver;
039
040    @Override
041    public void service(ServiceManager smanager) throws ServiceException
042    {
043        super.service(smanager);
044        _sourceResolver = (SourceResolver) smanager.lookup(SourceResolver.class.getName());
045    }
046
047    @Override
048    protected UserIdentity tryConnect(Map<String, Object> params, Request request, String context, UserPopulation userPopulation, CredentialProvider credentialProvider, int credentialProviderIndex)
049    {
050        if (credentialProvider instanceof FormCredentialProvider)
051        {
052            try
053            {
054                String login = (String) getParameter("login", params, request);
055                String password = (String) getParameter("password", params, request);
056                
057                request.setAttribute(AuthenticateAction.REQUEST_ATTRIBUTE_AUTHENTICATED, "false");
058                
059                String loginParameters = "Username=" + URIUtils.encodeParameter(login);
060                loginParameters += "&Password=" + URIUtils.encodeParameter(password);
061                loginParameters += "&UserPopulation=" + URIUtils.encodeParameter(userPopulation.getId());
062                loginParameters += "&CredentialProviderIndex=" + credentialProviderIndex;
063                loginParameters += "&context=" + URIUtils.encodeParameter(context);
064
065                _sourceResolver.resolveURI("cocoon:/authenticate?" + loginParameters);
066                
067                return _currentUserProvider.getUser();
068            }
069            catch (IOException e)
070            {
071                getLogger().error("Impossible to test logins on population '" + userPopulation.getId() + "' using credential provider at position '" + credentialProviderIndex + "'");
072            }
073        }
074        
075        return null;
076    }
077}