mirror of
https://github.com/dadosfera/maestro.git
synced 2026-09-20 23:04:49 +00:00
228 lines
5.2 KiB
TypeScript
228 lines
5.2 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Headers,
|
|
Post,
|
|
HttpCode,
|
|
HttpStatus,
|
|
} from '@nestjs/common';
|
|
import {
|
|
AuthSignInRequest,
|
|
AuthRefreshAccessTokenRequest,
|
|
AuthEnableTotpMfaRequest,
|
|
AuthDisableTotpMfaRequest,
|
|
AuthVerifyTotpMfaRequest,
|
|
AuthChangePasswordRequest,
|
|
AuthResetPasswordRequest,
|
|
AuthVerifyResetPasswordCodeRequest,
|
|
AuthConfirmResetPasswordRequest,
|
|
} from '@victorradael/protospack';
|
|
|
|
import { AuthClientService } from 'src/clients/auth/client.service';
|
|
|
|
import ErrorBuilder from '../../utils/ErrorBuilder';
|
|
|
|
@Controller('auth')
|
|
export class AuthController {
|
|
constructor(private authClient: AuthClientService) {}
|
|
|
|
// DEPRECADO!
|
|
@Post()
|
|
@HttpCode(HttpStatus.OK)
|
|
async signIn_old(@Body() body: AuthSignInRequest) {
|
|
console.log(`/auth`, 'SignIn_old');
|
|
|
|
return this.signIn(body);
|
|
}
|
|
|
|
@Post('login')
|
|
@HttpCode(HttpStatus.OK)
|
|
async signIn_login(@Body() body: AuthSignInRequest) {
|
|
console.log(`/auth`, 'SignIn_login');
|
|
|
|
return this.signIn(body);
|
|
}
|
|
|
|
@Post('sign-in')
|
|
@HttpCode(HttpStatus.OK)
|
|
async signIn(@Body() { username, password, totp }: AuthSignInRequest) {
|
|
console.log(`/auth`, 'SignIn');
|
|
|
|
const tokens = await this.authClient
|
|
.signIn({ username, password, totp })
|
|
.catch((err) => {
|
|
throw ErrorBuilder(err);
|
|
});
|
|
|
|
return tokens;
|
|
}
|
|
|
|
@Post('refresh-access-token')
|
|
@HttpCode(HttpStatus.OK)
|
|
async refreshAccessToken(
|
|
@Body() { refreshToken }: AuthRefreshAccessTokenRequest,
|
|
) {
|
|
console.log(`/auth`, 'RefreshAccessToken');
|
|
|
|
const accessToken = await this.authClient
|
|
.refreshAccessToken({ refreshToken })
|
|
.catch((err) => {
|
|
throw ErrorBuilder(err);
|
|
});
|
|
|
|
return accessToken;
|
|
}
|
|
|
|
@Post('enable-totp')
|
|
@HttpCode(HttpStatus.OK)
|
|
async enableTotpMFA(
|
|
@Body() body: AuthEnableTotpMfaRequest,
|
|
@Headers() headers,
|
|
) {
|
|
console.log(`/auth`, 'enable-totp');
|
|
|
|
const { password } = body;
|
|
const { authorization: accessToken } = headers;
|
|
|
|
const response = await this.authClient
|
|
.enableTotpMFA({ accessToken, password })
|
|
.catch((err) => {
|
|
throw ErrorBuilder(err);
|
|
});
|
|
|
|
return response;
|
|
}
|
|
|
|
@Post('disable-totp')
|
|
@HttpCode(HttpStatus.OK)
|
|
async disableTotpMFA(
|
|
@Body() body: AuthDisableTotpMfaRequest,
|
|
@Headers() headers,
|
|
) {
|
|
console.log(`/auth`, 'disable-totp');
|
|
|
|
const { password } = body;
|
|
const { authorization: accessToken } = headers;
|
|
|
|
const response = await this.authClient
|
|
.disableTotpMFA({ accessToken, password })
|
|
.catch((err) => {
|
|
throw ErrorBuilder(err);
|
|
});
|
|
|
|
return response;
|
|
}
|
|
|
|
@Post('dismiss-totp')
|
|
@HttpCode(HttpStatus.OK)
|
|
async dismissTotpMFA(@Headers() headers) {
|
|
console.log(`/auth`, 'disable-totp');
|
|
|
|
const { authorization: accessToken } = headers;
|
|
|
|
const response = await this.authClient
|
|
.dismissTotpMFA({ accessToken })
|
|
.catch((err) => {
|
|
throw ErrorBuilder(err);
|
|
});
|
|
|
|
return response;
|
|
}
|
|
|
|
@Post('verify-totp')
|
|
@HttpCode(HttpStatus.OK)
|
|
async verifyTotp(
|
|
@Body() body: AuthVerifyTotpMfaRequest,
|
|
@Headers() headers,
|
|
): Promise<any> {
|
|
console.log(`/auth`, 'enable-totp');
|
|
|
|
const { totp } = body;
|
|
const { authorization: accessToken } = headers;
|
|
|
|
const response = await this.authClient
|
|
.verifyTotp({ accessToken, totp })
|
|
.catch((err) => {
|
|
throw ErrorBuilder(err);
|
|
});
|
|
|
|
return response;
|
|
}
|
|
|
|
@Post('change-password')
|
|
@HttpCode(HttpStatus.OK)
|
|
async changePassword(
|
|
@Body() body: AuthChangePasswordRequest,
|
|
@Headers() headers,
|
|
): Promise<any> {
|
|
console.log(`/auth`, 'change-password');
|
|
|
|
const { oldPassword, newPassword } = body;
|
|
const { authorization: accessToken } = headers;
|
|
|
|
const response = await this.authClient
|
|
.changePassword({
|
|
accessToken,
|
|
oldPassword,
|
|
newPassword,
|
|
})
|
|
.catch((err) => {
|
|
throw ErrorBuilder(err);
|
|
});
|
|
|
|
return response;
|
|
}
|
|
|
|
@Post('reset-password')
|
|
@HttpCode(HttpStatus.OK)
|
|
async resetPassword(@Body() body: AuthResetPasswordRequest): Promise<any> {
|
|
console.log(`/auth`, 'reset-password');
|
|
|
|
const { username } = body;
|
|
|
|
const response = await this.authClient
|
|
.resetPassword({ username })
|
|
.catch((err) => {
|
|
throw ErrorBuilder(err);
|
|
});
|
|
|
|
return response;
|
|
}
|
|
|
|
@Post('verify-reset-password-code')
|
|
@HttpCode(HttpStatus.OK)
|
|
async verifyResetPasswordCode(
|
|
@Body() body: AuthVerifyResetPasswordCodeRequest,
|
|
): Promise<any> {
|
|
console.log(`/auth`, 'verify-reset-password-code');
|
|
|
|
const { username, code } = body;
|
|
|
|
const response = await this.authClient
|
|
.verifyResetPasswordCode({ username, code })
|
|
.catch((err) => {
|
|
throw ErrorBuilder(err);
|
|
});
|
|
|
|
return response;
|
|
}
|
|
|
|
@Post('confirm-reset-password')
|
|
@HttpCode(HttpStatus.OK)
|
|
async confirmResetPassword(
|
|
@Body() body: AuthConfirmResetPasswordRequest,
|
|
): Promise<any> {
|
|
console.log(`/auth`, 'confirm-reset-password');
|
|
|
|
const { username, code, newPassword } = body;
|
|
|
|
const response = await this.authClient
|
|
.confirmResetPassword({ username, code, newPassword })
|
|
.catch((err) => {
|
|
throw ErrorBuilder(err);
|
|
});
|
|
|
|
return response;
|
|
}
|
|
}
|