001/* 002 * (C) Copyright 2006-2008 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 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.oauth.consumers; 023 024import java.util.ArrayList; 025import java.util.Collections; 026import java.util.List; 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029import org.nuxeo.ecm.core.api.DocumentModel; 030import org.nuxeo.ecm.core.api.DocumentModelList; 031import org.nuxeo.ecm.directory.DirectoryException; 032import org.nuxeo.ecm.directory.Session; 033import org.nuxeo.ecm.directory.api.DirectoryService; 034import org.nuxeo.runtime.api.Framework; 035import org.nuxeo.runtime.model.DefaultComponent; 036 037/** 038 * Implementation of the {@link OAuthConsumerRegistry} Service. It's basically a simple Storage API on top of an SQL 039 * Directory. 040 * 041 * @author tiry 042 */ 043public class OAuthConsumerRegistryImpl extends DefaultComponent implements OAuthConsumerRegistry { 044 045 protected static final Log log = LogFactory.getLog(OAuthConsumerRegistryImpl.class); 046 047 public static final String DIRECTORY_NAME = "oauthConsumers"; 048 049 @Override 050 public NuxeoOAuthConsumer getConsumer(String consumerKey, String keyType) { 051 try { 052 NuxeoOAuthConsumer consumer = getEntry(consumerKey, keyType); 053 return consumer; 054 } catch (DirectoryException e) { 055 log.error("Unable to read consumer " + consumerKey + " from Directory backend", e); 056 return null; 057 } 058 } 059 060 @Override 061 public NuxeoOAuthConsumer getConsumer(String consumerKey) { 062 return getConsumer(consumerKey, null); 063 } 064 065 protected NuxeoOAuthConsumer getEntry(String consumerKey, String keyType) { 066 DirectoryService ds = Framework.getService(DirectoryService.class); 067 return Framework.doPrivileged(() -> { 068 try (Session session = ds.open(DIRECTORY_NAME)) { 069 DocumentModel entry = session.getEntry(consumerKey); 070 if (entry == null) { 071 return null; 072 } 073 return NuxeoOAuthConsumer.createFromDirectoryEntry(entry, keyType); 074 } 075 }); 076 } 077 078 @Override 079 public NuxeoOAuthConsumer storeConsumer(NuxeoOAuthConsumer consumer) { 080 DirectoryService ds = Framework.getService(DirectoryService.class); 081 return Framework.doPrivileged(() -> { 082 try (Session session = ds.open(DIRECTORY_NAME)) { 083 DocumentModel entry = session.createEntry( 084 Collections.singletonMap("consumerKey", consumer.consumerKey)); 085 consumer.asDocumentModel(entry); 086 session.updateEntry(entry); 087 return NuxeoOAuthConsumer.createFromDirectoryEntry(entry, null); 088 } 089 }); 090 } 091 092 @Override 093 public void deleteConsumer(String consumerKey) { 094 Framework.doPrivileged(() -> { 095 try { 096 DirectoryService ds = Framework.getService(DirectoryService.class); 097 try (Session session = ds.open(DIRECTORY_NAME)) { 098 session.deleteEntry(consumerKey); 099 } 100 } catch (DirectoryException e) { 101 log.error("Unable to delete consumer " + consumerKey, e); 102 } 103 }); 104 } 105 106 @Override 107 public List<NuxeoOAuthConsumer> listConsumers() { 108 return Framework.doPrivileged(() -> { 109 try { 110 DirectoryService ds = Framework.getService(DirectoryService.class); 111 try (Session session = ds.open(DIRECTORY_NAME)) { 112 DocumentModelList entries = session.query(Collections.emptyMap()); 113 List<NuxeoOAuthConsumer> result = new ArrayList<>(); 114 for (DocumentModel entry : entries) { 115 result.add(NuxeoOAuthConsumer.createFromDirectoryEntry(entry, null)); 116 } 117 return result; 118 } 119 } catch (DirectoryException e) { 120 log.error("Error while fetching consumer directory", e); 121 return Collections.emptyList(); 122 } 123 }); 124 } 125 126}