【问题标题】:What does Passport.js use the client_id and client_secret for?Passport.js 使用 client_id 和 client_secret 做什么?
【发布时间】:2014-02-21 00:17:35
【问题描述】:

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

客户端通过添加 以下参数使用“application/x-www-form-urlencoded” 符合附录 B 的格式,HTTP 中的字符编码为 UTF-8 请求实体主体:

授予类型

    REQUIRED.  Value MUST be set to "password".

用户名

    REQUIRED.  The resource owner username.

密码

    REQUIRED.  The resource owner password.

范围

    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?

【问题讨论】:

  • 格伦,你解决过这个问题吗?我现在也在想同样的事情。该规范不需要 client_id 或 client_secret,所以我想知道为什么该策略需要。
  • 说实话,我已经不太记得提问的依据了。正如我现在所看到的,clientId 和 clientSecret 只是任意参数名称,可以很容易地成为用户名和密码。对于所有许多护照模块,如果有一些复制/粘贴可能解释为什么回调参数有相当奇怪的名称,我不会感到惊讶。这有帮助吗,还是我完全错过了重点? (对不起,我已经有一段时间没有阅读有关 auth 和 Passportjs 的所有内容了)
  • 它们似乎是任意的,但该策略实际上会检查主体是否存在 clientId 和 clientSecret。无论如何,我能够解决这个问题。不过,仍然对这些支票的存在感到困惑。
  • 在研究同一件事时,我发现了一个不错的代码示例 (Node.js),它提供了一个可能有用的工作模型 - aleksandrov.ws/2013/09/12/restful-api-with-nodejs-plus-mongodb

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


【解决方案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);

    });

    });

    };

【讨论】:

  • 对。我不记得我为什么要问这个问题。现在看来完全有道理。但感谢您的回答。如果其他人遇到困难,这可能会有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-08
  • 2017-06-06
  • 2022-10-04
  • 1970-01-01
  • 2017-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多