001/*
002 * (C) Copyright 2015 Nuxeo SA (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-2.1.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 *      Nelson Silva
016 */
017package org.nuxeo.ecm.admin.oauth2;
018
019import javax.faces.context.FacesContext;
020import javax.servlet.http.HttpServletRequest;
021
022import org.jboss.seam.ScopeType;
023import org.jboss.seam.annotations.Name;
024import org.jboss.seam.annotations.Scope;
025import org.nuxeo.ecm.admin.oauth.DirectoryBasedEditor;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.platform.oauth2.providers.NuxeoOAuth2ServiceProvider;
028import org.nuxeo.ecm.platform.oauth2.providers.OAuth2ServiceProvider;
029import org.nuxeo.ecm.platform.oauth2.providers.OAuth2ServiceProviderRegistry;
030import org.nuxeo.ecm.platform.oauth2.providers.OAuth2ServiceProviderRegistryImpl;
031import org.nuxeo.runtime.api.Framework;
032
033import java.util.ArrayList;
034import java.util.List;
035
036@Name("oauth2ServiceProvidersActions")
037@Scope(ScopeType.CONVERSATION)
038public class OAuth2ServiceProvidersActionBean extends DirectoryBasedEditor {
039
040    private static final long serialVersionUID = 1L;
041
042    protected static final String DIRECTORY = OAuth2ServiceProviderRegistryImpl.DIRECTORY_NAME;
043
044    protected static final String SCHEMA = NuxeoOAuth2ServiceProvider.SCHEMA;
045
046    @Override
047    protected String getDirectoryName() {
048        return DIRECTORY;
049    }
050
051    @Override
052    protected String getSchemaName() {
053        return SCHEMA;
054    }
055
056    public String getAuthorizationURL(String provider) {
057        OAuth2ServiceProviderRegistry oauth2ProviderRegistry = Framework.getLocalService(OAuth2ServiceProviderRegistry.class);
058        OAuth2ServiceProvider serviceProvider = oauth2ProviderRegistry.getProvider(provider);
059
060        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
061        return serviceProvider.getAuthorizationUrl(request);
062    }
063
064    public List<DocumentModel> getEnabledProviders() {
065        List<DocumentModel> enabledProviders = new ArrayList<>();
066        for (DocumentModel entry : getEntries()) {
067            boolean isEnabled = (boolean) entry.getProperty(SCHEMA, "enabled");
068            String clientId = (String) entry.getProperty(SCHEMA, "clientId");
069            String clientSecret = (String) entry.getProperty(SCHEMA, "clientSecret");
070
071            if (isEnabled && clientId != null && clientSecret != null) {
072                enabledProviders.add(entry);
073            }
074        }
075        return enabledProviders;
076    }
077}