001/* 002 * (C) Copyright 2006-2013 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 * Nelson Silva <nelson.silva@inevo.pt> - initial API and implementation 016 * Nuxeo 017 */ 018 019package org.nuxeo.ecm.platform.oauth2.openid.auth; 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.core.api.NuxeoException; 030import org.nuxeo.ecm.directory.DirectoryException; 031import org.nuxeo.ecm.platform.oauth2.openid.OpenIDConnectProvider; 032import org.nuxeo.ecm.platform.usermanager.UserManager; 033import org.nuxeo.runtime.api.Framework; 034 035/** 036 * Helper class to manage mapping between identification info comming from the OpenID provider and Nuxeo UserManager. 037 * 038 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 039 * @since 5.7 040 */ 041public class EmailBasedUserResolver extends UserResolver { 042 043 private static final Log log = LogFactory.getLog(EmailBasedUserResolver.class); 044 045 public EmailBasedUserResolver(OpenIDConnectProvider provider) { 046 super(provider); 047 } 048 049 @Override 050 public String findNuxeoUser(OpenIDUserInfo userInfo) { 051 052 try { 053 UserManager userManager = Framework.getLocalService(UserManager.class); 054 Map<String, Serializable> query = new HashMap<String, Serializable>(); 055 query.put(userManager.getUserEmailField(), userInfo.getEmail()); 056 057 DocumentModelList users = userManager.searchUsers(query, null); 058 059 if (users.isEmpty()) { 060 return null; 061 } 062 063 DocumentModel user = users.get(0); 064 return (String) user.getPropertyValue(userManager.getUserIdField()); 065 066 } catch (NuxeoException e) { 067 log.error("Error while search user in UserManager using email " + userInfo.getEmail(), e); 068 return null; 069 } 070 } 071 072 @Override 073 public DocumentModel updateUserInfo(DocumentModel user, OpenIDUserInfo userInfo) { 074 try { 075 UserManager userManager = Framework.getLocalService(UserManager.class); 076 user.setPropertyValue(userManager.getUserEmailField(), userInfo.getEmail()); 077 } catch (NuxeoException e) { 078 log.error("Error while search user in UserManager using email " + userInfo.getEmail(), e); 079 return null; 080 } 081 return user; 082 } 083 084}