@dmapper/passport-oauth2

中文文档
1.0.0发布于 2022/08/08MITCommonJS近 30 天下载 3708

OAuth 2.0 authentication strategy for Passport.

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. Related Modules
  4. Contributing
  5. License

passport-oauth2

This fork implements optional callback function that can be used to pass client id and secret to initialise oauth2 each time directly before an authentication. This function is called with the request object.

passport.use(new OAuth2Strategy({
    authorizationURL: 'https://www.example.com/oauth2/authorize',
    tokenURL: 'https://www.example.com/oauth2/token',
    getClient: async function(req) {
      // some possible async operations
      // ...
      return {
        id: '#####',
        secret: '#####'
      }
    },
    callbackURL: "http://localhost:3000/auth/example/callback"
  },
  function(accessToken, refreshToken, profile, cb) {
    User.findOrCreate({ exampleId: profile.id }, function (err, user) {
      return cb(err, user);
    });
  }
));

====================================================================================

General-purpose OAuth 2.0 authentication strategy for Passport.

This module lets you authenticate using OAuth 2.0 in your Node.js applications. By plugging into Passport, OAuth 2.0 authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.

Note that this strategy provides generic OAuth 2.0 support. In many cases, a provider-specific strategy can be used instead, which cuts down on unnecessary configuration, and accommodates any provider-specific quirks. See the list for supported providers.

Developers who need to implement authentication against an OAuth 2.0 provider that is not already supported are encouraged to sub-class this strategy. If you choose to open source the new provider-specific strategy, please add it to the list so other people can find it.


Advertisement
Learn OAuth 2.0 - Get started as an API Security Expert
Just imagine what could happen to YOUR professional career if you had skills in OAuth > 8500 satisfied students


npm build coverage ...

Install

$ npm install passport-oauth2

Usage

Configure Strategy

The OAuth 2.0 authentication strategy authenticates users using a third-party account and OAuth 2.0 tokens. The provider's OAuth 2.0 endpoints, as well as the client identifer and secret, are specified as options. The strategy requires a verify callback, which receives an access token and profile, and calls cb providing a user.

passport.use(new 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"
  },
  function(accessToken, refreshToken, profile, cb) {
    User.findOrCreate({ exampleId: profile.id }, function (err, user) {
      return cb(err, user);
    });
  }
));

Authenticate Requests

Use passport.authenticate(), specifying the 'oauth2' strategy, to authenticate requests.

For example, as route middleware in an Express application:

app.get('/auth/example',
  passport.authenticate('oauth2'));

app.get('/auth/example/callback',
  passport.authenticate('oauth2', { failureRedirect: '/login' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
  });

Contributing

Tests

The test suite is located in the test/ directory. All new features are expected to have corresponding test cases. Ensure that the complete test suite passes by executing:

$ make test

Coverage

All new feature development is expected to have test coverage. Patches that increse test coverage are happily accepted. Coverage reports can be viewed by executing:

$ make test-cov
$ make view-cov

License

The MIT License

Copyright (c) 2011-2016 Jared Hanson <http://jaredhanson.net/>

版本与发布记录

@dmapper/passport-oauth2 共发布 1 个版本,首次发布于 2022/08/08;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2022/08/084 年前

关于 @dmapper/passport-oauth2 的常见问题

@dmapper/passport-oauth2 是什么?

OAuth 2.0 authentication strategy for Passport.

如何安装 @dmapper/passport-oauth2?

在项目目录运行 npm install @dmapper/passport-oauth2,或使用 pnpm add / yarn add / bun add。

@dmapper/passport-oauth2 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/dmapper__passport-oauth2 是否存在。

@dmapper/passport-oauth2 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@dmapper/passport-oauth2 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 0.4.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@dmapper/passport-oauth2 可以商用吗?

@dmapper/passport-oauth2 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dmapper/passport-oauth2 的下载量有多少?

根据 npm 官方下载统计,@dmapper/passport-oauth2 在 2026/10/13 – 2026/11/11 这 30 天内被下载 3,708 次,日均约 124 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dmapper/passport-oauth2 还在维护吗?

最新版本 1.0.0 发布于 2022/08/08(4 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/08/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。