Skip to content

Commit c72f4fc

Browse files
alainabbastacxou
authored andcommitted
gestion code
1 parent ee98e01 commit c72f4fc

File tree

8 files changed

+245
-82
lines changed

8 files changed

+245
-82
lines changed
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import { ApiProperty } from '@nestjs/swagger';
22
import { IsString } from 'class-validator';
33

4-
export class AskCodeDto {
4+
export class InitResetDto {
55
@IsString()
66
@ApiProperty({ example: 'paul.bismuth', description: 'User id' })
77
uid: string;
8-
9-
@ApiProperty({ example: 'monemail@mondomaine.com', description: 'secondary mail' })
10-
@IsString()
11-
mail: string;
12-
138
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import {IsNumber, IsString} from 'class-validator';
3+
4+
export class ResetByCodeDto {
5+
@IsString()
6+
@ApiProperty({ example: '3F4AC...', description: 'Token received by initreset' })
7+
token: string;
8+
9+
@IsNumber
10+
@ApiProperty({ example: '123456', description: 'Code received by email or Sms' })
11+
code: number;
12+
}
Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
import {Controller, Post, Body, Res, Logger, HttpStatus, Get} from '@nestjs/common';
2-
import { PasswdService } from './passwd.service';
3-
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
4-
import { Response } from 'express';
5-
import { ChangePasswordDto } from './dto/change-password.dto';
6-
import { AskTokenDto } from './dto/ask-token.dto';
7-
import { VerifyTokenDto } from './dto/verify-token.dto';
8-
import { ResetPasswordDto } from './dto/reset-password.dto';
2+
import {PasswdService} from './passwd.service';
3+
import {ApiTags, ApiOperation, ApiResponse} from '@nestjs/swagger';
4+
import {Response} from 'express';
5+
import {ChangePasswordDto} from './dto/change-password.dto';
6+
import {AskTokenDto} from './dto/ask-token.dto';
7+
import {VerifyTokenDto} from './dto/verify-token.dto';
8+
import {ResetPasswordDto} from './dto/reset-password.dto';
99
import {omit} from "radash";
1010
import {PasswdadmService} from "~/settings/passwdadm/passwdadm.service";
1111
import {PasswordPolicies} from "~/settings/passwdadm/_schemas/PasswordPolicies";
1212
import {InitAccountDto} from "~/management/passwd/dto/init-account.dto";
13+
import {InitResetDto} from "~/management/passwd/dto/init-reset.dto";
14+
import crypto from "crypto";
15+
import {ResetByCodeDto} from "~/management/passwd/dto/reset-by-code-dto";
1316

1417
@Controller('passwd')
1518
@ApiTags('management/passwd')
1619
export class PasswdController {
1720
private readonly logger = new Logger(PasswdController.name);
1821

19-
public constructor(private passwdService: PasswdService,private passwdadmService: PasswdadmService) { }
22+
public constructor(private passwdService: PasswdService, private passwdadmService: PasswdadmService) {
23+
}
2024

2125
@Post('change')
22-
@ApiOperation({ summary: 'Execute un job de changement de mot de passe sur le/les backends' })
23-
@ApiResponse({ status: HttpStatus.OK, description: 'Mot de passe synchronisé sur le/les backends' })
26+
@ApiOperation({summary: 'Execute un job de changement de mot de passe sur le/les backends'})
27+
@ApiResponse({status: HttpStatus.OK, description: 'Mot de passe synchronisé sur le/les backends'})
2428
public async change(@Body() body: ChangePasswordDto, @Res() res: Response): Promise<Response> {
2529
const debug = {}
2630
const [_, data] = await this.passwdService.change(body);
@@ -31,34 +35,52 @@ export class PasswdController {
3135
}
3236

3337
return res.status(HttpStatus.OK).json({
34-
message: 'Password changed', status:0,
38+
message: 'Password changed', status: 0,
3539
...debug,
3640
});
3741
}
3842

3943
@Post('gettoken')
40-
@ApiOperation({ summary: 'Récupère un jeton de réinitialisation de mot de passe' })
41-
@ApiResponse({ status: HttpStatus.OK, description: 'Retourne un jeton de réinitialisation de mot de passe' })
44+
@ApiOperation({summary: 'Récupère un jeton de réinitialisation de mot de passe'})
45+
@ApiResponse({status: HttpStatus.OK, description: 'Retourne un jeton de réinitialisation de mot de passe'})
4246
public async gettoken(@Body() asktoken: AskTokenDto, @Res() res: Response): Promise<Response> {
4347
this.logger.log('GetToken for : ' + asktoken.uid);
44-
const token = await this.passwdService.askToken(asktoken);
48+
const k = crypto.randomBytes(PasswdService.RANDOM_BYTES_K).toString('hex');
49+
const token = await this.passwdService.askToken(asktoken, k, PasswdService.TOKEN_EXPIRATION);
4550

46-
return res.status(HttpStatus.OK).json({ data: { token } });
51+
return res.status(HttpStatus.OK).json({data: {token}});
4752
}
4853

4954
@Post('verifytoken')
50-
@ApiOperation({ summary: 'Vérifie un jeton de réinitilisation de mot de passe' })
51-
@ApiResponse({ status: HttpStatus.OK })
55+
@ApiOperation({summary: 'Vérifie un jeton de réinitilisation de mot de passe'})
56+
@ApiResponse({status: HttpStatus.OK})
5257
public async verifyToken(@Body() body: VerifyTokenDto, @Res() res: Response): Promise<Response> {
5358
this.logger.log('Verify token : ' + body.token);
5459
const data = await this.passwdService.decryptToken(body.token);
5560

56-
return res.status(HttpStatus.OK).json({ data });
61+
return res.status(HttpStatus.OK).json({data});
62+
}
63+
64+
@Post('resetbycode')
65+
@ApiOperation({summary: 'reinitialise le mot de passe avec le code reçu'})
66+
@ApiResponse({status: HttpStatus.OK})
67+
public async resetbycode(@Body() body: ResetByCodeDto, @Res() res: Response): Promise<Response> {
68+
const debug = {}
69+
this.logger.log('Reset by code : ' + body.token + " code : " + body.code);
70+
const [_, data] = await this.passwdService.resetByCode(body);
71+
if (process.env.NODE_ENV === 'development') {
72+
debug['_debug'] = data;
73+
}
74+
75+
return res.status(HttpStatus.OK).json({
76+
message: 'Password changed',
77+
...debug,
78+
});
5779
}
5880

5981
@Post('reset')
60-
@ApiOperation({ summary: 'Execute un job de réinitialisation de mot de passe sur le/les backends' })
61-
@ApiResponse({ status: HttpStatus.OK })
82+
@ApiOperation({summary: 'Execute un job de réinitialisation de mot de passe sur le/les backends'})
83+
@ApiResponse({status: HttpStatus.OK})
6284
public async reset(@Body() body: ResetPasswordDto, @Res() res: Response): Promise<Response> {
6385
const debug = {}
6486
const [_, data] = await this.passwdService.reset(body);
@@ -72,23 +94,40 @@ export class PasswdController {
7294
...debug,
7395
});
7496
}
97+
7598
@Get('getpolicies')
76-
@ApiOperation({ summary: 'Retourne la politique de mot de passe à appliquer' })
77-
@ApiResponse({ status: HttpStatus.OK })
99+
@ApiOperation({summary: 'Retourne la politique de mot de passe à appliquer'})
100+
@ApiResponse({status: HttpStatus.OK})
78101
public async getPolicies(@Res() res: Response): Promise<Response> {
79102
const data = await this.passwdadmService.getPolicies()
80103
//const datax=omit(data.toObject,['_id'])
81104
return res.status(HttpStatus.OK).json({data})
82105
}
106+
83107
@Post('init')
84-
@ApiOperation({ summary: 'Initialise le compte envoi un jeton par mail à l\'identité' })
85-
@ApiResponse({ status: HttpStatus.OK })
108+
@ApiOperation({summary: 'Initialise le compte envoi un jeton par mail à l\'identité'})
109+
@ApiResponse({status: HttpStatus.OK})
86110
public async init(@Body() body: InitAccountDto, @Res() res: Response): Promise<Response> {
87111
const debug = {}
88-
const ok=await this.passwdService.initAccount(body)
112+
const ok = await this.passwdService.initAccount(body)
89113
return res.status(HttpStatus.OK).json({
90114
message: 'Email envoyé verifiez votre boite mail alternative et vos spam',
91115
...debug,
92116
});
93117
}
118+
119+
@Post('initreset')
120+
@ApiOperation({summary: 'Demande l envoi de mail pour le reset'})
121+
@ApiResponse({status: HttpStatus.OK})
122+
public async initreset(@Body() body: InitResetDto, @Res() res: Response): Promise<Response> {
123+
const debug = {}
124+
const data = await this.passwdService.initReset(body)
125+
126+
return res.status(HttpStatus.OK).json({
127+
message: 'Email envoyé verifiez votre boite mail alternative et vos spam',
128+
token: data,
129+
...debug,
130+
});
131+
}
132+
94133
}

0 commit comments

Comments
 (0)