> ## 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 Plain-Text Authentication Policy to REST API via Database Provider

> Explains how to apply Plain-Text Authentication policy to Swagger Petstore REST API via database provider. Includes all steps from establishing database connection to authentication settings, adding policy to testing.

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

* **Security Manager** in **Apinizer** expects **Plain-Text type** security information from **API Client**.
* **Authentication** information is sent to the database to be checked.
* **From the database**, a response regarding authentication information is returned to Apinizer. If this authentication cannot be verified, Apinizer stops the request and returns an error message. If verification can be performed, the flow continues.
* **Apinizer makes a request to Backend API**.
* **Backend API responds to Apinizer**.
* **Apinizer transmits the response to API Client**.

<img src="https://mintcdn.com/apinizer/fVODWwZ3NOBhJZqU/images/tutorials/database1.png?fit=max&auto=format&n=fVODWwZ3NOBhJZqU&q=85&s=fe04357f84bca29518b2753bf965aa2d" alt="Scenario Diagram" width="800" data-path="images/tutorials/database1.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/).

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!** may appear.
</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" />

## Establishing Database Connection

Click the **Database** menu under the **Connection Management** menu.

<Info>
  If no database definition has been made before, the text **No records found!** may be seen on this page.
</Info>

Click the **Create** button in the top right corner to create the database connection.

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

The fields on this screen:

* The **Name** field expresses the name of the connection to be created.
* A description can be added to the created connection with the **Description** expression.
* Write the JDBC address required to connect to the database to be connected to with the **JDBC URL** field. This information can be obtained from the database team.
* Enter the username information to be used during connection with the **Username** field, and enter the password to be used during connection with the **Password** field.

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

<Tip>
  This section is arranged by obtaining information from the database administrator or team about what the most appropriate values will be for the connection information on the right.
</Tip>

Click the **Test Connection** button to perform the test, and click the **Save and Deploy** button to save the operations.

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

After the registration operation is completed, the created connection is displayed as in the image below.

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

## Adding Database Authentication Connections

Go to **Identity Management > Identity Providers > Database** section to add the database authentication definition created earlier.

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

### Database Authentication Connection

Press the **Create** button and enter connection settings:

* Enter name and description for the connection.
* If the **password** information in the table to be used in the database connection is kept encrypted, the encryption type of the password stored in the table must be selected with **Encryption Type**.
* Select the database prepared in previous steps with the **Database Connection Pool Definition** field.
* A query is used to retrieve username/password pairs or role lists from the database. The username parameter in the query must be defined as `:username` and the password parameter as `:password`. Apinizer recognizes the colon character and processes accordingly, knowing that the relevant parameter will be sent from outside.

<img src="https://mintcdn.com/apinizer/09PxOro0-7rcnyUk/images/tutorials/database6.png?fit=max&auto=format&n=09PxOro0-7rcnyUk&q=85&s=3e2b28adcf848ad113e4159835e5fb6a" alt="Scenario Diagram" width="800" data-path="images/tutorials/database6.png" />

<Warning>
  The **Test Username** and **Test Password** fields can be filled with information in the **database** to test whether the query works. **It is recommended to clear these fields before saving the definition.**
</Warning>

### Database Authorization Connection

The only difference from the authentication connection is the query entered. Since identity authorization will be performed in this field, only the name of the relevant role is retrieved in the query section.

<img src="https://mintcdn.com/apinizer/09PxOro0-7rcnyUk/images/tutorials/database7.png?fit=max&auto=format&n=09PxOro0-7rcnyUk&q=85&s=9be471d63e141307f7a5f7f511ba34ef" alt="Scenario Diagram" width="800" data-path="images/tutorials/database7.png" />

## Creating Authentication Policy

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

Go to the **Develop** tab and click the **Add Policy** button.

On the opened page, the **Plain-Text Authentication** policy is selected.

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

The fields on this screen:

* Select the **database** from the **Identity/Role/Group Service** field that will provide the control.
* The **Variable for username** and **variable for password** expressions also select which variable the username and password information will be retrieved with. In this scenario, these values will be expected and retrieved from the **username** and **password** values in the **header**.

<Warning>
  If the **Clear Authentication Information** option is selected, authentication information in the incoming message is deleted when going to the backend api. **Activating this setting is always recommended unless there is a special situation.**
</Warning>

* The **Add Client Info To Header** option enables the username to be transmitted in a separate header value in the Header going to the backend API.
* The **Authorization Configuration** field is the field where rules related to the roles of users whose authentication has been performed can be entered.
* If the **Add Roles To Header** option is selected, if authorization is successful, it adds the client's roles to the "X-Authenticated-UserRoles" header.
* Select the **database** from the **Identity/Role/Group Service** field where role information is located. This is usually done with the same definition as the database that provides identity control.
* Enter the roles to be authorized in the **Roles/Group** field.
* Set whether the client needs to have **all** or **any** of the roles/groups defined here to access the API.
* When the **Enable Role/Group Based Method Access** option is selected, first the roles granted access to the API are checked. Then, other roles are looked at to control access to methods.

<img src="https://mintcdn.com/apinizer/09PxOro0-7rcnyUk/images/tutorials/database8.png?fit=max&auto=format&n=09PxOro0-7rcnyUk&q=85&s=2f0d5762303905c6018c746502ff8baa" alt="Scenario Diagram" width="800" data-path="images/tutorials/database8.png" />

<Info>
  The relevant icon is seen when the policy is registered.
</Info>

For the operation to be valid, the proxy must be **Redeployed**. After the Redeploy operation, the API proxy can be tested.

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

## Testing the API Proxy

After selecting an endpoint such as **"/pet/{petId}"**, click the **Test Endpoint** button.

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

Enter the desired pet id value in the URL or parameter tab, and when the **Send** button is pressed, the returned response will be an error message.

<img src="https://mintcdn.com/apinizer/09PxOro0-7rcnyUk/images/tutorials/database9.png?fit=max&auto=format&n=09PxOro0-7rcnyUk&q=85&s=edf6477e04dde5486d12782794a892d7" alt="Scenario Diagram" width="800" data-path="images/tutorials/database9.png" />

<Warning>
  It will be seen that this error is related to the applied **Plain-Text Authentication**. Because no **authentication** information has been placed in the **headers** in any way.
</Warning>

Enter the information of a user in the database in the **username** and **password** headers in the **headers** to be sent to the **Backend API**, repeat the test, and expect to receive a successful response.

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