【问题标题】:How Can I test a basic Bearer Strategy for Passport.js using Postman如何使用 Postman 测试 Passport.js 的基本承载策略
【发布时间】: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


    【解决方案1】:

    以防万一有人正在寻找后期邮递员版本的解决方案。您可以设置您的邮递员以包含密钥“授权”,其值将是“承载 XXXXX”,其中 XXX 是 access_token 值。

    【讨论】:

    • 非常感谢。 :-) 我在敲我的头,为什么该死的邮递员不发送身份验证令牌....
    【解决方案2】:

    所以我发现我想做的是在需要 access_token 时使用 token 参数

    这是一个屏幕截图,以防有人需要知道如何测试

    【讨论】:

    • 太好了,但这是旧的邮递员版本...当时使用 normal 身份验证很容易,但现在新的 potman chrome 应用程序中没有这样的东西。
    猜你喜欢
    • 1970-01-01
    • 2013-08-29
    • 2013-12-25
    • 2014-03-20
    • 2016-07-29
    • 2022-01-21
    • 2021-12-06
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多