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.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.Request;
025import org.apache.commons.lang3.StringUtils;
026
027import org.ametys.core.authentication.AuthenticateAction;
028import org.ametys.core.authentication.token.AuthenticationTokenManager;
029import org.ametys.core.user.UserIdentity;
030import org.ametys.plugins.mobileapp.PostConstants;
031import org.ametys.plugins.mobileapp.UserPreferencesHelper;
032
033/**
034 * Returns the list of feeds for a user
035 */
036public class LogoutAction extends AbstractLoggedAction
037{
038    /** User Preferences Helper */
039    protected UserPreferencesHelper _userPreferencesHelper;
040
041    /** Authentication Token Manager */
042    protected AuthenticationTokenManager _authenticationTokenManager;
043
044    @Override
045    public void service(ServiceManager smanager) throws ServiceException
046    {
047        super.service(smanager);
048        _userPreferencesHelper = (UserPreferencesHelper) smanager.lookup(UserPreferencesHelper.ROLE);
049        _authenticationTokenManager = (AuthenticationTokenManager) smanager.lookup(AuthenticationTokenManager.ROLE);
050    }
051
052    @Override
053    protected Map<String, Object> doLoggedInAction(Request request, Map<String, Object> jsonParams)
054    {
055        Map<String, Object> result = new HashMap<>();
056
057        String token = request.getHeader(AuthenticateAction.HEADER_TOKEN);
058        if (StringUtils.isBlank(token))
059        {
060            token = (String) getParameter(AuthenticateAction.REQUEST_PARAMETER_TOKEN, jsonParams, request);
061        }
062        _authenticationTokenManager.deleteTokenByValue(token, "mobileapp");
063
064        // remove the push notification token
065        String notificationToken = (String) getParameter(PostConstants.NOTIF_TOKEN, jsonParams, request);
066        _userPreferencesHelper.removeNotificationToken(notificationToken);
067
068        // logout
069        try
070        {
071            // No possiblity to do a redirection here.
072            // It should not be an issue as the only credential provider used with mobile app is the FormCredentialProvider
073            _currentUserProvider.logout(null);
074            result.put("code", 200);
075        }
076        catch (ProcessingException e)
077        {
078            getLogger().warn("Exception while loggin out user '" + UserIdentity.userIdentityToString(_currentUserProvider.getUser()) + "'", e);
079            result.put("code", 500);
080        }
081
082        return result;
083    }
084
085}