mirror of
https://github.com/dadosfera/maestro.git
synced 2026-11-28 04:00:05 +00:00
Merge branch 'feat/change-password' into main
This commit is contained in:
+1
-1
@@ -31,7 +31,7 @@
|
|||||||
"@nestjs/platform-express": "^8.4.3",
|
"@nestjs/platform-express": "^8.4.3",
|
||||||
"@nestjs/schedule": "^1.1.0",
|
"@nestjs/schedule": "^1.1.0",
|
||||||
"@nestjs/swagger": "^5.2.1",
|
"@nestjs/swagger": "^5.2.1",
|
||||||
"@victorradael/protospack": "2.4.1",
|
"@victorradael/protospack": "2.4.2-1",
|
||||||
"axios": "^0.25.0",
|
"axios": "^0.25.0",
|
||||||
"dotenv": "^14.2.0",
|
"dotenv": "^14.2.0",
|
||||||
"helmet": "^5.0.2",
|
"helmet": "^5.0.2",
|
||||||
|
|||||||
@@ -12,6 +12,10 @@ import {
|
|||||||
AuthDisableTotpMfaRequest,
|
AuthDisableTotpMfaRequest,
|
||||||
AuthDismissTotpMfaRequest,
|
AuthDismissTotpMfaRequest,
|
||||||
AuthVerifyTotpMfaRequest,
|
AuthVerifyTotpMfaRequest,
|
||||||
|
AuthChangePasswordRequest,
|
||||||
|
AuthResetPasswordRequest,
|
||||||
|
AuthVerifyResetPasswordCodeRequest,
|
||||||
|
AuthConfirmResetPasswordRequest,
|
||||||
} from '@victorradael/protospack';
|
} from '@victorradael/protospack';
|
||||||
|
|
||||||
export class AuthClientService implements OnModuleInit {
|
export class AuthClientService implements OnModuleInit {
|
||||||
@@ -32,11 +36,7 @@ export class AuthClientService implements OnModuleInit {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.authService.signIn({ username, password, totp }).subscribe({
|
this.authService.signIn({ username, password, totp }).subscribe({
|
||||||
next: resolve,
|
next: resolve,
|
||||||
//error: (err) => reject(err.details),
|
error: (err) => reject(err.details),
|
||||||
error: (err) => {
|
|
||||||
console.log(err);
|
|
||||||
reject(err.details);
|
|
||||||
},
|
|
||||||
complete() {
|
complete() {
|
||||||
console.log('done');
|
console.log('done');
|
||||||
},
|
},
|
||||||
@@ -181,4 +181,79 @@ export class AuthClientService implements OnModuleInit {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async changePassword({
|
||||||
|
accessToken,
|
||||||
|
oldPassword,
|
||||||
|
newPassword,
|
||||||
|
}: AuthChangePasswordRequest): Promise<any> {
|
||||||
|
console.log('AuthClientService', 'ChangePassword');
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.authService
|
||||||
|
.changePassword({
|
||||||
|
accessToken,
|
||||||
|
oldPassword,
|
||||||
|
newPassword,
|
||||||
|
})
|
||||||
|
.subscribe({
|
||||||
|
next: resolve,
|
||||||
|
error: (err) => reject(err.details),
|
||||||
|
complete() {
|
||||||
|
console.log('done');
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async resetPassword({ username }: AuthResetPasswordRequest): Promise<any> {
|
||||||
|
console.log('AuthClientService', 'resetPassword');
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.authService.resetPassword({ username }).subscribe({
|
||||||
|
next: resolve,
|
||||||
|
error: (err) => reject(err.details),
|
||||||
|
complete() {
|
||||||
|
console.log('done');
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async verifyResetPasswordCode({
|
||||||
|
username,
|
||||||
|
code,
|
||||||
|
}: AuthVerifyResetPasswordCodeRequest): Promise<any> {
|
||||||
|
console.log('AuthClientService', 'verifyResetPasswordCode');
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.authService.verifyResetPasswordCode({ username, code }).subscribe({
|
||||||
|
next: resolve,
|
||||||
|
error: (err) => reject(err.details),
|
||||||
|
complete() {
|
||||||
|
console.log('done');
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async confirmResetPassword({
|
||||||
|
username,
|
||||||
|
code,
|
||||||
|
newPassword,
|
||||||
|
}: AuthConfirmResetPasswordRequest): Promise<any> {
|
||||||
|
console.log('AuthClientService', 'confirmResetPassword');
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.authService
|
||||||
|
.confirmResetPassword({ username, code, newPassword })
|
||||||
|
.subscribe({
|
||||||
|
next: resolve,
|
||||||
|
error: (err) => reject(err.details),
|
||||||
|
complete() {
|
||||||
|
console.log('done');
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import {
|
|||||||
Post,
|
Post,
|
||||||
HttpCode,
|
HttpCode,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
UnauthorizedException,
|
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import {
|
import {
|
||||||
AuthSignInRequest,
|
AuthSignInRequest,
|
||||||
@@ -13,6 +12,10 @@ import {
|
|||||||
AuthEnableTotpMfaRequest,
|
AuthEnableTotpMfaRequest,
|
||||||
AuthDisableTotpMfaRequest,
|
AuthDisableTotpMfaRequest,
|
||||||
AuthVerifyTotpMfaRequest,
|
AuthVerifyTotpMfaRequest,
|
||||||
|
AuthChangePasswordRequest,
|
||||||
|
AuthResetPasswordRequest,
|
||||||
|
AuthVerifyResetPasswordCodeRequest,
|
||||||
|
AuthConfirmResetPasswordRequest,
|
||||||
} from '@victorradael/protospack';
|
} from '@victorradael/protospack';
|
||||||
|
|
||||||
import { AuthClientService } from 'src/clients/auth/client.service';
|
import { AuthClientService } from 'src/clients/auth/client.service';
|
||||||
@@ -145,4 +148,80 @@ export class AuthController {
|
|||||||
|
|
||||||
return response;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,6 +67,30 @@ export default function ErrorBuilder(code: string) {
|
|||||||
code,
|
code,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
case ErrorCodes.AUTH.RESET_PASSWORD_CODE_EXPIRED:
|
||||||
|
return Builder({
|
||||||
|
statusCode: HttpStatus.UNAUTHORIZED,
|
||||||
|
error: 'Não permitido',
|
||||||
|
message: 'Token para recuperar senha expirado',
|
||||||
|
code,
|
||||||
|
});
|
||||||
|
|
||||||
|
case ErrorCodes.AUTH.RESET_PASSWORD_CODE_INVALID:
|
||||||
|
return Builder({
|
||||||
|
statusCode: HttpStatus.UNAUTHORIZED,
|
||||||
|
error: 'Não permitido',
|
||||||
|
message: 'Token para recuperar senha inválido',
|
||||||
|
code,
|
||||||
|
});
|
||||||
|
|
||||||
|
case ErrorCodes.AUTH.WEAK_NEW_PASSWORD:
|
||||||
|
return Builder({
|
||||||
|
statusCode: HttpStatus.BAD_REQUEST,
|
||||||
|
error: 'Não permitido',
|
||||||
|
message: 'Senha muito fraca. Escolha uma senha mais forte',
|
||||||
|
code,
|
||||||
|
});
|
||||||
|
|
||||||
case ErrorCodes.RATE_LIMIT:
|
case ErrorCodes.RATE_LIMIT:
|
||||||
return Builder({
|
return Builder({
|
||||||
statusCode: HttpStatus.TOO_MANY_REQUESTS,
|
statusCode: HttpStatus.TOO_MANY_REQUESTS,
|
||||||
@@ -93,6 +117,7 @@ export default function ErrorBuilder(code: string) {
|
|||||||
code,
|
code,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
case ErrorCodes.INTERNAL:
|
||||||
case ErrorCodes.UNKNOWN:
|
case ErrorCodes.UNKNOWN:
|
||||||
default:
|
default:
|
||||||
return Builder({
|
return Builder({
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
export const Auth = {
|
export const AUTH = {
|
||||||
UNAUTHORIZED: 'AUTH.UNAUTHORIZED',
|
UNAUTHORIZED: 'AUTH.UNAUTHORIZED',
|
||||||
FORBIDDEN: 'AUTH.FORBIDDEN',
|
FORBIDDEN: 'AUTH.FORBIDDEN',
|
||||||
WRONG_CREDENTIALS: 'AUTH.WRONG_CREDENTIALS',
|
WRONG_CREDENTIALS: 'AUTH.WRONG_CREDENTIALS',
|
||||||
@@ -9,12 +9,16 @@ export const Auth = {
|
|||||||
TOTP_REQUIRED: 'AUTH.TOTP_REQUIRED',
|
TOTP_REQUIRED: 'AUTH.TOTP_REQUIRED',
|
||||||
CODE_MISMATCH: 'AUTH.CODE_MISMATCH',
|
CODE_MISMATCH: 'AUTH.CODE_MISMATCH',
|
||||||
CODE_ALREADY_USED: 'AUTH.CODE_ALREADY_USED',
|
CODE_ALREADY_USED: 'AUTH.CODE_ALREADY_USED',
|
||||||
|
RESET_PASSWORD_CODE_EXPIRED: 'AUTH.RESET_PASSWORD_CODE_EXPIRED',
|
||||||
|
RESET_PASSWORD_CODE_INVALID: 'AUTH.RESET_PASSWORD_CODE_INVALID',
|
||||||
|
WEAK_NEW_PASSWORD: 'AUTH.WEAK_NEW_PASSWORD',
|
||||||
};
|
};
|
||||||
|
|
||||||
const ErrorCodes = {
|
const ErrorCodes = {
|
||||||
UNKNOWN: 'UNKNOWN',
|
UNKNOWN: 'UNKNOWN',
|
||||||
RATE_LIMIT: 'RATE_LIMIT',
|
RATE_LIMIT: 'RATE_LIMIT',
|
||||||
AUTH: Auth,
|
INTERNAL: 'INTERNAL',
|
||||||
|
AUTH,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ErrorCodes;
|
export default ErrorCodes;
|
||||||
|
|||||||
+1
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user