# Auth0+Django Setting Roles and Permissions

**URL:** <https://community.auth0.com/t/auth0-django-setting-roles-and-permissions/78702>\
**Category:** Get Help\
**Tags:** django, rbac, auth0\
**Created:** [February 22, 2022, 10:03am UTC](https://community.auth0.com/t/auth0-django-setting-roles-and-permissions/78702 "2022-02-22T10:03:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ovezovv](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ovezovv/32/35366_2.png) [@ovezovv](https://community.auth0.com/u/ovezovv)\
**Post date:** [February 22, 2022, 10:03am UTC](https://community.auth0.com/t/auth0-django-setting-roles-and-permissions/78702/1 "2022-02-22T10:03:42Z")

</div>

When I implement Auth0 Authentication and “Set Roles to User” Rule

```auto
function (user, context, callback) {
  const namespace = 'https://dev-o0hx8kbg.us.auth0.com';
  const assignedRoles = (context.authorization || {}).roles;
  const assignedPermis = (user.app_metadata || {}).permissions;

  let idTokenClaims = context.idToken || {};
  let accessTokenClaims = context.accessToken || {};

  idTokenClaims[`${namespace}/roles`] = assignedRoles;
  accessTokenClaims[`${namespace}/roles`] = assignedRoles;
  idTokenClaims[`${namespace}/permissions`] = assignedPermis;
  accessTokenClaims[`${namespace}/permissions`] = assignedPermis;

  context.idToken = idTokenClaims;
  context.accessToken = accessTokenClaims;
  callback(null, user, context);
} 

```

When I am trying to jwt.decode, payload returns

```auto
Key Error at "https://dev-o0hx8kbg.us.auth0.com/roles"

```

Here is below my function in Django for retrieving payload

```auto
def get_user_details(self, response):
        # Obtain JWT and the keys to validate the signature
        id_token = response.get('id_token')
        audience = self.setting('SOCIAL_AUTH_AUTH0_KEY') # CLIENT_ID
        jwks = request.urlopen(
            'https://' + self.setting('SOCIAL_AUTH_AUTH0_DOMAIN') + '/.well-known/jwks.json')
        issuer = 'https://' + self.setting('SOCIAL_AUTH_AUTH0_DOMAIN') + '/'

        payload = jwt.decode(id_token, jwks.read(), algorithms=[
                             'RS256'], audience=audience, issuer=issuer)

        return {
            'username': payload['nickname'],
            'first_name': payload['name'],
            'picture': payload['picture'],
            'user_id': payload['sub'],
            'role': payload['https://dev-o0hx8kbg.us.auth0.com/roles']
        }

```

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [February 28, 2022, 8:41pm UTC](https://community.auth0.com/t/auth0-django-setting-roles-and-permissions/78702/3 "2022-02-28T20:41:30Z")

</div>

Hi @ovezovv,

Welcome to the Auth0 Community!

I understand you encountered issues setting roles and permissions in the ID token using an Auth0 Rule.

After reviewing your Rule and error carefully, I noticed that you used a reserved namespaced, specifically with the `auth0.com` domain.

Please be aware that custom namespace claims must use any non-Auth0 HTTP or HTTPS URL as a namespace identifier. Auth0 domains cannot be used as namespace identifiers, and include:

- [auth0.com](http://auth0.com)
- [webtask.io](http://webtask.io)
- webtask.run

> **[Create Custom Claims](https://auth0.com/docs/secure/tokens/json-web-tokens/create-custom-claims)**
>
> Learn how to create collision-resistant names for custom claims.

Once the namespace value is fixed, you can get the users’ roles and permissions from the ID token.

Please let me know if there’s anything else I can do to help.

Thank you.

---

<div class="post-metadata">

**Author:** ![ovezovv](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ovezovv/32/35366_2.png) [@ovezovv](https://community.auth0.com/u/ovezovv)\
**Post date:** [March 1, 2022, 7:44am UTC](https://community.auth0.com/t/auth0-django-setting-roles-and-permissions/78702/4 "2022-03-01T07:44:22Z")

</div>

> [@ovezovv](#):
>
> ```auto
> 'roles': payload['http://sample-web.com/api/v2/roles']
> 
> ```

With changing as well as you mentioned, from this API identifier I can get the all permissions and roles, What if I try to get the currently signed-in user’s permissions and roles, how should I implement the rule or is there any specific configuration for that, thanks in advance.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [March 2, 2022, 1:25am UTC](https://community.auth0.com/t/auth0-django-setting-roles-and-permissions/78702/5 "2022-03-02T01:25:49Z")

</div>

Hi @ovezovv,

Thank you for your response.

> [@ovezovv](#):
>
> What if I try to get the currently signed-in user’s permissions and roles, how should I implement the rule or is there any specific configuration for that, thanks in advance.

The Rule snippet you shared with me will get the permissions and roles of the user Post-Login and append them to the ID Token. There should not be any additional configuration needed.

You could alternatively use the Management API v2 [Get a user Role endpoint](https://auth0.com/docs/api/management/v2#!/Users/get_user_roles) and [Get a user Permissions endpoint](https://auth0.com/docs/api/management/v2#!/Users/get_permissions) to accomplish the same results.

Hoped this helps!

Please do not hesitate to reach out if you have any further questions.

Thank you.

---

<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:** [March 17, 2022, 1:25am UTC](https://community.auth0.com/t/auth0-django-setting-roles-and-permissions/78702/6 "2022-03-17T01:25:57Z")

</div>

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