如何将自己的 Oauth2 Passport 策略添加到 FeathersJS 应用程序?

How do you add your own Oauth2 Passport strategy to a FeathersJS app?

提前为明显的问题道歉 question/answer,但我一直在搜索文档,但找不到它。

我知道 FeathersJS 有 Facebook/Twitter/Github 的插入策略——我在文档中看到了这些。而且我知道您可以执行各种自定义授权策略。我想要做的就是通过尚未具有预打包策略的 Oauth2 提供程序对用户进行身份验证。我找不到执行此操作的工作示例。

更令人沮丧的是,当我尝试遵循 examples/docs 时,我收到来自 feathersjs npm 模块的错误,例如:

    <...>/node_modules/@feathersjs/authentication-oauth2/lib/index.js:96
      app.passport.use(name, new Strategy(oauth2Settings, verifier.verify.bind(verifier)));
                             ^
TypeError: Strategy is not a constructor

有人有工作示例吗?

该错误意味着您没有通过 Passport oAuth2 策略。您可以设置 general Passport oAuth2 adapter very similar to the example in the documentation:

const oauth2 = require('@feathersjs/authentication-oauth2');
const OAuth2Strategy = require('passport-oauth2').Strategy;

app.configure(oauth2({
  name: 'custom',
  Strategy: OAuth2Strategy,
  authorizationURL: 'https://www.example.com/oauth2/authorize',
  tokenURL: 'https://www.example.com/oauth2/token',
  clientID: EXAMPLE_CLIENT_ID,
  clientSecret: EXAMPLE_CLIENT_SECRET,
  callbackURL: "http://localhost:3000/auth/example/callback"
}));