lelylan / simple-oauth2

A simple Node.js client library for Oauth2
http://lelylan.github.com/simple-oauth2/
1.62k stars 292 forks source link

refresh() API does not work #420

Closed pathikrit closed 1 year ago

pathikrit commented 1 year ago

The following piece of code works fine:

const {ClientCredentials} = require('simple-oauth2')

async function test(client_id, client_secret) {
  const credentials = new ClientCredentials({
    client: {id: client_id, secret: client_secret},
    auth: {tokenHost: 'https://portal.getjoan.com', tokenPath: '/api/token/'}
  })
  const accessToken = await credentials.getToken()

  if (true || accessToken.expired()) {   // Emulate token expiry
     accessToken = await credentials.getToken()
  }
}

However if I change the last lines to following:

if (true || accessToken.expired()) {   // Emulate token expiry
     accessToken = await accessToken.refresh()
 }

I get following error:

 throw new Boom.Boom(`Response Error: ${res.statusCode} ${res.statusMessage}`, { statusCode: res.statusCode, data });
              ^

Error: Response Error: 400 Bad Request
    at internals.Client._shortcut (/Users/pbhowmick/workspace/node-joan/node_modules/@hapi/wreck/lib/index.js:569:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Client.request (/Users/pbhowmick/workspace/node-joan/node_modules/simple-oauth2/lib/client/client.js:39:22)
    at async AccessToken.refresh (/Users/pbhowmick/workspace/node-joan/node_modules/simple-oauth2/lib/access-token.js:49:22) {

This might be because the library is putting auth headers for the refresh request (see #32) but #54 was never merged.

jonathansamines commented 1 year ago

Hey @pathikrit really hard to tell without a complete reproduction or the details our issues template require, but you can try the following options:

See: https://github.com/lelylan/simple-oauth2/blob/master/API.md#options

I'll close this, but feel free to re-open with a complete reproduction.