npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nest-oauth2-server

v2.0.1

Published

A Nest module wrapper for oauth2-server

Downloads

270

Readme

nest-oauth2-server

NPM version Build Status

Complete, compliant and well tested module for implementing an OAuth2 server with Nest in Node.js.

This is the Nest module wrapper for @node-oauth/oauth2-server.

Installation

To begin using it, we first install the required dependencies.

$ npm install --save nest-oauth2-server @node-oauth/oauth2-server

Getting started

Once the installation process is complete, we can import the OAuth2ServerModule into the root AppModule.

import { Module } from '@nestjs/common';
import { OAuth2ServerModule } from 'nest-oauth2-server';
import { model } from './model';

@Module({
  imports: [
    OAuth2ServerModule.forRoot({
      model: model
    }),
  ],
})
export class AppModule {}

The forRoot() method accepts the same configuration object to create a new OAuth2Server instance.

Note that OAuth2Server requires a model object through which some aspects or storage, retrieval and custom validation are abstracted. Therefore, in most cases you will need to use async configuration to import your repository module for the model implementation.

The model specification see documentation for details.

Decorators

The module provides decorators to help you create OAuth2Server handlers (endpoints).

| Decorator | OAuth2Server handler | | ----------------------------------------------------------- | ----------------------------- | | @OAuth2ServerAuthenticate(options?: AuthenticateOptions) | OAuth2Server#authenticate() | | @OAuth2ServerAuthorize(options?: AuthorizeOptions) | OAuth2Server#authorize() | | @OAuth2ServerToken(options?: TokenOptions) | OAuth2Server#token() |

Any valid option for @OAuth2ServerAuthenticate(), @OAuth2ServerAuthorize() and @OAuth2ServerToken() can be passed to the OAuth2ServerModule.forRoot() method as well. The supplied options will be used as default for the other methods.

In addition, we provide the @OAuth2ServerOAuth() decorator lets you retrieve oauth information from the res.locals.oauth property.

The following is an example controller for oauth2 server endpoints:

import { Controller, Get, Post } from '@nestjs/common';
import { OAuth2ServerAuthenticate, OAuth2ServerAuthorize, OAuth2ServerToken, OAuth2ServerOAuth, OAuth } from 'nest-oauth2-server';

@Controller('oauth')
export class OAuthController {
  @Get('user')
  @OAuth2ServerAuthenticate()
  user(@OAuth2ServerOAuth() oauth: OAuth) {
    return oauth.token.user;
  }

  @Post('authorize')
  @OAuth2ServerAuthorize()
  authorize() {}

  @Post('token')
  @OAuth2ServerToken()
  token() {}
}

Async configuration

When you need to pass module options asynchronously instead of statically, use the forRootAsync() method. As with most dynamic modules, Nest provides several techniques to deal with async configuration.

One technique is to use a factory function:

OAuth2ServerModule.forRootAsync({
  useFactory: () => ({
    model: model,
  }),
});

Like other factory providers, our factory function can be async and can inject dependencies through inject.

OAuth2ServerModule.forRootAsync({
  imports: [OAuthModule],
  useFactory: async (model: OAuth2ServerModel) => ({
    model: model
  }),
  inject: [OAuth2ServerModel],
});

Alternatively, you can configure the OAuth2ServerModule using a class instead of a factory, as shown below.

OAuth2ServerModule.forRootAsync({
  useClass: OAuth2ServerConfigService,
});

The construction above instantiates OAuth2ServerConfigService inside OAuth2ServerModule, using it to create an options object. Note that in this example, the OAuth2ServerConfigService has to implement OAuth2ServerOptionsFactory interface as shown below. The OAuth2ServerModule will call the createOAuth2ServerOptions() method on the instantiated object of the supplied class.

@Injectable()
class OAuth2ServerConfigService implements OAuth2ServerOptionsFactory {
  constructor(private readonly model: OAuth2ServerModel) {}

  createOAuth2ServerOptions(): OAuth2ServerModuleOptions {
    return {
      model: this.model,
    };
  }
}

If you want to reuse an existing options provider instead of creating a private copy inside the OAuth2ServerModule, use the useExisting syntax.

OAuth2ServerModule.forRootAsync({
  imports: [ConfigModule],
  useExisting: OAuth2ServerConfigService,
});

Example

A working example is available in test directory.

License

MIT