Adding custom payload data during M2M authentication(Client credentials flow)

We are trying to pass some data in the form body when calling the “/oauth/token” endpoint during the m2m authentication using client client credential flow.


custom data added :

audience=xxx  (Custom data in payload)

Now, when making the call we have a hook that is currently able to find the custom payload data and performs some operation. Below is the Auth0 hook where we access the key and add it to the token.

Auth0 hook

module.exports = function(client, scope, audience, context, cb) {
  var access_token = {};
  access_token.scope = scope;  
// Operation after retrieving custom payload
 const key = "";
  if (context.body[key]) {
    access_token[key] = context.body[key];
    access_token[key] = {}
  cb(null, access_token);

This approach works but when I try to use a normal string as custom payload instead of using a URL. I cannot access the custom data in Auth0 hooks anymore.
custom data added : a_general_key=User_3123

a_general_key=User_3123  (Custom data in payload)

Now the documentations are not very clear regarding this but it would be nice we could get a clear why this works in some cases and not in others.

Here are the doubts/questions we have:

  • Why custom data can be added and seen inside auth0 hook when the key is in URL form instead of a string? vs a_general_key=User_3123
  • Is there a way we can send some custom data while doing m2m auth using client credentials flow and access it through Auth0 hook?
    like a additional parameter, header or key in form body.

Thank you

Hi @kamal.pradhan,

Welcome to the Community!

You should be able to send data to your hook, whether it is a valid URL or string. Either should work. I just tested it and I can see a string in the Hook’s logs using this method:

Here is the request I am making:

curl --request POST \
       --url \
       --header 'content-type: application/json' \
       --data '{"client_id":"xxxx","client_secret":"xxxx","audience":"xxxx","grant_type":"client_credentials","foo":"bar"}'

Here is the Hook:

module.exports = function(client, scope, audience, context, cb) {
  var access_token = {};
  access_token.scope = scope;
  cb(null, access_token);


9:03:10 AM: new webtask request
9:03:10 AM: bar

I think the problem may be that you are trying to add an improperly namespaced claim to the token. The custom claim must conform to the namespacing guidelines, which require a valid URL as it is collision-resistant.

1 Like

@dan.woda Many thanks for the response, It works now. Is it a valid case to send such custom payloads in when making a request to /oauth/token ? I didn’t found it anywhere in Auth0 documentation. So I just want to confirm if this a standard practice and documented somewhere. Thanks.

I don’t see an issue with it, it sounds like you are simply trying to attach a param/payload to a token, which isn’t an issue inherently.

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