001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Antoine Taillefer
016 */
017
018package org.nuxeo.ecm.tokenauth.webapp;
019
020import java.io.Serializable;
021import java.util.Map;
022
023import org.jboss.seam.ScopeType;
024import org.jboss.seam.annotations.In;
025import org.jboss.seam.annotations.Name;
026import org.jboss.seam.annotations.Scope;
027import org.jboss.seam.faces.FacesMessages;
028import org.jboss.seam.international.StatusMessage;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.DocumentModelList;
031import org.nuxeo.ecm.core.api.NuxeoPrincipal;
032import org.nuxeo.ecm.core.api.PropertyException;
033import org.nuxeo.ecm.tokenauth.service.TokenAuthenticationService;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * Manages user's authentication token bindings.
038 *
039 * @author Antoine Taillefer (ataillefer@nuxeo.com)
040 * @since 5.7
041 */
042@Name("tokenAuthenticationActions")
043@Scope(ScopeType.CONVERSATION)
044public class TokenAuthenticationActionsBean implements Serializable {
045
046    private static final long serialVersionUID = 1L;
047
048    @In(create = true)
049    private transient NuxeoPrincipal currentNuxeoPrincipal;
050
051    @In(create = true, required = false)
052    protected transient FacesMessages facesMessages;
053
054    @In(create = true)
055    protected Map<String, String> messages;
056
057    protected DocumentModelList currentUserAuthTokenBindings;
058
059    public DocumentModelList getCurrentUserAuthTokenBindings() {
060
061        if (currentUserAuthTokenBindings == null) {
062            TokenAuthenticationService tokenAuthenticationService = Framework.getLocalService(TokenAuthenticationService.class);
063            currentUserAuthTokenBindings = tokenAuthenticationService.getTokenBindings(currentNuxeoPrincipal.getName());
064        }
065        return currentUserAuthTokenBindings;
066    }
067
068    public void deleteAuthTokenBinding(String tokenId) {
069
070        TokenAuthenticationService tokenAuthenticationService = Framework.getLocalService(TokenAuthenticationService.class);
071        tokenAuthenticationService.revokeToken(tokenId);
072
073        reset();
074        facesMessages.add(StatusMessage.Severity.INFO, messages.get("label.tokenauth.revoked"));
075    }
076
077    public void deleteAllTokenBindings() throws PropertyException {
078        reset();
079        TokenAuthenticationService tokenAuthenticationService = Framework.getLocalService(TokenAuthenticationService.class);
080        for (DocumentModel tokenBinding : getCurrentUserAuthTokenBindings()) {
081            String tokenId = (String) tokenBinding.getPropertyValue("authtoken:token");
082            tokenAuthenticationService.revokeToken(tokenId);
083
084        }
085        reset();
086        facesMessages.add(StatusMessage.Severity.INFO, messages.get("label.tokenauth.revoked"));
087    }
088
089    public void refreshAuthTokenBindings() {
090        reset();
091    }
092
093    protected void reset() {
094        currentUserAuthTokenBindings = null;
095    }
096
097}