nestjs-hmac256-guard
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

NestJS module to support HMAC 256 verification

This module implement HMAC256 request verification base on requirement here - (https://learn.microsoft.com/en-us/azure/azure-app-configuration/rest-api-authentication-hmac)

Basic usage with all default

import { HMacModule } from 'nestjs-hmac256-guard';

@Module({
  imports: [
    HMacModule.register({ HMAC_HASH_SECRET: 'this is my hash secret' }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Then use Guard

import { HmacGuard } from 'nestjs-hmac256-guard';
@Controller()
@UseGuards(HmacGuard)
export class AppController {

}

Customise header name

import { HMacModule } from 'nestjs-hmac256-guard';

@Module({
  imports: [
    HMacModule.register({ 
        HMAC_HASH_SECRET: 'this is my hash secret',
        HMAC_SIGNATURE_HEADER_NAME : 'x-my-hmac256-signature'
        REQUEST_CONTENT_HASH_HEADER_NAME: 'x-content-sha26',
        REQUEST_DATE_HEADER_NAME: 'x-date',
        MAX_REQUEST_MINUTES_ALLOW = 900
     }),
  ],
  controllers: [AppController],
  providers: [AppService],
})

Default value

HMAC_SIGNATURE_HEADER_NAME: x-signature

REQUEST_CONTENT_HASH_HEADER_NAME: x-content-sha26

MAX_REQUEST_MINUTES_ALLOW: 900 REQUEST_DATE_HEADER_NAME: x-date

Custom hash format

import { HMacModule } from 'nestjs-hmac256-guard';

@Module({
  imports: [
    HMacModule.register({ 
        HMAC_HASH_SECRET: 'this is my hash secret',
        getStringToHash: ({
            verb,
            url,
            host,
            date,
            contentHash,
        }) => {
            return 'your custom string format if that not same with default '
        }
     }),
  ],
  controllers: [AppController],
  providers: [AppService],
})

Readme

Keywords

Package Sidebar

Install

npm i nestjs-hmac256-guard

Weekly Downloads

104

Version

1.0.6

License

MIT

Unpacked Size

213 kB

Total Files

47

Last publish

Collaborators

  • samuraitruong