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