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