001/*
002 * (C) Copyright 2014 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 *     Arnaud Kervern
018 */
019package org.nuxeo.ecm.admin.oauth2;
020
021import static org.nuxeo.ecm.platform.oauth2.clients.OAuth2ClientService.OAUTH2CLIENT_DIRECTORY_NAME;
022import static org.nuxeo.ecm.platform.oauth2.clients.OAuth2ClientService.OAUTH2CLIENT_SCHEMA;
023
024import java.util.Arrays;
025import java.util.List;
026
027import javax.faces.application.FacesMessage;
028import javax.faces.component.UIComponent;
029import javax.faces.context.FacesContext;
030import javax.faces.validator.ValidatorException;
031
032import org.jboss.seam.ScopeType;
033import org.jboss.seam.annotations.Name;
034import org.jboss.seam.annotations.Scope;
035import org.nuxeo.ecm.admin.oauth.DirectoryBasedEditor;
036import org.nuxeo.ecm.platform.oauth2.clients.OAuth2Client;
037import org.nuxeo.ecm.platform.oauth2.clients.OAuth2ClientService;
038import org.nuxeo.ecm.platform.ui.web.util.ComponentUtils;
039import org.nuxeo.runtime.api.Framework;
040
041/**
042 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a>
043 * @since 5.9.2
044 */
045@Name("oauth2ClientsActions")
046@Scope(ScopeType.CONVERSATION)
047public class OAuth2ClientsActions extends DirectoryBasedEditor {
048
049    private static final long serialVersionUID = 1L;
050
051    @Override
052    protected String getDirectoryName() {
053        return OAUTH2CLIENT_DIRECTORY_NAME;
054    }
055
056    @Override
057    protected String getSchemaName() {
058        return OAUTH2CLIENT_SCHEMA;
059    }
060
061    public void validateRedirectURIs(FacesContext context, UIComponent component, Object value) {
062        if (!(value instanceof String)) {
063            handleValidationError(context, "label.oauth2.missing.redirectURI");
064        }
065        List<String> redirectURIs = Arrays.asList(((String) value).split(","));
066        if (redirectURIs.isEmpty()) {
067            handleValidationError(context, "label.oauth2.missing.redirectURI");
068        }
069        redirectURIs.stream().map(String::trim).forEach(redirectURI -> {
070            if (redirectURI.isEmpty()) {
071                handleValidationError(context, "label.oauth2.empty.redirectURI");
072            }
073            if (!OAuth2Client.isRedirectURIValid(redirectURI)) {
074                handleValidationError(context, "label.oauth2.invalid.redirectURIs");
075            }
076        });
077    }
078
079    protected void handleValidationError(FacesContext context, String label) {
080        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, ComponentUtils.translate(context, label),
081                null);
082        throw new ValidatorException(message);
083    }
084
085    public void validateClientId(FacesContext context, UIComponent component, Object value) {
086        OAuth2ClientService clientService = Framework.getService(OAuth2ClientService.class);
087        if (value instanceof String && clientService.hasClient((String) value)) {
088            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
089                    ComponentUtils.translate(context, "label.oauth2.existing.clientId"), null);
090            throw new ValidatorException(message);
091        }
092    }
093}