hcaptcha
This commit is contained in:
parent
30bc761f3b
commit
ebd6e8a608
2307
package-lock.json
generated
2307
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -32,12 +32,12 @@
|
|||||||
"@nestjs/swagger": "^6.0.5",
|
"@nestjs/swagger": "^6.0.5",
|
||||||
"@nestjs/typeorm": "^9.0.1",
|
"@nestjs/typeorm": "^9.0.1",
|
||||||
"@nestjs/websockets": "^9.0.11",
|
"@nestjs/websockets": "^9.0.11",
|
||||||
"@nestlab/google-recaptcha": "^3.1.2",
|
|
||||||
"bcrypt": "^5.0.1",
|
"bcrypt": "^5.0.1",
|
||||||
"class-transformer": "^0.5.1",
|
"class-transformer": "^0.5.1",
|
||||||
"class-validator": "^0.13.2",
|
"class-validator": "^0.13.2",
|
||||||
"csvtojson": "^2.0.10",
|
"csvtojson": "^2.0.10",
|
||||||
"dotenv": "^16.0.3",
|
"dotenv": "^16.0.3",
|
||||||
|
"hcaptcha": "^0.1.1",
|
||||||
"moment": "^2.29.4",
|
"moment": "^2.29.4",
|
||||||
"mysql2": "^2.3.3",
|
"mysql2": "^2.3.3",
|
||||||
"nodemailer": "^6.7.8",
|
"nodemailer": "^6.7.8",
|
||||||
|
@ -1,10 +1,6 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
import {
|
|
||||||
GoogleRecaptchaModule,
|
|
||||||
GoogleRecaptchaNetwork,
|
|
||||||
} from '@nestlab/google-recaptcha';
|
|
||||||
|
|
||||||
import { AuthModule } from './auth/auth.module';
|
import { AuthModule } from './auth/auth.module';
|
||||||
import { BcryptModule } from './bcrypt/bcrypt.module';
|
import { BcryptModule } from './bcrypt/bcrypt.module';
|
||||||
@ -99,17 +95,6 @@ import { ModuloMotivoModule } from './modulo-motivo/modulo-motivo.module';
|
|||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
ConfigModule.forRoot({ isGlobal: true }),
|
ConfigModule.forRoot({ isGlobal: true }),
|
||||||
GoogleRecaptchaModule.forRootAsync({
|
|
||||||
inject: [ConfigService],
|
|
||||||
useFactory: (configService: ConfigService) => {
|
|
||||||
return {
|
|
||||||
secretKey: configService.get<string>('GOOGLE_RECAPTCHA_KEY'),
|
|
||||||
response: (req) => req.headers.recaptcha,
|
|
||||||
skipIf: configService.get<string>('STATE') !== 'produccion',
|
|
||||||
network: GoogleRecaptchaNetwork.Recaptcha,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
TypeOrmModule.forRootAsync({
|
TypeOrmModule.forRootAsync({
|
||||||
inject: [ConfigService],
|
inject: [ConfigService],
|
||||||
useFactory: (configService: ConfigService) => {
|
useFactory: (configService: ConfigService) => {
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
|
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
|
||||||
import { AuthGuard } from '@nestjs/passport';
|
import { AuthGuard } from '@nestjs/passport';
|
||||||
import { ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
|
import { ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||||
import { Recaptcha } from '@nestlab/google-recaptcha';
|
|
||||||
import { Serealize } from '../interceptors/serialize.interceptor';
|
import { Serealize } from '../interceptors/serialize.interceptor';
|
||||||
|
import { HcaptchaGuard } from '../guards/hcaptcha.guard';
|
||||||
import { AuthService } from './auth.service';
|
import { AuthService } from './auth.service';
|
||||||
import { LoginAdminDto } from './dto/input/login-admin.dto';
|
import { LoginAdminDto } from './dto/input/login-admin.dto';
|
||||||
import { LoginOperadorDto } from './dto/input/login-operador.dto';
|
import { LoginOperadorDto } from './dto/input/login-operador.dto';
|
||||||
@ -15,7 +15,7 @@ export class AuthController {
|
|||||||
constructor(private authService: AuthService) {}
|
constructor(private authService: AuthService) {}
|
||||||
|
|
||||||
@Serealize(AuthTokenOutputDto)
|
@Serealize(AuthTokenOutputDto)
|
||||||
@Recaptcha()
|
@UseGuards(HcaptchaGuard)
|
||||||
@Post('login-admin')
|
@Post('login-admin')
|
||||||
@ApiOperation({ description: 'Login del admin.' })
|
@ApiOperation({ description: 'Login del admin.' })
|
||||||
@ApiBody({
|
@ApiBody({
|
||||||
@ -27,7 +27,7 @@ export class AuthController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Serealize(AuthTokenOutputDto)
|
@Serealize(AuthTokenOutputDto)
|
||||||
@Recaptcha()
|
@UseGuards(HcaptchaGuard)
|
||||||
@Post('login-operador')
|
@Post('login-operador')
|
||||||
@ApiOperation({ description: 'Login del operador.' })
|
@ApiOperation({ description: 'Login del operador.' })
|
||||||
@ApiBody({
|
@ApiBody({
|
||||||
@ -37,6 +37,7 @@ export class AuthController {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
loginOperador(@Body() body: LoginOperadorDto) {
|
loginOperador(@Body() body: LoginOperadorDto) {
|
||||||
|
console.log('Hola');
|
||||||
return this.authService.loginOperador(
|
return this.authService.loginOperador(
|
||||||
body.id_modulo,
|
body.id_modulo,
|
||||||
body.operador,
|
body.operador,
|
||||||
@ -45,7 +46,7 @@ export class AuthController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Serealize(AuthTokenOutputDto)
|
@Serealize(AuthTokenOutputDto)
|
||||||
@Recaptcha()
|
@UseGuards(HcaptchaGuard)
|
||||||
@Post('login-usuario')
|
@Post('login-usuario')
|
||||||
@ApiOperation({ description: 'Login del usuario.' })
|
@ApiOperation({ description: 'Login del usuario.' })
|
||||||
@ApiBody({
|
@ApiBody({
|
||||||
|
37
src/guards/hcaptcha.guard.ts
Normal file
37
src/guards/hcaptcha.guard.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
const { verify } = require('hcaptcha');
|
||||||
|
import {
|
||||||
|
BadRequestException,
|
||||||
|
CanActivate,
|
||||||
|
ExecutionContext,
|
||||||
|
ForbiddenException,
|
||||||
|
Injectable,
|
||||||
|
InternalServerErrorException,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { ConfigService } from '@nestjs/config';
|
||||||
|
import { Observable } from 'rxjs';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class HcaptchaGuard implements CanActivate {
|
||||||
|
constructor(private configService: ConfigService) {}
|
||||||
|
|
||||||
|
canActivate(
|
||||||
|
context: ExecutionContext,
|
||||||
|
): boolean | Promise<boolean> | Observable<boolean> {
|
||||||
|
const secret = this.configService.get<string>('HCAPTCHA_KEY');
|
||||||
|
const token = context.switchToHttp().getRequest().headers.hcaptcha;
|
||||||
|
|
||||||
|
// if (this.configService.get<string>('STATE') !== 'produccion') return true;
|
||||||
|
if (!token)
|
||||||
|
throw new BadRequestException('No se mando un token de hcaptcha.');
|
||||||
|
return verify(secret, token)
|
||||||
|
.then((data) => {
|
||||||
|
if (data.success === true) return true;
|
||||||
|
else throw new ForbiddenException('El token de hcaptcha no es válido.');
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
throw new InternalServerErrorException(
|
||||||
|
'Ocurrio un error con el hcaptcha.',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -21,6 +21,7 @@ async function bootstrap() {
|
|||||||
app.useStaticAssets(join(__dirname, '..', 'public'), {
|
app.useStaticAssets(join(__dirname, '..', 'public'), {
|
||||||
prefix: '/public/',
|
prefix: '/public/',
|
||||||
});
|
});
|
||||||
|
console.log(process.env.HCAPTCHA_KEY);
|
||||||
await app.listen(process.env.API_PORT);
|
await app.listen(process.env.API_PORT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,8 +16,8 @@ import {
|
|||||||
ApiQuery,
|
ApiQuery,
|
||||||
ApiTags,
|
ApiTags,
|
||||||
} from '@nestjs/swagger';
|
} from '@nestjs/swagger';
|
||||||
import { Recaptcha } from '@nestlab/google-recaptcha';
|
|
||||||
import { Serealize } from '../interceptors/serialize.interceptor';
|
import { Serealize } from '../interceptors/serialize.interceptor';
|
||||||
|
import { HcaptchaGuard } from '../guards/hcaptcha.guard';
|
||||||
import { UsuarioService } from './usuario.service';
|
import { UsuarioService } from './usuario.service';
|
||||||
import { ValidarUsuarioService } from '../validar-usuario/validar-usuario.service';
|
import { ValidarUsuarioService } from '../validar-usuario/validar-usuario.service';
|
||||||
import { Operador } from '../operador/entity/operador.entity';
|
import { Operador } from '../operador/entity/operador.entity';
|
||||||
@ -39,7 +39,7 @@ export class UsuarioController {
|
|||||||
|
|
||||||
@Serealize(MessageOutputDto)
|
@Serealize(MessageOutputDto)
|
||||||
@Post('registrar')
|
@Post('registrar')
|
||||||
@Recaptcha()
|
@UseGuards(HcaptchaGuard)
|
||||||
@ApiOperation({ description: 'Registro de usuario.' })
|
@ApiOperation({ description: 'Registro de usuario.' })
|
||||||
@ApiBody({
|
@ApiBody({
|
||||||
description: 'Variables que necesita el endpoint.',
|
description: 'Variables que necesita el endpoint.',
|
||||||
|
Loading…
Reference in New Issue
Block a user