Skip to content

Commit ea293c8

Browse files
alainabbastacxou
authored andcommitted
save
1 parent 30731dc commit ea293c8

File tree

5 files changed

+120
-3
lines changed

5 files changed

+120
-3
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { AbstractSchema } from '~/_common/abstracts/schemas/abstract.schema';
2+
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
3+
import {Identities} from "~/management/identities/_schemas/identities.schema";
4+
5+
export type PasswordPoliciesDocument = Identities & Document;
6+
7+
@Schema({ versionKey: false })
8+
export class PasswordPolicies extends AbstractSchema {
9+
@Prop({ type: Number, default: 8 })
10+
len: Number;
11+
12+
@Prop({ type: Number,default:1 })
13+
hasUpperCase: Number;
14+
15+
@Prop({ type: Number,default:1 })
16+
hasLowerCase: Number;
17+
18+
@Prop({ type: Number,default:1 })
19+
hasNumbers: Number;
20+
21+
@Prop({ type: Number,default:1 })
22+
hasSpecialChars: Number;
23+
24+
@Prop({ type: Number,default:20 })
25+
minComplexity: Number;
26+
27+
@Prop({ type: Number,default:60 })
28+
goodComplexity: Number;
29+
30+
@Prop({ type: Boolean,default:true })
31+
checkPwned: Boolean;
32+
33+
@Prop({ type: Number,default:10 })
34+
maxRetry: Number;
35+
36+
@Prop({ type: Number,default:3600 })
37+
bannedTime: Number;
38+
39+
}
40+
41+
export const PasswordPoliciesSchema = SchemaFactory.createForClass(PasswordPolicies);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsNumber, IsBoolean } from 'class-validator';
3+
4+
export class PasswordPoliciesDto {
5+
@IsNumber()
6+
@ApiProperty({ example: '8', description: 'Password minimal Length', type: Number })
7+
public len: Number;
8+
9+
@IsNumber()
10+
@ApiProperty({ example: '1', description: 'Minimal amount of letters in uppercase', type: Number })
11+
public hasUpperCase: Number;
12+
13+
@IsNumber()
14+
@ApiProperty({ example: '1', description: 'Minimal amount of letters in lowercase', type: Number })
15+
public hasLowerCase: Number;
16+
17+
@IsNumber()
18+
@ApiProperty({ example: '1', description: 'Minimal amount of numbers', type: Number })
19+
public hasNumbers: Number;
20+
21+
@IsNumber()
22+
@ApiProperty({ example: '1', description: 'Minimal amount of special characters', type: Number })
23+
public hasSpecialChars: Number;
24+
25+
@IsNumber()
26+
@ApiProperty({ example: '30', description: 'Minimal complexity (entropy), Below this number the password wont be accepted', type: Number })
27+
public minComplexity: Number;
28+
29+
@IsNumber()
30+
@ApiProperty({ example: '70', description: 'Good complexity (entropy), Upper this number the password is considered good', type: Number })
31+
public goodComplexity: Number;
32+
33+
@IsBoolean()
34+
@ApiProperty({ example: true, description: 'Teh password will be checked on Pwned', type: Boolean })
35+
public checkPwned: Boolean;
36+
37+
@IsNumber()
38+
@ApiProperty({ example: '10', description: 'after X bad logins the user will be banned for bannedTime', type: Number })
39+
public maxRetry: Number;
40+
41+
@IsNumber()
42+
@ApiProperty({ example: '3600', description: 'in Seconds', type: Number })
43+
public bannedTime: Number;
44+
}

src/management/passwd/passwd.controller.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { Controller, Post, Body, Res, Logger, HttpStatus } from '@nestjs/common';
1+
import {Controller, Post, Body, Res, Logger, HttpStatus, Get} from '@nestjs/common';
22
import { PasswdService } from './passwd.service';
33
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
44
import { Response } from 'express';
55
import { ChangePasswordDto } from './dto/change-password.dto';
66
import { AskTokenDto } from './dto/ask-token.dto';
77
import { VerifyTokenDto } from './dto/verify-token.dto';
88
import { ResetPasswordDto } from './dto/reset-password.dto';
9+
import {omit} from "radash";
910

1011
@Controller('passwd')
1112
@ApiTags('management/passwd')
@@ -27,7 +28,7 @@ export class PasswdController {
2728
}
2829

2930
return res.status(HttpStatus.OK).json({
30-
message: 'Password changed',
31+
message: 'Password changed', status:0,
3132
...debug,
3233
});
3334
}
@@ -68,4 +69,12 @@ export class PasswdController {
6869
...debug,
6970
});
7071
}
72+
@Get('getpolicies')
73+
@ApiOperation({ summary: 'Retourne la politique de mot de passe à appliquer' })
74+
@ApiResponse({ status: HttpStatus.OK })
75+
public async getPolicies(@Res() res: Response): Promise<Response> {
76+
const data = await this.passwdService.getPolicies()
77+
//const datax=omit(data.toObject,['_id'])
78+
return res.status(HttpStatus.OK).json({data})
79+
}
7180
}

src/management/passwd/passwd.module.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,20 @@ import { PasswdService } from './passwd.service';
33
import { PasswdController } from './passwd.controller';
44
import { BackendsModule } from '~/core/backends/backends.module';
55
import { IdentitiesModule } from '../identities/identities.module';
6+
import {MongooseModule} from "@nestjs/mongoose";
7+
import {Agents, AgentsSchema} from "~/core/agents/_schemas/agents.schema";
8+
import {PasswordPolicies,PasswordPoliciesSchema} from "~/management/passwd/_schemas/PasswordPolicies";
69

710
@Module({
8-
imports: [BackendsModule, IdentitiesModule],
11+
imports: [BackendsModule,
12+
IdentitiesModule,
13+
MongooseModule.forFeatureAsync([
14+
{
15+
name: PasswordPolicies.name,
16+
useFactory: () => PasswordPoliciesSchema,
17+
},
18+
]),
19+
],
920
controllers: [PasswdController],
1021
providers: [PasswdService],
1122
})

src/management/passwd/passwd.service.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import { ResetPasswordDto } from './dto/reset-password.dto';
1212
import { IdentitiesService } from '../identities/identities.service';
1313
import { pick } from 'radash';
1414
import { Identities } from '../identities/_schemas/identities.schema';
15+
import {PasswordPolicies} from "~/management/passwd/_schemas/PasswordPolicies";
16+
import {Model} from "mongoose";
17+
import {InjectModel} from "@nestjs/mongoose";
1518

1619
interface TokenData {
1720
k: string;
@@ -37,6 +40,7 @@ export class PasswdService extends AbstractService {
3740
protected readonly backends: BackendsService,
3841
protected readonly identities: IdentitiesService,
3942
@InjectRedis() private readonly redis: Redis,
43+
@InjectModel(PasswordPolicies.name) protected passwordPolicies: Model<PasswordPolicies>
4044
) {
4145
super();
4246
}
@@ -159,4 +163,12 @@ export class PasswdService extends AbstractService {
159163
throw new BadRequestException('Une erreur est survenue : Tentative de réinitialisation de mot de passe impossible');
160164
}
161165
}
166+
167+
public async getPolicies(): Promise<any>{
168+
const passwordPolicies = await this.passwordPolicies.findOne()
169+
if (passwordPolicies === null){
170+
return new this.passwordPolicies()
171+
}
172+
return passwordPolicies
173+
}
162174
}

0 commit comments

Comments
 (0)