> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apinizer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Application of OAuth2 Authentication Policy to REST API via Security Manager Provider

> Explains how to apply OAuth2 Authentication policy to Swagger Petstore REST API via Security Manager provider. Includes all steps from credential creation to adding policy, token creation to testing.

The numbering in the diagram below corresponds to the **order of operations**.

* **Security Manager** in **Apinizer** requests **OAuth2 type authentication** information from **API Client**. If this authentication is correct, the flow continues.
* **Apinizer** makes a request to **Backend API**.
* **Backend API** responds to **Apinizer**.
* **Apinizer** responds to **API Client**.

<img src="https://mintcdn.com/apinizer/bxDpmriTStVknLzL/images/tutorials/oauth1.png?fit=max&auto=format&n=bxDpmriTStVknLzL&q=85&s=a67c09fb07dc8db9833c31dad9639fb0" alt="Scenario Diagram" width="800" data-path="images/tutorials/oauth1.png" />

## Creating API Proxy

<img src="https://mintcdn.com/apinizer/VUbwu9iu7Snx108F/images/tutorials/senaryo2.png?fit=max&auto=format&n=VUbwu9iu7Snx108F&q=85&s=052195ac9313eff2da0121245624677f" alt="Scenario Diagram" width="200" data-path="images/tutorials/senaryo2.png" />

