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