# Actions to get permissions

**URL:** <https://community.auth0.com/t/actions-to-get-permissions/66159>\
**Category:** Get Help\
**Tags:** actions\
**Created:** [July 14, 2021, 2:32pm UTC](https://community.auth0.com/t/actions-to-get-permissions/66159 "2021-07-14T14:32:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jesum](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jesum/32/27831_2.png) [@jesum](https://community.auth0.com/u/jesum)\
**Post date:** [July 14, 2021, 2:32pm UTC](https://community.auth0.com/t/actions-to-get-permissions/66159/1 "2021-07-14T14:32:15Z")

</div>

hi everyone.

I have the following piece of code in an Action that is triggered during post-login:

```auto
exports.onExecutePostLogin = async (event, api) => {
  var map = require('array-map');

  var ManagementClient = require('auth0').ManagementClient;
  var management = new ManagementClient({
      domain: event.secrets.domain,
      clientId: event.secrets.clientId,
      clientSecret: event.secrets.clientSecret
  });

  var params = { id: event.user.user_id, page: 0, per_page: 50, include_totals: false };
  console.log('1');
  management.getUserPermissions(params, 
    function (err, permissions) {
      console.log('2');
      if (err) {
        // Handle error.
        console.log('err: ', err);
      } else {
        console.log('3');
        var permissionsArr = map(permissions.permissions, function (permission) {
            return permission.permission_name;
        });
        console.log('4');
        api.accessToken.setCustomClaim("https://abc.com/access_actions", permissions.Arr.join(" "));
        console.log('5');
      }
    }
  );
};

```

It does absolutely nothing. Can anyone spot what is wrong? I’m new to writing rules…the only thing I see in my logs is the output from console.log(‘1’).

---

<div class="post-metadata">

**Author:** ![jesum](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jesum/32/27831_2.png) [@jesum](https://community.auth0.com/u/jesum)\
**Post date:** [July 14, 2021, 2:36pm UTC](https://community.auth0.com/t/actions-to-get-permissions/66159/2 "2021-07-14T14:36:12Z")

</div>

By the way, I assume that domain should be “[dev-xxxxxxxx.xx.auth0.com](http://dev-xxxxxxxx.xx.auth0.com)”, right? No need to specify https or http etc. and no trailing forward slash.

I’ve manually called the management API using curl, the same client-id and client-secret, and it works just fine. I can get the results I want. So I know that part is correct (I’ve also tried hard coding them directly in the action).

Plus, I can see in my logs that the credentials are successfully sent across to the management API.

 ![image](https://global.discourse-cdn.com/auth0/original/3X/b/d/bd3d088ba55e1d4532f853384136356efa254a1f.png)

---

<div class="post-metadata">

**Author:** ![jesum](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jesum/32/27831_2.png) [@jesum](https://community.auth0.com/u/jesum)\
**Post date:** [July 14, 2021, 3:23pm UTC](https://community.auth0.com/t/actions-to-get-permissions/66159/3 "2021-07-14T15:23:41Z")

</div>

Nevermind. I managed to fix the issue.

FWIW, i changed the code to

```auto
exports.onExecutePostLogin = async (event, api) => 
{

  var map = require('array-map');

  var ManagementClient = require('auth0').ManagementClient;

  var management = new ManagementClient

  (

    {

      domain: event.secrets.domain,

      clientId: event.secrets.clientId,

      clientSecret: event.secrets.clientSecret

    }

  );

  var params = { id: event.user.user_id, page: 0, per_page: 50, include_totals: false };

  console.log('1');

  let userPermissions = await management.getUserPermissions(params);

  var permissionsArray = map(userPermissions, function (permission) 

    {

      return permission.permission_name;

    }

  );

  api.accessToken.setCustomClaim("https://abc.com/access", permissionsArray.join(" "));

};

```

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [July 14, 2021, 3:30pm UTC](https://community.auth0.com/t/actions-to-get-permissions/66159/5 "2021-07-14T15:30:10Z")

</div>

Thanks for posting a solution!

---

<div class="post-metadata">

**Author:** ![jesum](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jesum/32/27831_2.png) [@jesum](https://community.auth0.com/u/jesum)\
**Post date:** [July 14, 2021, 3:55pm UTC](https://community.auth0.com/t/actions-to-get-permissions/66159/6 "2021-07-14T15:55:43Z")

</div>

I’m back. Unfortunately, the same code doesn’t work in a M2M flow. ☹

```auto
exports.onExecuteCredentialsExchange = async (event, api) => {

  //var map = require('array-map');

  var ManagementClient = require('auth0').ManagementClient;

  var management = new ManagementClient

  (

    {

      domain: event.secrets.domain,

      clientId: event.secrets.clientId,

      clientSecret: event.secrets.clientSecret

    }

  );

  var params = { client_id: event.client.client_id };

  let clientObject = await management.getClient(params);

  console.log(clientObject);  

};

```

I’m seeing {“error”:“server\_error”,“error\_description”:“Timeout awaiting ‘response’ for 10000ms”} in curl.

What am I doing wrong? Basically I am trying to get all scopes given to a M2M application and insert it into a custom claim. I assume that’s in the getClient api call?

---

<div class="post-metadata">

**Author:** ![jesum](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jesum/32/27831_2.png) [@jesum](https://community.auth0.com/u/jesum)\
**Post date:** [July 14, 2021, 4:06pm UTC](https://community.auth0.com/t/actions-to-get-permissions/66159/7 "2021-07-14T16:06:34Z")

</div>

You know what else is strange? When I manually call the mgmt api using curl, i see this:

{“statusCode”:403,“error”:“Forbidden”,“message”:“Insufficient scope, expected any of: read:clients,read:client\_keys,read:client\_credentials,read:client\_summary”,“errorCode”:“insufficient\_scope”}

I don’t see “read:client\_summary” in the web console as a permission I can add. I even tried adding ALL the available permissions - I still see the same error.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [July 14, 2021, 5:53pm UTC](https://community.auth0.com/t/actions-to-get-permissions/66159/8 "2021-07-14T17:53:18Z")

</div>

> [@jesum](#):
>
> I’m seeing {“error”:“server\_error”,“error\_description”:“Timeout awaiting ‘response’ for 10000ms”} in curl.

Are you sure you have the correct domain?

> [@jesum](#):
>
> {“statusCode”:403,“error”:“Forbidden”,“message”:“Insufficient scope, expected any of: read:clients,read:client\_keys,read:client\_credentials,read:client\_summary”,“errorCode”:“insufficient\_scope”}

You may need to add it manually via the management API.

* * *

Also, how often are you expecting to make this call? You could run into rate limits pretty quickly. It looks like you are planning on adding permissions to each user’s token, and that wouldn’t be advisable.

---

<div class="post-metadata">

**Author:** ![jesum](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jesum/32/27831_2.png) [@jesum](https://community.auth0.com/u/jesum)\
**Post date:** [July 14, 2021, 9:50pm UTC](https://community.auth0.com/t/actions-to-get-permissions/66159/9 "2021-07-14T21:50:16Z")

</div>

Yup I see the rate limit for the management api is quite low and I will easily breach that threshold. I have a different approach to achieving what I need.

Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [July 29, 2021, 9:50pm UTC](https://community.auth0.com/t/actions-to-get-permissions/66159/10 "2021-07-29T21:50:18Z")

</div>

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