001/*
002 * (C) Copyright 2015 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-2.1.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 *      Nelson Silva
016 */
017package org.nuxeo.ecm.platform.oauth2.providers;
018
019import java.io.Serializable;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.DocumentModelList;
027import org.nuxeo.ecm.directory.Session;
028import org.nuxeo.ecm.directory.api.DirectoryService;
029import org.nuxeo.ecm.platform.oauth2.tokens.NuxeoOAuth2Token;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * Directory backed storage for mapping between users and services The current implementation reuses the existing token
034 * directory as storage.
035 *
036 * @since 7.3
037 */
038public class OAuth2ServiceUserStore {
039
040    protected static final Log log = LogFactory.getLog(OAuth2ServiceUserStore.class);
041
042    public static final String DIRECTORY_NAME = "oauth2Tokens";
043
044    public static final String ENTRY_ID = "id";
045
046    private String serviceName;
047
048    public OAuth2ServiceUserStore(String serviceName) {
049        this.serviceName = serviceName;
050    }
051
052    public String store(String nuxeoLogin) {
053        return store(nuxeoLogin, new HashMap<>());
054    }
055
056    public String store(String nuxeoLogin, Map<String, Object> fields) {
057        DirectoryService ds = Framework.getLocalService(DirectoryService.class);
058        try (Session session = ds.open(DIRECTORY_NAME)) {
059            fields.put("nuxeoLogin", nuxeoLogin);
060            fields.put("serviceName", serviceName);
061            DocumentModel entry = session.createEntry(fields);
062            Long id = (Long) entry.getProperty(NuxeoOAuth2Token.SCHEMA, ENTRY_ID);
063            return id.toString();
064        }
065    }
066
067    public String find(Map<String, Serializable> filter) {
068        filter.put("serviceName", serviceName);
069        DocumentModelList entries = query(filter);
070        if (entries == null || entries.size() == 0) {
071            return null;
072        }
073        if (entries.size() > 1) {
074            log.error("Found several tokens");
075        }
076        Long id = (Long) entries.get(0).getProperty(NuxeoOAuth2Token.SCHEMA, ENTRY_ID);
077        return id.toString();
078    }
079
080    protected DocumentModelList query(Map<String, Serializable> filter) {
081        DirectoryService ds = Framework.getLocalService(DirectoryService.class);
082        try (Session session = ds.open(DIRECTORY_NAME)) {
083            return session.query(filter);
084        }
085    }
086}