<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~  Copyright (c) 2020, WSO2 Inc. (http://wso2.com) All Rights Reserved.
  ~
  ~  WSO2 Inc. licenses this file to you under the Apache License,
  ~  Version 2.0 (the "License"); you may not use this file except
  ~  in compliance with the License.
  ~  You may obtain a copy of the License at
  ~
  ~    http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~  Unless required by applicable law or agreed to in writing,
  ~  software distributed under the License is distributed on an
  ~  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  ~  KIND, either express or implied.  See the License for the
  ~  specific language governing permissions and limitations
  ~  under the License.
  ~
  -->
<!-- OAuth2.0 configured HTTP endpoints -->
<definitions xmlns="http://ws.apache.org/ns/synapse">
    <api name="FoodAPI" context="/foodapi">
        <resource uri-template="/list/{val1}" methods="GET">
            <inSequence>
                <property name="uri.var.val1" expression="get-property('uri.var.val1')"/>
                <switch source="get-property('uri.var.val1')">
                    <case regex="invalidCredentials">
                        <send>
                            <endpoint>
                                <http method="get" uri-template="http://localhost:9000/foodservice/food">
                                    <authentication>
                                        <oauth>
                                            <clientCredentials>
                                                <clientId>my_client_id</clientId>
                                                <clientSecret>my_secret</clientSecret>
                                                <tokenUrl>http://localhost:9000/foodservice/token</tokenUrl>
                                            </clientCredentials>
                                        </oauth>
                                    </authentication>
                                </http>
                            </endpoint>
                        </send>
                    </case>
                    <case regex="oauthServerFailed">
                        <send>
                            <endpoint>
                                <http method="get" uri-template="http://localhost:9000/foodservice/food">
                                    <authentication>
                                        <oauth>
                                            <clientCredentials>
                                                <clientId>my_client_id</clientId>
                                                <clientSecret>my_client_secret</clientSecret>
                                                <tokenUrl>http://localhost:9898/foodservice/token</tokenUrl>
                                            </clientCredentials>
                                        </oauth>
                                    </authentication>
                                </http>
                            </endpoint>
                        </send>
                    </case>
                    <case regex="refreshTokenGrant">
                        <send>
                            <endpoint>
                                <http method="get" uri-template="http://localhost:9000/foodservice/food">
                                    <authentication>
                                        <oauth>
                                            <authorizationCode>
                                                <clientId>my_client_id</clientId>
                                                <clientSecret>my_client_secret</clientSecret>
                                                <refreshToken>wxyz#9876</refreshToken>
                                                <tokenUrl>http://localhost:9000/foodservice/token</tokenUrl>
                                            </authorizationCode>
                                        </oauth>
                                    </authentication>
                                </http>
                            </endpoint>
                        </send>
                    </case>
                    <case regex="passwordGrant">
                        <property name="clientID" value="my_client_id"/>
                        <payloadFactory media-type="json">
                            <format>{"account_id" : "$1"}</format>
                            <args>
                                <arg value="1234"/>
                            </args>
                        </payloadFactory>
                        <send>
                            <endpoint>
                                <http method="get" uri-template="http://localhost:9000/foodservice/food">
                                    <authentication>
                                        <oauth>
                                            <passwordCredentials>
                                                <clientId>{$ctx:clientID}</clientId>
                                                <clientSecret>my_client_secret</clientSecret>
                                                <username>tester123</username>
                                                <password>abc@123</password>
                                                <tokenUrl>http://localhost:9000/foodservice/password-token</tokenUrl>
                                                <requestParameters>
                                                    <parameter name="account_id">{json-eval($.account_id)}</parameter>
                                                    <parameter name="user_role">tester</parameter>
                                                </requestParameters>
                                            </passwordCredentials>
                                        </oauth>
                                    </authentication>
                                </http>
                            </endpoint>
                        </send>
                    </case>
                    <case regex="withCustomParams">
                        <property name="clientID" value="my_client_id"/>
                        <property name="account_token" value="zxy@123"/>
                        <payloadFactory media-type="json">
                            <format>{"account_id" : "$1"}</format>
                            <args>
                                <arg value="1234"/>
                            </args>
                        </payloadFactory>
                        <send>
                            <endpoint>
                                <http method="get" uri-template="http://localhost:9000/foodservice/food">
                                    <authentication>
                                        <oauth>
                                            <clientCredentials>
                                                <clientId>{$ctx:clientID}</clientId>
                                                <clientSecret>my_client_secret</clientSecret>
                                                <tokenUrl>http://localhost:9000/foodservice/custom-token</tokenUrl>
                                                <requestParameters>
                                                    <parameter name="account_id">{json-eval($.account_id)}</parameter>
                                                    <parameter name="user_role">tester</parameter>
                                                </requestParameters>
                                                <customHeaders>
                                                    <header name="user_token">abc#123</header>
                                                    <header name="account_token">{$ctx:account_token}</header>
                                                </customHeaders>
                                            </clientCredentials>
                                        </oauth>
                                    </authentication>
                                </http>
                            </endpoint>
                        </send>
                    </case>
                    <case regex="withoutTransportHeaders">
                        <property name="clientID" value="my_client_id"/>
                        <payloadFactory media-type="json">
                            <format>{"account_id" : "$1"}</format>
                            <args>
                                <arg value="1234"/>
                            </args>
                        </payloadFactory>
                        <property name="TRANSPORT_HEADERS" scope="axis2" action="remove"/>
                        <send>
                            <endpoint>
                                <http method="get" uri-template="http://localhost:9000/foodservice/food">
                                    <authentication>
                                        <oauth>
                                            <clientCredentials>
                                                <clientId>{$ctx:clientID}</clientId>
                                                <clientSecret>my_client_secret</clientSecret>
                                                <tokenUrl>http://localhost:9000/foodservice/token</tokenUrl>
                                            </clientCredentials>
                                        </oauth>
                                    </authentication>
                                </http>
                            </endpoint>
                        </send>
                    </case>
                    <case regex="dynamicValues">
                        <property name="tokenUrl" value="http://localhost:9000/foodservice/token"/>
                        <property name="authMode" value="header"/>
                        <send>
                            <endpoint>
                                <http method="get" uri-template="http://localhost:9000/foodservice/food">
                                    <authentication>
                                        <oauth>
                                            <clientCredentials>
                                                <clientId>my_client_id</clientId>
                                                <clientSecret>my_client_secret</clientSecret>
                                                <tokenUrl>{$ctx:tokenUrl}</tokenUrl>
                                                <authMode>{$ctx:authMode}</authMode>
                                            </clientCredentials>
                                        </oauth>
                                    </authentication>
                                </http>
                            </endpoint>
                        </send>
                    </case>
                    <default>
                        <send>
                            <endpoint>
                                <http method="get" uri-template="http://localhost:9000/foodservice/{uri.var.val1}">
                                    <authentication>
                                        <oauth>
                                            <clientCredentials>
                                                <clientId>my_client_id</clientId>
                                                <clientSecret>my_client_secret</clientSecret>
                                                <tokenUrl>http://localhost:9000/foodservice/token</tokenUrl>
                                            </clientCredentials>
                                        </oauth>
                                    </authentication>
                                </http>
                            </endpoint>
                        </send>
                    </default>
                </switch>
            </inSequence>
            <outSequence>
                <send/>
            </outSequence>
            <faultSequence>
                <property name="HTTP_SC" value="500" scope="axis2"/>
                <respond/>
            </faultSequence>
        </resource>
        <resource uri-template="/list/dynamicURL" methods="POST">
            <inSequence>
                <property expression="$body//ep_url" name="uri.var.ep" scope="default" type="STRING"/>
                <property expression="$body//token_ep" name="token_ep" scope="default" type="STRING"/>
                <send>
                    <endpoint>
                        <http method="get" uri-template="{uri.var.ep}">
                            <authentication>
                                <oauth>
                                    <clientCredentials>
                                        <clientId>my_client_id</clientId>
                                        <clientSecret>my_client_secret</clientSecret>
                                        <tokenUrl>{$ctx:token_ep}</tokenUrl>
                                    </clientCredentials>
                                </oauth>
                            </authentication>
                        </http>
                    </endpoint>
                </send>
            </inSequence>
            <outSequence>
                <send/>
            </outSequence>
            <faultSequence>
                <property name="HTTP_SC" value="500" scope="axis2"/>
                <respond/>
            </faultSequence>
        </resource>
    </api>
</definitions>
