AdonisJS endpoint protection with Unkey RBAC
Overview
This a starter AdonisJS application which implements API key verification with Unkey RBAC. There are two endpoints, a public one and a protected one that requires a valid API Key.
Features
- Public endpoint accessible without authenticationn
- Protected endpoint only accessible with a valid API key
- Verification middleware to check API key validity on protected route
Quickstart
Create an Unkey account
- Go to unkey and create your account for free.
Create a permission and role
- Go to app/authorization/permissions and click on the "Create New Permission" button.
- Enter call-protected-route as the name and add a description if you want.
- Click "Create New Permission"
- Now head over to /app/authorization/roles and click on the "Create New Role" button.
- Enter a name for the role, for example, admin and select the permission from the prevous step
- Click "Create".
Create your API
- Go to https://app.unkey.com/apis and click on the "Create New API" button.
- Give it a name.
- Click "Create".
- Get the
API ID
.
Create your first key
- Click "Create Key" in the top right corner.
- Click "Create"
- Copy the key and save it somewhere safe.
Connect the key to the role
- Go to /app/apis and click on the API you created.
- Click on "Keys" in the tabs.
- Click on the key you created.
- Click on "Permissions" in the tabs.
- Check the role's checkbox to give the key the role and permission.
Set up the example
-
Clone the repository:
1git clone https://github.com/Ionfinisher/unkey-adonisjs-template.git
-
Navigate to the project directory:
-
Install the dependencies:
-
Change the .env.example to .env file and add the following:
1UNKEY_ROOT_KEY=your-root-key
2UNKEY_API_ID=your-api-id
Get the root key from https://app.unkey.com/settings/root-keys
-
Start the server
1cd to the root of the project directory
2node ace serve --watch
-
curl the unprotected route
1 curl http://127.0.0.1:3333/api/v1/public
It should return Heeyaaa!! Touchdown to the public endpoint!!
-
curl the protected route
1 curl http://127.0.0.1:3333/api/v1/protected -H "Authorization: Bearer <YOUR_KEY>"
It should return Woohoo!! Touchdown to the protected endpoint!!
License
This project is licensed under the MIT License.
Further information
For further information, go to unkey.