Skip to content

Commit de43c62

Browse files
author
Shunsuke Tsutsui
committed
Fixed to pass two unit tests.
1 parent 56cf4d4 commit de43c62

File tree

3 files changed

+38
-36
lines changed

3 files changed

+38
-36
lines changed

lib/state/pkcesession.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@ PKCESessionStore.prototype.store = function(req, verifier, state, meta, callback
3939
if (!req.session) { return callback(new Error('OAuth 2.0 authentication requires session support when using state. Did you forget to use express-session middleware?')); }
4040

4141
var key = this._key;
42-
var state = {
43-
handle: uid(24),
44-
code_verifier: verifier
42+
var handle = state;
43+
if (!handle) {
44+
// generate if `state` is not provided when authorization call
45+
handle = uid(24);
46+
}
47+
var stateObj = {
48+
handle: handle,
49+
code_verifier: verifier
4550
};
4651
if (!req.session[key]) { req.session[key] = {}; }
47-
req.session[key].state = state;
48-
callback(null, state.handle);
52+
req.session[key].state = stateObj;
53+
callback(null, stateObj.handle);
4954
};
5055

5156
/**

lib/state/session.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ function SessionStore(options) {
3535
* @param {Function} callback
3636
* @api protected
3737
*/
38-
SessionStore.prototype.store = function(req, callback) {
38+
SessionStore.prototype.store = function(req, state, meta, callback) {
3939
if (!req.session) { return callback(new Error('OAuth 2.0 authentication requires session support when using state. Did you forget to use express-session middleware?')); }
4040

4141
var key = this._key;
42-
var state = uid(24);
42+
if (!state) {
43+
state = uid(24);
44+
}
4345
if (!req.session[key]) { req.session[key] = {}; }
4446
req.session[key].state = state;
4547
callback(null, state);

lib/strategy.js

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
172172

173173
self._oauth2.getOAuthAccessToken(code, params,
174174
function(err, accessToken, refreshToken, params) {
175+
175176
if (err) { return self.error(self._createOAuthError('Failed to obtain access token', err)); }
176177

177178
self._loadUserProfile(accessToken, function(err, profile) {
@@ -249,41 +250,35 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
249250
params.code_challenge_method = this._pkceMethod;
250251
}
251252

252-
var state = options.state;
253-
if (state) {
254-
params.state = state;
255-
256-
var parsed = url.parse(this._oauth2._authorizeUrl, true);
253+
function stored(err, state) {
254+
console.log(`stored err=${err}, state=${state}`);
255+
if (err) { return self.error(err); }
256+
257+
if (state) { params.state = state; }
258+
var parsed = url.parse(self._oauth2._authorizeUrl, true);
257259
utils.merge(parsed.query, params);
258-
parsed.query['client_id'] = this._oauth2._clientId;
260+
parsed.query['client_id'] = self._oauth2._clientId;
259261
delete parsed.search;
260262
var location = url.format(parsed);
261-
this.redirect(location);
262-
} else {
263-
function stored(err, state) {
264-
if (err) { return self.error(err); }
265-
266-
if (state) { params.state = state; }
267-
var parsed = url.parse(self._oauth2._authorizeUrl, true);
268-
utils.merge(parsed.query, params);
269-
parsed.query['client_id'] = self._oauth2._clientId;
270-
delete parsed.search;
271-
var location = url.format(parsed);
272-
self.redirect(location);
273-
}
263+
self.redirect(location);
264+
}
274265

275-
try {
276-
var arity = this._stateStore.store.length;
277-
if (arity == 5) {
278-
this._stateStore.store(req, verifier, undefined, meta, stored);
279-
} else if (arity == 3) {
280-
this._stateStore.store(req, meta, stored);
281-
} else { // arity == 2
282-
this._stateStore.store(req, stored);
266+
try {
267+
var arity = this._stateStore.store.length;
268+
if (arity == 5) {
269+
this._stateStore.store(req, verifier, options.state, meta, stored);
270+
} else if (arity == 4) {
271+
this._stateStore.store(req, options.state, meta, stored);
272+
} else if (arity == 3) {
273+
this._stateStore.store(req, meta, stored);
274+
} else { // arity == 2
275+
if (options.state) {
276+
params.state = options.state;
283277
}
284-
} catch (ex) {
285-
return this.error(ex);
278+
this._stateStore.store(req, stored);
286279
}
280+
} catch (ex) {
281+
return this.error(ex);
287282
}
288283
}
289284
};

0 commit comments

Comments
 (0)