2022-10-09 22:30:32 +02:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
ForbiddenException,
|
|
|
|
|
HttpCode,
|
2022-12-05 15:53:24 +01:00
|
|
|
Patch,
|
2022-10-09 22:30:32 +02:00
|
|
|
Post,
|
2022-12-05 15:53:24 +01:00
|
|
|
UseGuards,
|
2022-10-09 22:30:32 +02:00
|
|
|
} from "@nestjs/common";
|
2022-10-24 12:11:10 +02:00
|
|
|
import { Throttle } from "@nestjs/throttler";
|
2022-12-05 15:53:24 +01:00
|
|
|
import { User } from "@prisma/client";
|
2022-11-28 15:04:32 +01:00
|
|
|
import { ConfigService } from "src/config/config.service";
|
2022-10-09 22:30:32 +02:00
|
|
|
import { AuthService } from "./auth.service";
|
2022-12-26 12:43:36 +01:00
|
|
|
import { AuthTotpService } from "./authTotp.service";
|
2022-12-05 15:53:24 +01:00
|
|
|
import { GetUser } from "./decorator/getUser.decorator";
|
2022-10-09 22:30:32 +02:00
|
|
|
import { AuthRegisterDTO } from "./dto/authRegister.dto";
|
2022-10-10 17:58:42 +02:00
|
|
|
import { AuthSignInDTO } from "./dto/authSignIn.dto";
|
2022-12-21 11:58:37 -05:00
|
|
|
import { AuthSignInTotpDTO } from "./dto/authSignInTotp.dto";
|
|
|
|
|
import { EnableTotpDTO } from "./dto/enableTotp.dto";
|
2022-10-09 22:30:32 +02:00
|
|
|
import { RefreshAccessTokenDTO } from "./dto/refreshAccessToken.dto";
|
2022-12-05 15:53:24 +01:00
|
|
|
import { UpdatePasswordDTO } from "./dto/updatePassword.dto";
|
2022-12-21 11:58:37 -05:00
|
|
|
import { VerifyTotpDTO } from "./dto/verifyTotp.dto";
|
2022-12-05 15:53:24 +01:00
|
|
|
import { JwtGuard } from "./guard/jwt.guard";
|
2022-10-09 22:30:32 +02:00
|
|
|
|
|
|
|
|
@Controller("auth")
|
|
|
|
|
export class AuthController {
|
|
|
|
|
constructor(
|
|
|
|
|
private authService: AuthService,
|
2022-12-26 12:43:36 +01:00
|
|
|
private authTotpService: AuthTotpService,
|
2022-10-09 22:30:32 +02:00
|
|
|
private config: ConfigService
|
|
|
|
|
) {}
|
|
|
|
|
|
2022-10-24 12:11:10 +02:00
|
|
|
@Throttle(10, 5 * 60)
|
2022-10-09 22:30:32 +02:00
|
|
|
@Post("signUp")
|
2022-11-28 15:04:32 +01:00
|
|
|
async signUp(@Body() dto: AuthRegisterDTO) {
|
2022-12-05 16:53:52 +01:00
|
|
|
if (!this.config.get("ALLOW_REGISTRATION"))
|
2022-10-09 22:30:32 +02:00
|
|
|
throw new ForbiddenException("Registration is not allowed");
|
|
|
|
|
return this.authService.signUp(dto);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-24 12:11:10 +02:00
|
|
|
@Throttle(10, 5 * 60)
|
2022-10-09 22:30:32 +02:00
|
|
|
@Post("signIn")
|
2022-10-13 23:23:33 +02:00
|
|
|
@HttpCode(200)
|
2022-10-10 17:58:42 +02:00
|
|
|
signIn(@Body() dto: AuthSignInDTO) {
|
2022-10-09 22:30:32 +02:00
|
|
|
return this.authService.signIn(dto);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-21 11:58:37 -05:00
|
|
|
@Throttle(10, 5 * 60)
|
|
|
|
|
@Post("signIn/totp")
|
|
|
|
|
@HttpCode(200)
|
|
|
|
|
signInTotp(@Body() dto: AuthSignInTotpDTO) {
|
2022-12-26 12:43:36 +01:00
|
|
|
return this.authTotpService.signInTotp(dto);
|
2022-12-21 11:58:37 -05:00
|
|
|
}
|
|
|
|
|
|
2022-12-05 15:53:24 +01:00
|
|
|
@Patch("password")
|
|
|
|
|
@UseGuards(JwtGuard)
|
|
|
|
|
async updatePassword(@GetUser() user: User, @Body() dto: UpdatePasswordDTO) {
|
|
|
|
|
await this.authService.updatePassword(user, dto.oldPassword, dto.password);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-09 22:30:32 +02:00
|
|
|
@Post("token")
|
|
|
|
|
@HttpCode(200)
|
|
|
|
|
async refreshAccessToken(@Body() body: RefreshAccessTokenDTO) {
|
|
|
|
|
const accessToken = await this.authService.refreshAccessToken(
|
|
|
|
|
body.refreshToken
|
|
|
|
|
);
|
|
|
|
|
return { accessToken };
|
|
|
|
|
}
|
2022-12-21 11:58:37 -05:00
|
|
|
|
|
|
|
|
@Post("totp/enable")
|
|
|
|
|
@UseGuards(JwtGuard)
|
|
|
|
|
async enableTotp(@GetUser() user: User, @Body() body: EnableTotpDTO) {
|
2022-12-26 12:43:36 +01:00
|
|
|
return this.authTotpService.enableTotp(user, body.password);
|
2022-12-21 11:58:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post("totp/verify")
|
|
|
|
|
@UseGuards(JwtGuard)
|
|
|
|
|
async verifyTotp(@GetUser() user: User, @Body() body: VerifyTotpDTO) {
|
2022-12-26 12:43:36 +01:00
|
|
|
return this.authTotpService.verifyTotp(user, body.password, body.code);
|
2022-12-21 11:58:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post("totp/disable")
|
|
|
|
|
@UseGuards(JwtGuard)
|
|
|
|
|
async disableTotp(@GetUser() user: User, @Body() body: VerifyTotpDTO) {
|
|
|
|
|
// Note: We use VerifyTotpDTO here because it has both fields we need: password and totp code
|
2022-12-26 12:43:36 +01:00
|
|
|
return this.authTotpService.disableTotp(user, body.password, body.code);
|
2022-12-21 11:58:37 -05:00
|
|
|
}
|
2022-10-09 22:30:32 +02:00
|
|
|
}
|