首页 文章

Passport.js使用client_id和client_secret做什么用途?

提问于
浏览
3

我对于client_id和client_scret应该是什么感到困惑?资源所有者密码凭据授权的specs说:

客户端通过使用附录B中的“application / x-www-form-urlencoded”格式添加以下参数来向令牌 endpoints 发出请求,其中HTTP请求实体的字符编码为UTF-8:grant_type REQUIRED . 值必须设置为“密码” .
用户名必填 . 资源所有者用户名 .
要求输入密码 . 资源所有者密码 .
范围可选 . 访问请求的范围如下所述
第3.3节 .

但Passport.js策略记录如下:

passport.use(new ClientPasswordStrategy(
  function(clientId, clientSecret, done) {
    Clients.findOne({ clientId: clientId }, function (err, client) {
      if (err) { return done(err); }
      if (!client) { return done(null, false); }
      if (client.clientSecret != clientSecret) { return done(null, false); }
      return done(null, client);
    });
  }
));

所以我的问题是,如果规范没有说明需要client_id或client_secret,为什么oauth2-client-password策略使用client_id和secret_id?

1 回答

  • 1

    我猜你现在有这个,但我想我还是会添加一个答案 .

    • ClientId是您与数据库匹配的ID

    • ClientSecret是您将与数据库中的哈希或加密密码进行比较的密码 .

    示例代码:

    Client.verify = function(clientId, secret, next){
    
        this.getByClientId(clientId, function(err, client){
    
        if(err) {
            return next(err);
        }
    
        if(!client){
            return next(null, false);
        }
    
        SecurityUtil.compare(secret, client.hash, function(err, result){
    
            if(err){
                return next(err);
            }
    
            next(null, result);
    
        });
    
        });
    
        };
    

相关问题