Search code examples
jwtauth0

How to pass a parameter from a URL to an auth0 hook or rule for a machine-to-machine JWT?


I'd like to add a custom claim to a JSON web token generated by auth0 for machine-to-machine authentication. Like

color:blue

but I want to make blue a parameter I can pass with my request to auth0 for the token.

I ask for the token like this:

POST https://mydomain.eu.auth0.com/oauth/token

with a request body

{
  "client_id":"myID",
  "client_secret":"mySecret",
  "audience" : "https://mydomain.eu.auth0.com/api/v2/",
  "grant_type" :"client_credentials"
}

I see from How can I add claims to a JWT assigned with auth0 for a machine-to-machine application type? how to use a hook or rule to add a fixed claim, but I want to add a variable something like

?color=blue

to my request URL or request body and have that accessible from my hook code to be added as a claim.

Is that possible, please? How?


Solution

  • When you process an authorization request with a custom rule you get access to at least a part of the request properties by the request object of the context function argument. I tried it out but unfortunately the fields of the request object seem to be limited to only a few fields of the original request.

    If your user information resides in Auth0 you could check out writing preferences into the user's metadata by using the Auth0 metadata API. This works well. But you will be able to only set metadata after the user has logged in, not before. Also you'll have to deal with two different Auth0 API endpoints now.