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

Add hook to handle OAuth Access Token Response #174

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions lib/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,12 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
params.code_verifier = ok;
}

self._oauth2.getOAuthAccessToken(code, params,
function(err, accessToken, refreshToken, params) {
if (err) { return self.error(self._createOAuthError('Failed to obtain access token', err)); }
self._oauth2.getOAuthAccessToken(code, params, function(err, _accessToken, _refreshToken, _params) {
if (err) { return self.error(self._createOAuthError('Failed to obtain access token', err)); }

self.handleOAuthAccessTokenResponse(_accessToken, _refreshToken, _params, function (err, accessToken, refreshToken, params) {
if (err) { return self.error(self._createOAuthError('Failed to handle oauth access token response', err)); }

if (!accessToken) { return self.error(new Error('Failed to obtain access token')); }

self._loadUserProfile(accessToken, function(err, profile) {
Expand Down Expand Up @@ -206,11 +209,14 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
}
}
} catch (ex) {
console.log(ex);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rm

return self.error(ex);
}
});
}
);

});

});
}

var state = (req.query && req.query.state) || (req.body && req.body.state);
Expand Down Expand Up @@ -247,7 +253,7 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
default:
return this.error(new Error('Unsupported code verifier transformation method: ' + this._pkceMethod));
}

params.code_challenge = challenge;
params.code_challenge_method = this._pkceMethod;
}
Expand All @@ -265,7 +271,7 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
// the state will be automatically managed and persisted by the
// state store.
params.state = state;

var parsed = url.parse(this._oauth2._authorizeUrl, true);
utils.merge(parsed.query, params);
parsed.query['client_id'] = this._oauth2._clientId;
Expand Down Expand Up @@ -303,6 +309,26 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
}
};

/**
* Handle OAuthAccessTokenResponse.
*
* OAuth 2.0-based authentication strategies can overrride this function in
* order to format the OAuth Access Token Response
*
* Some auth providers overload the OAuthAccessToken response
* which may require reformatting before continuing with the standard flow
* See https://github.com/nmaves/passport-slack-oauth2/issues/9#issuecomment-1544231819
*
* @param {String:null} accessToken
* @param {String:null} refreshToken
* @param {Object} params
* @param {Function} done
*/

OAuth2Strategy.prototype.handleOAuthAccessTokenResponse = function(accessToken, refreshToken, params, done) {
done(null, accessToken, refreshToken, params);
};

/**
* Retrieve user profile from service provider.
*
Expand Down
Loading