Caching Resources in Auth0 Hooks

My apologies if this information is present somewhere, but I’m trying to figure out how to cache a machine to machine bearer token in an Auth0 hook in a fashion that’s similar to this method that works for rules: How do I get an access token to call Management API v2 from a rule?

I tried searching the docs and the community, and I turned up this doc article:

However, this appears to be a different type of hook than the one I’m working with because the signature is very different. Regardless, I tried printing the value of context.global and running a test of my client credentials exchange hook and it appears to be undefined:

module.exports = function(client, scope, audience, context, cb) {
console.log('global: ', context.global);
}

Any advice would be appreciated.

In hooks try using the global object directly, as shown in the Webtask documentation. E.g.

module.exports = function(client, scope, audience, context, cb) {
  console.log('global: ', global);
}
3 Likes

Thanks for the quick reply Nicolas.

I actually tried what you had initially, but was confused by the output and assumed global was undefined.

Example:

module.exports = function(client, scope, audience, context, cb) {
  var access_token = {};
  access_token.scope = scope;
  
  console.log('Before Global');
  console.log(global);
  console.log('After Global');

  cb(null, access_token);
};

Prints the following:

10:14:50 AM: 
new webtask request 1563286490891.737686
10:14:50 AM: 
Before Global
10:14:50 AM: 
finished webtask request 1563286490891.737686 with HTTP 200 in 13ms

This print statement works in rules where I noticed there was a circular reference. I had a sneaking suspicion that the console in the hook editor was discarding the log statement due to the circular reference, so I checked to see if stringify would throw an error on it.

module.exports = function(client, scope, audience, context, cb) {
  var access_token = {};
  access_token.scope = scope;
  
  console.log(JSON.stringify(global));

  cb(null, access_token);
};
10:18:53 AM: 
Code generated an uncaught exception: TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
at module.exports (/data/io/6cea6856-0cb1-4dbe-b36a-40b711e4a364/webtask.js:17:20)
at Authz.is_authorized.error (/data/sandbox/node_modules/auth0-ext-compilers/lib/compilers/client_credentials_exchange.js:27:16)
at Object.is_authorized (/data/sandbox/node_modules/auth0-ext-compilers/lib/authorization.js:13:81)
at clientCredentialsExchangeHandler (/data/sandbox/node_modules/auth0-ext-compilers/lib/compilers/client_credentials_exchange.js:9:18)
at parseBody (/data/sandbox/node_modules/auth0-ext-compilers/lib/adapter.js:90:20)
at finish (/data/sandbox/node_modules/auth0-ext-compilers/node_modules/wreck/lib/index.js:369:16)
at wrapped (/data/sandbox/node_modules/auth0-ext-compilers/node_modules/hoek/lib/index.js:879:20)
at module.exports.internals.Recorder.onReaderFinish (/data/sandbox/node_modules/auth0-ext-compilers/node_modules/wreck/lib/index.js:415:16)
at Object.onceWrapper (events.js:313:30)

That confirms that global exists and is set. Thanks for your assistance Nicolas!

1 Like

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