A Nestjs wrapper module for the oauth2-server package.
Table of content (click to expand)
Installation is as simple as running:
npm install @dieyne/nestjs-oauth2-server
or
yarn add @dieyne/nestjs-oauth2-server
.
- Include the module as a dependency in the module where pdf will be generated:
app.module.ts
import { Module } from '@nestjs/common';
import { OAuth2ServerModule } from '@dieyne/nestjs-oauth2-server';
@Module({
imports: [
// ... other modules
OAuth2ServerModule.forRoot({}),
],
})
export class AppModule {}
In addition to the above the, oauth2-server requires a model to create the server. This can be provided as a service
import { Global, Module } from '@nestjs/common';
import { OAuth2ServerModule } from '@dieyne/nestjs-oauth2-server';
import { OAuth2ServerController } from '../controller/oauth2-server.controller';
import { ModelForOAuth2Module } from './model-for-oauth2.module';
import { ModelForOAuth2Service } from 'src/service/model-for-oauth2.service';
@Module({
imports: [
OAuth2ServerModule.forRootAsync(
{
useFactory: () => ({
requireClientAuthentication: true,
accessTokenLifetime: 60 * 60 * 12,
refreshTokenLifetime: 60 * 60 * 14,
authorizationCodeLifetime: 60 * 60,
}),
},
{
useFactory(modelService) {
return modelService;
},
inject: [ModelForOAuth2Service],
imports: [ModelForOAuth2Module],
},
),
],
providers: [],
exports: [],
controllers: [OAuth2ServerController],
})
@Global()
export class MyOAuth2ServerModule {}
@Injectable()
export class ModelForOAuth2Service implements RefreshTokenModel {
constructor(
// inject others services here
) {}
}
@Module({
imports: [
// others modules here
],
providers: [ModelForOAuth2Service],
exports: [ModelForOAuth2Service],
})
export class ModelForOAuth2Module {}
// if ModelForOAuth2Service don't have any dependancies, you can use
@Module({
imports: [
OAuth2ServerModule.forRoot({
requireClientAuthentication: true,
accessTokenLifetime: 60 * 60 * 12,
refreshTokenLifetime: 60 * 60 * 14,
authorizationCodeLifetime: 60 * 60,
}, ModelForOAuth2Service)]
)}
export class ModelForOAuth2Module {}
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
import { Controller } from '@nestjs/common';
import {} from '@dieyne/nestjs-oauth2-server';
@Controller()
export class ExampleController {
@Post()
@OAuth2Authenticate()
authenticateClient(@OAuth2Token() token: Token) {
return of(token);
}
@OAuth2Authorize()
@Post()
authorizeClient(
@OAuth2Authorization()
authorization: AuthorizationCode,
) {
return of(authorization);
}
@Post()
@OAuth2RenewToken()
renewToken(@OAuth2Token() token: Token) {
return of(token);
}
}
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 '@dieyne/nestjs-oauth2-server';
@Module({
imports: [
// ... other modules
OAuth2ServerModule.forRootAsync({
useFactory: (): IOAuth2ServerModuleOptions => ({}),
}),
],
})
export class AppModule {}
- Using class or existing provider approach:
./oauth2-server-config.service.ts
import {
IOAuth2ServerModuleOptions,
IOAuth2ServerOptionsFactory,
} from '@dieyne/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 '@dieyne/nestjs-oauth2-server';
import { OAuth2ServerConfigService } from './oauth2-server-config.service.ts';
@Module({
imports: [
// ... other modules
OAuth2ServerModule.forRootAsync({
useClass: OAuth2ServerConfigService,
}),
],
})
export class AppModule {}
The concept of OAuth2 can be further understood in this article here. Also you can head over to the oauth2-server package documentation
Suggestions for improvement are welcomed, however, please adhere to the contributing guidelines