Spaces:
Runtime error
Runtime error
| import { | |
| Controller, | |
| Post, | |
| Body, | |
| Req, | |
| UseGuards, | |
| Get, | |
| HttpCode, | |
| HttpStatus, | |
| } from '@nestjs/common'; | |
| import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger'; | |
| import type { Request } from 'express'; | |
| import { TwoFactorAuthService } from './two-factor-auth.service'; | |
| import { SuperAdminAuthGuard } from '../auth/guards/super-admin-auth.guard'; | |
| import { | |
| EnableEmail2FADto, | |
| SendEmail2FACodeDto, | |
| VerifyEmail2FADto, | |
| } from './dto/email-2fa.dto'; | |
| import { | |
| InitTotp2FADto, | |
| VerifyTotpSetupDto, | |
| VerifyTotp2FADto, | |
| } from './dto/totp-2fa.dto'; | |
| import { | |
| VerifyRecoveryCodeDto, | |
| RegenerateRecoveryCodesDto, | |
| } from './dto/recovery-code.dto'; | |
| import { LogToDiscord } from 'src/shared/decorators/log-to-discord.decorator'; | |
| ('Super Admin - Two Factor Authentication') | |
| ('super-admin/2fa') | |
| () | |
| export class TwoFactorAuthController { | |
| constructor(private readonly twoFactorAuthService: TwoFactorAuthService) {} | |
| // ==================== EMAIL 2FA ==================== | |
| ('email/enable') | |
| (SuperAdminAuthGuard) | |
| () | |
| ({ summary: 'Enable Email 2FA' }) | |
| async enableEmail2FA(() dto: EnableEmail2FADto) { | |
| return this.twoFactorAuthService.enableEmail2FA(dto.email); | |
| } | |
| ('email/send-code') | |
| (HttpStatus.OK) | |
| ({ summary: 'Send Email 2FA Code' }) | |
| async sendEmail2FACode( | |
| () dto: SendEmail2FACodeDto, | |
| () request: Request, | |
| ) { | |
| return this.twoFactorAuthService.sendEmail2FACode( | |
| dto.pending2faToken, | |
| request, | |
| ); | |
| } | |
| ('email/verify') | |
| (HttpStatus.OK) | |
| ({ summary: 'Verify Email 2FA Code' }) | |
| async verifyEmail2FACode( | |
| () dto: VerifyEmail2FADto, | |
| () request: Request, | |
| ) { | |
| return this.twoFactorAuthService.verifyEmail2FACode( | |
| dto.pending2faToken, | |
| dto.code, | |
| dto.trustDevice || false, | |
| request, | |
| ); | |
| } | |
| // ==================== TOTP/AUTHENTICATOR 2FA ==================== | |
| ('totp/init') | |
| (SuperAdminAuthGuard) | |
| () | |
| ({ summary: 'Initialize TOTP Setup' }) | |
| async initTotp2FA(() dto: InitTotp2FADto) { | |
| return this.twoFactorAuthService.initTotp2FA(dto.email); | |
| } | |
| ('totp/verify-setup') | |
| (SuperAdminAuthGuard) | |
| () | |
| ({ summary: 'Verify TOTP Setup' }) | |
| async verifyTotpSetup(() dto: VerifyTotpSetupDto, () request: any) { | |
| return this.twoFactorAuthService.verifyTotpSetup( | |
| request.user.email, | |
| dto.code, | |
| ); | |
| } | |
| ('totp/verify') | |
| (HttpStatus.OK) | |
| ({ summary: 'Verify TOTP Code' }) | |
| async verifyTotp2FACode( | |
| () dto: VerifyTotp2FADto, | |
| () request: Request, | |
| ) { | |
| return this.twoFactorAuthService.verifyTotp2FACode( | |
| dto.pending2faToken, | |
| dto.code, | |
| dto.trustDevice || false, | |
| request, | |
| ); | |
| } | |
| // ==================== RECOVERY CODES ==================== | |
| ('recovery/verify') | |
| (HttpStatus.OK) | |
| ({ summary: 'Verify Recovery Code' }) | |
| async verifyRecoveryCode( | |
| () dto: VerifyRecoveryCodeDto, | |
| () request: Request, | |
| ) { | |
| return this.twoFactorAuthService.verifyRecoveryCode( | |
| dto.pending2faToken, | |
| dto.code, | |
| dto.trustDevice || false, | |
| request, | |
| ); | |
| } | |
| ('recovery/regenerate') | |
| (SuperAdminAuthGuard) | |
| () | |
| ({ summary: 'Regenerate Recovery Codes' }) | |
| async regenerateRecoveryCodes(() dto: RegenerateRecoveryCodesDto) { | |
| return this.twoFactorAuthService.regenerateRecoveryCodes(dto.email); | |
| } | |
| // ==================== INFO/STATUS ==================== | |
| ('status') | |
| (SuperAdminAuthGuard) | |
| () | |
| ({ summary: 'Get 2FA Status' }) | |
| async get2FAStatus(() request: any) { | |
| return this.twoFactorAuthService.get2FAStatus(request.user.email); | |
| } | |
| } | |