How to Add Roles and Permissions to the ID Token Using Actions

Last Updated: Jul 26, 2024

Overview

This article clarifies whether it is possible to retrieve the user’s Roles and/or Permissions and include them in the JWT Token during the login flow.

Applies To

  • Actions

Solution

A Post-Login Action script must be used. Follow the video or steps below for Roles and/or Permissions.

Roles

Make sure that the user has a role by checking their user profile in the Dashboard. To do this:

  1. Go to Dashboard > User Management > Users and find the user’s profile page.

  2. Click on the Roles tab.

  3. If the user has no Roles assigned, click the Assign Roles button and select a role from the dropdown list.

When adding the user’s Roles to the token, call the event.authorization.roles property and add it as a custom claim to the Token. Please see here on creating namespaced custom claims. Below is an example of using a Post Login script to add Roles to the tokens.

/** 
* @param {Event} event - Details about the user and the context in which they are logging in. 

* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login. 
*/ 
exports.onExecutePostLogin = async (event, api) => { 
const namespace = '[https://my-app.example.com'](https://my-app.example.com'/);; 
if (event.authorization) { 
api.idToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles); api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles); 
} 
}

Permissions

For Permissions, use the Management API in Actions to call the Get a User’s Permission endpoint to include into the Token.

The user’s Roles can also be retrieved by calling the Management API’s Get a user’s roles endpoint.

Related References

4 Likes