Validate access tokens for MFA
In addition to checking the scope, the API must validate the access token to:- Verify the token’s signature, used to verify that the sender of the token is who it says it is and to ensure that the message wasn’t changed along the way.
-
Validate the standard claims:
Claim Description expToken expiration issToken issuer audIntended recipient of the token
Scenario: Bank transactions with push notifications
In the following scenario, an application authenticates a user with username and password and then requests an account balance. Before retrieving the account balance information, the user must authenticate with Guardian push factor. The banking API can accept two different levels of authorization: view account balance (scopeview:balance) or transfer funds (scope transfer:funds). When the application asks the API to retrieve the user’s balance, the access token should contain the view:balance scope. To transfer money to another account, the access token should contain the transfer:funds scope.
Workflow
- The user logs in to the application using username and password authentication. The standard login gives this user the ability to interact with the API and fetch their balance. This means that the access token that the app receives after the user authenticates contains the
view:balancescope. - The application sends a request to the API to retrieve the balance, using the access token as credentials.
- The API validates the token and sends the balance info to the application, so the user can view it.
- The user wants to transfer funds from one account to another, which is deemed a high-value transaction that requires the
transfer:fundsscope. The application sends a request to the API using the same access token. - The API validates the token and denies access because the token is missing the required
transfer:fundsscope. - The application redirects to Auth0, where an Action is used to challenge the user to authenticate with MFA since a high-value scope was requested. Once the user successfully authenticates with MFA, a new access token that includes the correct scope is generated and sent to the application as part of the response.
- The application sends another transfer funds request using the new access token, which includes the
transfer:fundsscope this time. - The API validates the token, discards it, and proceeds with the operation.
Prerequisites
For this scenario, you must configure the following items in the Dashboard:- Register a Single-Page Web App.
- Create a database connection.
- Register the API. Create two scopes:
view:balanceandtransfer:funds. - Enable MFA to use push notifications.
Create an Action
Create an Action that challenges the user to authenticate with MFA when thetransfer:funds scope is requested. Go to Dashboard > Actions > Flows and create an Action that contains the following content:
- The
CLIENTS_WITH_MFAvariable contains the of the applications you want this Action to apply to. You can remove this (and theifconditional that follows) if you don’t need it. - The
event.transaction.requested_scopesproperty contains all the scopes for which the authentication request asked. If it includes the valuetransfer:funds, then we ask for MFA by setting thecontext.multifactorproperty to the appropriate value. In this case, we are asking for MFA using push.
Configure app
Configure the app to send the appropriate authentication request to the API, depending on whether the user is attempting to perform the high-value transaction of transferring funds. Notice that the only difference between the two authentication requests (with or without MFA) is the scope.-
With MFA:
-
Without MFA:
| Parameter | Setting |
|---|---|
audience | Set to the Identifier of your API (find it at API Settings). We set ours to https://my-banking-api. |
response_type | Set to id_token token so we get both an ID Token and an Access Token in the response. |
client_id | Set to the Client ID of your application (find it at Application Settings). |
redirect_uri | Set to a URL in your application that Auth0 should redirect back to after authentication (find it at Application Settings). |
nonce | Set to a secure string value which will be included in the response from Auth0. This is used to prevent token replay attacks and is required for response_type=id_token token. |
state | Set to an opaque value that Auth0 includes when redirecting back to the application. This value must be used by the application to prevent CSRF attacks. |
Configure API
Configure the API to validate the incoming token and check the authorized permissions.-
Configure two endpoints for our API:
GET /balance: to retrieve the current balancePOST /transfer: to transfer funds -
Use
Node.jsand a number of modules:- express: adds the Express web application framework.
- jwks-rsa: retrieves RSA signing keys from a JWKS (JSON Web Key Set) endpoint. Using
expressJwtSecret, we can generate a secret provider that will issue the right signing key toexpress-jwtbased on thekidin the JWT header. - express-jwt: lets you authenticate HTTP requests using JWT tokens in your Node.js applications. It provides several functions that make working with JWTs easier.
- express-jwt-authz: checks if the access token contains a specific scope.
-
Install the dependencies:
npm install express express-jwt jwks-rsa express-jwt-authz --save -
Define the API endpoints, create a middleware function to validate the access token, and secure the endpoints using that middleware. The code in your
server.jsfile should look like the following sample script:Each time the API receives a request the following happens:- The endpoint calls the
checkJwtmiddleware. express-jwtdecodes the token and passes the request, the header, and the payload tojwksRsa.expressJwtSecret.jwks-rsadownloads all signing keys from the JWKS endpoint and checks if one of the signing keys matches thekidin the header of the access token. If none of the signing keys match the incomingkid, an error is thrown. If there is a match, we pass the right signing key toexpress-jwt.express-jwtcontinues its own logic to validate the signature of the token, the expiration, audience, and the issuer.jwtAuthzchecks if the scope that the endpoint requires is part of the access token. If the specified scopes are missing from the access token, the request is rejected with a 403 error message.
- The endpoint calls the