amélioration de la 2Fa

This commit is contained in:
batche
2022-12-07 19:59:05 +01:00
parent 1a584483e7
commit bccb342043
9 changed files with 71 additions and 62 deletions

View File

@@ -36,7 +36,8 @@ export class AuthenticationController {
console.log('On redirige');
const user : User = request.user
if (user.isEnabledTwoFactorAuth === false || user.isTwoFactorAuthenticated === true)
return response.status(200).redirect('http://transcendance:8080/#/profile');
{console.log('ON VA VERS PROFILE');return response.status(200).redirect('http://transcendance:8080/#/profile');}
console.log('ON VA VERS 2FA')
return response.status(200).redirect('http://transcendance:8080/#/2fa');
}
@@ -69,24 +70,17 @@ export class AuthenticationController {
}
}
@Post('2fa/turn-on')
@Post('2fa/check')
@UseGuards(AuthenticateGuard)
async verify(@Req() request, @Body() {twoFaCode} : TwoFaDto, @Res() response){
const user : User = request.user;
if (user.isEnabledTwoFactorAuth === true)
{
console.log('ON EST DANS VERIFY POUR 2FA AUTH CONTROLLER')
const isCodeIsValid = await this.authService.verify2FaCode(request.user, twoFaCode);
if (isCodeIsValid === false)
{
throw new UnauthorizedException('Wrong Code.');
}
await this.userService.enableTwoFactorAuth(request.user.id);
console.log('ON REDIRIGE');
// return response.status(200);
// return 200;
// needs to be looked at by Cherif
}
console.log('ON EST DANS VERIFY POUR 2FA AUTH CONTROLLER')
const isCodeIsValid = await this.authService.verify2FaCode(request.user, twoFaCode);
if (isCodeIsValid === false)
throw new UnauthorizedException('Wrong Code.');
await this.userService.authenticateUserWith2FA(request.user.id);
console.log('ON REDIRIGE');
return response.status(200).redirect('http://transcendance:8080/');
}
}

View File

@@ -37,6 +37,7 @@ export class AuthenticationService {
}
async verify2FaCode(user : User, code : string) {
console.log("User : " + user.username);
return authenticator.verify({ token: code, secret: user.secretTwoFactorAuth });
}

View File

@@ -1,9 +1,10 @@
import { IsBoolean, IsEmail, IsOptional, IsString } from 'class-validator';
import { IsBoolean, IsEmail, IsNotEmpty, IsOptional, IsString } from 'class-validator';
import { isSet } from 'util/types';
export class CreateUsersDto {
@IsString()
@IsNotEmpty()
readonly username: string;
@IsString()
readonly fortyTwoId: string;
@IsEmail()
readonly email: string;

View File

@@ -39,7 +39,6 @@ export class UsersController {
@Get()
findOne(@Req() req) {
console.log("Backend Getting current user");
// console.log(this.usersService.findOne(req.user.id));
return this.usersService.findOne(req.user.id);
}
@@ -63,12 +62,16 @@ export class UsersController {
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Patch()
update(@Req() req, @Body(new ValidationPipe()) usersUpdateDto: UpdateUsersDto, @Res() response) {
async update(@Req() req, @Body(new ValidationPipe()) usersUpdateDto: UpdateUsersDto, @Res() response) {
console.log("DANS PATCH USERS");
this.usersService.update(req.user.id, usersUpdateDto);
const user : User = req.user;
const user = await this.usersService.update(req.user.id, usersUpdateDto);
// const user : User = req.user;
console.log ("Enbale 2FA " + user.isEnabledTwoFactorAuth + " Is authenticated " + user.isTwoFactorAuthenticated);
if (user.isEnabledTwoFactorAuth === true && user.isTwoFactorAuthenticated === false)
return response.status.redirect("http://transcendance:8080/#/2fa");
{
console.log("On est dans la boucle de redirection 2FA / Au niveau de l'update du user")
return response.status(200).redirect("http://transcendance:8080/#/2fa");
}
}
@UseGuards(AuthenticateGuard)

View File

@@ -34,13 +34,13 @@ export class UsersService {
async findOne(id: string) {
console.log(`FIND ONE USER SERVICE Find user ${id}`);
const user = await this.userRepository.createQueryBuilder('user')
const user = await this.userRepository.createQueryBuilder('user')
.leftJoinAndSelect('user.stats', 'stats')
.where('user.id = :id', { id: +id })
.getOne();
if (!user)
throw new NotFoundException(`The requested user not found.`);
console.log(`FIND ONE USER SERVICE The requested user found.`
console.log(`FIND ONE USER SERVICE The requested user found.` + user.username
+ user.stats.id + user.stats.winGame + user.stats.loseGame + user.stats.drawGame + user.stats.totalGame);
const partialUser : Partial<User> = {
username: user.username,
@@ -49,6 +49,7 @@ export class UsersService {
status: user.status,
stats: user.stats,
};
console.log(`Returned Partial User.` + partialUser.username + user.username);
return partialUser;
}
@@ -115,7 +116,11 @@ export class UsersService {
}
async enableTwoFactorAuth(id: string) {
return this.userRepository.update(id, {isEnabledTwoFactorAuth: true, isTwoFactorAuthenticated: true});
return this.userRepository.update(id, {isEnabledTwoFactorAuth: true});
}
async authenticateUserWith2FA(id: string) {
return this.userRepository.update(id, { isTwoFactorAuthenticated: true})
}
async setIsTwoFactorAuthenticatedWhenLogout(id: number) {