我正在构建一个项目,使用 Deployd 协助我的 API,并使用 dpd-passport 进行身份验证。
我似乎对所有内容进行了身份验证, session key 在后面分发,用户通过 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 });
}
}
};
认证成功后,我得到一个returnUrl
:
http://localhost:3000/auth/google/callback?code=4/l4o-H2F4QKJ5tdKbVbGfWygTGRvhHgr9zrHWImFFKdM#
主体为:
{"path":"/users","id":"d03c0faccfe41134c193266afef979c5af33adf935aeff45844b0f9473dee4ab1fbd1114240e13ea9a542785da3845cfec984e3a5b8cb188d6c595b6fc39a726","uid":"747f97a9bcfa9811"}
在我看来,我的结果命中了最顶层代码块中的最终 else
语句。
如果这是真的,那么我的 returnUrl
就是 NULL
。
追溯 dpd-passport 文件中的 returnUrl
代码,看起来它应该从以下代码片段中的 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/.*$
我不知所措,希望有一些明显的东西是我遗漏的。
我看过类似下面的passport路由和认证策略,但是没成功实现到dpd-passport中:
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/');
});
为了补充这一切,我正在使用 ui-router/AngularJS。
最佳答案
您必须通过启动 oauth 过程的链接向 dpd-passport 提供 redirectURL:
http://localhost:2403/auth/google?redirectURL=http://localhost
关于javascript - 如何通过 Google 策略处理来自 dpd-passport 的回调/重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30898327/