Files
maestro/src/modules/auth/auth.controller.ts
T

101 lines
2.4 KiB
TypeScript

import {
Body,
Controller,
Headers,
Post,
UnauthorizedException,
} from '@nestjs/common';
import {
AuthEnableTotpMfaRequest,
AuthVerifyTotpMfaRequest,
} from '@victorradael/protospack';
import { AuthClientService } from 'src/clients/auth/client.service';
import { ISignIn, IRefreshAccessToken } from 'src/clients/auth/interfaces';
@Controller('auth')
export class AuthController {
constructor(private authClient: AuthClientService) {}
// DEPRECADO!
@Post()
async signIn_old(@Body() body: ISignIn) {
console.log(`/auth`, 'SignIn_old');
return this.signIn(body);
}
@Post('login')
async signIn_login(@Body() body: ISignIn) {
console.log(`/auth`, 'SignIn_login');
return this.signIn(body);
}
@Post('sign-in')
async signIn(@Body() { username, password, totp }: ISignIn) {
console.log(`/auth`, 'SignIn');
const tokens = await this.authClient
.signIn({ username, password, totp })
.then((result) => result)
.catch((err) => {
throw new UnauthorizedException(err.message);
});
return tokens;
}
@Post('refresh-access-token')
async refreshAccessToken(
@Body() { username, refreshToken }: IRefreshAccessToken,
) {
console.log(`/auth`, 'RefreshAccessToken');
const accessToken = await this.authClient
.refreshAccessToken({ username, refreshToken })
.then((result) => result)
.catch((err) => {
throw new UnauthorizedException(err.message);
});
return accessToken;
}
@Post('enable-totp')
async enableTotpMFA(
@Body() { username, password }: AuthEnableTotpMfaRequest,
) {
console.log(`/auth`, 'enable-totp');
const enableTotpResponse = await this.authClient
.enableTotpMFA({ username, password })
.then((result) => result)
.catch((err) => {
throw new UnauthorizedException(err.message);
});
return enableTotpResponse;
}
@Post('verify-totp')
async verifyTotp(
@Body() body: AuthVerifyTotpMfaRequest,
@Headers() headers,
): Promise<any> {
console.log(`/auth`, 'enable-totp');
const { username, totp } = body;
const { Authorization } = headers;
const enableTotpResponse = await this.authClient
.verifyTotp({ username, totp, accessToken: Authorization })
.then((result) => result)
.catch((err) => {
throw new UnauthorizedException(err.message);
});
return enableTotpResponse;
}
}