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
045    @Metadata(enums = "implicit,password,application,accessCode")
046    private String flow;
047
048    @XmlElement(name = "scopes")
049    private List<RestPropertyDefinition> scopes = new ArrayList<>();
050
051    public RestSecurityOAuth2() {
052    }
053
054    public RestSecurityOAuth2(RestDefinition rest) {
055        super(rest);
056    }
057
058    public String getAuthorizationUrl() {
059        return authorizationUrl;
060    }
061
062    /**
063     * The authorization URL to be used for this flow. This SHOULD be in the
064     * form of a URL. Required for implicit and access code flows
065     */
066    public void setAuthorizationUrl(String authorizationUrl) {
067        this.authorizationUrl = authorizationUrl;
068    }
069
070    public String getTokenUrl() {
071        return tokenUrl;
072    }
073
074    /**
075     * The token URL to be used for this flow. This SHOULD be in the form of a
076     * URL. Required for password, application, and access code flows.
077     */
078    public void setTokenUrl(String tokenUrl) {
079        this.tokenUrl = tokenUrl;
080    }
081
082    public String getFlow() {
083        return flow;
084    }
085
086    /**
087     * The flow used by the OAuth2 security scheme. Valid values are "implicit",
088     * "password", "application" or "accessCode".
089     */
090    public void setFlow(String flow) {
091        this.flow = flow;
092    }
093
094    public List<RestPropertyDefinition> getScopes() {
095        return scopes;
096    }
097
098    /**
099     * The available scopes for an OAuth2 security scheme
100     */
101    public void setScopes(List<RestPropertyDefinition> scopes) {
102        this.scopes = scopes;
103    }
104
105    public RestSecurityOAuth2 authorizationUrl(String authorizationUrl) {
106        setAuthorizationUrl(authorizationUrl);
107        setFlow("implicit");
108        return this;
109    }
110
111    public RestSecurityOAuth2 password(String tokenUrl) {
112        setTokenUrl(tokenUrl);
113        setFlow("password");
114        return this;
115    }
116
117    public RestSecurityOAuth2 application(String tokenUrl) {
118        setTokenUrl(tokenUrl);
119        setFlow("application");
120        return this;
121    }
122
123    public RestSecurityOAuth2 accessCode(String authorizationUrl, String tokenUrl) {
124        setAuthorizationUrl(authorizationUrl);
125        setTokenUrl(tokenUrl);
126        setFlow("accessCode");
127        return this;
128    }
129
130    public RestSecurityOAuth2 withScope(String key, String description) {
131        scopes.add(new RestPropertyDefinition(key, description));
132        return this;
133    }
134
135    public RestSecuritiesDefinition end() {
136        return rest.getSecurityDefinitions();
137    }
138
139}