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;
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAttribute;
024import javax.xml.bind.annotation.XmlElement;
025import javax.xml.bind.annotation.XmlRootElement;
026
027import org.apache.camel.spi.Metadata;
028
029/**
030 * Rest security OAuth2 definition
031 */
032@Metadata(label = "rest,security")
033@XmlRootElement(name = "oauth2")
034@XmlAccessorType(XmlAccessType.FIELD)
035public class RestSecurityOAuth2 extends RestSecurityDefinition {
036
037    @XmlAttribute
038    private String authorizationUrl;
039
040    @XmlAttribute
041    private String tokenUrl;
042
043    @XmlAttribute @Metadata(enums = "implicit,password,application,accessCode")
044    private String flow;
045
046    @XmlElement(name = "scopes")
047    private List<RestPropertyDefinition> scopes = new ArrayList<>();
048
049    public RestSecurityOAuth2() {
050    }
051
052    public RestSecurityOAuth2(RestDefinition rest) {
053        super(rest);
054    }
055
056    public String getAuthorizationUrl() {
057        return authorizationUrl;
058    }
059
060    /**
061     * The authorization URL to be used for this flow. This SHOULD be in the form of a URL.
062     * Required for implicit and access code flows
063     */
064    public void setAuthorizationUrl(String authorizationUrl) {
065        this.authorizationUrl = authorizationUrl;
066    }
067
068    public String getTokenUrl() {
069        return tokenUrl;
070    }
071
072    /**
073     * The token URL to be used for this flow. This SHOULD be in the form of a URL.
074     * Required for password, application, and access code flows.
075     */
076    public void setTokenUrl(String tokenUrl) {
077        this.tokenUrl = tokenUrl;
078    }
079
080    public String getFlow() {
081        return flow;
082    }
083
084    /**
085     * The flow used by the OAuth2 security scheme.
086     * Valid values are "implicit", "password", "application" or "accessCode".
087     */
088    public void setFlow(String flow) {
089        this.flow = flow;
090    }
091
092    public List<RestPropertyDefinition> getScopes() {
093        return scopes;
094    }
095
096    /**
097     * The available scopes for an OAuth2 security scheme
098     */
099    public void setScopes(List<RestPropertyDefinition> scopes) {
100        this.scopes = scopes;
101    }
102
103    public RestSecurityOAuth2 authorizationUrl(String authorizationUrl) {
104        setAuthorizationUrl(authorizationUrl);
105        setFlow("implicit");
106        return this;
107    }
108
109    public RestSecurityOAuth2 password(String tokenUrl) {
110        setTokenUrl(tokenUrl);
111        setFlow("password");
112        return this;
113    }
114
115    public RestSecurityOAuth2 application(String tokenUrl) {
116        setTokenUrl(tokenUrl);
117        setFlow("application");
118        return this;
119    }
120
121    public RestSecurityOAuth2 accessCode(String authorizationUrl, String tokenUrl) {
122        setAuthorizationUrl(authorizationUrl);
123        setTokenUrl(tokenUrl);
124        setFlow("accessCode");
125        return this;
126    }
127
128    public RestSecurityOAuth2 withScope(String key, String description) {
129        scopes.add(new RestPropertyDefinition(key, description));
130        return this;
131    }
132
133    public RestSecuritiesDefinition end() {
134        return rest.getSecurityDefinitions();
135    }
136
137}