Javascript 航海JS护照http 401
我试图在passport http包的帮助下保护我的sails js rest api,但目前我无法确定代码中的错误在哪里。 我用这个和这个来了解这个应该如何工作。我的问题是我的代码总是返回401。 我真的不知道在哪里可以找到错误。如果您需要有关我的代码的更多信息,请发表评论。 布鲁诺 编辑: 我(在@Viktor的帮助下)找到了问题的根源。我只是不太明白HTTP基本身份验证是如何工作的。现在的问题是如何发送身份验证凭据和数据?如果我只是用auth(…)测试路由,它们会工作。。。但是如何添加数据呢?或者我必须首先对自己进行身份验证,然后在第二个请求中发送数据 passport.jsJavascript 航海JS护照http 401,javascript,node.js,rest,sails.js,passport.js,Javascript,Node.js,Rest,Sails.js,Passport.js,我试图在passport http包的帮助下保护我的sails js rest api,但目前我无法确定代码中的错误在哪里。 我用这个和这个来了解这个应该如何工作。我的问题是我的代码总是返回401。 我真的不知道在哪里可以找到错误。如果您需要有关我的代码的更多信息,请发表评论。 布鲁诺 编辑: 我(在@Viktor的帮助下)找到了问题的根源。我只是不太明白HTTP基本身份验证是如何工作的。现在的问题是如何发送身份验证凭据和数据?如果我只是用auth(…)测试路由,它们会工作。。。但是如何添加数据
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);
});
});
}
));
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);
};
module.exports.policies = {
'*': true,
'UserController': {
update: 'isAuthenticated'
}
}
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);
});
});
});
isAuthenticated.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);
});
});
}
));
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);
};
module.exports.policies = {
'*': true,
'UserController': {
update: 'isAuthenticated'
}
}
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);
});
});
});
policies.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);
});
});
}
));
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);
};
module.exports.policies = {
'*': true,
'UserController': {
update: 'isAuthenticated'
}
}
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);
});
});
});
UserController.test.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);
});
});
}
));
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);
};
module.exports.policies = {
'*': true,
'UserController': {
update: 'isAuthenticated'
}
}
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);
});
});
从文档中可以看出,您需要用户ID和密码。所以,看看你的代码,你有邮件代替用户ID,这就是为什么你得到401或者至少你可以开始寻找的地方。如果您需要验证这一点,您可以查看
passport.use(新基本策略)(
函数(用户标识、密码、完成){
findOne({mail:userid,password:password},函数(err,User){
完成(错误,用户);
});
}
));代码>尝试使用“基本”而不是用户身份验证OK,但我想我可以命名不同的策略,我希望这将有助于您错误为null,用户为false(如您所建议的)。信息显示“基本领域=“用户”。最让我困惑的是,每次我试图在passport.use中记录一些内容时('user-authentication',…
函数什么都不会发生。我使用自定义路由,我将发布我的测试函数…可能我在那里出错了…@Bruno,数据库中是否有要登录的用户?是的,我更改了策略,以便新用户始终可以登录created@Bruno用户创建是否在测试中正常工作现在使用更新的策略,创建测试通过了吗?我看到您的测试中没有设置任何身份验证。添加.auth('test@test.com","测试")
更新测试调用?请参阅。@Bruno我不知道否决票来自何方或原因,但正如我所提到的,您的Passport.js配置在一个全新的带有blueprint routes和Controller的Sails.js项目中对我有效。只要我对blueprint routes和contact进行必要的调整,您的测试也能正常工作滚动(路径、方法等)并将我在前面的注释中提到的auth()
调用添加到更新测试中。如果您仍然存在身份验证问题,那么如果您同时发布相关的模型、控制器和路由,可能更容易找到错误。