Files
maestro/src/modules/auth/auth.controller.ts
T
2022-06-27 19:22:36 -03:00

186 lines
4.9 KiB
TypeScript

import {
Logger,
Body,
Controller,
Headers,
Post,
HttpCode,
HttpStatus,
OnApplicationBootstrap,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import {
AuthSignInRequest,
AuthRefreshAccessTokenRequest,
AuthChangePasswordRequest,
AuthResetPasswordRequest,
AuthVerifyResetPasswordCodeRequest,
AuthConfirmResetPasswordRequest,
AuthEnableTotpMfaRequest,
AuthDisableTotpMfaRequest,
AuthVerifyTotpMfaRequest,
} from '@victorradael/protospack-v2/dist/lib/Duc/interfaces/messages';
import { AuthClientService } from '../../clients/auth/client.service';
import { PermissionsClientService } from '../../clients/permissions/client.service';
import { Permissions } from '../../authentication/permissions.enum';
import ErrorBuilder from '../../utils/ErrorBuilder';
import { LoginDto } from './dtos/login';
@ApiTags('Auth')
@Controller('auth')
export class AuthController implements OnApplicationBootstrap {
private readonly logger = new Logger(AuthController.name);
constructor(
private authClient: AuthClientService,
private permissionsClient: PermissionsClientService,
) {}
// internally used to send permissions to duc on microservice startup
async onApplicationBootstrap() {
this.logger.log('sending permissions to DUC...');
const permissions = Object.values(Permissions).flatMap((namespace) =>
Object.values(namespace),
);
return this.permissionsClient
.injectPermissions({ permissions })
.catch((err: ErrorBuilder) => {
if (
err.code === 'No connection established' &&
process.env.LOCAL_ENV === 'true'
) {
return this.logger.log(
"couldn't connect to DUC. suppresing in local env",
);
}
throw err;
});
}
@Post('sign-in')
@HttpCode(HttpStatus.OK)
async signIn(@Body() { username, password, totp }: AuthSignInRequest) {
this.logger.log(`/auth`, 'SignIn');
return this.authClient.signIn({ username, password, totp });
}
@Post('refresh-access-token')
@HttpCode(HttpStatus.OK)
async refreshAccessToken(
@Body() { refreshToken }: AuthRefreshAccessTokenRequest,
) {
this.logger.log(`/auth`, 'RefreshAccessToken');
return this.authClient.refreshAccessToken({ refreshToken });
}
@Post('change-password')
@HttpCode(HttpStatus.OK)
async changePassword(
@Body() body: AuthChangePasswordRequest,
@Headers() headers,
) {
this.logger.log(`/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.log(`/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.log(`/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.log(`/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.log(`/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.log(`/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.log(`/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.log(`/auth`, 'enable-totp');
const { totp } = body;
const { authorization: accessToken } = headers;
return this.authClient.verifyTotp({ accessToken, totp });
}
}