import { Body, Controller, Headers, Post, HttpCode, HttpStatus, Inject, UseFilters, Query, } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; import { AuthSignInRequest, AuthRefreshAccessTokenRequest, AuthChangePasswordRequest, AuthResetPasswordRequest, AuthVerifyResetPasswordCodeRequest, AuthConfirmResetPasswordRequest, AuthEnableTotpMfaRequest, AuthDisableTotpMfaRequest, AuthVerifyTotpMfaRequest, } from '@dadosfera/protospack-v2/dist/lib/Duc/interfaces/messages'; import { PERMISSIONS } from 'src/authentication/permissions.enum'; import { Authenticated, RequireAllPermissions, } from 'src/authentication/authentication.decorator'; import { AuthClientService } from './auth.service'; import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; import { GrpcToHttpExceptionFilter } from '../../error/grpc-to-http-exception.filter'; import { RequestUser, User } from 'src/authentication/user.decorator'; @ApiTags('Auth') @UseFilters(new GrpcToHttpExceptionFilter()) @Controller('auth') export class AuthController { logger: DadosferaLogger; constructor( @Inject(DadosferaLogger) dadosferaLogger: DadosferaLogger, private authClient: AuthClientService, ) { this.logger = dadosferaLogger.logger; } @Post('/sso/snowflake') @RequireAllPermissions(PERMISSIONS.SNOWFLAKE.permissions.OPEN) @HttpCode(HttpStatus.OK) async snowflakeSignIn( @User() user: RequestUser, @Body('RelayState') relayState: string, ) { this.logger.info('/auth - snowflakeSignIn'); return this.authClient.snowflakeSignIn({ userId: user.user_id, relayState, }); } @Post('sign-in') @HttpCode(HttpStatus.OK) async signIn(@Body() { username, password, totp }: AuthSignInRequest) { this.logger.info('/auth - SignIn'); return this.authClient.signIn({ username, password, totp }); } @Post('refresh-access-token') @HttpCode(HttpStatus.OK) async refreshAccessToken( @Body() { refreshToken }: AuthRefreshAccessTokenRequest, ) { this.logger.info('/auth - RefreshAccessToken'); return this.authClient.refreshAccessToken({ refreshToken }); } @Post('change-password') @HttpCode(HttpStatus.OK) async changePassword( @Body() body: AuthChangePasswordRequest, @Headers() headers, ) { this.logger.info('/auth - change-password'); const { oldPassword, newPassword } = body; const { authorization: accessToken } = headers; return this.authClient.changePassword({ accessToken, oldPassword, newPassword, }); } @Post('reset-password') @HttpCode(HttpStatus.OK) async resetPassword(@Body() body: AuthResetPasswordRequest) { this.logger.info('/auth - reset-password'); const { username } = body; return this.authClient.resetPassword({ username }); } @Post('verify-reset-password-code') @HttpCode(HttpStatus.OK) async verifyResetPasswordCode( @Body() body: AuthVerifyResetPasswordCodeRequest, ) { this.logger.info('/auth - verify-reset-password-code'); const { username, code } = body; return this.authClient.verifyResetPasswordCode({ username, code }); } @Post('confirm-reset-password') @HttpCode(HttpStatus.OK) async confirmResetPassword(@Body() body: AuthConfirmResetPasswordRequest) { this.logger.info('/auth - confirm-reset-password'); const { username, code, newPassword } = body; return this.authClient.confirmResetPassword({ username, code, newPassword, }); } @Post('enable-totp') @HttpCode(HttpStatus.OK) async enableTotpMFA( @Body() body: AuthEnableTotpMfaRequest, @Headers() headers, ) { this.logger.info('/auth - enable-totp'); const { password } = body; const { authorization: accessToken } = headers; return this.authClient.enableTotpMFA({ accessToken, password }); } @Post('disable-totp') @HttpCode(HttpStatus.OK) async disableTotpMFA( @Body() body: AuthDisableTotpMfaRequest, @Headers() headers, ) { this.logger.info('/auth - disable-totp'); const { password } = body; const { authorization: accessToken } = headers; return this.authClient.disableTotpMFA({ accessToken, password }); } @Post('dismiss-totp') @HttpCode(HttpStatus.OK) async dismissTotpMFA(@Headers() headers) { this.logger.info('/auth - disable-totp'); const { authorization: accessToken } = headers; return this.authClient.dismissTotpMFA({ accessToken }); } @Post('verify-totp') @HttpCode(HttpStatus.OK) async verifyTotp(@Body() body: AuthVerifyTotpMfaRequest, @Headers() headers) { this.logger.info('/auth - enable-totp'); const { totp } = body; const { authorization: accessToken } = headers; return this.authClient.verifyTotp({ accessToken, totp }); } }