Currently, I have an Oauth2 flow for an API. The code works but I dont believe it is working the way it should.
const OAuth2Strategy = require('passport-oauth2');
passport.use(new OAuth2Strategy({
authorizationURL: authorizationURL,
tokenURL: tokenURL,
clientID: clientId,
clientSecret: clientSecret,
callbackURL: callbackURL,
}, function(accessToken, refreshToken, results, profile, callback) {
return callback(null, results);
}))
app.get('/auth/api', passport.authenticate('oauth2', {failureRedirect: '/fail', successRedirect: '/', scope: ['profile']}));
I manage to get an accestoken, refreshtokenand results in the callback function, however, minor problems are occuring. The first is that profile is returning an empty object {}. I dont believe passport can do anything about this due to the stratergy not being native to the API. The other problem is that results is returning the accesstoken and its expiration but it doesnt return the refresh token in the results. The thing I dont understand is that the refreshtoken callback parameter is returining the refresh token but the result doesnt contain it.
What I expect to recieve accoring to the api documentation is:
{
access_token: "...",
expires_in: 300,
refresh_expires_in: 1800,
refresh_token: "...",
token_type: "bearer",
'not-before-policy': 0,
scope: "profile"
}
but what im getting is:
{
access_token: '...',
expires_in: 900,
refresh_expires_in: 0,
token_type: 'Bearer',
'not-before-policy': 0,
scope: 'offline_access email profile'
}
I could just merge the refreshtoken and the results together but if this is the current outcome, surely im doing something wrong. Is this to do with the stratergy im using?
According to the docs, the callback receives only four parameters:
function(accessToken, refreshToken, profile, cb) {
...
}
There is no results parameter. Still, you can pass to your callback (cb in the code above) anything you like from the things you received in the passport's callback. If you have the refresh token in the refreshToken parameter, then just pass it to your cb if you need it there.