Access to the REST API named Swagger Petstore can be provided from [https://petstore.swagger.io/](https://petstore.swagger.io/).

First, this address must be defined as an **API Proxy**.

For this, click the **API Proxies** option under the **Development** menu.

<Info>
  Since no **proxy** definition has been made before on the opened page, the text **No records found!** appears.
</Info>

Click the **Create** button in the top right corner and start creating a new **proxy**.

<img src="https://mintcdn.com/apinizer/VUbwu9iu7Snx108F/images/tutorials/senaryo3.png?fit=max&auto=format&n=VUbwu9iu7Snx108F&q=85&s=938b0de54e7641fce65b865f3f5182b0" alt="Scenario Diagram" width="800" data-path="images/tutorials/senaryo3.png" />

In this section, it is necessary to select what type the **API Proxy** to be created is.

Since the type of API to be used in this scenario will be **Swagger 2.X**, this type is selected.

Click on the **Enter URL** expression to switch to the screen where the address of the API to be used will be entered.

<img src="https://mintcdn.com/apinizer/ocsi_kVjLluGlu4Z/images/tutorials/swagger.png?fit=max&auto=format&n=ocsi_kVjLluGlu4Z&q=85&s=13223791b611fc908487605c87b11809" alt="Senaryo Diyagramı" width="800" data-path="images/tutorials/swagger.png" />

Enter the **address to be accessed** in the URL section and click the **Parse** button.

<img src="https://mintcdn.com/apinizer/yvmWFcjBSxooF86u/images/tutorials/senaryo5.png?fit=max&auto=format&n=yvmWFcjBSxooF86u&q=85&s=ab4f310e6a80807e54f36f0c72c39e4e" alt="Scenario Diagram" width="800" data-path="images/tutorials/senaryo5.png" />

After the Parse operation is performed, settings for the API Proxy can be made:

* The **Usage** field specifies who will use the created API Proxy. Options such as **publisher, consumer, publisher and consumer** are available here.
* The **Sharing Type** field specifies the sharing type of the created API Proxy. Options such as **external, internal, external and internal** are available here.
* One or both of the two API addresses under the **Addresses** tab can be selected. If both addresses are selected, Apinizer will perform the **Load Balance** operation itself.
* **Relative Path** is the address where the created API Proxy will be opened for access.
* The **Category List** field also allows categorization of the created API Proxy.

<img src="https://mintcdn.com/apinizer/VUbwu9iu7Snx108F/images/tutorials/senaryo6.png?fit=max&auto=format&n=VUbwu9iu7Snx108F&q=85&s=511fa694bec2af3fdd31ccc6bb17115f" alt="Scenario Diagram" width="800" data-path="images/tutorials/senaryo6.png" />

After these settings are made, the API Proxy is saved.

After the save operation, click the **Develop** tab on the opened page.

<img src="https://mintcdn.com/apinizer/VUbwu9iu7Snx108F/images/tutorials/senaryo7.png?fit=max&auto=format&n=VUbwu9iu7Snx108F&q=85&s=423589a8890368635457719c9d59acec" alt="Scenario Diagram" width="800" data-path="images/tutorials/senaryo7.png" />

<Info>
  Policies to be added with the **All** expression above these endpoints can be applied to **all endpoints**.
</Info>

The created API Proxy is deployed. For this, click the **Deploy** button in the middle section above.

<img src="https://mintcdn.com/apinizer/VUbwu9iu7Snx108F/images/tutorials/senaryo8.png?fit=max&auto=format&n=VUbwu9iu7Snx108F&q=85&s=3338ba7df1da3c1536385471851b0fff" alt="Scenario Diagram" width="800" data-path="images/tutorials/senaryo8.png" />

## Creating Credentials

The information for the **Credentials** to be added will be **username = apinizer**, **password = 123123aA**.

For this, go to the **Identity Management** menu.

Here, click the **Credentials** menu under the **Credential Management** menu.

<img src="https://mintcdn.com/apinizer/VUbwu9iu7Snx108F/images/tutorials/senaryo9.png?fit=max&auto=format&n=VUbwu9iu7Snx108F&q=85&s=b6251e96ba25024507bf54bb9c42088e" alt="Scenario Diagram" width="200" data-path="images/tutorials/senaryo9.png" />

Click the **Create** button in the top right corner on the opened screen.

<img src="https://mintcdn.com/apinizer/VUbwu9iu7Snx108F/images/tutorials/senaryo10.png?fit=max&auto=format&n=VUbwu9iu7Snx108F&q=85&s=7f11ddaa0164cd3fe50bb9632b47235a" alt="Scenario Diagram" width="600" data-path="images/tutorials/senaryo10.png" />

Here, the required fields are filled as previously specified and the created **credential** is saved by clicking the **Save and Deploy** button.

<img src="https://mintcdn.com/apinizer/VUbwu9iu7Snx108F/images/tutorials/senaryo11.png?fit=max&auto=format&n=VUbwu9iu7Snx108F&q=85&s=69a55f3f592c442fb8b0606c6e226b08" alt="Scenario Diagram" width="800" data-path="images/tutorials/senaryo11.png" />

The proxy that this **credential element** will provide access to must be selected. Hover over the created **credential** and click the **Edit** option from the menu on the side.

<img src="https://mintcdn.com/apinizer/VUbwu9iu7Snx108F/images/tutorials/senaryo12.png?fit=max&auto=format&n=VUbwu9iu7Snx108F&q=85&s=bef3068f5bab8cd8bdb0db0989543527" alt="Scenario Diagram" width="800" data-path="images/tutorials/senaryo12.png" />

Click the **API Proxy ACL** tab from the opened screen, and click the button in this tab.

<img src="https://mintcdn.com/apinizer/bxDpmriTStVknLzL/images/tutorials/api-proxy-acl.png?fit=max&auto=format&n=bxDpmriTStVknLzL&q=85&s=4382f031671c9a1db129f0cbec08544f" alt="Scenario Diagram" width="800" data-path="images/tutorials/api-proxy-acl.png" />

On the opened page, **API Proxies** in the project currently being worked on are listed. The **proxy** named **Swagger Petstore** is selected.

Click the **Add** button to specify that the created **Credential** element will have access to this **proxy**.

<img src="https://mintcdn.com/apinizer/VUbwu9iu7Snx108F/images/tutorials/senaryo14.png?fit=max&auto=format&n=VUbwu9iu7Snx108F&q=85&s=86bfab93f7d0e4dfe924a20118d31bce" alt="Scenario Diagram" width="600" data-path="images/tutorials/senaryo14.png" />

Click the **Save and Deploy** button in the top right corner and save the operation.

<img src="https://mintcdn.com/apinizer/DnFDBUWIRDlha2Jf/images/tutorials/save.png?fit=max&auto=format&n=DnFDBUWIRDlha2Jf&q=85&s=8ba1e2ddbbe1b48baf9a833b3337b967" alt="Scenario Diagram" width="600" data-path="images/tutorials/save.png" />

## Adding Authentication Policy

The **OAuth2 Authentication** policy can now be added.

Go to the page where API proxies are listed and select the proxy named **Swagger Petstore** from here.

Then go to the **Develop** tab and click the **Add Policy** button.

On the opened page, the **OAuth2 Authentication** policy is selected.

<img src="https://mintcdn.com/apinizer/bxDpmriTStVknLzL/images/tutorials/oauth2.png?fit=max&auto=format&n=bxDpmriTStVknLzL&q=85&s=e562c5dc4a22908715fcc62786f60336" alt="Scenario Diagram" width="800" data-path="images/tutorials/oauth2.png" />

The fields on this screen:

* The **Grant Type** field specifies how user information of the **token service** to be used will be verified. If the **Client Credentials** expression is selected, **(Identity/Role/Group) Service** cannot be used.
* The **Show API Key** field displays the **API proxy key** values belonging to the created proxy.
* The **Delete Previous Token** field invalidates the previously found token.
* With the **Token Never Expires** field, no time value is assigned to the created token and it can be used as desired. If this option is not selected, a menu appears immediately below as shown in the image below.
* The **Token Expires In** field specifies how long the token will be valid. This specification can be adjusted with time expressions in the dropdown menu.
* The **Refresh Token Allowed** field activates the renewal feature of the created token. If this option is selected, a field appears for setting how many times it can be renewed, and the image for this field is shown below.
* **Refresh Token Count** specifies how many times the token can be renewed.
* The **Refresh Token Expires In** field specifies the lifetime of the renewed token.
* The **Allow URL Parameters** field allows information to be sent only in the message body when a request is sent for token generation. If this information is desired to be sent as "URL Parameter", this option must be selected, but this situation will create a security risk and is not recommended.

<Warning>
  With the **Clear Authentication Information** field, if there is any authentication information in the message going to the backend API, this information is deleted and not sent to the **backend API**. Activating this setting is always recommended unless there is a special situation.
</Warning>

* With the **Add Client Info to Header** field, when authentication is successfully performed, it sends the authorized username to the backend API by placing it in the header. When this option is checked, an input field containing **X-Authenticated-UserId** appears immediately below. This field is the default name of the header information.

<img src="https://mintcdn.com/apinizer/P7f4dmgx7HxuJgfb/images/tutorials/oauth3.png?fit=max&auto=format&n=P7f4dmgx7HxuJgfb&q=85&s=d7712557d6e0522ccedf961d802c504a" alt="Scenario Diagram" width="800" data-path="images/tutorials/oauth3.png" />

After making the settings to be used in this scenario, click the **Save** button in the top right corner.

<img src="https://mintcdn.com/apinizer/P7f4dmgx7HxuJgfb/images/tutorials/oauth4.png?fit=max&auto=format&n=P7f4dmgx7HxuJgfb&q=85&s=d95d2237fbebb891630bdce1770f65f1" alt="Scenario Diagram" width="800" data-path="images/tutorials/oauth4.png" />

For the operation to be valid, the proxy must be **Deployed**.

## Creating Token for Authentication

The Public Key information belonging to the relevant proxy is obtained from the **Show API Key** option in the OAuth2 Authentication policy.

<img src="https://mintcdn.com/apinizer/hgrNPZYFT2UBF_R9/images/tutorials/token1.png?fit=max&auto=format&n=hgrNPZYFT2UBF_R9&q=85&s=00f4c6fb6f7d9b56f148fb27f84e736f" alt="Scenario Diagram" width="400" data-path="images/tutorials/token1.png" />

Then go to the **Test Console** menu under the **Test** menu.

<img src="https://mintcdn.com/apinizer/hgrNPZYFT2UBF_R9/images/tutorials/token2.png?fit=max&auto=format&n=hgrNPZYFT2UBF_R9&q=85&s=eff44109815ffdf27ed437245454aa5e" alt="Scenario Diagram" width="200" data-path="images/tutorials/token2.png" />

After coming to this screen, click the **New** button under the **Collection** tab and create a new **Collection**.

Write **OAuth2** in the **Name** field and click the **Save** button.

<img src="https://mintcdn.com/apinizer/P7f4dmgx7HxuJgfb/images/tutorials/oauth5.png?fit=max&auto=format&n=P7f4dmgx7HxuJgfb&q=85&s=8c3c960ded7e081dff489cec1c2685a3" alt="Scenario Diagram" width="400" data-path="images/tutorials/oauth5.png" />

If the expressions on this screen are examined one by one,

* Select the method type from the **Method** field.
* Write the address where the token will be received in the URL field.
* Since the message content will be in the **body** section, select the **body** expression from the tab below.
* Write the values required to obtain the token here and click the **Send** button.

<img src="https://mintcdn.com/apinizer/P7f4dmgx7HxuJgfb/images/tutorials/oauth6.png?fit=max&auto=format&n=P7f4dmgx7HxuJgfb&q=85&s=1f9b98f6ba9f0b41b64c09e28c1e0de2" alt="Scenario Diagram" width="800" data-path="images/tutorials/oauth6.png" />

The **access\_token** information in the response has been created to use in the proxy where the **OAuth2 Authentication** policy is created.

This token is copied.

Switch back to the page where the API proxy is located.

<img src="https://mintcdn.com/apinizer/P7f4dmgx7HxuJgfb/images/tutorials/oauth7.png?fit=max&auto=format&n=P7f4dmgx7HxuJgfb&q=85&s=c5480031462b23782a466d4f41233e63" alt="Scenario Diagram" width="800" data-path="images/tutorials/oauth7.png" />

## Testing the API Proxy

Select the proxy named **Swagger Petstore**.

Select the endpoint named **"/pet/{petId}"** under the **Develop** tab.

Click the **Test Endpoint** expression to test this endpoint.

Enter the desired **petId** value as **"1"** in the URL, and when the **Send** button is pressed, it is seen that the returned response is an error message.

<img src="https://mintcdn.com/apinizer/P7f4dmgx7HxuJgfb/images/tutorials/oauth8.png?fit=max&auto=format&n=P7f4dmgx7HxuJgfb&q=85&s=d6dac3b1a045c005d0af955c56c58b0a" alt="Scenario Diagram" width="800" data-path="images/tutorials/oauth8.png" />

<Warning>
  It is seen that this error is related to the applied **OAuth2 Authentication**. Because no **authentication** information has been placed in the **header** in any way.
</Warning>

This time, place the **Authorization header** in the **header** and use the obtained **token information** here.

When the **Send** button is clicked, a successful response is received.

<img src="https://mintcdn.com/apinizer/P7f4dmgx7HxuJgfb/images/tutorials/oauth9.png?fit=max&auto=format&n=P7f4dmgx7HxuJgfb&q=85&s=988ebb6398670f0f027d63adb51b6de1" alt="Scenario Diagram" width="800" data-path="images/tutorials/oauth9.png" />
