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