-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): recaptcha guard on faucet route
- Loading branch information
Showing
4 changed files
with
58 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import {HttpModule} from "@nestjs/axios"; | ||
import { CacheModule } from '@nestjs/cache-manager'; | ||
import { Module } from '@nestjs/common'; | ||
|
||
import {RecaptchaGuard} from "../recaptcha/RecaptchaGuard"; | ||
import { FaucetController } from './FaucetController'; | ||
import { FaucetService } from './FaucetService'; | ||
|
||
@Module({ | ||
imports: [CacheModule.register()], | ||
imports: [CacheModule.register(), HttpModule], | ||
controllers: [FaucetController], | ||
providers: [FaucetService], | ||
providers: [FaucetService, RecaptchaGuard], | ||
}) | ||
export class FaucetModule {} |
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,40 @@ | ||
import { HttpService } from '@nestjs/axios'; | ||
import { ExecutionContext, Injectable, Logger } from '@nestjs/common'; | ||
import { Request } from 'express'; | ||
|
||
@Injectable() | ||
export class RecaptchaGuard { | ||
private readonly logger: Logger; | ||
|
||
constructor(private readonly httpService: HttpService) { | ||
this.logger = new Logger(RecaptchaGuard.name); | ||
} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest<Request>(); | ||
return this.validateRecaptcha(request); | ||
} | ||
|
||
async validateRecaptcha(request: Request): Promise<boolean> { | ||
const response = request.body.recaptchaValue; | ||
|
||
if (!response) { | ||
this.logger.log('Invalid body in recaptcha request'); | ||
return false; | ||
} | ||
|
||
const { data } = await this.httpService | ||
.post( | ||
`https://www.google.com/recaptcha/api/siteverify`, | ||
null, // Since we're sending data in the body, set it to null | ||
{ | ||
params: { | ||
secret: process.env.SECRET_KEY, | ||
response, | ||
}, | ||
}, | ||
) | ||
.toPromise(); | ||
return data.success; | ||
} | ||
} |