# Authorization

### Overview

Authorization in QueryDeck governs access control by defining roles and permissions for database actions. It ensures that users can only perform actions they are allowed to, based on their assigned roles.

### Setting up Authorization

First set up a demo app (or with any databse of your choice), then change the menthod to select.

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2FbvIEv5eAHkaIMmKTLWDN%2Fimage.png?alt=media&#x26;token=d72afbff-9248-4337-ba2c-b22fccfb6c2b" alt=""><figcaption></figcaption></figure>

Now click on the security icon (shown below) present on the left toolbar

(note: make sure you already have existing authentication)

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2F7R8x7BnZxlxp9KSCco7p%2Fimage.png?alt=media&#x26;token=e3857280-607c-48ef-899f-319c07f971d3" alt=""><figcaption></figcaption></figure>

Now we get this page, which allows us to set up CORS, Authentication and Authorization

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2FXl7qYOiq2rrCydwnAnaJ%2Fimage.png?alt=media&#x26;token=678c4fd2-f7fa-4dcb-9068-0a0ee6dae502" alt=""><figcaption></figcaption></figure>

Click on the Roles & Authorization tab on the top right task bar

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2FWA0OWrjpnFRcSyB9Z9uy%2Fimage.png?alt=media&#x26;token=359fdeb7-0259-4069-bcd2-56b6af3bfef6" alt=""><figcaption></figcaption></figure>

Now click on the button to create a role

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2Fk95S1Q0QNeTEnI4Lw8cd%2Fimage.png?alt=media&#x26;token=8827c39d-5214-490e-b8b8-e0ed84aa12a1" alt=""><figcaption></figcaption></figure>

Now we get this pop up form

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2FPgMOiCoZKaaiJizzbXED%2Fimage.png?alt=media&#x26;token=f90e444e-7c48-440b-ac90-dc29fc45977f" alt=""><figcaption></figcaption></figure>

Enter Role name as desired, this is like a nickname to refer to your role

Role value is the variable you chose to assign to it, this will be used in jwt to access your role

Select desired role type from the dropdown (admin/custom). Admin grants complete access, whereas in custom you can choose how much access to give the user.

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2FhwJ55xEWoTzxUnsZHbPf%2Fimage.png?alt=media&#x26;token=7272ba64-b7c1-4395-842f-ae8c5ed26f67" alt=""><figcaption></figcaption></figure>

Now click on the "add" button, to add the role&#x20;

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2F26fBDN40suYFB8LPFxAy%2Fimage.png?alt=media&#x26;token=10ce2b33-4298-4553-b5e9-e5ab4f226f51" alt=""><figcaption></figcaption></figure>

The role for authentication has been set up successfully

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2FkiawmVscgSY2Wz4yc070%2Fimage.png?alt=media&#x26;token=77de6a2e-3aad-4a0a-b7d9-3c9ae052b637" alt=""><figcaption></figcaption></figure>

## Testing Authorization

### Getting the JWT

Goto <https://jwt.io/>&#x20;

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2FuVuKs8ZKejBhqyUAlggO%2FScreenshot%202025-01-08%20at%2010.35.50%E2%80%AFAM.png?alt=media&#x26;token=82804429-f46b-4294-a9de-4ba88e856572" alt=""><figcaption></figcaption></figure>

Scroll down to the debugger

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2FrIxrUfJBUi35uP17ElIO%2Fimage.png?alt=media&#x26;token=dff76e2b-72af-4662-9d10-bfdce8d0559c" alt=""><figcaption></figcaption></figure>

Now in the "Header" section in the decoded column, change the algorithm (alg) according to ur Authentication settings in the security tab (In this case, the default value matches our algorithm, so we leave it as is)

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2FjlWELmoow7JikjCoy0aU%2Fimage.png?alt=media&#x26;token=999b2048-7eec-432b-8f5e-cbc4c16fdfbe" alt=""><figcaption></figcaption></figure>

Next change the payload, delete the "sub" and "name" parameters, leave the "iat" as is, this is the valid time period parameter

