001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.model.rest;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import javax.xml.bind.annotation.XmlAccessType;
023import javax.xml.bind.annotation.XmlAccessorType;
024import javax.xml.bind.annotation.XmlAttribute;
025import javax.xml.bind.annotation.XmlElement;
026import javax.xml.bind.annotation.XmlRootElement;
027
028import org.apache.camel.spi.Metadata;
029
030/**
031 * Rest security OAuth2 definition
032 */
033@Metadata(label = "rest,security")
034@XmlRootElement(name = "oauth2")
035@XmlAccessorType(XmlAccessType.FIELD)
036public class RestSecurityOAuth2 extends RestSecurityDefinition {
037
038    @XmlAttribute
039    private String authorizationUrl;
040
041    @XmlAttribute
042    private String tokenUrl;
043
044    @XmlAttribute @Metadata(enums = "implicit,password,application,accessCode")
045    private String flow;
046
047    @XmlElement(name = "scopes")
048    private List<RestPropertyDefinition> scopes = new ArrayList<>();
049
050    public RestSecurityOAuth2() {
051    }
052
053    public RestSecurityOAuth2(RestDefinition rest) {
054        super(rest);
055    }
056
057    public String getAuthorizationUrl() {
058        return authorizationUrl;
059    }
060
061    /**
062     * The authorization URL to be used for this flow. This SHOULD be in the form of a URL.
063     * Required for implicit and access code flows
064     */
065    public void setAuthorizationUrl(String authorizationUrl) {
066        this.authorizationUrl = authorizationUrl;
067    }
068
069    public String getTokenUrl() {
070        return tokenUrl;
071    }
072
073    /**
074     * The token URL to be used for this flow. This SHOULD be in the form of a URL.
075     * Required for password, application, and access code flows.
076     */
077    public void setTokenUrl(String tokenUrl) {
078        this.tokenUrl = tokenUrl;
079    }
080
081    public String getFlow() {
082        return flow;
083    }
084
085    /**
086     * The flow used by the OAuth2 security scheme.
087     * Valid values are "implicit", "password", "application" or "accessCode".
088     */
089    public void setFlow(String flow) {
090        this.flow = flow;
091    }
092
093    public List<RestPropertyDefinition> getScopes() {
094        return scopes;
095    }
096
097    /**
098     * The available scopes for an OAuth2 security scheme
099     */
100    public void setScopes(List<RestPropertyDefinition> scopes) {
101        this.scopes = scopes;
102    }
103
104    public RestSecurityOAuth2 authorizationUrl(String authorizationUrl) {
105        setAuthorizationUrl(authorizationUrl);
106        setFlow("implicit");
107        return this;
108    }
109
110    public RestSecurityOAuth2 password(String tokenUrl) {
111        setTokenUrl(tokenUrl);
112        setFlow("password");
113        return this;
114    }
115
116    public RestSecurityOAuth2 application(String tokenUrl) {
117        setTokenUrl(tokenUrl);
118        setFlow("application");
119        return this;
120    }
121
122    public RestSecurityOAuth2 accessCode(String authorizationUrl, String tokenUrl) {
123        setAuthorizationUrl(authorizationUrl);
124        setTokenUrl(tokenUrl);
125        setFlow("accessCode");
126        return this;
127    }
128
129    public RestSecurityOAuth2 withScope(String key, String description) {
130        scopes.add(new RestPropertyDefinition(key, description));
131        return this;
132    }
133
134    public RestSecuritiesDefinition end() {
135        return rest.getSecurityDefinitions();
136    }
137
138}