Compare commits

...
5 Commits
6 changed files with 193 additions and 10 deletions
+1 -1
View File
@@ -31,7 +31,7 @@
"@nestjs/platform-express": "^8.4.3",
"@nestjs/schedule": "^1.1.0",
"@nestjs/swagger": "^5.2.1",
"@victorradael/protospack": "2.4.1",
"@victorradael/protospack": "2.4.2-1",
"axios": "^0.25.0",
"dotenv": "^14.2.0",
"helmet": "^5.0.2",
+80 -5
View File
@@ -12,6 +12,10 @@ import {
AuthDisableTotpMfaRequest,
AuthDismissTotpMfaRequest,
AuthVerifyTotpMfaRequest,
AuthChangePasswordRequest,
AuthResetPasswordRequest,
AuthVerifyResetPasswordCodeRequest,
AuthConfirmResetPasswordRequest,
} from '@victorradael/protospack';
export class AuthClientService implements OnModuleInit {
@@ -32,11 +36,7 @@ export class AuthClientService implements OnModuleInit {
return new Promise((resolve, reject) => {
this.authService.signIn({ username, password, totp }).subscribe({
next: resolve,
//error: (err) => reject(err.details),
error: (err) => {
console.log(err);
reject(err.details);
},
error: (err) => reject(err.details),
complete() {
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');
},
});
});
}
}
+80 -1
View File
@@ -5,7 +5,6 @@ import {
Post,
HttpCode,
HttpStatus,
UnauthorizedException,
} from '@nestjs/common';
import {
AuthSignInRequest,
@@ -13,6 +12,10 @@ import {
AuthEnableTotpMfaRequest,
AuthDisableTotpMfaRequest,
AuthVerifyTotpMfaRequest,
AuthChangePasswordRequest,
AuthResetPasswordRequest,
AuthVerifyResetPasswordCodeRequest,
AuthConfirmResetPasswordRequest,
} from '@victorradael/protospack';
import { AuthClientService } from 'src/clients/auth/client.service';
@@ -145,4 +148,80 @@ export class AuthController {
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;
}
}
+25
View File
@@ -67,6 +67,30 @@ export default function ErrorBuilder(code: string) {
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:
return Builder({
statusCode: HttpStatus.TOO_MANY_REQUESTS,
@@ -93,6 +117,7 @@ export default function ErrorBuilder(code: string) {
code,
});
case ErrorCodes.INTERNAL:
case ErrorCodes.UNKNOWN:
default:
return Builder({
+6 -2
View File
@@ -1,4 +1,4 @@
export const Auth = {
export const AUTH = {
UNAUTHORIZED: 'AUTH.UNAUTHORIZED',
FORBIDDEN: 'AUTH.FORBIDDEN',
WRONG_CREDENTIALS: 'AUTH.WRONG_CREDENTIALS',
@@ -9,12 +9,16 @@ export const Auth = {
TOTP_REQUIRED: 'AUTH.TOTP_REQUIRED',
CODE_MISMATCH: 'AUTH.CODE_MISMATCH',
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 = {
UNKNOWN: 'UNKNOWN',
RATE_LIMIT: 'RATE_LIMIT',
AUTH: Auth,
INTERNAL: 'INTERNAL',
AUTH,
};
export default ErrorCodes;
+1 -1
View File
File diff suppressed because one or more lines are too long