1- import { BadRequestException , Injectable , InternalServerErrorException , NotFoundException } from '@nestjs/common' ;
1+ import { BadRequestException , Injectable , InternalServerErrorException , NotFoundException } from '@nestjs/common' ;
22import Redis from 'ioredis' ;
3- import { AbstractService } from '~/_common/abstracts/abstract.service' ;
3+ import { AbstractService } from '~/_common/abstracts/abstract.service' ;
44import { PasswordPolicies } from "~/settings/passwdadm/_schemas/PasswordPolicies" ;
55import { Model } from "mongoose" ;
66import { InjectModel } from "@nestjs/mongoose" ;
77import { IdentitiesService } from "~/management/identities/identities.service" ;
8+ import stringEntropy from 'fast-password-entropy'
89
910@Injectable ( )
1011export 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}
0 commit comments