我正在尝试借助 passport http 包来保护我的 sails js rest api,但目前我无法弄清楚代码中的错误所在。 我使用这个 repo 和这个 tutorial 来了解它应该如何工作。我的问题是我的代码总是返回 401。 我真的不知道在哪里寻找错误。如果您需要有关我的代码的更多信息,请发表评论。 布鲁诺
编辑: 我找到了问题的根源(在@Viktor 的帮助下)。我只是不太了解 HTTP-Basic 身份验证的工作原理。现在的问题是如何发送我的身份验证凭证和数据?如果我只是用 auth(...) 测试路由,它们就可以工作...但是我该如何添加数据呢?或者我是否必须先对我进行身份验证并在第二个请求中发送数据?
passport.js
var passport = require('passport');
var BasicStrategy = require('passport-http').BasicStrategy;
var bcrypt = require('bcrypt');
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findOne({
id: id
}, function(err, user) {
done(err, user);
});
});
passport.use('user-authentication', new BasicStrategy(
function(mail, password, done) {
sails.log.error("hallo");
User.findOne({
mail: mail
}, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false, {
message: 'Incorrect email.'
});
}
// Make sure the password is correct
bcrypt.compare(password, user.password, function(err, isMatch) {
if (err) {
return done(err);
}
// Password did not match
if (!isMatch) {
return done(null, false, {
message: 'Invalid Password'
});
}
// Success
return done(null, user);
});
});
}
));
isAuthenticated.js
var passport = require("passport");
module.exports = function (req, res, ok) {
passport.authenticate("user-authentication", {
session: false
}, function (err, user, info) {
if (err || !user) {
res.set("WWW-Authenticate", "Basic realm=\"Restricted\"");
return res.send("You are not permitted to perform this action", 401);
}
req.session.user = user;
return ok(null, user);
})(req, res, ok);
};
policies.js
module.exports.policies = {
'*': true,
'UserController': {
update: 'isAuthenticated'
}
}
UserController.test.js
var request = require('supertest');
var async = require('async');
describe('UserController', function() {
describe('#new()', function() {
it('...', function (done) {
request(sails.hooks.http.app)
.post('/user/new')
.send({ own_number: '654122', password: 'test', mail: 'test@test.com', device_id: '1234', numbers: [1234567] })
.expect(200)
.end(done);
});
});
describe('#update()', function(){
it('...', function (done) {
async.series([
function(callback){
request(sails.hooks.http.app)
.post('/contact/update')
.send({ number: 1234, mail: "test@test.com", password: "test" })
.expect(200)
.end(callback);
},
function(callback){
request(sails.hooks.http.app)
.post('/user/update')
.send({ numbers: [1234], mail: "tet@test.com", password: "test" })
.expect(200)
.end(callback);
}
], done);
});
});
});
最佳答案
对我有用 – 一旦数据库中存在有效用户,我就能够进行身份验证以及访问和管理用户数据。对于一个空的用户数据库,您会一直得到 401
,当然,因为这些策略甚至不允许您创建第一个用户来进行身份验证。暂时禁用 config/policies.js
中的 UserController
策略让您有机会创建第一个用户。
假设数据库中至少有一个有效用户,让我们缩小问题范围。在 isAuthenticated.js
中记录 err
和 user
会得到什么输出?如果你得到 null
和 false
,在 passport.js
的不同步骤中会发生什么 - 你能通过电子邮件找到用户吗数据库中的地址和密码是否匹配?
您有自定义路由和 Controller 操作还是使用蓝图?
编辑:使用 HTTP 基本身份验证,您的更新测试看起来像这样:
describe('#update()', function(){
it('...', function (done) {
async.series([
function(callback){
request(sails.hooks.http.app)
.post('/contact/update')
.auth('test@test.com', 'test')
.send({ number: 1234, mail: "test@test.com", password: "test" })
.expect(200)
.end(callback);
},
function(callback){
request(sails.hooks.http.app)
.post('/user/update')
.auth('test@test.com', 'test')
.send({ numbers: [1234], mail: "tet@test.com", password: "test" })
.expect(200)
.end(callback);
}
], done);
});
});
关于javascript - Sails JS Passport http 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40129722/