# Implementing personal access token

**URL:** <https://community.auth0.com/t/implementing-personal-access-token/89651>\
**Category:** Get Help\
**Created:** [August 30, 2022, 4:34am UTC](https://community.auth0.com/t/implementing-personal-access-token/89651 "2022-08-30T04:34:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![amit9](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/amit9/32/34059_2.png) [@amit9](https://community.auth0.com/u/amit9)\
**Post date:** [August 30, 2022, 4:34am UTC](https://community.auth0.com/t/implementing-personal-access-token/89651/1 "2022-08-30T04:34:07Z")

</div>

In the absence of per user personal access token [Personal Access Tokens or API Keys](https://community.auth0.com/t/personal-access-tokens-or-api-keys/77922), was wondering how are auth0 customers implementing this feature? Are they managing the tokens inside their app?

---

<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, 12:17am UTC](https://community.auth0.com/t/implementing-personal-access-token/89651/4 "2022-09-01T00:17:35Z")

</div>

Hey @amit9 - The only possible approach I’m aware of currently would be essentially using client credentials via an M2M app per user. Outside of that it’s not something that’s supported for the time being. I saw that you added to the feedback request, thanks for that!

---

<div class="post-metadata">

**Author:** ![vlad.pesternikov](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vlad.pesternikov/32/36795_2.png) [@vlad.pesternikov](https://community.auth0.com/u/vlad.pesternikov)\
**Post date:** [September 14, 2022, 9:28pm UTC](https://community.auth0.com/t/implementing-personal-access-token/89651/5 "2022-09-14T21:28:47Z")

</div>

Regarding M2M approach per user, is it possible to attach auth0 app metadata to access token? By attaching app metadata I mean attaching to token within rule not user metadata, but application metadata in case we request access token with client\_id and client\_secret.

---

<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 14, 2022, 9:38pm UTC](https://community.auth0.com/t/implementing-personal-access-token/89651/6 "2022-09-14T21:38:40Z")

</div>

Hey @vlad.pesternikov welcome to the community!

> [@vlad.pesternikov](#):
>
> By attaching app metadata I mean attaching to token within rule not user metadata, but application metadata in case we request access token with client\_id and client\_secret.

That should be possible, see:

> [@Authenticate third-party apps](https://community.auth0.com/t/authenticate-third-party-apps/90508/3):
>
> Hello @kvirk welcome to the community! While not “officially” supported (see this [feedback request](https://community.auth0.com/t/personal-access-tokens-or-api-keys/77922)) - You’re on the right track in terms of what you can achieve with Auth0. I assume you mean the associated third party app in this case? If so, you might want to look into using an M2M flow action in order to [add a custom claim](https://auth0.com/docs/customize/actions/flows-and-triggers/machine-to-machine-flow#add-custom-claims-to-the-access-token). Taking advantage of [app\_metadata](https://auth0.com/docs/get-started/applications/configure-application-metadata#add-application-metadata-key-value-pairs) could work in this use case. Hope this helps!

---

<div class="post-metadata">

**Author:** ![amit9](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/amit9/32/34059_2.png) [@amit9](https://community.auth0.com/u/amit9)\
**Post date:** [September 14, 2022, 10:26pm UTC](https://community.auth0.com/t/implementing-personal-access-token/89651/7 "2022-09-14T22:26:37Z")

</div>

Thanks @ty.frith for the prompt reply. I tried the following things and none of them seem to work:

- Rules - i had a rule which will add user metadata into the JWT token for the PKCE flow and it works fine, i just extended it to take care of M2M

```auto
function (user, context, callback) {
  const namespace = "https://unskript.io/"; // my_namespace is a placeholder...
  const user_metadata = user.app_metadata || {};
  if (user_metadata !== {}) {
    context.accessToken[namespace + "app_metadata"] = user_metadata;
  } else {
		context.accessToken[namespace + "app_metadata"] = context.clientMetadata;
  }
  return callback(null, user, context);
}

```

- Actions -

```auto
exports.onExecuteCredentialsExchange = async (event, api) => {
  api.accessToken.setCustomClaim("https://foo.bar", event.client.metadata)
};

```

When i do the curl POST to get the access token

```auto
POST https://<YOUR_AUTH0_DOMAIN>/oauth/token
Content-Type: application/json
{
  "audience": "<API_IDENTIFIER>",
  "grant_type": "client_credentials",
  "client_id": "<YOUR_CLIENT_ID>",
  "client_secret": "<YOUR_CLIENT_SECRET>"
}

```

and i parse the JWT token, i dont see the custom claim or app\_metadata. What am i doing wrong here?

---

<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 14, 2022, 10:37pm UTC](https://community.auth0.com/t/implementing-personal-access-token/89651/8 "2022-09-14T22:37:31Z")

</div>

No problem, happy to help!

> [@amit9](#):
>
> and i parse the JWT token, i dont see the custom claim or app\_metadata. What am i doing wrong here?

Your action looks good - Are you positive you have it set on the Machine to Machine flow (Actions → Flows → Machine to Machine). I just tested with the same action and it’s pulling the claim and app\_metadata through to the token.

---

<div class="post-metadata">

**Author:** ![amit9](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/amit9/32/34059_2.png) [@amit9](https://community.auth0.com/u/amit9)\
**Post date:** [September 14, 2022, 10:49pm UTC](https://community.auth0.com/t/implementing-personal-access-token/89651/9 "2022-09-14T22:49:10Z")

</div>

Oh you know what i just created an action and didnt put it in the flow. Its working fine, however the rules way didnt work, any idea why?

---

<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 14, 2022, 10:55pm UTC](https://community.auth0.com/t/implementing-personal-access-token/89651/10 "2022-09-14T22:55:08Z")

</div>

Great! Good to know the action is working as expected 🙂 You’d need to use a hook instead as rules won’t apply to an M2M flow.

> [@Getting client metadata in non-interactive clients access tokens](https://community.auth0.com/t/getting-client-metadata-in-non-interactive-clients-access-tokens/7201/2):
>
> You should be able to accomplish your requirements by implementing a [client credentials hook](https://auth0.com/docs/api-auth/tutorials/client-credentials/customize-with-hooks) that will let you add additional custom claims into the issued access tokens. The hook logic would have access to the client metadata data and as such you could add the claims you are interested to received in the API. Have in mind that custom claims added need to use a namespace so there’s no risk that they clash with standard ones. A sample implementation for the hook could be the following: module.e…

---

<div class="post-metadata">

**Author:** ![amit9](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/amit9/32/34059_2.png) [@amit9](https://community.auth0.com/u/amit9)\
**Post date:** [September 15, 2022, 12:23am UTC](https://community.auth0.com/t/implementing-personal-access-token/89651/11 "2022-09-15T00:23:52Z")

</div>

Oh good to know. One last question @ty.frith , M2M access tokens derive the expiration time from the API attached to it and AFAIK, API cant have indefinite expiration. Any way, we can achieve indefinite expiration for such tokens?

---

<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 20, 2022, 10:45pm UTC](https://community.auth0.com/t/implementing-personal-access-token/89651/12 "2022-09-20T22:45:19Z")

</div>

Hey there @amit9 just following up on this - While there is no way to mint access tokens that don’t expire, in an M2M flow you should just be able to request a new token when needed. The following posts outline this:

> [@Request new M2M token after expiry date](https://community.auth0.com/t/request-new-m2m-token-after-expiry-date/49342/2):
>
> Hello, @katab, Before moving on, it’s good to understand the concept of a [Machine to Machine Authentication](https://auth0.com/blog/using-m2m-authorization/). This grant is intended for non-interactive clients, where a machine is requesting a token to be used on behalf of itself (never on behalf of a user). If you are using this token on behalf of a user, you have chosen the incorrect grant. As such, the machine should be able to understand when the token has expired, and just request another one, as the machine itself, considering it’s a sec…

> [@Correct way to handle M2M token renewal?](https://community.auth0.com/t/correct-way-to-handle-m2m-token-renewal/41196/5):
>
> This looks pretty standard. A refresh token and a client secret are very similar when we boil them down in a client credentials grant. This is a secure server, correct? If an attacker has access to your server you probably have a bigger problem than just the tokens, your client secret is more powerful.

---

<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, 10:46pm UTC](https://community.auth0.com/t/implementing-personal-access-token/89651/14 "2022-10-05T22:46:20Z")

</div>

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