Ajout des fichiers de lucky + quelques corrections mineures sur l'auth (2FA) et le flow pour le user

This commit is contained in:
batche
2022-12-07 11:50:39 +01:00
parent f8095c7ec6
commit f835f74443
84 changed files with 2450 additions and 15 deletions

View File

@@ -7,6 +7,7 @@ import { ConfigModule } from '@nestjs/config';
import { FriendshipsModule } from './friendship/friendships.module';
import { AuthenticationModule } from './auth/42/authentication.module';
import { PassportModule } from '@nestjs/passport';
import { GameModule } from './game/game/game.module';
@Module({
imports: [UsersModule,
@@ -26,6 +27,7 @@ import { PassportModule } from '@nestjs/passport';
//avec une classe pour le module
synchronize: true,
}),
GameModule,
],
controllers: [AppController],
providers: [AppService],

View File

@@ -4,6 +4,7 @@ import { AuthenticationService } from './authentication.service';
import { Response } from 'express';
import { TwoFaDto } from './dto/2fa.dto';
import { UsersService } from 'src/users/users.service';
import { User } from 'src/users/entities/user.entity';
@Controller('auth')
export class AuthenticationController {
@@ -33,7 +34,8 @@ export class AuthenticationController {
async redirect(@Res() response : Response, @Req() request) {
console.log('ON EST DANS REDIRECT AUTH CONTROLLER');
console.log('On redirige');
if (request.user.isEnabledTwoFactorAuth === false)
const user : User = request.user
if (user.isEnabledTwoFactorAuth === false || user.isTwoFactorAuthenticated === true)
return response.status(200).redirect('http://transcendance:8080/#/profile');
return response.status(200).redirect('http://transcendance:8080/#/2fa');
}
@@ -58,25 +60,33 @@ export class AuthenticationController {
@Post('2fa/generate')
@UseGuards(AuthenticateGuard)
async register(@Req() request, @Res() response){
console.log('ON EST DANS REGISTER POUR 2FA AUTH CONTROLLER')
const { otpauth } = await this.authService.generate2FaSecret(request.user);
return this.authService.pipeQrCodeStream(response, otpauth);
const user : User = request.user;
if (user.isEnabledTwoFactorAuth === true)
{
console.log('ON EST DANS REGISTER POUR 2FA AUTH CONTROLLER')
const { otpauth } = await this.authService.generate2FaSecret(request.user);
return this.authService.pipeQrCodeStream(response, otpauth);
}
}
@Post('2fa/turn-on')
@UseGuards(AuthenticateGuard)
async verify(@Req() request, @Body() {twoFaCode} : TwoFaDto, @Res() response){
console.log('ON EST DANS VERIFY POUR 2FA AUTH CONTROLLER')
const isCodeIsValid = await this.authService.verify2FaCode(request.user, twoFaCode);
if (isCodeIsValid === false)
const user : User = request.user;
if (user.isEnabledTwoFactorAuth === true)
{
throw new UnauthorizedException('Wrong Code.');
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
}
await this.userService.enableTwoFactorAuth(request.user.id);
console.log('ON REDIRIGE');
// return response.status(200);
// return 200;
// needs to be looked at by Cherif
return response.status(200).redirect('http://transcendance:8080/');
}
}

View File

@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { GameController } from './game.controller';
describe('GameController', () => {
let controller: GameController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [GameController],
}).compile();
controller = module.get<GameController>(GameController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View File

@@ -0,0 +1,4 @@
import { Controller } from '@nestjs/common';
@Controller('game')
export class GameController {}

View File

@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { GameController } from './game.controller';
import { GameService } from './game.service';
@Module({
controllers: [GameController],
providers: [GameService]
})
export class GameModule {}

View File

@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { GameService } from './game.service';
describe('GameService', () => {
let service: GameService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [GameService],
}).compile();
service = module.get<GameService>(GameService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

@@ -0,0 +1,4 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class GameService {}

View File

@@ -61,9 +61,12 @@ export class UsersController {
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Patch()
update(@Req() req, @Body(new ValidationPipe()) usersUpdateDto: UpdateUsersDto) {
update(@Req() req, @Body(new ValidationPipe()) usersUpdateDto: UpdateUsersDto, @Res() response) {
console.log("DANS PATCH USERS");
return this.usersService.update(req.user.id, usersUpdateDto);
this.usersService.update(req.user.id, usersUpdateDto);
const user : User = req.user;
if (user.isEnabledTwoFactorAuth === true && user.isTwoFactorAuthenticated === false)
return response.status.redirect("http://transcendance:8080/#/2fa");
}
@UseGuards(AuthenticateGuard)

View File

@@ -89,6 +89,7 @@ export const primaryRoutes = {
"/profile": ProfilePage,
"/profile/*": ProfilePage,
'/unauthorized-access': UnauthorizedAccessPage,
"/game" : GamePage,
"*": NotFound
};