What is the best way to dynamically specify a redirect URL for OAuth strategies in passport.js?

I installed my facebook file in the passport documents:

var passport = require('passport') , FacebookStrategy = require('passport-facebook').Strategy; passport.use(new FacebookStrategy({ clientID: FACEBOOK_APP_ID, clientSecret: FACEBOOK_APP_SECRET, callbackURL: "http://www.example.com/facebook/callback" }, function(accessToken, refreshToken, profile, done) { ... }); } )); app.get('/login/facebook', passport.authenticate('facebook')) .get('/facebook/callback', passport.authenticate('facebook', {successRedirect: '/', failureRedirect: '/login'})); 

All of this works great. However, there are cases (for example, the expiration of tokens) when I want to automatically redirect the user to the page on which the user was included before initiating a login request. So I tried to drop the query string parameter through the login request (from client to server on facebook and vice versa). But I see no way to specify this in callbackURL.

Also, when I tried hard-coded some context parameter in config callbackURL (for example: http://www.example.com/facebook/callback?redir=lastUserPage "), I get OAuth parsing error. Interestingly, Facebook responds correctly with an access code, as well as with the redir parameter, but with an OAUTH error:

 FacebookTokenError: Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request at Strategy.parseErrorResponse (C:\Sources\node_modules\passport-facebook\lib\strategy.js:198:12) at Strategy.OAuth2Strategy._createOAuthError (C:\Sources\node_modules\passport-facebook\node_modules\passport-oauth2\lib\strategy.js:345:16) at C:\Sources\node_modules\passport-facebook\node_modules\passport-oauth2\lib\strategy.js:171:43 at C:\Sources\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:177:18 at passBackControl (C:\Sources\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:124:9) at IncomingMessage.<anonymous> (C:\Sources\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:143:7) at IncomingMessage.emit (events.js:117:20) at _stream_readable.js:943:16 at process._tickCallback (node.js:419:13) 

Please note that I used to work with WIF. I do not see any security issues when passing additional query string parameters through the OAuth process.

Any idea how I can get past this?

+5
source share
1 answer

I'm not sure how to do what you ask, but to achieve your desired end goal you could:

  • Save cookie before authentication
  • User authentication
  • on the resulting callback page, check for cookies and redirect if there are any.

Wouldn't that work just as easily?

0
source

Source: https://habr.com/ru/post/1210706/


All Articles