mirror of
https://github.com/dadosfera/maestro.git
synced 2026-09-09 01:14:49 +00:00
149 lines
3.3 KiB
TypeScript
149 lines
3.3 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Headers,
|
|
Post,
|
|
HttpCode,
|
|
HttpStatus,
|
|
UnauthorizedException,
|
|
} from '@nestjs/common';
|
|
import {
|
|
AuthSignInRequest,
|
|
AuthRefreshAccessTokenRequest,
|
|
AuthEnableTotpMfaRequest,
|
|
AuthDisableTotpMfaRequest,
|
|
AuthVerifyTotpMfaRequest,
|
|
} 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() { username, refreshToken }: AuthRefreshAccessTokenRequest,
|
|
) {
|
|
console.log(`/auth`, 'RefreshAccessToken');
|
|
|
|
const accessToken = await this.authClient
|
|
.refreshAccessToken({ username, 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;
|
|
}
|
|
}
|