如何通过Google策略处理来自dpd-passport的回拨/重定向?

Nic*_*ich 5 javascript express angularjs deployd passport.js

我正在使用Deployd构建一个项目来帮助我的API,并使用dpd-passport进行身份验证.

我似乎已经对所有内容进行了身份验证,后面分发了会话密钥并通过Google进行了用户身份验证,但是我遇到了问题redirectURL,并且翻译了我返回的回调页面.

我已经挖掘了dpd-passport/index.js文件,我相信这是相关的信息:

var sendResponse = function(ctx, err, config) {
var sessionData = ctx.session.data;
var returnUrl = (ctx.req.cookies && ctx.req.cookies.get('_passportReturnUrl')) || null;

if(returnUrl) {
    var redirectURL = url.parse(returnUrl, true);

    // only append if not disabled
    if(!config.disableReturnParams) {
        // delete search so that query is used
        delete redirectURL.search;

        // make sure query is inited
        redirectURL.query = redirectURL.query || {};
        if(err) {
            redirectURL.query.success = false;
            redirectURL.query.error = err;
        } else {
            // append user + session id to the redirect url
            redirectURL.query.success = true;

            if(!config.disableSessionId) {
                redirectURL.query.sid = sessionData.id;
                redirectURL.query.uid = sessionData.uid;
            }
        }
    }

    var redirectURLString = '';
    try {
        redirectURLString = url.format(redirectURL);
    } catch(ex) {
        console.warn('An error happened while formatting the redirectURL', ex);
    }

    // redirect the user
    ctx.res.setHeader("Location", redirectURLString);
    ctx.res.statusCode = 302;

    ctx.done(null, 'This page has moved to ' + redirectURLString);
    } else {
        if(err) {
            ctx.res.statusCode = 401;
            console.error(err);
            return ctx.done('bad credentials');
        } else {
            ctx.done(err, { path: sessionData.path, id: sessionData.id, uid: sessionData.uid });
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

成功通过身份验证后,我获得了returnUrl以下信息:

http://localhost:3000/auth/google/callback?code=4/l4o-H2F4QKJ5tdKbVbGfWygTGRvhHgr9zrHWImFFKdM#

身体:

{"path":"/users","id":"d03c0faccfe41134c193266afef979c5af33adf935aeff45844b0f9473dee4ab1fbd1114240e13ea9a542785da3845cfec984e3a5b8cb188d6c595b6fc39a726","uid":"747f97a9bcfa9811"}

在我看来,我的结果似乎是else在最顶层的代码块中的最后声明.

如果这是真的,那么我returnUrl就是NULL.

追溯returnUrldpd-passport文件中的代码,看起来它应该从以下代码段中的cookie中获取:

if(ctx.query.redirectURL && this.config.allowedRedirectURLs) {
    try {
        this.regEx = this.regEx || new RegExp(this.config.allowedRedirectURLs, 'i');

        if(ctx.query.redirectURL.match(this.regEx)) {
            // save this info into the users session, so that we can access it later (even if the user was redirected to facebook)
            if (ctx.res.cookies) ctx.res.cookies.set('_passportReturnUrl', ctx.query.redirectURL);
        } else {
            debug(ctx.query.redirectURL, 'did not match', this.config.allowedRedirectURLs);
        }
    } catch(ex) {
        debug('Error parsing RedirectURL Regex!', ex);
    }
}
Run Code Online (Sandbox Code Playgroud)

要添加到这个,我allowedRedirectUrls在配置中有:

^http://localhost:3000/.*$
Run Code Online (Sandbox Code Playgroud)

我很茫然,我希望有一些明显的东西让我失踪.

我已经看到了类似于以下的护照路由和身份验证策略,但是在将这个实现到dpd-passport中是不成功的:

app.get('/auth/google/callback', 
  passport.authenticate('google', { failureRedirect: '/login' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
  });
Run Code Online (Sandbox Code Playgroud)

要添加到这一切,我使用ui-router/AngularJS.

小智 2

您必须通过启动 oauth 过程的链接向 dpd-passport 提供重定向 URL:

http://localhost:2403/auth/google?redirectURL=http://localhost
Run Code Online (Sandbox Code Playgroud)