Skip to content

Commit 438404c

Browse files
alainabbastacxou
authored andcommitted
save
1 parent 46c483c commit 438404c

File tree

3 files changed

+47
-58
lines changed

3 files changed

+47
-58
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"class-transformer": "^0.5.1",
6868
"class-validator": "^0.14.1",
6969
"cookie-parser": "^1.4.6",
70+
"fast-password-entropy": "^1.1.1",
7071
"handlebars": "^4.7.8",
7172
"helmet": "^7.1.0",
7273
"ioredis": "^5.4.1",
@@ -102,6 +103,7 @@
102103
"@swc/core": "^1.6.5",
103104
"@types/cookie-parser": "^1.4.7",
104105
"@types/express": "^4.17.21",
106+
"@types/fast-password-entropy": "^1.1.3",
105107
"@types/inquirer": "^9.0.7",
106108
"@types/jest": "^29.5.12",
107109
"@types/node": "^20.14.8",
Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { BadRequestException, Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common';
1+
import {BadRequestException, Injectable, InternalServerErrorException, NotFoundException} from '@nestjs/common';
22
import Redis from 'ioredis';
3-
import { AbstractService } from '~/_common/abstracts/abstract.service';
3+
import {AbstractService} from '~/_common/abstracts/abstract.service';
44
import {PasswordPolicies} from "~/settings/passwdadm/_schemas/PasswordPolicies";
55
import {Model} from "mongoose";
66
import {InjectModel} from "@nestjs/mongoose";
77
import {IdentitiesService} from "~/management/identities/identities.service";
8+
import stringEntropy from 'fast-password-entropy'
89

910
@Injectable()
1011
export class PasswdadmService extends AbstractService {
@@ -15,76 +16,52 @@ export class PasswdadmService extends AbstractService {
1516
super();
1617
}
1718

18-
public async getPolicies(): Promise<any>{
19+
public async getPolicies(): Promise<PasswordPolicies> {
1920
const passwordPolicies = await this.passwordPolicies.findOne()
20-
if (passwordPolicies === null){
21+
if (passwordPolicies === null) {
2122
return new this.passwordPolicies()
2223
}
2324
return passwordPolicies
2425
}
25-
public async checkPolicies(password: string):Promise<boolean>{
26-
const policies=this.getPolicies()
26+
27+
public async checkPolicies(password: string): Promise<boolean> {
28+
const policies = this.getPolicies()
2729
if (password.length < policies.len) {
2830
this.logger.error('Password too short')
2931
return false
3032
}
31-
32-
return true
33-
}
34-
/*
35-
function checkPolicy(password) {
36-
has_len.value='highlight_off'
37-
let statut=true
38-
if (/[!@#\$%\^\&*\)\(+=._-]/.test(password) === false){
39-
pwdColor.value = 'red'
40-
iconSpecialOK(false)
41-
statut=false
42-
}else{
43-
iconSpecialOK(true)
33+
//tes caracteres speciaux
34+
if (policies.hasSpecialChars > 0) {
35+
if (/[!@#\$%\^\&*\)\(+=._-]/.test(password) === false) {
36+
this.logger.error('must have special characters')
37+
return false
38+
}
4439
}
45-
if (/\d/.test(password) === false){
46-
pwdColor.value = 'red'
47-
iconNumberOK(false)
48-
statut=false
49-
}else{
50-
iconNumberOK(true)
40+
if (policies.hasLowerCase > 0) {
41+
if (/[a-z]/.test(password) === false) {
42+
this.logger.error('must have lower case characters')
43+
return false
44+
}
5145
}
52-
if (/[a-z]/.test(password) === false){
53-
pwdColor.value = 'red'
54-
iconLowerOK(false)
55-
statut=false
56-
}else{
57-
iconLowerOK(true)
46+
if (policies.hasUpperCase > 0) {
47+
if (/[A-Z]/.test(password) === false) {
48+
this.logger.error('must have upper case characters')
49+
return false
50+
}
5851
}
59-
if (/[A-Z]/.test(password) === false){
60-
pwdColor.value = 'red'
61-
iconUpperOK(false)
62-
statut=false
63-
}else{
64-
iconUpperOK(true)
52+
if (policies.hasNumbers > 0) {
53+
if (/[A-Z]/.test(password) === false) {
54+
this.logger.error('must have number')
55+
return false
56+
}
6557
}
66-
if (password.length < props.min) {
67-
console.log('trop court ' + props.min)
68-
iconLenOK(false)
69-
statut=false
70-
}else{
71-
iconLenOK(true)
58+
//calcul de l'entropie
59+
let c = stringEntropy(password)
60+
if (c < policies.minComplexity) {
61+
this.logger.error('entropie trop faible')
7262
}
73-
console.log('password OK ')
74-
if (statut === true){
75-
pwdColor.value = 'green'
76-
}else {
77-
pwdColor.value = 'red'
78-
}
79-
//entropie
80-
if (complexity(password) === false){
81-
statut=false
82-
iconComplexityOK(false)
83-
}else{
84-
iconComplexityOK(true)
85-
}
86-
return statut
63+
return true
8764
}
88-
*/
65+
8966

9067
}

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,6 +2687,11 @@
26872687
"@types/qs" "*"
26882688
"@types/serve-static" "*"
26892689

2690+
"@types/fast-password-entropy@^1.1.3":
2691+
version "1.1.3"
2692+
resolved "https://registry.yarnpkg.com/@types/fast-password-entropy/-/fast-password-entropy-1.1.3.tgz#298300f46160f65e648266a65f54a1f3d49e1a39"
2693+
integrity sha512-vio+mpna1L/nQVAqeolggx428AtODBe1KFUVOStUV75gCCDID9Oz+ElVS5L54kqeooDW2gzT7AfZWlSG+zyTuw==
2694+
26902695
"@types/graceful-fs@^4.1.3":
26912696
version "4.1.9"
26922697
resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.9.tgz#2a06bc0f68a20ab37b3e36aa238be6abdf49e8b4"
@@ -5588,6 +5593,11 @@ fast-levenshtein@^2.0.6:
55885593
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
55895594
integrity sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==
55905595

5596+
fast-password-entropy@^1.1.1:
5597+
version "1.1.1"
5598+
resolved "https://registry.yarnpkg.com/fast-password-entropy/-/fast-password-entropy-1.1.1.tgz#47ba9933095fd5a32fb184915fc8e76ee19cf429"
5599+
integrity sha512-dxm29/BPFrNgyEDygg/lf9c2xQR0vnQhG7+hZjAI39M/3um9fD4xiqG6F0ZjW6bya5m9CI0u6YryHGRtxCGCiw==
5600+
55915601
fast-safe-stringify@2.1.1, fast-safe-stringify@^2.1.1:
55925602
version "2.1.1"
55935603
resolved "https://registry.yarnpkg.com/fast-safe-stringify/-/fast-safe-stringify-2.1.1.tgz#c406a83b6e70d9e35ce3b30a81141df30aeba884"

0 commit comments

Comments
 (0)