Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommended way to re-inject renewed token for Management API in a long-running script? #164

Closed
alexstrat opened this issue Feb 18, 2017 · 6 comments

Comments

@alexstrat
Copy link

alexstrat commented Feb 18, 2017

I got a script running indefinitely (a web server actually) that needs to access the Management API. I manually generate a token, like described here, use it, but after 24h it's expired. Normal.

I understood I got to automate the process of periodically generate a new token and inject it in my running script (right?), but can't find any recommended way to do it, nor come up with an elegant solution.

The solution I'm using now looks like this:

let cachedToken = null

const getToken () => {
  if (cachedToken && !isExpired(cachedToken)) {
    return Promise.resolve(cachedToken)
  }
  return generateToken() // will ask a token
    .then(token => {
      cachedToken = token
      return token
    }) 
}

const getClient => {
  return getToken()
    .then(token => new ManagementClient({
      domain: 'MY_DOMAIN',
      token
    }))
}

// usage
getClient()
  .then(c => c.getUsers())
  .then(users => doSomething(users))

It's clumsy and un-elegant, esp. because I got to asynchronously get the client each time I need it.

Has anyone found a better method?

It would be much better if the ManagementClient could hide that logic and asynchronousity:

const auth0 = ManagementClient({ domain, clientId, clientSecret })
auth0.getUsers() // will get a token or use the one cached before actually making the API request

Alternatively, the ManagementClient could add the possibility to dynamically inject the token and let the developer responsible for generating and caching the token

const auth0 = ManagementClient({
  domain,
  token: () => getTokenFromCacheOrGenerate()
})
// getTokenFormCacheOrGenerate is implemented by developer
// it should return a promise that resolves to a valid token to be used in API calls
@dcworldwide
Copy link

@alexstrat I have the same issue.

Do you mind sharing your implementation for isExpired?

@alexstrat
Copy link
Author

@dcworldwide here is what i'm using today. You'll find an implementation inisCachedTokenValid.

@dcworldwide
Copy link

Thanks @alexstrat

@mmazzarolo
Copy link

@alexstrat thank you for your snippet, I just embraced your solution and created a simple Koa middleware with it.

I can clean it up a bit and post it on GitHub if anyone is interested.

@mmazzarolo
Copy link

If anyone else is still interested, this middleware is now running in a production ready app

@luisrudge
Copy link
Contributor

This is part of the SDK now: https://github.com/auth0/node-auth0#management-api-client

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants