5

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

我似乎对所有东西都进行了身份验证,会话密钥在后面分发,用户通过 Google 进行身份验证,但我redirectURL的 s 和翻译返回的回调页面有问题。

我已经深入研究了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 });
        }
    }
};

成功验证后,我得到一个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);
    }
}

为了补充这一点,我allowedRedirectUrls在配置中有我的:

^http://localhost:3000/.*$

我不知所措,希望有一些明显的东西我失踪了。

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

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

除此之外,我正在使用 ui-router/AngularJS。

4

1 回答 1

2

您必须通过启动 oauth 过程的链接将 redirectURL 提供给 dpd-passport:

http://localhost:2403/auth/google?redirectURL=http://localhost
于 2015-09-02T01:48:23.647 回答