001/*
002 * (C) Copyright 2015 Nuxeo SAS (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 *     Nuxeo - initial API and implementation
016 *
017 */
018
019package org.nuxeo.ecm.platform.oauth2.openid.auth;
020
021import org.nuxeo.ecm.core.api.DocumentModel;
022import org.nuxeo.ecm.core.api.NuxeoPrincipal;
023import org.nuxeo.ecm.platform.oauth2.openid.OpenIDConnectProvider;
024import org.nuxeo.runtime.api.Framework;
025import org.nuxeo.usermapper.service.UserMapperService;
026
027/**
028 * UserResolver implementation that uses the {@link UserMapperService}
029 *
030 * @author tiry
031 * @since 7.4
032 */
033public class UserMapperResolver extends UserResolver {
034
035    protected String mapperName;
036
037    public UserMapperResolver(OpenIDConnectProvider provider, String mapperName) {
038        super(provider);
039        this.mapperName = mapperName;
040    }
041
042    @Override
043    public String findOrCreateNuxeoUser(OpenIDUserInfo userInfo) {
044        NuxeoPrincipal principal = Framework.getService(UserMapperService.class)
045                                            .getMapper(mapperName)
046                                            .getOrCreateAndUpdateNuxeoPrincipal(userInfo);
047        if (principal != null) {
048            return principal.getName();
049        } else {
050            return null;
051        }
052    }
053
054    @Override
055    protected String findNuxeoUser(OpenIDUserInfo userInfo) {
056        throw new UnsupportedOperationException();
057    }
058
059    @Override
060    protected DocumentModel updateUserInfo(DocumentModel user, OpenIDUserInfo userInfo) {
061        throw new UnsupportedOperationException();
062    }
063
064}