Sails JS 护照 http 401
Sails JS passport http 401
我试图在 passport http 包的帮助下保护我的 sails js rest api 但目前我无法弄清楚我的代码中的错误在哪里。
我用这个 repo and this tutorial 来了解它应该如何工作。我的问题是我的代码总是 returns 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
的不同步骤中会发生什么 - 你能通过数据库中的电子邮件地址找到用户并且密码匹配吗?
您有自定义路由和控制器操作还是使用蓝图?
编辑:使用 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);
});
});
来自文档 here it shows you need userid and password. So looking at your code you have mail in place of userid and that is why you are getting 401 or at least where you can start looking. If you need to verify this you can look at passport-http basic strategy here。
passport.use(new BasicStrategy(
function(userid, password, done) {
User.findOne({ mail: userid, password: password }, function (err, user) {
done(err, user);
});
}
));
我试图在 passport http 包的帮助下保护我的 sails js rest api 但目前我无法弄清楚我的代码中的错误在哪里。 我用这个 repo and this tutorial 来了解它应该如何工作。我的问题是我的代码总是 returns 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
的不同步骤中会发生什么 - 你能通过数据库中的电子邮件地址找到用户并且密码匹配吗?
您有自定义路由和控制器操作还是使用蓝图?
编辑:使用 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);
});
});
来自文档 here it shows you need userid and password. So looking at your code you have mail in place of userid and that is why you are getting 401 or at least where you can start looking. If you need to verify this you can look at passport-http basic strategy here。
passport.use(new BasicStrategy(
function(userid, password, done) {
User.findOne({ mail: userid, password: password }, function (err, user) {
done(err, user);
});
}
));