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