mirror of
https://github.com/dadosfera/maestro.git
synced 2026-10-04 14:19:07 +00:00
219 lines
5.2 KiB
TypeScript
219 lines
5.2 KiB
TypeScript
import { OnModuleInit, Inject } from '@nestjs/common';
|
|
import { ClientGrpc } from '@nestjs/microservices';
|
|
|
|
import {
|
|
AuthConfirmationRequest,
|
|
AuthDisableTotpMfaRequest,
|
|
AuthEnableTotpMfaRequest,
|
|
AuthResendConfirmationCodeRequest,
|
|
AuthServiceInterface,
|
|
AuthVerifyTotpMfaRequest,
|
|
DucServicesNames,
|
|
} from '@victorradael/protospack';
|
|
|
|
import { ISignIn, IRefreshAccessToken } from './interfaces';
|
|
|
|
export class AuthClientService implements OnModuleInit {
|
|
private authService: AuthServiceInterface;
|
|
constructor(
|
|
@Inject('AUTH_PACKAGE') private readonly grpcClient: ClientGrpc,
|
|
) {}
|
|
|
|
onModuleInit() {
|
|
this.authService = this.grpcClient.getService<AuthServiceInterface>(
|
|
DucServicesNames.AuthProtoService,
|
|
);
|
|
}
|
|
|
|
async signIn({ username, password, totp }: ISignIn): Promise<any> {
|
|
console.log('AuthClientService', 'SignIn');
|
|
|
|
const tokens = await new Promise((resolve, reject) => {
|
|
this.authService.signIn({ username, password, totp }).subscribe({
|
|
next(x) {
|
|
resolve(x);
|
|
},
|
|
error(err) {
|
|
console.log('Observable Error');
|
|
reject(err);
|
|
},
|
|
complete() {
|
|
console.log('done');
|
|
},
|
|
});
|
|
})
|
|
.then((res) => res)
|
|
.catch((err) => {
|
|
throw new Error(err);
|
|
});
|
|
return tokens;
|
|
}
|
|
|
|
async enableTotpMFA({
|
|
username,
|
|
password,
|
|
}: AuthEnableTotpMfaRequest): Promise<any> {
|
|
console.log('AuthClientService', 'enableTotpMFA');
|
|
|
|
const tokens = await new Promise((resolve, reject) => {
|
|
this.authService.enableTotpMfa({ username, password }).subscribe({
|
|
next(x) {
|
|
resolve(x);
|
|
},
|
|
error(err) {
|
|
console.log('Observable Error');
|
|
reject(err);
|
|
},
|
|
complete() {
|
|
console.log('done');
|
|
},
|
|
});
|
|
})
|
|
.then((res) => res)
|
|
.catch((err) => {
|
|
throw new Error(err);
|
|
});
|
|
return tokens;
|
|
}
|
|
|
|
async disableTotpMFA({
|
|
username,
|
|
password,
|
|
totp,
|
|
}: AuthDisableTotpMfaRequest): Promise<any> {
|
|
console.log('AuthClientService', 'disableTotpMFA');
|
|
|
|
const tokens = await new Promise((resolve, reject) => {
|
|
this.authService.disableTotpMfa({ username, password, totp }).subscribe({
|
|
next(x) {
|
|
resolve(x);
|
|
},
|
|
error(err) {
|
|
console.log('Observable Error');
|
|
reject(err);
|
|
},
|
|
complete() {
|
|
console.log('done');
|
|
},
|
|
});
|
|
})
|
|
.then((res) => res)
|
|
.catch((err) => {
|
|
throw new Error(err);
|
|
});
|
|
return tokens;
|
|
}
|
|
|
|
async verifyTotp({
|
|
username,
|
|
accessToken,
|
|
totp,
|
|
}: AuthVerifyTotpMfaRequest): Promise<any> {
|
|
console.log('AuthClientService', 'disableTotpMFA');
|
|
|
|
const tokens = await new Promise((resolve, reject) => {
|
|
this.authService.verifyTotp({ username, accessToken, totp }).subscribe({
|
|
next(x) {
|
|
resolve(x);
|
|
},
|
|
error(err) {
|
|
console.log('Observable Error');
|
|
reject(err);
|
|
},
|
|
complete() {
|
|
console.log('done');
|
|
},
|
|
});
|
|
})
|
|
.then((res) => res)
|
|
.catch((err) => {
|
|
throw new Error(err);
|
|
});
|
|
return tokens;
|
|
}
|
|
|
|
async confirmRegister({
|
|
username,
|
|
code,
|
|
}: AuthConfirmationRequest): Promise<any> {
|
|
console.log('AuthClientService', 'confirmRegister');
|
|
|
|
const tokens = await new Promise((resolve, reject) => {
|
|
this.authService.confirmRegister({ username, code }).subscribe({
|
|
next(x) {
|
|
resolve(x);
|
|
},
|
|
error(err) {
|
|
console.log('Observable Error');
|
|
reject(err);
|
|
},
|
|
complete() {
|
|
console.log('done');
|
|
},
|
|
});
|
|
})
|
|
.then((res) => res)
|
|
.catch((err) => {
|
|
throw new Error(err);
|
|
});
|
|
return tokens;
|
|
}
|
|
|
|
async resendeConfirmationCode({
|
|
username,
|
|
}: AuthResendConfirmationCodeRequest): Promise<any> {
|
|
console.log('AuthClientService', 'resendeConfirmationCode');
|
|
|
|
const authResendConfirmationCodeRequestReturn = await new Promise(
|
|
(resolve, reject) => {
|
|
this.authService.resendeConfirmationCode({ username }).subscribe({
|
|
next(x) {
|
|
resolve(x);
|
|
},
|
|
error(err) {
|
|
console.log('Observable Error');
|
|
reject(err);
|
|
},
|
|
complete() {
|
|
console.log('done');
|
|
},
|
|
});
|
|
},
|
|
)
|
|
.then((res) => res)
|
|
.catch((err) => {
|
|
throw new Error(err);
|
|
});
|
|
return authResendConfirmationCodeRequestReturn;
|
|
}
|
|
|
|
async refreshAccessToken({
|
|
username,
|
|
refreshToken,
|
|
}: IRefreshAccessToken): Promise<any> {
|
|
console.log('AuthClientService', 'RefreshAccessToken');
|
|
|
|
const tokens = await new Promise((resolve, reject) => {
|
|
this.authService
|
|
.refreshAccessToken({ username, refreshToken })
|
|
.subscribe({
|
|
next(x) {
|
|
resolve(x);
|
|
},
|
|
error(err) {
|
|
console.log('Observable Error');
|
|
reject(err);
|
|
},
|
|
complete() {
|
|
console.log('done');
|
|
},
|
|
});
|
|
})
|
|
.then((res) => res)
|
|
.catch((err) => {
|
|
throw new Error(err);
|
|
});
|
|
return tokens;
|
|
}
|
|
}
|