# How do I encode custom claims in the access token JWT returned from the oauth 2.0 flow?

**URL:** <https://community.auth0.com/t/how-do-i-encode-custom-claims-in-the-access-token-jwt-returned-from-the-oauth-2-0-flow/98455>\
**Category:** Get Help\
**Tags:** user-password-management, user-management\
**Created:** [January 18, 2023, 11:13pm UTC](https://community.auth0.com/t/how-do-i-encode-custom-claims-in-the-access-token-jwt-returned-from-the-oauth-2-0-flow/98455 "2023-01-18T23:13:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![david.zirinsky](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/david.zirinsky/32/42568_2.png) [@david.zirinsky](https://community.auth0.com/u/david.zirinsky)\
**Post date:** [January 18, 2023, 11:13pm UTC](https://community.auth0.com/t/how-do-i-encode-custom-claims-in-the-access-token-jwt-returned-from-the-oauth-2-0-flow/98455/1 "2023-01-18T23:13:31Z")

</div>

When logging in with a username password combo my custom claims are encoded, but that isn’t the case when I go through the Oauth2.0 flow. My custom claims are in the id token, but not in the access token.

I don’t think it’s safe to gate api access on an id token so this won’t work

---

<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:** [January 23, 2023, 3:45pm UTC](https://community.auth0.com/t/how-do-i-encode-custom-claims-in-the-access-token-jwt-returned-from-the-oauth-2-0-flow/98455/3 "2023-01-23T15:45:54Z")

</div>

Hi @david.zirinsky,

Can you explain what you mean by “Oauth2.0 flow”? Do you have an example of the call you are making?

---

<div class="post-metadata">

**Author:** ![david.zirinsky](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/david.zirinsky/32/42568_2.png) [@david.zirinsky](https://community.auth0.com/u/david.zirinsky)\
**Post date:** [January 23, 2023, 5:28pm UTC](https://community.auth0.com/t/how-do-i-encode-custom-claims-in-the-access-token-jwt-returned-from-the-oauth-2-0-flow/98455/4 "2023-01-23T17:28:59Z")

</div>

Two was to get the tokens missing claims:

Going through the oauth flow does not encode claims in our access token. Going through the tutorial at: [Auth0 Python SDK Quickstarts: Login](https://auth0.com/docs/quickstart/webapp/python/01-login) yields a token that can no custom claims. The same can be seen with the follow curl command:

curl --location --request POST ‘https://MY\_AUTH0\_DOMAIN/oauth/token’ \

–header ‘content-type: application/x-www-form-urlencoded’ \

–data-urlencode ‘grant\_type=refresh\_token’ \

–data-urlencode 'client\_id=OUR CLIENT ID \

–data-urlencode ‘client\_secret=OUR SECRET’ \

–data-urlencode ‘refresh\_token=VALID\_REFRESH\_TOKEN’

---

<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:** [January 24, 2023, 2:07pm UTC](https://community.auth0.com/t/how-do-i-encode-custom-claims-in-the-access-token-jwt-returned-from-the-oauth-2-0-flow/98455/5 "2023-01-24T14:07:00Z")

</div>

Can you please share an example of the Action you are using to add custom claims?

---

<div class="post-metadata">

**Author:** ![david.zirinsky](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/david.zirinsky/32/42568_2.png) [@david.zirinsky](https://community.auth0.com/u/david.zirinsky)\
**Post date:** [January 24, 2023, 4:50pm UTC](https://community.auth0.com/t/how-do-i-encode-custom-claims-in-the-access-token-jwt-returned-from-the-oauth-2-0-flow/98455/6 "2023-01-24T16:50:12Z")

</div>

```auto
exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'our_namespace';
  const { field_1, field_2, field_3, field_4 } = event.user.user_metadata;

  if (event.authorization) {
    // Set claims 
    api.accessToken.setCustomClaim(`${namespace}/field_1`, field_1);
    api.accessToken.setCustomClaim(`${namespace}/field_2`, field_2);
    api.accessToken.setCustomClaim(`${namespace}/field_3`, field_3);
    api.accessToken.setCustomClaim(`${namespace}/account_uuid`, account_uuid);
    api.idToken.setCustomClaim(`${namespace}/field_1`, field_1);
    api.idToken.setCustomClaim(`${namespace}/field_2`, field_2);
    api.idToken.setCustomClaim(`${namespace}/field_3`, field_3);
    api.idToken.setCustomClaim(`${namespace}/field_4`, field_4);
  }
};

```

---

<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:** [January 25, 2023, 6:56pm UTC](https://community.auth0.com/t/how-do-i-encode-custom-claims-in-the-access-token-jwt-returned-from-the-oauth-2-0-flow/98455/7 "2023-01-25T18:56:35Z")

</div>

@david.zirinsky,

Make sure your namespace follows the guidelines:

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

Also, can you confirm you are sending an audience param with your requests? You may not be requesting a JWT.

---

<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 28, 2023, 1:22pm UTC](https://community.auth0.com/t/how-do-i-encode-custom-claims-in-the-access-token-jwt-returned-from-the-oauth-2-0-flow/98455/8 "2023-03-28T13:22:16Z")

</div>

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