使用 composer-rest-server 访问 OAuth 范围信息

Accessing the OAuth scope information with composer-rest-server

我已经成功构建了一个网络,并且可以将其部署到 Hyperledger Fabric 的本地实例中。使用 Hyperledger Composer 文档 here,我有两个 composer-rest-server 实例 运行 - 一个启用了多用户身份验证,一个没有启用,从这个角度来看一切都运行良好。

对于启用了多用户身份验证的 REST 服务器,我已经成功地为 passport-githubpassport-google-oauth 设置了提供程序,使用以下 COMPOSER_PROVIDERS 的值:

export COMPOSER_PROVIDERS='{
  "github": {
    "provider": "github",
    "module": "passport-github",
    "clientID": "<client_id>",
    "clientSecret": "<client_secret>",
    "scope": "read:user,user:email",
    "authPath": "/auth/github",
    "callbackURL": "/auth/github/callback",
    "successRedirect": "http://localhost:4200/callback",
    "failureRedirect": "http://localhost:4200/login-failed"
  },
  "google": {
    "provider": "google",
    "module": "passport-google-oauth",
    "strategy": "OAuth2Strategy",
    "clientID": "<client_id>",
    "clientSecret": "<client_secret>",
    "scope": [
      "https://www.googleapis.com/auth/plus.login",
      "https://www.googleapis.com/auth/userinfo.email",
      "https://www.googleapis.com/auth/userinfo.profile"
    ],
    "authPath": "/auth/google",
    "callbackURL": "/auth/google/callback",
    "successRedirect": "http://localhost:4200/callback",
    "failureRedirect": "http://localhost:4200/login-failed"
  }
}'

如您所见,我在两个提供商中都指定了一个 scope 变量来尝试检索用户个人资料的电子邮件+用户名。 Loopback 在 Passport 身份验证过程中成功检索到此信息,这太棒了!但是随后 composer-rest-server 使用 Loopback 的 defaultCallback 进行身份验证过程,这意味着 Loopback 丢弃所有这些信息并选择仅设置两个 cookie - access_tokenuserId。所有 scope 信息显然都被丢弃并永远丢失了。

我可以破解我的 node_modules/ 中的 composer-rest-server 代码,以使用我自己的自定义 Passport 身份验证回调来保存此范围数据供以后使用,但是有推荐的方法吗?

谢谢!


我目前的"solution"是加

let cb = require('../lib/custom-callback')(s, c);
c.customCallback = cb.callback();

就在

之前
passportConfigurator.configureProvider(s, c);

在 composer-rest-server 的 server.js 内。然后我可以把我自己的自定义回调代码放在../lib/custom-callback.js中。但是这个功能似乎不是开箱即用的。

如您所知,它在幕后使用了 Loopback 框架及其 loopbackPassport.PassportConfigurator(以及设置的内容)。您可以生成自己的自定义 REST 服务器(在功能上等同于 Composer REST 服务器)以根据需要进行自定义 https://hyperledger.github.io/composer/latest/integrating/customizing-the-rest-server - 这将生成 Loopback 3 应用程序。所以推荐的方法是生成 REST 服务器并进行相应的自定义。