# How to add user permissions to ID token

**URL:** <https://community.auth0.com/t/how-to-add-user-permissions-to-id-token/88714>\
**Category:** Get Help\
**Tags:** id\_token, scopes, permissions\
**Created:** [August 13, 2022, 11:00pm UTC](https://community.auth0.com/t/how-to-add-user-permissions-to-id-token/88714 "2022-08-13T23:00:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aaron.hardy](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/aaron.hardy/32/39201_2.png) [@aaron.hardy](https://community.auth0.com/u/aaron.hardy)\
**Post date:** [August 13, 2022, 11:00pm UTC](https://community.auth0.com/t/how-to-add-user-permissions-to-id-token/88714/1 "2022-08-13T23:00:48Z")

</div>

Hey community. From our single-page application, we want to know the current user’s permission so we can control access to certain sections or buttons of our app.

We’re using organizations. As you’re probably aware, permissions are assigned to roles. Users, in my understanding, can be assigned to roles _within_ the context of organizations and can also be assigned to roles directly (outside the context of organizations). Because of this, we essentially need the union of permissions from (1) all the roles the user is assigned to within the organization the user is currently logged into as well as (2) the permissions from all roles that the user is assigned to directly (outside the context of organizations). This concept of unioning permissions from roles is basically [what’s described in the RBAC doc](https://auth0.com/docs/manage-users/access-control/rbac#overlapping-role-assignments).

I see no good way to do this. A solution I can come up with is to take the following steps within an post-login action:

- Call [management.organizations.getMemberRoles](https://auth0.github.io/node-auth0/OrganizationsManager.html#getMemberRoles) to get all the groups the user is assigned to within the organization the user is logged into.
- Call [management.getPermissionsInRole](https://auth0.github.io/node-auth0/ManagementClient.html#getPermissionsInRole) for each of the roles retrieved in the last step.
- Call [management.getUserPermissions](https://auth0.github.io/node-auth0/ManagementClient.html#getUserPermissions) to get all the permissions related to the roles the user is assigned to outside the context of organizations. I assume that’s what this method is doing anyway. The docs are sparse.
- Merge all the permissions retrieved in the last two steps and add the result to a custom claim on the ID token.

This is obviously very expensive and not feasible.

Another potential solution in our case would be to just copy over the scopes or permissions (if we use the _Add Permissions in the Access Token_ feature) from the access token to the ID token, but I don’t see any way to reference the access token scopes or permissions through actions or rules.

How are we supposed to accomplish this? If we’re not supposed to accomplish this for some security reason, can we get an explanation? This seems like it would be a very common use case.

FWIW, I’ve read the following related posts and I don’t see an answer that actually solves this issue.

The following question received no answers and doesn’t take organizations into consideration.

> [@Enforcing permissions in our SPA](https://community.auth0.com/t/enforcing-permissions-in-our-spa/46312):
>
> How should we determine whether a user has a specific permission in the frontend of our SPA? Let’s say we have “delete a Foo” functionality in our application which calls /delete/foo in the API, but we want to only permit users who have the “delete:foo” permission to be able to use it. If we turn on “Enable RBAC” and “Add Permissions in the Access Token” then the API can check the claims in the access token and check that the user has the “delete:foo” permission. That is great and a good start.…

The following question’s “solution” references a thread that doesn’t take organizations into consideration. Am I misunderstanding?

> [@Including organization member' permission in the IdToken](https://community.auth0.com/t/including-organization-member-permission-in-the-idtoken/78717/4):
>
> Hi @matheus.ferreira, Thank you for your patience. After looking into this further, there are some things I would like to address. First, the permissions assigned to a user are not dependent on an Organization, but rather on the API; Meaning that they are separate from one another. Moreover, the [thread](https://community.auth0.com/t/accessing-the-permissions-array-in-the-access-token/27559/9) you shared is the correct way to get the user’s permissions appended to the ID token. The proposed Rule will append the user’s permissions to the ID token for every user, regardless of their O…

The following question wanted permissions from not only the org the user is logged into but other orgs as well. In our case, we don’t need (or want) permissions from other orgs, but the comments seem to suggest there’s no good way of solving even our simpler requirements.

> [@Get User permissions within an organisation](https://community.auth0.com/t/get-user-permissions-within-an-organisation/70756/4):
>
> Hi @dan.woda, Unfortunately that won’t work for our use case, because the user may not be authenticated against the organisation that we’re checking the permissions for. In our app a user can be member of multiple organisations. Given that the current token will only contain permission data related to the organisation the user currently logged in to, we need a way of retrieving the users permissions for a given org that they’re attempting to access. Although ideally a single call that can give…

The following question has an answer that provides guidance on how to add a user’s permissions to the ID token, but, as far as I’m aware, doesn’t take organizations into consideration.

> [@Accessing the permissions array in the access token](https://community.auth0.com/t/accessing-the-permissions-array-in-the-access-token/27559/9):
>
> Unfortunately this is a bit confusing, I admit (already mentioned it to our documentation team): The Rules engine in Auth0 isn’t using the latest node-sdk as referenced in the API docs on Github by default. In order to enforce the latest version, you need to require it manually like below. Then the docs as on [https://auth0.github.io/node-auth0/module-management.ManagementClient.html#getUserPermissions](https://auth0.github.io/node-auth0/module-management.ManagementClient.html#getUserPermissions) apply. function (user, context, callback) { var ManagementClient = require('auth0@2.17.0…

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [September 1, 2022, 10:05pm UTC](https://community.auth0.com/t/how-to-add-user-permissions-to-id-token/88714/3 "2022-09-01T22:05:12Z")

</div>

Hey there @aaron.hardy apologize for the delayed response on this one!

> [@aaron.hardy](#):
>
> We’re using organizations. As you’re probably aware, permissions are assigned to roles. Users, in my understanding, can be assigned to roles _within_ the context of organizations and can also be assigned to roles directly (outside the context of organizations). Because of this, we essentially need the union of permissions from (1) all the roles the user is assigned to within the organization the user is currently logged into as well as (2) the permissions from all roles that the user is assigned to directly (outside the context of organizations). This concept of unioning permissions from roles is basically [what’s described in the RBAC doc](https://auth0.com/docs/manage-users/access-control/rbac#overlapping-role-assignments).

The joining of permissions is taken care of by RBAC either outside of the context of an organization (multiple direct roles) or within the context of an organization (multiple roles assigned at org level) but not both as you’ve discovered.

What might be an option, is to rely on the permission array added to the Access Token from a login within the context of a Organization while using the management API to [get a user’s direct roles](https://auth0.com/docs/api/management/v2/#!/Users/get_user_roles) and add those in a separate [custom claim](https://auth0.com/docs/get-started/apis/scopes/sample-use-cases-scopes-and-claims#add-custom-claims-to-a-token). You should be able to infer permissions based on the roles. This does however still require a call to the Management API:

[https://community.auth0.com/t/how-can-i-use-the-management-api-in-actions/64947](https://community.auth0.com/t/how-can-i-use-the-management-api-in-actions/64947)

This could be a good candidate for a [feedback request](https://community.auth0.com/c/feedback/95) as I totally understand where you are coming from.

Let us know what you think!

---

<div class="post-metadata">

**Author:** ![aaron.hardy](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/aaron.hardy/32/39201_2.png) [@aaron.hardy](https://community.auth0.com/u/aaron.hardy)\
**Post date:** [September 8, 2022, 8:41pm UTC](https://community.auth0.com/t/how-to-add-user-permissions-to-id-token/88714/4 "2022-09-08T20:41:45Z")

</div>

Thanks @ty.frith.

> [@ty.frith](#):
>
> What might be an option, is to rely on the permission array added to the Access Token from a login within the context of a Organization

This is the primary thing I’m trying to avoid. The frontend is supposed to treat the access token as an opaque string and shouldn’t try to decode it:

> [@Custom claims in access token with auth0-spa-js](https://community.auth0.com/t/custom-claims-in-access-token-with-auth0-spa-js/41518/2):
>
> Welcome to the Auth0 community, @n.selikoff! The access token is meant to be sent to an API/backend, and not to be decoded by the SPA (frontend). The SPA should treat it as an opaque string. This is why the SDK does not decode the access token or provide a method to access the contents. The ID token can be used in the SPA though. As you’ve already figured out, getIdTokenClaims() method can be used to get the claims.

Respecting this, if I want to access the permissions that are on the access token, I would have to make an extra call to my server API so that the server can decode the access token and return the permissions from it.

Also, if the permissions are in the access token, it means they would be sent to the server on every request to the server when they aren’t actually needed by the server at all. They’re only needed by the SPA.

It seems like it would be much more appropriate and simpler to just add the permissions to the identity token, which _is_ intended for the SPA to use, but I don’t see a feasible way to accomplish this with Auth0.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [September 9, 2022, 5:38pm UTC](https://community.auth0.com/t/how-to-add-user-permissions-to-id-token/88714/5 "2022-09-09T17:38:25Z")

</div>

> [@aaron.hardy](#):
>
> It seems like it would be much more appropriate and simpler to just add the permissions to the identity token, which _is_ intended for the SPA to use, but I don’t see a feasible way to accomplish this with Auth0.

Thanks for following up! Have you considered just using roles and/or even [user/app metadata](https://auth0.com/docs/manage-users/user-accounts/metadata)? While not as fine grained, you should be able to make decisions in your SPA based on the roles themselves. You might be able to add both sets of roles to an id token by way of an action - Organization context roles (`event.authorization.roles`) and direct roles added via the Management API. Again this is not an ideal solution (requires management API) and a bit outside of the current scope so I think a feedback request could be helpful, I’d be curious to see how much traction it would get with others.

I’ve opened this up for discussion to see if anyone else has ideas 💡

---

<div class="post-metadata">

**Author:** ![aaron.hardy](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/aaron.hardy/32/39201_2.png) [@aaron.hardy](https://community.auth0.com/u/aaron.hardy)\
**Post date:** [September 9, 2022, 7:36pm UTC](https://community.auth0.com/t/how-to-add-user-permissions-to-id-token/88714/6 "2022-09-09T19:36:50Z")

</div>

Thanks @ty.frith. If we only add roles to the ID token, we would end up needing to map the roles to permissions within the SPA. I hope you can appreciate that having conditionals like:

`canCreateTransactions = adminRole | editorRole | supportRole | ...`

is less scalable than just having a permission of `create:transactions` provided to the SPA on the ID token. Within the SPA, we don’t actually care what the user’s roles are; we only care about what they have permission to do.

If we were forced to maintain a mapping between roles to permissions within the SPA, it would end up being identical to the mapping that already exists within Auth0, leading to maintainability issues.

---

<div class="post-metadata">

**Author:** ![deanb](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/deanb/32/33281_2.png) [@deanb](https://community.auth0.com/u/deanb)\
**Post date:** [September 20, 2022, 1:25am UTC](https://community.auth0.com/t/how-to-add-user-permissions-to-id-token/88714/7 "2022-09-20T01:25:22Z")

</div>

I’m running into this exact problem as well.

@aaron.hardy have you managed to get this working with any kind of work around? I’m yet to find a work around that works or is acceptable.

---

<div class="post-metadata">

**Author:** ![aaron.hardy](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/aaron.hardy/32/39201_2.png) [@aaron.hardy](https://community.auth0.com/u/aaron.hardy)\
**Post date:** [September 20, 2022, 1:57am UTC](https://community.auth0.com/t/how-to-add-user-permissions-to-id-token/88714/8 "2022-09-20T01:57:20Z")

</div>

If you just want access to the permissions the user has within the organization, then there is a feasible workaround. The workaround is to put the permissions on the access token, then open up an endpoint on your server that returns the permissions that are on the access token, then make a request to the new endpoint from your SPA to get the permissions. It’s not ideal for multiple reasons. I’m hoping the team at Auth0 can provide a way to just simply put the permissions on the identity token so we can save a request and keep permissions out of the access token since the access token is sent on every request and we only need the permissions in the SPA.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [October 5, 2022, 1:58am UTC](https://community.auth0.com/t/how-to-add-user-permissions-to-id-token/88714/9 "2022-10-05T01:58:06Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
