Security
Securing REST APIs created in API Express.
A REST API created in API Express can be invoked only if you know its API key. In certain cases, the API key security is not enough. There are certain cases where you will need to add extra security, such as user login before a particular API can be invoked. Today API Express supports storing user information in the following data sources:
User information will be stored in one of the data sources listed above, and a login will be required before a service can be invoked.
Turning on Security
To turn on security for an API Express project:
- Go to Settings.
- Check the Secure REST API check box to allow only authenticated users to call REST APIs in this project.
- Select the security provider from the list or use your custom security service. If you don't have any security providers listed, the sections below will explain the options available and how to selected and sign up with a provider.
Appery.io Database
You can use the Appery.io Database for security. When selecting this option, user login information will be stored in the built-in Users collection.
- To enable user authentication with the Appery.io Database, go to Resources > Security > Add new security provider.
- Select Appery.io Database Security for Provider type.
- Then select a database that you have in your account.
- Click Save to save the new security provider.
- Now to back to the API Express project Settings tab, and select the provider you just created. If you don't see the provider on the list, please refresh the page.
When this security option is configured and enabled, only users from the Users collection will be able to invoke APIs from this project. In other words, a session token will be required before an API can be invoked. The session token is added with the X-Appery-Session-Token header parameter.
Want to know more?
Please check this document to learn how you can add security to your database (Appery.io Database).
Authorization Flow
The following shows the authorization flow when using the Appery.io Database:
- A user invokes the login method of the API Express and passes his/her username and password.
- API Express invokes the login method of the Appery.io backend (database) and passes the user’s username/password.
- If the Appery.io backend doesn’t return the session token, API Express returns the information about the invalid credentials and the user gets the
403
error. - If the Appery.io backend returns the session token, API Express generates the API Express session token, then saves both: API Express session token and the backend session token, and returns API Express session token to the user.
- The user invokes a service of API Express (for example, find operation) and provides the session token.
- If API Express doesn’t find the session token in the session token storage, API Express returns the information about the invalid session token and the user gets the
403
error. - If API Express finds the session token in the session token storage, API Express invokes a backend service (from user database or Appery.io Database) with the backend session token, and returns the
200
response to the user.
Social Network
You can also use a social network (Twitter, Facebook, Google) to perform authentication.
API Express cloud version
Social network authentication is supported in API Express cloud version only.
The following shows the authorization flow when using a social network:
- A user invokes the JavaScript login method of a social network (for example, loginFB for Facebook) and gets the backend session token.
- The user invokes the set the SessionToken method of Client SDK of API Express (required when using when using the Client SDK).
- The user invokes a service of the API Express (for example, find operation), and provides the session token.
- If API Express doesn’t find the session token in the session token storage, API Express starts searching for the session token in the Appery.io backend.
- If the token isn’t found, API Express returns information about the invalid session token and the user gets the
403
error. - If the token is found, API Express saves it in the session token storage and invokes backend service with the backend session token to return the
200
response to the user.
Updated almost 2 years ago