11import { 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' ;
99import { omit } from "radash" ;
1010import { PasswdadmService } from "~/settings/passwdadm/passwdadm.service" ;
1111import { PasswordPolicies } from "~/settings/passwdadm/_schemas/PasswordPolicies" ;
1212import { 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' )
1619export 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