forked from SOS-RS/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added hmac decorator to protect endpoints
- Loading branch information
1 parent
6388fce
commit 48d9e1b
Showing
8 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { applyDecorators, UseGuards } from '@nestjs/common'; | ||
|
||
import { HmacGuard } from '@/guards/hmac.guard'; | ||
|
||
export function Hmac() { | ||
return applyDecorators(UseGuards(HmacGuard)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Hmac } from './hmac.decorator'; | ||
|
||
export { Hmac }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { | ||
Injectable, | ||
CanActivate, | ||
ExecutionContext, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import * as crypto from 'crypto'; | ||
|
||
@Injectable() | ||
export class HmacGuard implements CanActivate { | ||
constructor() {} | ||
|
||
canActivate(context: ExecutionContext): boolean { | ||
const request = context.switchToHttp().getRequest(); | ||
const hmacHeader = request.headers['x-hmac-signature']; | ||
const timestamp = request.headers['x-hmac-timestamp']; | ||
|
||
if (!hmacHeader || !timestamp) { | ||
throw new UnauthorizedException(); | ||
} | ||
|
||
const secretKey = process.env.HMAC_SECRET_KEY ?? ''; | ||
const currentTimestamp = Math.floor(Date.now() / 1000); | ||
|
||
if (Math.abs(currentTimestamp - parseInt(timestamp)) > 10) { | ||
throw new UnauthorizedException(); | ||
} | ||
|
||
const payload = `${request.method}:${request.url}:${timestamp}:${JSON.stringify(request.body)}`; | ||
|
||
const hmac = crypto | ||
.createHmac('sha256', secretKey) | ||
.update(payload) | ||
.digest('hex'); | ||
|
||
if (hmac !== hmacHeader) { | ||
throw new UnauthorizedException(); | ||
} | ||
|
||
return true; | ||
} | ||
} |