FEAT: auth module refactored

This commit is contained in:
Arthur Simas
2022-07-06 17:20:00 -03:00
parent 7e92ca2c36
commit 1cd191e970
10 changed files with 93 additions and 49 deletions
+158
View File
@@ -0,0 +1,158 @@
import { OnModuleInit, Inject, Injectable } from '@nestjs/common';
import { ClientGrpc } from '@nestjs/microservices';
import { DadosferaLogger } from 'dadosfera-logs';
import { ProtoServices } from 'protospack-v2/dist/lib/Duc';
import { AuthProtoService as AuthServiceInterface } from 'protospack-v2/dist/lib/Duc/interfaces/write-service';
import {
AuthGetPublicKeysResponse,
AuthSignInRequest,
AuthSignInResponse,
AuthRefreshAccessTokenRequest,
AuthRefreshAccessTokenResponse,
AuthEnableTotpMfaRequest,
AuthEnableTotpMfaResponse,
AuthDisableTotpMfaRequest,
AuthDisableTotpMfaResponse,
AuthDismissTotpMfaRequest,
AuthDismissTotpMfaResponse,
AuthVerifyTotpMfaRequest,
AuthVerifyTotpMfaResponse,
AuthChangePasswordRequest,
AuthChangePasswordResponse,
AuthResetPasswordRequest,
AuthResetPasswordResponse,
AuthVerifyResetPasswordCodeRequest,
AuthVerifyResetPasswordCodeResponse,
AuthConfirmResetPasswordRequest,
AuthConfirmResetPasswordResponse,
} from 'protospack-v2/dist/lib/Duc/interfaces/messages';
import grpcHandler from '../../utils/grpcHandler';
@Injectable()
export class AuthClientService implements OnModuleInit {
logger: any;
private authService: AuthServiceInterface;
constructor(
@Inject(DadosferaLogger)
dadosferaLogger: DadosferaLogger,
@Inject('DUC_PACKAGE') private readonly grpcClient: ClientGrpc,
) {
this.logger = dadosferaLogger.logger;
}
onModuleInit() {
this.authService = this.grpcClient.getService<AuthServiceInterface>(
ProtoServices.AuthProtoService,
);
}
async getPublicKeys() {
this.logger.info('GetPublicKeys');
return grpcHandler<AuthGetPublicKeysResponse>(
this.authService.AuthGetPublicKeys({}),
);
}
async signIn({ username, password, totp }: AuthSignInRequest) {
this.logger.info('SignIn');
return grpcHandler<AuthSignInResponse>(
this.authService.AuthSignIn({ username, password, totp }),
);
}
async refreshAccessToken({ refreshToken }: AuthRefreshAccessTokenRequest) {
this.logger.info('RefreshAccessToken');
return grpcHandler<AuthRefreshAccessTokenResponse>(
this.authService.AuthRefreshAccessToken({ refreshToken }),
);
}
async changePassword({
accessToken,
oldPassword,
newPassword,
}: AuthChangePasswordRequest) {
this.logger.info('ChangePassword');
return grpcHandler<AuthChangePasswordResponse>(
this.authService.AuthChangePassword({
accessToken,
oldPassword,
newPassword,
}),
);
}
async resetPassword({ username }: AuthResetPasswordRequest) {
this.logger.info('resetPassword');
return grpcHandler<AuthResetPasswordResponse>(
this.authService.AuthResetPassword({ username }),
);
}
async verifyResetPasswordCode({
username,
code,
}: AuthVerifyResetPasswordCodeRequest) {
this.logger.info('verifyResetPasswordCode');
return grpcHandler<AuthVerifyResetPasswordCodeResponse>(
this.authService.AuthVerifyResetPasswordCode({ username, code }),
);
}
async confirmResetPassword({
username,
code,
newPassword,
}: AuthConfirmResetPasswordRequest) {
this.logger.info('confirmResetPassword');
return grpcHandler<AuthConfirmResetPasswordResponse>(
this.authService.AuthConfirmResetPassword({
username,
code,
newPassword,
}),
);
}
async enableTotpMFA({ accessToken, password }: AuthEnableTotpMfaRequest) {
this.logger.info('enableTotpMFA');
return grpcHandler<AuthEnableTotpMfaResponse>(
this.authService.AuthEnableTotpMfa({ accessToken, password }),
);
}
async disableTotpMFA({ accessToken, password }: AuthDisableTotpMfaRequest) {
this.logger.info('disableTotpMFA');
return grpcHandler<AuthDisableTotpMfaResponse>(
this.authService.AuthDisableTotpMfa({ accessToken, password }),
);
}
async dismissTotpMFA({ accessToken }: AuthDismissTotpMfaRequest) {
this.logger.info('dismissTotpMFA');
return grpcHandler<AuthDismissTotpMfaResponse>(
this.authService.AuthDismissTotpMfa({ accessToken }),
);
}
async verifyTotp({ accessToken, totp }: AuthVerifyTotpMfaRequest) {
this.logger.info('disableTotpMFA');
return grpcHandler<AuthVerifyTotpMfaResponse>(
this.authService.AuthVerifyTotpMfa({ accessToken, totp }),
);
}
}