FEAT: squash - auth guard

nestjs access controll mock

swagger implementation

middleware changes and lib

permissions file back to maestro

FEAT: sending permissions to duc on startup


FEAT: new permissions.enum format


FEAT: permissions injection (maestro -> duc)


FIX: changing method invocation order


FEAT: refactored permission enum + duc permission injector + grpcHandler


FEAT: auth guards


FEAT: seq ids updated
This commit is contained in:
Arthur Simas
2022-06-27 18:32:08 -03:00
committed by arthur simas
parent 89b571d7f8
commit 1566efc490
23 changed files with 908 additions and 707 deletions
+103 -146
View File
@@ -1,60 +1,72 @@
import {
Logger,
Body,
Controller,
Headers,
Post,
HttpCode,
HttpStatus,
OnApplicationBootstrap,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import {
AuthSignInRequest,
AuthRefreshAccessTokenRequest,
AuthEnableTotpMfaRequest,
AuthDisableTotpMfaRequest,
AuthVerifyTotpMfaRequest,
AuthChangePasswordRequest,
AuthResetPasswordRequest,
AuthVerifyResetPasswordCodeRequest,
AuthConfirmResetPasswordRequest,
} from '@victorradael/protospack';
AuthEnableTotpMfaRequest,
AuthDisableTotpMfaRequest,
AuthVerifyTotpMfaRequest,
} from '@victorradael/protospack-v2/dist/lib/Duc/interfaces/messages';
import { AuthClientService } from 'src/clients/auth/client.service';
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';
@ApiTags('Auth')
@Controller('auth')
export class AuthController {
constructor(private authClient: AuthClientService) {}
export class AuthController implements OnApplicationBootstrap {
private readonly logger = new Logger(AuthController.name);
// DEPRECADO!
@Post()
@HttpCode(HttpStatus.OK)
async signIn_old(@Body() body: AuthSignInRequest) {
console.log(`/auth`, 'SignIn_old');
constructor(
private authClient: AuthClientService,
private permissionsClient: PermissionsClientService,
) {}
return this.signIn(body);
}
// internally used to send permissions to duc on microservice startup
async onApplicationBootstrap() {
this.logger.log('sending permissions to DUC...');
@Post('login')
@HttpCode(HttpStatus.OK)
async signIn_login(@Body() body: AuthSignInRequest) {
console.log(`/auth`, 'SignIn_login');
const permissions = Object.values(Permissions).flatMap((namespace) =>
Object.values(namespace),
);
return this.signIn(body);
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) {
console.log(`/auth`, 'SignIn');
this.logger.log(`/auth`, 'SignIn');
const tokens = await this.authClient
.signIn({ username, password, totp })
.catch((err) => {
throw ErrorBuilder(err);
});
return tokens;
return this.authClient.signIn({ username, password, totp });
}
@Post('refresh-access-token')
@@ -62,15 +74,63 @@ export class AuthController {
async refreshAccessToken(
@Body() { refreshToken }: AuthRefreshAccessTokenRequest,
) {
console.log(`/auth`, 'RefreshAccessToken');
this.logger.log(`/auth`, 'RefreshAccessToken');
const accessToken = await this.authClient
.refreshAccessToken({ refreshToken })
.catch((err) => {
throw ErrorBuilder(err);
});
return this.authClient.refreshAccessToken({ refreshToken });
}
return accessToken;
@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')
@@ -79,18 +139,12 @@ export class AuthController {
@Body() body: AuthEnableTotpMfaRequest,
@Headers() headers,
) {
console.log(`/auth`, 'enable-totp');
this.logger.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;
return this.authClient.enableTotpMFA({ accessToken, password });
}
@Post('disable-totp')
@@ -99,129 +153,32 @@ export class AuthController {
@Body() body: AuthDisableTotpMfaRequest,
@Headers() headers,
) {
console.log(`/auth`, 'disable-totp');
this.logger.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;
return this.authClient.disableTotpMFA({ accessToken, password });
}
@Post('dismiss-totp')
@HttpCode(HttpStatus.OK)
async dismissTotpMFA(@Headers() headers) {
console.log(`/auth`, 'disable-totp');
this.logger.log(`/auth`, 'disable-totp');
const { authorization: accessToken } = headers;
const response = await this.authClient
.dismissTotpMFA({ accessToken })
.catch((err) => {
throw ErrorBuilder(err);
});
return response;
return this.authClient.dismissTotpMFA({ accessToken });
}
@Post('verify-totp')
@HttpCode(HttpStatus.OK)
async verifyTotp(
@Body() body: AuthVerifyTotpMfaRequest,
@Headers() headers,
): Promise<any> {
console.log(`/auth`, 'enable-totp');
async verifyTotp(@Body() body: AuthVerifyTotpMfaRequest, @Headers() headers) {
this.logger.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;
return this.authClient.verifyTotp({ accessToken, totp });
}
}