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 */ 018package org.nuxeo.ecm.platform.oauth2.openid.auth; 019 020import java.util.List; 021 022import org.apache.commons.lang.RandomStringUtils; 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 abstract class UserResolver { 032 033 private static final Log log = LogFactory.getLog(UserResolver.class); 034 035 private OpenIDConnectProvider provider; 036 037 public UserResolver(OpenIDConnectProvider provider) { 038 this.provider = provider; 039 } 040 041 public OpenIDConnectProvider getProvider() { 042 return provider; 043 } 044 045 protected abstract String findNuxeoUser(OpenIDUserInfo userInfo); 046 047 protected DocumentModel createNuxeoUser(String nuxeoLogin) { 048 DocumentModel userDoc; 049 050 try { 051 UserManager userManager = Framework.getLocalService(UserManager.class); 052 053 userDoc = userManager.getBareUserModel(); 054 userDoc.setPropertyValue(userManager.getUserIdField(), nuxeoLogin); 055 056 userManager.createUser(userDoc); 057 058 } catch (NuxeoException e) { 059 log.error("Error while creating user " + nuxeoLogin + "in UserManager", e); 060 return null; 061 } 062 063 return userDoc; 064 } 065 066 protected abstract DocumentModel updateUserInfo(DocumentModel user, OpenIDUserInfo userInfo); 067 068 public String findOrCreateNuxeoUser(OpenIDUserInfo userInfo) { 069 String user = findNuxeoUser(userInfo); 070 if (user == null) { 071 user = generateRandomUserId(); 072 DocumentModel userDoc = createNuxeoUser(user); 073 updateUserInfo(userDoc, userInfo); 074 } 075 return user; 076 } 077 078 protected String generateRandomUserId() { 079 String userId = null; 080 081 try { 082 UserManager userManager = Framework.getLocalService(UserManager.class); 083 List<String> userIds = userManager.getUserIds(); 084 085 while (userId == null || userIds.contains(userId)) { 086 userId = "user_" + RandomStringUtils.randomNumeric(4); 087 } 088 } catch (NuxeoException e) { 089 log.error("Error while generating random user id", e); 090 return null; 091 } 092 return userId; 093 } 094}