@boyuai/nestjs-oauth2-server

中文文档
0.1.0发布于 2022/02/26MITTypeScriptCommonJS零依赖近 30 天下载 1792

A NestJS wrapper for oauth2-server

原始使用文档
文档目录7 节
  1. NestJS OAuth2 Server
  2. Installation
  3. Configuration
  4. Usage
  5. Async Configuration
  6. Learnings
  7. Contributing

NestJS OAuth2 Server

Nest Logo OAuth2 Logo

npm npm version LICENCE synk vulnerabilities

A NestJS wrapper module for the oauth2-server package.

Table of content (click to expand)

Installation

Installation is as simple as running:

npm install @boyuai/nestjs-oauth2-server

or

yarn add @boyuai/nestjs-oauth2-server.

Configuration

  1. Include the module as a dependency in the module where oauth will happen:

oauth2.module.ts

import { Module } from '@nestjs/common';
import { OAuth2ServerModule } from '@boyuai/nestjs-oauth2-server';
import { OAuth2Controller } from './oauth2.controller';
import { OAuth2ServiceModule } from './oauth2-service.module';
import { OAuth2Model } from './oauth2.model';

@Module({
  imports: [
    OAuth2ServiceModule,
    OAuth2ServerModule.forRoot({
      imports: [OAuth2ServiceModule], // import your repository for OAuth2Model here
      modelClass: OAuth2Model,
    }),
  ],
  controllers: [OAuth2Controller],
})
export class OAuth2Module {}

In addition to the above the, oauth2-server requires a model to create the server. This can be provided as a service from any part of the application. This should be able to fetch data about clients, users, token, and authorization codes. This MUST be a service decorated with the Injectable decorator.

oauth2.model.ts

import { Injectable } from '@nestjs/common';

@Injectable()
export class OAuth2Model implements RequestAuthenticationModel {
    getAccessToken() {}

    verifyScope() {}
    // ...
    // check more codes in test/src/test-model.service.ts
}

Usage

The module also provides some nifty decorators to help with configuring the oauth2 handler endpoints. An example controller covering the entire array of decorators is given below

oauth2.controller.ts

import { Controller } from '@nestjs/common';
import {
  OAuth2Authorization,
  OAuth2Authorize,
  OAuth2RenewToken,
  OAuth2Token,
} from '@boyuai/nestjs-oauth2-server';

@Controller()
export class OAuth2Controller {
    @Post()
    @OAuth2Authenticate()
    authenticateClient(@OAuth2Token() token: Token) {
        return token;
    }

    @Post()
    @OAuth2Authorize()
    authorizeClient(
        @OAuth2Authorization()
        authorization: AuthorizationCode,
    ) {
        return authorization;
    }

    @Post()
    @OAuth2RenewToken()
    renewToken(@OAuth2Token() token: Token) {
        return token;
    }
}

Async Configuration

The module could also be included asynchronously using the forRootAsync method.

Examples below:

  • Using factory provider approach
import { Module } from '@nestjs/common';
import {
    OAuth2ServerModule,
    IOAuth2ServerModuleOptions,
} from '@boyuai/nestjs-oauth2-server';

@Module({
    imports: [
        // ... other modules
        OAuth2ServerModule.forRootAsync({
            useFactory: (factory: IOAuth2ServerOptionsFactory) => ({}),
            modelClass: YourModelService,
        }),
    ],
})
export class YourModule {}
  • Using class or existing provider approach:

oauth2-server-config.service.ts

import {
    IOAuth2ServerModuleOptions,
    IOAuth2ServerOptionsFactory,
} from '@boyuai/nestjs-oauth2-server';
import { Injectable } from '@nestjs/common';

@Injectable()
export class OAuth2ServerConfigService
    implements IOAuth2ServerOptionsFactory {
    createOAuth2ServerOptions(): IOAuth2ServerModuleOptions {
        return {};
    }
}

The OAuth2ServerConfigService SHOULD implement the IOAuth2ServerOptionsFactory, MUST declare the createOAuth2ServerOptions method and MUST return IOAuth2ServerModuleOptions object.

import { Module } from '@nestjs/common';
import { OAuth2ServerModule } from '@boyuai/nestjs-oauth2-server';
import { OAuth2ServerConfigService } from './oauth2-server-config.service.ts';

@Module({
    imports: [
        // ... other modules
        OAuth2ServerModule.forRootAsync({
            useClass: OAuth2ServerConfigService,
            modelClass: YourModelService,
        }),
    ],
})
export class YourModule {}

Learnings

The concept of OAuth2 can be further understood in this article here. Also you can head over to the oauth2-server package documentation.

Contributing

Suggestions for improvement are welcomed, however, please adhere to the contributing guidelines.

版本与发布记录

@boyuai/nestjs-oauth2-server 共发布 2 个版本,首次发布于 2021/11/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.0latest2022/02/264 年前
0.1.0-rc.02021/11/204 年前

关于 @boyuai/nestjs-oauth2-server 的常见问题

@boyuai/nestjs-oauth2-server 是什么?

A NestJS wrapper for oauth2-server

如何安装 @boyuai/nestjs-oauth2-server?

在项目目录运行 npm install @boyuai/nestjs-oauth2-server,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@nestjs/common(^6.10.0 || ^7.0.0 || ^8.0.10)、reflect-metadata(^0.1.12)、rxjs(^6.0.0 || ^7.2.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@boyuai/nestjs-oauth2-server 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@boyuai/nestjs-oauth2-server 支持 ESM 还是 CommonJS?

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

@boyuai/nestjs-oauth2-server 可以商用吗?

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

@boyuai/nestjs-oauth2-server 的下载量有多少?

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

@boyuai/nestjs-oauth2-server 还在维护吗?

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