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