Secure an API
By default, an API's endpoints are public: anyone can call them. When security is enabled, the users of your API sign in to get a token (a JWT), then send it with every call. Each endpoint states which roles can call it.
This page is about REST API projects.
Enable security
Security is enabled when you create the project, with the Security Configuration option (see Create a project). It can't be enabled afterwards from the project edit screen.
On creation, Grizzly adds the Authentication Grizzly group, which holds the sign-in and user management endpoints.
The project's Security section
On the project page, below the endpoints, the Security section holds the settings. Click the arrow to open or close it.
Token Configuration
| Field | Purpose |
|---|---|
| Client ID | The API's identifier. By default, the project name. |
| Secret Key (64 characters) | The key that signs the tokens, generated by Grizzly. If you replace it, enter at least 64 characters, letters and digits only. |
| Token Expiration (in seconds) | How long a token stays valid. Default: 3600 (one hour). |
To change how long tokens stay valid, edit Token Expiration (in seconds), then click Save.
Manage Roles
A secured project starts with two roles: admin and user. To add one:
- Open the Manage Roles tab.
- Type the role name in the Add Role field, then click the Add Role button.
- Click Save.
The trash icon removes a role; remember to click Save afterwards. The admin role is locked (padlock icon) and can't be removed. If the role already exists, the message "This role already exists." is shown.
Choose who can call each endpoint
Each endpoint shows its access roles. Click them to open Access Roles and select public, all, admin, user or one of your custom roles. The details are in Resource groups and endpoints.
The roles of the endpoints in the Authentication Grizzly group are set by Grizzly and can't be changed.
Get a token
The examples in this section use the project's Runtime URL. The Security Documentation block, in the Security section, gives the same examples with your API's address already filled in, ready to copy.
Sign up a user
POST /signup creates an account. The firstname, lastname, username, password, email and phone fields are required, and email must be a valid address. Otherwise, the API responds 400 with a message naming the field at fault.
curl --header "Content-Type: application/json" \
--request POST \
--data '{"firstname":"Mary","lastname":"Smith","username":"mary","password":"myPassword","email":"mary@example.com","phone":"0600000000"}' \
https://<runtime-url>/signupThe new user gets the user role, but the account is inactive: an administrator must activate it (see below). If the username is already taken, the sign-up is refused.
Sign in
POST /signin checks the username and password, and returns a token:
curl --header "Content-Type: application/json" \
--request POST \
--data '{"username":"mary","password":"myPassword"}' \
https://<runtime-url>/signin{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1Ni..."
}If the credentials are wrong, the API responds 401. If the account hasn't been activated yet, it returns the message Your account will be activated soon by the admin instead of a token.
Call a protected endpoint
Add the token to the Authorization header, preceded by Bearer:
curl --header "Authorization: Bearer <token>" \
https://<runtime-url>/productsWhen the token expires, sign in again with POST /signin.
Manage your API's users
The Authentication Grizzly group also contains endpoints to manage accounts. Call them with the token of a user who has the required role.
| Endpoint | Required role | What it's for |
|---|---|---|
GET /me | all | The signed-in user's profile |
GET /allusers | admin | The list of users |
GET /allroles | admin | The list of roles |
POST /activate?username=mary | admin | Activate an account created with /signup |
PUT /grant?username=mary | admin | Give roles, with the body {"roles": ["user", "seller"]} |
DELETE /deleteuser?username=mary | admin | Delete a user |
With /grant, each role must exist in the Manage Roles tab, otherwise the API responds This role does not exist. Please enter a valid role.
What's next?
- Create an authentication service, with Keycloak, Google or GitHub for example: IAM and identity providers.
- Test your protected endpoints in Swagger: Test, export and share.
- Understand error responses: Errors.
