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 org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.nuxeo.ecm.core.api.DocumentModel; 024import org.nuxeo.ecm.core.api.NuxeoException; 025import org.nuxeo.ecm.platform.oauth2.openid.OpenIDConnectProvider; 026import org.nuxeo.ecm.platform.usermanager.UserManager; 027import org.nuxeo.runtime.api.Framework; 028 029public class StoredUserInfoResolver extends UserResolver { 030 031 private OpenIDUserInfoStore userInfoStore; 032 033 private static final Log log = LogFactory.getLog(StoredUserInfoResolver.class); 034 035 public StoredUserInfoResolver(OpenIDConnectProvider provider) { 036 super(provider); 037 } 038 039 public OpenIDUserInfoStore getUserInfoStore() { 040 if (userInfoStore == null) { 041 userInfoStore = new OpenIDUserInfoStoreImpl(getProvider().getName()); 042 } 043 return userInfoStore; 044 } 045 046 @Override 047 public String findNuxeoUser(OpenIDUserInfo userInfo) { 048 String nuxeoLogin = getUserInfoStore().getNuxeoLogin(userInfo); 049 // Check if the user exists 050 try { 051 UserManager userManager = Framework.getLocalService(UserManager.class); 052 if (userManager.getUserModel(nuxeoLogin) == null) { 053 nuxeoLogin = null; 054 } 055 056 } catch (NuxeoException e) { 057 log.error("Error while search user in UserManager using email " + userInfo.getEmail(), e); 058 return null; 059 } 060 return nuxeoLogin; 061 } 062 063 @Override 064 public DocumentModel updateUserInfo(DocumentModel user, OpenIDUserInfo userInfo) { 065 try { 066 UserManager userManager = Framework.getLocalService(UserManager.class); 067 String userId = (String) user.getPropertyValue(userManager.getUserIdField()); 068 getUserInfoStore().storeUserInfo(userId, userInfo); 069 } catch (NuxeoException e) { 070 log.error("Error while updating user info for user " + userInfo.getEmail(), e); 071 return null; 072 } 073 return user; 074 075 } 076 077}