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