
Research
/Security News
Critical Vulnerability in NestJS Devtools: Localhost RCE via Sandbox Escape
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
nestjs-cognito
Advanced tools
AWS Cognito utilities module for Nest.
npm i --save @nestjs/jwt nestjs-cognito
/**
* @interface CognitoModuleOptions - Options for the CognitoModule
* @property {string} region - The region
*/
export type CognitoModuleOptions = CognitoIdentityProviderClientConfig &
Required<Pick<CognitoIdentityProviderClientConfig, 'region'>>;
/**
* @interface CognitoModuleOptionsFactory - Metadata for the CognitoModule
* @property {() => Promise<CognitoModuleOptions>} createCognitoModuleOptions - A factory function to create the CognitoModuleOptions
* @property {Type<any>[]} imports - The imports to be used by the module
* @property {Provider[]} providers - The providers to be used by the module
* @property {(string | Provider)[]} exports - The exports to be used by the module
* @property {string} name - The name of the module
*/
export interface CognitoModuleOptionsFactory {
createCognitoModuleOptions():
| Promise<CognitoModuleOptions>
| CognitoModuleOptions;
}
/**
* @interface CognitoModuleAsyncOptions - Options for the CognitoModule
* @property {Function} imports - Imports the module asyncronously
* @property {Function} inject - Injects the module asyncronously
* @property {CognitoModuleOptions} useFactory - The factory function to create the CognitoModuleOptions
* @property {CognitoModuleOptions} useClass - The class to create the CognitoModuleOptions
* @property {CognitoModuleOptions} useExisting - The existing instance of the CognitoModuleOptions
*/
export interface CognitoModuleAsyncOptions
extends Pick<ModuleMetadata, 'imports'> {
useExisting?: Type<CognitoModuleOptionsFactory>;
useClass?: Type<CognitoModuleOptionsFactory>;
useFactory?: (
...args: any[]
) => Promise<CognitoModuleOptions> | CognitoModuleOptions;
inject?: any[];
extraProviders?: Provider[];
}
Use CognitoModule.register
method with options of CognitoModuleOptions interface
import { CognitoModule } from 'nestjs-cognito';
import { Module } from '@nestjs/common';
@Module({
imports: [
CognitoModule.register({
region: 'eu-west-X',
}),
],
})
export class AppModule {}
With CognitoModule.registerAsync
you can import your ConfigModule and inject ConfigService to use it in useFactory
method.
It's also possible to use useExisting
or useClass
.
You can find more details here.
Here's an example:
import { CognitoModule } from 'nestjs-cognito';
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
CognitoModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
region: configService.get('COGNITO_REGION'),
}),
inject: [ConfigService],
}),
],
})
export class AppModule {}
You can use the cognito identity provider injectors or the built-in nestjs-cognito
decorators and guards.
import {
InjectCognitoIdentityProvider,
InjectCognitoIdentityProviderClient,
} from 'nestjs-cognito';
export class MyService {
constructor(
@InjectCognitoIdentityProvider()
private readonly client: CognitoIdentityProvider,
@InjectCognitoIdentityProviderClient()
private readonly cognitoIdentityProviderClient: CognitoIdentityProviderClient,
) {}
}
@Authentication
decorator or with the @UseGuards
decorator to apply the AuthenticationGuard
to the controller in order to ensure that the user is authenticated.@Authorization
decorator or with the @UseGuards
decorator to apply the AuthorizationGuard
in order to ensure that the user is authorized.@CurrentUser
decorator to get the current user.During the authorization
process, we already check if the user is authenticated, so you don't need to use authentication
guard or decorator.
In addition, you can find more details about @UseGuards
decorator here.
Here is an example that shows how to use authentication:
import {
Authentication,
AuthenticationGuard,
CurrentUser,
} from 'nestjs-cognito';
import { Controller, Get, UseGuards } from '@nestjs/common';
@Controller('dogs')
@Authentication()
export class DogsController {
@Get()
findAll(@CurrentUser() me: User): string {
return 'This action returns all my dogs';
}
}
@Controller('cats')
@UseGuards(AuthenticationGuard)
export class CatsController {
@Get()
findAll(@CurrentUser() me: User): string {
return 'This action returns all my cats';
}
}
@Controller('dogs')
export class DogsController {
@Get()
@UseGuards(AuthenticationGuard)
findAll(@CurrentUser() me: User): string {
return 'This action returns all my dogs';
}
}
Here is an example that shows how to use authorization:
import { Authorization, AuthorizationGuard, CurrentUser } from 'nestjs-cognito';
import { Controller, Get, UseGuards } from '@nestjs/common';
@Controller('dogs')
@Authorization({
allowedGroups: ['user', 'admin'],
requiredGroups: ['moderator'],
prohibitedGroups: ['visitor'],
})
export class DogsController {
@Get()
findAll(@CurrentUser() me: User): string {
return 'This action returns all my dogs';
}
}
@Controller('cats')
@Authorization(['user']) // allowedGroups by default
export class CatsController {
@Get()
findAll(@CurrentUser() me: User): string {
return 'This action returns all my cats';
}
}
@Controller('cats')
@UseGuards(
AuthorizationGuard({
allowedGroups: ['user', 'admin'],
requiredGroups: ['moderator'],
prohibitedGroups: ['visitor'],
}),
)
export class CatsController {
@Get()
findAll(@CurrentUser() me: User): string {
return 'This action returns all my cats';
}
}
@Controller('cats')
export class CatsController {
@Get()
@UseGuards(AuthorizationGuard(['user', 'admin']))
findAll(@CurrentUser() me: User): string {
return 'This action returns all my cats';
}
}
NestJS-Cognito is MIT licensed.
FAQs
NestJS Cognito Authentication Module
The npm package nestjs-cognito receives a total of 10 weekly downloads. As such, nestjs-cognito popularity was classified as not popular.
We found that nestjs-cognito demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.