javascript - Passport.js 使用 client_id 和 client_secret 有什么用?

标签 javascript node.js authentication oauth-2.0 passport.js

我正在使用 Passport.js 和 API 的 oauth2-client-password 策略实现 OAuth2 资源所有者密码凭证授权,但我对 client_id 和 client_scret 应该是什么感到困惑? specs对于资源所有者密码凭据授予说:

The client makes a request to the token endpoint by adding the following parameters using the "application/x-www-form-urlencoded" format per Appendix B with a character encoding of UTF-8 in the HTTP request entity-body:

grant_type

    REQUIRED.  Value MUST be set to "password".

username

    REQUIRED.  The resource owner username.

password

    REQUIRED.  The resource owner password.

scope

    OPTIONAL.  The scope of the access request as described by
     Section 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?

最佳答案

我猜你现在已经有了这个,但我想我还是会添加一个答案。

  • 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);

    });

    });

    };

关于javascript - Passport.js 使用 client_id 和 client_secret 有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21922580/

相关文章:

javascript - 在 z 轴上将图像夹在子对象和父对象之间

javascript - Intersection Observer 不可信?

javascript - req.body 中的 JSON 数据数组

ajax - 保持数据与服务器同步

Java认证安全

asp.net - 在 ASP.NET 中使用 ADFS 实现身份验证?

javascript - d3JS : Drawing Line Segments from CSV

javascript - jquery、css、html5 的导航栏问题

javascript - 与新 React 应用程序运行相关的问题

python - 了解 Django-LDAP 身份验证