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

@@ -3886,9 +3886,9 @@
}
},
"node_modules/dezalgo": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.3.tgz",
"integrity": "sha512-K7i4zNfT2kgQz3GylDw40ot9GAE47sFZ9EXHFSPP6zONLgH6kWXE0KWJchkbQJLBkRazq4APwZ4OwiFFlT95OQ==",
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.4.tgz",
"integrity": "sha512-rXSP0bf+5n0Qonsb+SVVfNfIsimO4HEtmnIpPHY8Q1UCzKlQrDMfdobr8nJOOsRgWCyMRqeSBQzmWUMq7zvVig==",
"dev": true,
"dependencies": {
"asap": "^2.0.0",
@@ -4823,25 +4823,28 @@
}
},
"node_modules/formidable": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/formidable/-/formidable-2.0.1.tgz",
"integrity": "sha512-rjTMNbp2BpfQShhFbR3Ruk3qk2y9jKpvMW78nJgx8QKtxjDVrwbZG+wvDOmVbifHyOUOQJXxqEy6r0faRrPzTQ==",
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/formidable/-/formidable-2.1.1.tgz",
"integrity": "sha512-0EcS9wCFEzLvfiks7omJ+SiYJAiD+TzK4Pcw1UlUoGnhUxDcMKjt0P7x8wEb0u6OHu8Nb98WG3nxtlF5C7bvUQ==",
"dev": true,
"dependencies": {
"dezalgo": "1.0.3",
"hexoid": "1.0.0",
"once": "1.4.0",
"qs": "6.9.3"
"dezalgo": "^1.0.4",
"hexoid": "^1.0.0",
"once": "^1.4.0",
"qs": "^6.11.0"
},
"funding": {
"url": "https://ko-fi.com/tunnckoCore/commissions"
}
},
"node_modules/formidable/node_modules/qs": {
"version": "6.9.3",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.9.3.tgz",
"integrity": "sha512-EbZYNarm6138UKKq46tdx08Yo/q9ZhFoAXAI1meAFd2GtbRDhbZY2WQSICskT0c5q99aFzLG1D4nvTk9tqfXIw==",
"version": "6.11.0",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz",
"integrity": "sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkIk4Q==",
"dev": true,
"dependencies": {
"side-channel": "^1.0.4"
},
"engines": {
"node": ">=0.6"
},
@@ -12651,9 +12654,9 @@
"dev": true
},
"dezalgo": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.3.tgz",
"integrity": "sha512-K7i4zNfT2kgQz3GylDw40ot9GAE47sFZ9EXHFSPP6zONLgH6kWXE0KWJchkbQJLBkRazq4APwZ4OwiFFlT95OQ==",
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.4.tgz",
"integrity": "sha512-rXSP0bf+5n0Qonsb+SVVfNfIsimO4HEtmnIpPHY8Q1UCzKlQrDMfdobr8nJOOsRgWCyMRqeSBQzmWUMq7zvVig==",
"dev": true,
"requires": {
"asap": "^2.0.0",
@@ -13380,22 +13383,25 @@
}
},
"formidable": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/formidable/-/formidable-2.0.1.tgz",
"integrity": "sha512-rjTMNbp2BpfQShhFbR3Ruk3qk2y9jKpvMW78nJgx8QKtxjDVrwbZG+wvDOmVbifHyOUOQJXxqEy6r0faRrPzTQ==",
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/formidable/-/formidable-2.1.1.tgz",
"integrity": "sha512-0EcS9wCFEzLvfiks7omJ+SiYJAiD+TzK4Pcw1UlUoGnhUxDcMKjt0P7x8wEb0u6OHu8Nb98WG3nxtlF5C7bvUQ==",
"dev": true,
"requires": {
"dezalgo": "1.0.3",
"hexoid": "1.0.0",
"once": "1.4.0",
"qs": "6.9.3"
"dezalgo": "^1.0.4",
"hexoid": "^1.0.0",
"once": "^1.4.0",
"qs": "^6.11.0"
},
"dependencies": {
"qs": {
"version": "6.9.3",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.9.3.tgz",
"integrity": "sha512-EbZYNarm6138UKKq46tdx08Yo/q9ZhFoAXAI1meAFd2GtbRDhbZY2WQSICskT0c5q99aFzLG1D4nvTk9tqfXIw==",
"dev": true
"version": "6.11.0",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz",
"integrity": "sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkIk4Q==",
"dev": true,
"requires": {
"side-channel": "^1.0.4"
}
}
}
},

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) {

View File

@@ -33,7 +33,7 @@
})()
const submitCode = async() => {
const response = await fetch("http://transcendance:8080/api/v2/auth/2fa/turn-on",
const response = await fetch("http://transcendance:8080/api/v2/auth/2fa/check",
{
method : 'POST',
headers : {
@@ -48,9 +48,8 @@
wrongCode = `Wrong code`;
}
if (response.status === 200) {
userLogout();
push('/profile');
console.log('vaid Code for 2FA')
console.log('valid Code for 2FA')
}
};
@@ -106,4 +105,4 @@
font-size: 0.8em;
color: red;
}
</style>
</style>

View File

@@ -18,7 +18,7 @@
onMount( async() => {
user = await fetch('http://transcendance:8080/api/v2/user')
.then( (x) => x.json() );
// do a .catch?
// do a .catch?
if (user === undefined) {
console.log('User did not load, something more official should prolly happen')
@@ -77,7 +77,7 @@
.then(response => response.json())
// .then((result) => console.log(result))
// .then(() => console.log('successful sub of new settings'))
.then(() => push('/profile'));
}
};
@@ -203,4 +203,4 @@
}
</style>
</style>

View File

@@ -150,7 +150,7 @@ export const primaryRoutes = {
const user = await fetch('http://transcendance:8080/api/v2/user')
.then((resp) => resp.json())
console.log('in /test what is in user')
console.log('in /profile what is in user')
console.log(user)
if (user && user.username)
@@ -167,7 +167,7 @@ export const primaryRoutes = {
const user = await fetch('http://transcendance:8080/api/v2/user')
.then((resp) => resp.json())
console.log('in /test what is in user')
console.log('in /profile/* what is in user')
console.log(user)
if (user && user.username)