Now add your user session key(email) and role session key(role\_id) as additional parameters, and set their value to the desired value  (here user email) and the role id set in authorization respectively

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2F0NuPLyzgR4ECaRpLarDB%2Fimage.png?alt=media&#x26;token=a94151e6-e973-45a7-8f44-7b9c7f59ada8" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2Fj3ycZZRN1ATyqRdzrknj%2Fimage.png?alt=media&#x26;token=2017d3da-1f57-420d-a254-5276746a1b45" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2FkaclArchGOl3kuRoJMjE%2Fimage.png?alt=media&#x26;token=2d9ddb80-3911-4980-a1a7-f6b5ef06bb50" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2F3bm4szXZ5bFmHFVmkkwW%2Fimage.png?alt=media&#x26;token=0517bfb3-e255-405d-9b0d-7b04a59a7b2f" alt=""><figcaption></figcaption></figure>

Next we change the secret key in the verify signature.

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2F1faZp1NjDF218gTRp2hK%2F769d8e3e69357de5e2ffc2e661a17d76.png?alt=media&#x26;token=de4e565b-3b90-425b-8fb4-cc212ae8bdfa" alt=""><figcaption></figcaption></figure>

(on click reveals password)

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2F8kgUjcgQzVPjUNve0yM9%2Fimage.png?alt=media&#x26;token=acd67992-e663-4c7d-a39a-493357047c58" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2Fr1BUNizuezkHy426oy48%2Fimage.png?alt=media&#x26;token=fffc327b-b9ea-4ff1-82e1-79e5be771c7a" alt=""><figcaption></figcaption></figure>

now copy the JWT string&#x20;

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2Fl0unmmFLRHhny4qRnI3j%2Fimage.png?alt=media&#x26;token=33749be1-d547-40b5-bc04-ddfeb58aab71" alt=""><figcaption></figcaption></figure>

Also goto the api you want to test on and enable authentication on the left hand side pane

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2F3hTqLupyyXvBsMaviGPK%2Fimage.png?alt=media&#x26;token=753ad890-d0b6-494c-9f13-5f55840c2b13" alt=""><figcaption></figcaption></figure>

Then copy the endpoint URL from the section on the right

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2FCS0vTiYSAMbH7eBblHhN%2Fimage.png?alt=media&#x26;token=6c38b6cc-8147-458b-9c32-2b9121adebde" alt=""><figcaption></figcaption></figure>

Now open postman and past the url and change the method to GET

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2Fh7IS1ildeP23CNlMc0gv%2Fimage.png?alt=media&#x26;token=9b4d11f3-44a6-4c9f-8b75-34af806c1709" alt=""><figcaption></figcaption></figure>

Click on the Headers tab

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2FPObm0qSOIhQWI3jHeJ2C%2Fimage.png?alt=media&#x26;token=7a21cb4e-cb27-412a-9dea-c9331868750b" alt=""><figcaption></figcaption></figure>

Add a key for authorization and paste the jwt from jwt.io

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2Fd6QpXypIQYGkclrOQJP8%2Fimage.png?alt=media&#x26;token=9f7dc1ba-caf5-439e-a10f-f22e5200b5b2" alt=""><figcaption></figcaption></figure>

Now click on the send button to place the request&#x20;

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2FLIw1qMcdju6LgvcpTXTd%2Fimage.png?alt=media&#x26;token=4cced6d0-fc2b-44cb-b366-996acc17f34c" alt=""><figcaption></figcaption></figure>

As you can see, we have obtained teh required response

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2FmZgwSgybX9me4D3NH68L%2Fimage.png?alt=media&#x26;token=2aa7ea0c-e3cf-4ba9-8c2a-2f75f78c69c3" alt=""><figcaption></figcaption></figure>

If we change the jwt in the header to some other value, we recieve an error response&#x20;

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2FRZvEo0vWehPP35PjJoNf%2Fimage.png?alt=media&#x26;token=dc0e1f59-0814-4ab7-bd88-699117ac8dd2" alt=""><figcaption></figcaption></figure>

We have successfully created and tested Authentication and Authorization

Note:

* session columns won't show up until we enable auth
* if session columns are selected then auth can't be disabled

<figure><img src="https://1803924624-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjtTl1rn3hJ3jyoJt6ty%2Fuploads%2F7ZkY5XjRYVZA0Qiu3YSu%2Fimage.png?alt=media&#x26;token=05771228-0b9e-4807-8ae4-74c5494b7b95" alt=""><figcaption></figcaption></figure>
