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