Sample Project

This tutorial shows you how to use the authorization features in the OAuth 2.0 framework to limit access to your or third-party applications.
For more information, read the API authorization documentation.

Create a Resource Server (API)

In the APIs section of the Auth0 dashboard, click Create API. Provide a name and an identifier for your API. You will use the identifier as an audience later, when you are configuring the Access Token verification. For Signing Algorithm, select RS256.

Add API Authorization

To restrict access to the resources served by your API, check the incoming requests for valid authorization information.
The authorization information is stored in the Access Token created for the user and needs to be sent in the Authorization header. To see if the token is valid, check it against the JSON Web Key Set (JWKS) for your Auth0 account. To learn more about validating Access Tokens, read the Verify Access Tokens tutorial.

Configuration

By default, your API will be set up to use RS256 as the algorithm for signing tokens. Since RS256 works by using a private/public keypair, tokens can be verified against the public key for your Auth0 account. This public key is accessible at https://YOUR_AUTH0_DOMAIN/.well-known/jwks.json.

Configure the express-jwt middleware to use the remote JWKS for your Auth0 account.

Configure the Scopes

Scopes let you define which resources can be accessed by the user with a given Access Token. For example, you might choose to give the read access to the messages resource if a user has the manager access level, and a write access to that resource if they have the administrator access level.

To configure scopes, in your Auth0 dashboard, in the APIs section, click the Scopes tab. Configure the scopes you need.

This example uses the read:messages scope.

Secure your API

To protect an individual route that requires a valid JWT, configure the route with the checkJwt express-jwt middleware.

If you are following along with the sample project you downloaded from the top of this page, base URL for Falcor's model should be set to http://localhost:3000.

Individual routes can be configured to look for a particular scope by setting up another middleware with the express-jwt-authz package. To do so, provide an array of required scopes and apply the middleware to any routes you wish to add authorization to.