# Authenticate third-party apps

**URL:** <https://community.auth0.com/t/authenticate-third-party-apps/90508>\
**Category:** Get Help\
**Tags:** app\_metadata, jwt, auth0, machine-to-machine\
**Created:** [September 13, 2022, 1:18pm UTC](https://community.auth0.com/t/authenticate-third-party-apps/90508 "2022-09-13T13:18:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kvirk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kvirk/32/39890_2.png) [@kvirk](https://community.auth0.com/u/kvirk)\
**Post date:** [September 13, 2022, 1:18pm UTC](https://community.auth0.com/t/authenticate-third-party-apps/90508/1 "2022-09-13T13:18:48Z")

</div>

Hi,

I am pretty much new to Auth0. I am building an API in nest.js that is secured by Auth0. So if a user has a valid access token, it can access the API.

I need to expose the API to third-party applications, which will have a sort of API key to access my API.  
I tried machine-2-machine authentication. So far, I am not able to achieve what I need.

Here’s what I’ve done:

1. Created a “Machine to Machine” application.
2. Granted it the permission to Auth0 API.
3. I can retrieve an access token

Now, I don’t know how can I get the application name in the JWT token.

Overall, I can manage permissions to each application dedicated to third-party applications but in my API, I need to check the clien’ts name for further validation.

What I’m trying to achieve:

1. Secure my API with Auth0
2. Expose my API to third-party clients with API keys
3. Get the client’s detail on my API server to check to whom the token belongs to

Is it doable with Auth0? Or If there is something wrong with my authentication approach.

Thanks,  
Kunal

---

<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, 1:02am UTC](https://community.auth0.com/t/authenticate-third-party-apps/90508/3 "2022-09-14T01:02:27Z")

</div>

Hello @kvirk welcome to the community!

> [@kvirk](#):
>
> Is it doable with Auth0? Or If there is something wrong with my authentication approach.

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.

> [@kvirk](#):
>
> Now, I don’t know how can I get the application name in the JWT token.

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:** ![kvirk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kvirk/32/39890_2.png) [@kvirk](https://community.auth0.com/u/kvirk)\
**Post date:** [September 15, 2022, 9:36am UTC](https://community.auth0.com/t/authenticate-third-party-apps/90508/4 "2022-09-15T09:36:07Z")

</div>

> [@ty.frith](#):
>
> 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?_ga=2.106565376.2045722798.1662989031-46240905.1651521790&_gl=1*1sg7hjs*rollup_ga*NDYyNDA5MDUuMTY1MTUyMTc5MA..*rollup_ga_F1G3E656YZ*MTY2MzE0OTYxMC4zNi4xLjE2NjMxNTExNTAuNDEuMC4w#add-custom-claims-to-the-access-token). Taking advantage of [app\_metadata](https://auth0.com/docs/get-started/applications/configure-application-metadata?_gl=1*3b1p5v*rollup_ga*NDYyNDA5MDUuMTY1MTUyMTc5MA..*rollup_ga_F1G3E656YZ*MTY2MzE1NDM4MS4zNy4xLjE2NjMxNTQzODQuNTcuMC4w&_ga=2.151047098.2045722798.1662989031-46240905.1651521790#add-application-metadata-key-value-pairs) could work in this use case.

I think that’s what I want, but can you please guide me to the documentation where the steps to get the app\_metadata in access\_token JWT is written?

---

<div class="post-metadata">

**Author:** ![kvirk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kvirk/32/39890_2.png) [@kvirk](https://community.auth0.com/u/kvirk)\
**Post date:** [September 15, 2022, 2:05pm UTC](https://community.auth0.com/t/authenticate-third-party-apps/90508/5 "2022-09-15T14:05:46Z")

</div>

Hi,

I have gone through the document at ([Machine to Machine Flow](https://auth0.com/docs/customize/actions/flows-and-triggers/machine-to-machine-flow))

I have created an action for my machine-to-machine application, the code is as follows

```auto
exports.onExecuteCredentialsExchange = async (event, api) => {
  console.log("=== onExecuteCredentialsExchange ===");
  console.log("Event", event.client, event.client_metadata);
  console.log("API", api);
  console.log("=== end: onExecuteCredentialsExchange ===")
};

```

But, `event.client` is undefined whereas I have defined application metadata in my application whose `client_id` and `client_secret` I’m using to get access token. Here is the screenshot of how I have defined it under “Application \> My Demo Application \> Advanced Settings : Application Metadata”

 ![image](https://global.discourse-cdn.com/auth0/original/3X/1/8/18a3d92acce6f1c9bd069e7c976b7f23436f64ab.png)

Can you please guide me on what I’m doing wrong here?

Thanks,  
Kunal

---

<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 24, 2022, 12:33am UTC](https://community.auth0.com/t/authenticate-third-party-apps/90508/6 "2022-09-24T00:33:50Z")

</div>

Hey @kvirk I apologize for the delayed response here!

Were you ever able to get this sorted?

You’ll need to refactor your Action code to [add a custom claim](https://auth0.com/docs/get-started/apis/scopes/sample-use-cases-scopes-and-claims#add-custom-claims-to-a-token) to the access token. A super simple example looks like:

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

```

Hope this helps!

---

<div class="post-metadata">

**Author:** ![kvirk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kvirk/32/39890_2.png) [@kvirk](https://community.auth0.com/u/kvirk)\
**Post date:** [September 29, 2022, 9:08am UTC](https://community.auth0.com/t/authenticate-third-party-apps/90508/7 "2022-09-29T09:08:20Z")

</div>

Hi,

I don’t know but actions didn’t work for me, `event.client` was undefined even though the client has metadata please refer to my last comment. Anyways, I used hooks and got the client metadata. Here is the code snippet:

```auto
module.exports = function(client, scope, audience, context, cb) {
  var access_token = {};
  access_token.scope = scope;
  access_token["client_info"] = client;
  cb(null, access_token);
};

```

Thanks

---

<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 1, 2022, 12:51am UTC](https://community.auth0.com/t/authenticate-third-party-apps/90508/8 "2022-10-01T00:51:00Z")

</div>

Thanks for confirming @kvirk and for sharing the code - Good to know you got it working! 💪

---

<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 15, 2022, 12:51am UTC](https://community.auth0.com/t/authenticate-third-party-apps/90508/9 "2022-10-15T00:51:44Z")

</div>

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