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 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        String nuxeoLogin = getUserInfoStore().getNuxeoLogin(userInfo);
051        // Check if the user exists
052        try {
053            UserManager userManager = Framework.getLocalService(UserManager.class);
054            if (userManager.getUserModel(nuxeoLogin) == null) {
055                nuxeoLogin = null;
056            }
057
058        } catch (NuxeoException e) {
059            log.error("Error while search user in UserManager using email " + userInfo.getEmail(), e);
060            return null;
061        }
062        return nuxeoLogin;
063    }
064
065    @Override
066    public DocumentModel updateUserInfo(DocumentModel user, OpenIDUserInfo userInfo) {
067        try {
068            UserManager userManager = Framework.getLocalService(UserManager.class);
069            String userId = (String) user.getPropertyValue(userManager.getUserIdField());
070            getUserInfoStore().storeUserInfo(userId, userInfo);
071        } catch (NuxeoException e) {
072            log.error("Error while updating user info for user " + userInfo.getEmail(), e);
073            return null;
074        }
075        return user;
076
077    }
078
079}