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