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 017package org.nuxeo.ecm.platform.oauth2.providers; 018 019import org.nuxeo.ecm.platform.oauth2.tokens.NuxeoOAuth2Token; 020 021import javax.servlet.http.HttpServletRequest; 022import java.io.IOException; 023import java.io.Serializable; 024import java.util.HashMap; 025import java.util.Map; 026 027/** 028 * {@link org.nuxeo.ecm.platform.oauth2.providers.OAuth2ServiceProvider} that relies on the user's email as key. 029 * 030 * @since 7.3 031 */ 032public abstract class AbstractOAuth2UserEmailProvider extends NuxeoOAuth2ServiceProvider { 033 034 @Override 035 protected String getOrCreateServiceUser(HttpServletRequest request, String accessToken) throws IOException { 036 String email = getUserEmail(accessToken); 037 String userId = getServiceUserId(email); 038 if (userId == null) { 039 String nuxeoLogin = request.getUserPrincipal().getName(); 040 Map<String, Object> fields = new HashMap<>(); 041 fields.put(NuxeoOAuth2Token.KEY_SERVICE_LOGIN, email); 042 userId = getServiceUserStore().store(nuxeoLogin, fields); 043 } 044 return userId; 045 } 046 047 @Override 048 protected String getServiceUserId(String email) { 049 Map<String, Serializable> filter = new HashMap<>(); 050 filter.put(NuxeoOAuth2Token.KEY_SERVICE_LOGIN, email); 051 return getServiceUserStore().find(filter); 052 } 053 054 protected abstract String getUserEmail(String accessToken) throws IOException; 055}