【发布时间】:2023-03-09 09:01:01
【问题描述】:
我目前正在尝试掌握护照,特别是如何验证 Restful api,我有点不确定如何测试我所拥有的
我有以下 BearerStrategy:
var BearerStrategy = new Bearer({
},
function(token, done) {
process.nextTick(function () {
findByToken(token, function(err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
return done(null, user);
})
});
}
);
findByToken 在哪里:
var api_users = [
{ id: 1, username: 'bob', token: '123456789', email: 'bob@example.com' }
, { id: 2, username: 'joe', token: 'abcdefghi', email: 'joe@example.com' }
];
function findByToken(token, fn) {
for (var i = 0, len = users.length; i < len; i++) {
var user = api_users[i];
if (user.token === token) {
return fn(null, user);
}
}
return fn(null, null);
}
然后我有以下路线:
app.get('/api', passport.authenticate('bearer', { session: false}), function (request, response){
response.send('BAM! you got a response');
});
我已经尝试在邮递员中进行测试,但我得到了Unauthorized,但我不确定如何测试它是否成功。上面的代码不是我的全部,它是我从网上找到的几个sn-ps拼凑起来的最简单的解决方案。
在 Post man 中,我有以下选项:Normal、Basic Auth,需要参数 username and Password
Digest Auth 寻找参数Username, Realm, Password, Nonce, Algorithm, qop, Nonce count, Client nonce, Opaque
OAuth 1.0 查找参数 Consumer Key, Consumer Secret, Token, Token Secret, Signature Method, Timestamp,Nonce, Version, Realm, Add params to header ,Auto add parameters
这对我来说是全新的,我已经尝试用谷歌搜索如何测试。任何人都可以提供的任何指导都会很棒。
【问题讨论】:
标签: javascript node.js express passport.js postman