Validate ID tokens for MFA
When a user logs in, you get an ID token that contains information relevant to the user’s session in the form of claims. The relevant claim isamr
(authentication methods reference) which is a JSON array of strings that indicates the authentication method used during login. It must be present in the ID token’s payload and must contain the value mfa
.
Its values may include any of the pre-defined Authentication Method Reference Values. Because it can contain claims other than mfa
, when validating you must both test for its existence and examine its contents for a value of mfa
.
If a user attempts to access a restricted page and the token shows that the user has not authenticated with MFA, then you can retrigger authentication, which you have configured to trigger MFA using an Action. Once the user provides the second factor, a new ID token that contains the amr
claim is generated and sent to the app.
- Get the ID token.
- Verify the token’s signature, which is 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 following claims:
Claim Description exp
Token expiration iss
Token issuer aud
Intended recipient of the token amr
If amr
does not exist in the payload or does not contain the valuemfa
, the user did not log in with MFA. Ifamr
exists in the payload and contains the valuemfa
, then the user did log in with MFA.
AMR claim exceptions
Theamr
claim is required except in the following use cases:
- In hosted login flows, only after the user successfully passes an MFA challenge, the
amr
claim is injected into the ID token. If the app uses silent authentication or Refresh Tokens for newly issued ID tokens, theamr
claim will not be present because the user previously completed login with MFA. - MFA API issued tokens do not contain the
amr
claim. Theamr
claim flags the authentication methods used when the user receives the ID Token. In the MFA API authentication process, the application controls the authentication flow and can enforce MFA as needed.
Example: Values with MFA
Example: Values without MFA
Scenario: Salary data with push notifications
In the following scenario, a web app authenticates a user with a username and password. Some users want to access a specific screen that displays salary data, so they must authenticate with Guardian push factor.Prerequisites
For this scenario, you must configure the following items in the Dashboard:- Register a web app.
- Create a database connection.
- Enable MFA to use push notifications.
Create an Action
Create an Action that challenges the user to authenticate with MFA when the web app requests it. Go to Dashboard > Actions > Flows, and create an Action that contains the following content:- The
CLIENTS_WITH_MFA
variable contains the of the applications you want this Action to apply to. You can remove this (and theif
conditional that follows) if you don’t need it. - The
event.transaction.acr_values
property is an array of strings that contains the authentication context class reference(s) (acr
). This is an optional property that only exists when the application includes it in the authentication request to the . In this example, our web app will include it in the authentication request, but only when a user who has not already authenticated with MFA tries to access salary information. When our web app includes it, it will set a value ofhttp://schemas.openid.net/pape/policies/2007/06/multi-factor
, which indicates that we want the Authorization Server to require MFA, and theapi.multifactor
property value that we set in our code will challenge the user for MFA using any of the available methods that have been configured in the tenant. To learn more about theapi.multifactor.enable()
method, read Action Triggers: post-login API object. - The
http://schemas.openid.net/pape/policies/2007/06/multi-factor
policy defines an authentication mechanism where the end user authenticates to the Provider by providing more than one authentication factor, or MFA. To learn more, read OpenID Provider Authentication Policy Extension 1.0.
Configure app
Configure the app to check that the user has authenticated using MFA when a user tries to access the restricted salary information page. (When a user has authenticated with MFA, the ID token claims contain theamr
claim with a value of mfa
.) If the user has already authenticated with MFA, then the web app will display the restricted page; otherwise, the web app will send a new authentication request that includes the acr_values
parameter with a value of:
http://schemas.openid.net/pape/policies/2007/06/multi-factor
which will trigger the Action.
The web app in this scenario uses the Authorization Code Flow to authenticate, so the request is as follows:
amr
claim with a value of mfa
. To learn how to exchange the code for an ID token, read Add Login Using the Authorization Code Flow.
Validate ID token
In this scenario, perform the validations using the JSON Web Token Sample Code, which verifies the token’s signature (jwt.verify
), decodes the token, checks whether the payload contains amr
, and if so, logs the results in the console.