lien serveur de jeu => serveur nestjs
This commit is contained in:
@@ -7,7 +7,7 @@ POSTGRES_DATABASE=transcendance_db
|
|||||||
|
|
||||||
# OAUTH2 42 API
|
# OAUTH2 42 API
|
||||||
FORTYTWO_CLIENT_ID=u-s4t2ud-49dc7b539bcfe1acb48b928b2b281671c99fc5bfab1faca57a536ab7e0075500
|
FORTYTWO_CLIENT_ID=u-s4t2ud-49dc7b539bcfe1acb48b928b2b281671c99fc5bfab1faca57a536ab7e0075500
|
||||||
FORTYTWO_CLIENT_SECRET=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
|
FORTYTWO_CLIENT_SECRET=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
|
||||||
FORTYTWO_CALLBACK_URL=http://transcendance:8080/api/v2/auth/redirect
|
FORTYTWO_CALLBACK_URL=http://transcendance:8080/api/v2/auth/redirect
|
||||||
COOKIE_SECRET=248cdc831110eec8796d7c1edbf79835
|
COOKIE_SECRET=248cdc831110eec8796d7c1edbf79835
|
||||||
# JWT
|
# JWT
|
||||||
@@ -20,3 +20,6 @@ REDIS_PORT=6379
|
|||||||
REDIS_PASSWORD=1a5e04138b91b3d683c708e4689454c2
|
REDIS_PASSWORD=1a5e04138b91b3d683c708e4689454c2
|
||||||
#2fa
|
#2fa
|
||||||
TWO_FACTOR_AUTHENTICATION_APP_NAME=Transcendance
|
TWO_FACTOR_AUTHENTICATION_APP_NAME=Transcendance
|
||||||
|
|
||||||
|
NAME_OF_REDIS_DB = tokenGameDatabase
|
||||||
|
TICKET_FOR_PLAYING_GAME_SECRET = a2aef785c388497f5fca18f9ccff37ed
|
||||||
|
|||||||
1687
srcs/requirements/nestjs/api_back/package-lock.json
generated
1687
srcs/requirements/nestjs/api_back/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -7,7 +7,7 @@ import { ConfigModule } from '@nestjs/config';
|
|||||||
import { FriendshipsModule } from './friendship/friendships.module';
|
import { FriendshipsModule } from './friendship/friendships.module';
|
||||||
import { AuthenticationModule } from './auth/42/authentication.module';
|
import { AuthenticationModule } from './auth/42/authentication.module';
|
||||||
import { PassportModule } from '@nestjs/passport';
|
import { PassportModule } from '@nestjs/passport';
|
||||||
// import { GameModule } from './game/game/game.module';
|
import { GameModule } from './game/game.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [UsersModule,
|
imports: [UsersModule,
|
||||||
@@ -27,7 +27,7 @@ import { PassportModule } from '@nestjs/passport';
|
|||||||
//avec une classe pour le module
|
//avec une classe pour le module
|
||||||
synchronize: true,
|
synchronize: true,
|
||||||
}),
|
}),
|
||||||
// GameModule,
|
GameModule,
|
||||||
],
|
],
|
||||||
controllers: [AppController],
|
controllers: [AppController],
|
||||||
providers: [AppService],
|
providers: [AppService],
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import { IsBoolean, IsInt, IsNotEmpty, IsNumber, IsString } from "class-validator";
|
||||||
|
|
||||||
|
export class GrantTicketDto {
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
playerOneUsername : string
|
||||||
|
@IsString()
|
||||||
|
playerTwousername : string
|
||||||
|
@IsNumber()
|
||||||
|
gameOptions : number
|
||||||
|
@IsBoolean()
|
||||||
|
isGameIsWithInvitation : boolean
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import { IsBase64, IsBoolean, IsNotEmpty, IsNumber, IsString } from "class-validator";
|
||||||
|
|
||||||
|
export class ValidateTicketDto {
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
playerOneUsername : string
|
||||||
|
@IsString()
|
||||||
|
playerTwousername : string
|
||||||
|
@IsNumber()
|
||||||
|
gameOptions : number
|
||||||
|
@IsBoolean()
|
||||||
|
isGameIsWithInvitation : boolean
|
||||||
|
@IsNumber()
|
||||||
|
numberOfRegisteredUser : number
|
||||||
|
@IsBase64()
|
||||||
|
@IsNotEmpty()
|
||||||
|
token : string
|
||||||
|
}
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
|
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
|
||||||
|
|
||||||
@Entity('game')
|
@Entity('game')
|
||||||
export class game {
|
export class Game {
|
||||||
|
|
||||||
@PrimaryGeneratedColumn()
|
@PrimaryGeneratedColumn()
|
||||||
id: number;
|
id: number;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
|
||||||
|
|
||||||
|
@Entity('tokenGame')
|
||||||
|
export class TokenGame {
|
||||||
|
|
||||||
|
@PrimaryGeneratedColumn()
|
||||||
|
id: number;
|
||||||
|
@Column()
|
||||||
|
playerOneUsername : string
|
||||||
|
@Column({nullable: true})
|
||||||
|
playerTwousername : string
|
||||||
|
@Column()
|
||||||
|
gameOptions : number
|
||||||
|
@Column()
|
||||||
|
isGameIsWithInvitation : boolean
|
||||||
|
@Column({default: 0, nullable: true})
|
||||||
|
numberOfRegisteredUser : number
|
||||||
|
@Column()
|
||||||
|
token : string
|
||||||
|
}
|
||||||
@@ -1,4 +1,58 @@
|
|||||||
import { Controller } from '@nestjs/common';
|
import { Body, Controller, Get, HttpException, HttpStatus, Post, Req, UseGuards } from '@nestjs/common';
|
||||||
|
import { request } from 'http';
|
||||||
|
import { use } from 'passport';
|
||||||
|
import { AuthenticateGuard, TwoFactorGuard } from 'src/auth/42/guards/42guards';
|
||||||
|
import { User } from 'src/users/entities/user.entity';
|
||||||
|
import { UsersService } from 'src/users/users.service';
|
||||||
|
import { GrantTicketDto } from './dto/grantTicket.dto';
|
||||||
|
import { ValidateTicketDto } from './dto/validateTicket.dto';
|
||||||
|
import { GameService } from './game.service';
|
||||||
|
|
||||||
@Controller('game')
|
@Controller('game')
|
||||||
export class GameController {}
|
export class GameController {
|
||||||
|
constructor (private readonly gameService : GameService,
|
||||||
|
private readonly userService : UsersService) { }
|
||||||
|
|
||||||
|
@Post('ticket')
|
||||||
|
@UseGuards(AuthenticateGuard)
|
||||||
|
@UseGuards(TwoFactorGuard)
|
||||||
|
async grantTicket(@Req() req, @Body() grantTicketDto : GrantTicketDto)
|
||||||
|
{
|
||||||
|
const user : User = req.user
|
||||||
|
if (grantTicketDto.playerOneUsername != user.username)
|
||||||
|
return new HttpException('You can\'t request a game for another person.', 403 )
|
||||||
|
else if (user.status !== "connected")
|
||||||
|
return new HttpException('You must not be in game...', 403 )
|
||||||
|
return this.gameService.generateToken(user, grantTicketDto);
|
||||||
|
}
|
||||||
|
|
||||||
|
//N'est valable que pour le game-serveur.
|
||||||
|
@Post('gameServer/validate')
|
||||||
|
async validateTicket(@Body() validateTicketDto : ValidateTicketDto, @Req() request)
|
||||||
|
{
|
||||||
|
if (await this.gameService.validateToken(validateTicketDto) === false)
|
||||||
|
return new HttpException("The token is not valid", HttpStatus.NOT_FOUND);
|
||||||
|
return HttpStatus.OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post('decline')
|
||||||
|
@UseGuards(AuthenticateGuard)
|
||||||
|
@UseGuards(TwoFactorGuard)
|
||||||
|
async declineInvitation(@Body() validateTicketDto : ValidateTicketDto, @Req() req)
|
||||||
|
{
|
||||||
|
const user : User = req.user;
|
||||||
|
if (user.username !== validateTicketDto.playerTwousername)
|
||||||
|
return new HttpException("This cannot be done", HttpStatus.FORBIDDEN);
|
||||||
|
return this.gameService.declineInvitation(validateTicketDto);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get('invitations')
|
||||||
|
@UseGuards(AuthenticateGuard)
|
||||||
|
@UseGuards(TwoFactorGuard)
|
||||||
|
async findInvitations(@Req() request)
|
||||||
|
{
|
||||||
|
const user : User = request.user;
|
||||||
|
return this.gameService.findInvitations(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
// import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
// import { GameController } from './game.controller';
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
// import { GameService } from './game.service';
|
import { User } from 'src/users/entities/user.entity';
|
||||||
|
import { UsersService } from 'src/users/users.service';
|
||||||
|
import { TokenGame } from './entity/tokenGame.entity';
|
||||||
|
import { GameController } from './game.controller';
|
||||||
|
import { GameService } from './game.service';
|
||||||
|
|
||||||
// @Module({
|
@Module({
|
||||||
// controllers: [GameController],
|
imports: [TypeOrmModule.forFeature([TokenGame, User])],
|
||||||
// providers: [GameService]
|
controllers: [GameController],
|
||||||
// })
|
providers: [GameService, UsersService]
|
||||||
// export class GameModule {}
|
})
|
||||||
|
export class GameModule {}
|
||||||
|
|||||||
@@ -1,5 +1,150 @@
|
|||||||
// import { Injectable } from '@nestjs/common';
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
|
import { createCipheriv, randomBytes, scrypt } from 'crypto';
|
||||||
|
import { User } from 'src/users/entities/user.entity';
|
||||||
|
import { Repository } from 'typeorm';
|
||||||
|
import { promisify } from 'util';
|
||||||
|
import { GrantTicketDto } from './dto/grantTicket.dto';
|
||||||
|
import { Game } from './entity/game.entity';
|
||||||
|
import { ValidateTicketDto } from './dto/validateTicket.dto';
|
||||||
|
import { TokenGame } from './entity/tokenGame.entity';
|
||||||
|
|
||||||
// @Injectable('game')
|
@Injectable()
|
||||||
// export class GameService {}
|
export class GameService {
|
||||||
|
constructor (
|
||||||
|
@InjectRepository(Game)
|
||||||
|
private readonly gameRepository : Repository<Game>,
|
||||||
|
@InjectRepository(User)
|
||||||
|
private readonly userRepository : Repository<User>,
|
||||||
|
@InjectRepository(TokenGame)
|
||||||
|
private readonly tokenGameRepository : Repository<TokenGame>,
|
||||||
|
) { }
|
||||||
|
|
||||||
|
async encryptToken(toEncrypt : string) : Promise<string> {
|
||||||
|
const iv = randomBytes(16);
|
||||||
|
const password = process.env.TICKET_FOR_PLAYING_GAME_SECRET + new Date();
|
||||||
|
const key = (await promisify(scrypt)(password, 'salt', 32)) as Buffer;
|
||||||
|
const cipher = createCipheriv('aes-256-ctr', key, iv);
|
||||||
|
const encryptedText = Buffer.concat([
|
||||||
|
cipher.update(toEncrypt),
|
||||||
|
cipher.final(),
|
||||||
|
]);
|
||||||
|
const encryptedTextToReturn = encryptedText.toString('base64');
|
||||||
|
return encryptedTextToReturn
|
||||||
|
}
|
||||||
|
|
||||||
|
async generateToken(user : User, grantTicketDto : GrantTicketDto)
|
||||||
|
{
|
||||||
|
if (grantTicketDto.isGameIsWithInvitation === true)
|
||||||
|
{
|
||||||
|
let secondUser : User;
|
||||||
|
(user.username === grantTicketDto.playerOneUsername) ?
|
||||||
|
secondUser = await this.userRepository.findOneBy({username : grantTicketDto.playerTwousername}) :
|
||||||
|
secondUser = await this.userRepository.findOneBy({username : grantTicketDto.playerOneUsername})
|
||||||
|
if (!secondUser)
|
||||||
|
return new HttpException("The requested second player does not exist", HttpStatus.NOT_FOUND);
|
||||||
|
const encryptedTextToReturn = await this.encryptToken(user.username + '_' + secondUser.username + '_'
|
||||||
|
+ grantTicketDto.gameOptions + '_' + grantTicketDto.isGameIsWithInvitation + '_' + new Date())
|
||||||
|
const validateTicketDto : ValidateTicketDto = new ValidateTicketDto();
|
||||||
|
validateTicketDto.playerOneUsername = grantTicketDto.playerOneUsername;
|
||||||
|
validateTicketDto.playerTwousername = grantTicketDto.playerTwousername;
|
||||||
|
validateTicketDto.numberOfRegisteredUser = 0;
|
||||||
|
validateTicketDto.isGameIsWithInvitation = grantTicketDto.isGameIsWithInvitation;
|
||||||
|
validateTicketDto.gameOptions = grantTicketDto.gameOptions;
|
||||||
|
validateTicketDto.token = encryptedTextToReturn;
|
||||||
|
const tok = this.tokenGameRepository.create(validateTicketDto);
|
||||||
|
this.tokenGameRepository.save(tok);
|
||||||
|
return { token : encryptedTextToReturn };
|
||||||
|
}
|
||||||
|
else if (grantTicketDto.isGameIsWithInvitation === false) {
|
||||||
|
const encryptedTextToReturn = await this.encryptToken(user.username + '_'
|
||||||
|
+ grantTicketDto.gameOptions + '_' + grantTicketDto.isGameIsWithInvitation + '_' + new Date())
|
||||||
|
const validateTicketDto : ValidateTicketDto = new ValidateTicketDto();
|
||||||
|
validateTicketDto.playerOneUsername = grantTicketDto.playerOneUsername;
|
||||||
|
validateTicketDto.playerTwousername = "";
|
||||||
|
validateTicketDto.numberOfRegisteredUser = 0;
|
||||||
|
validateTicketDto.isGameIsWithInvitation = grantTicketDto.isGameIsWithInvitation;
|
||||||
|
validateTicketDto.gameOptions = grantTicketDto.gameOptions;
|
||||||
|
validateTicketDto.token = encryptedTextToReturn;
|
||||||
|
const tok = this.tokenGameRepository.create(validateTicketDto);
|
||||||
|
this.tokenGameRepository.save(tok);
|
||||||
|
return { token : encryptedTextToReturn };
|
||||||
|
}
|
||||||
|
return new HttpException("Something went wrong !", HttpStatus.INTERNAL_SERVER_ERROR)
|
||||||
|
}
|
||||||
|
|
||||||
|
async validateToken(validateTicketDto : ValidateTicketDto) {
|
||||||
|
|
||||||
|
let tokenGame : TokenGame;
|
||||||
|
if (validateTicketDto.isGameIsWithInvitation === true)
|
||||||
|
{
|
||||||
|
tokenGame = await this.tokenGameRepository.createQueryBuilder('tokenGame')
|
||||||
|
.where('tokenGame.playerOneUsername = :playerOneUsername', {playerOneUsername : validateTicketDto.playerOneUsername})
|
||||||
|
.andWhere('tokenGame.playerTwousername = :playerTwousername', {playerTwousername : validateTicketDto.playerTwousername})
|
||||||
|
.andWhere('tokenGame.gameOption = :gameOption', {gameOption : validateTicketDto.gameOptions})
|
||||||
|
.andWhere('tokenGame.isGameIsWithInvitation = :isGameIsWithInvitation', {isGameIsWithInvitation: validateTicketDto.isGameIsWithInvitation})
|
||||||
|
.andWhere('tokenGame.token = :token', {token : validateTicketDto.token})
|
||||||
|
.getOne();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tokenGame = await this.tokenGameRepository.createQueryBuilder('tokenGame')
|
||||||
|
.where('tokenGame.playerOneUsername = :playerOneUsername', {playerOneUsername : validateTicketDto.playerOneUsername})
|
||||||
|
.andWhere('tokenGame.gameOption = :gameOption', {gameOption : validateTicketDto.gameOptions})
|
||||||
|
.andWhere('tokenGame.isGameIsWithInvitation = :isGameIsWithInvitation', {isGameIsWithInvitation: validateTicketDto.isGameIsWithInvitation})
|
||||||
|
.andWhere('tokenGame.token = :token', {token : validateTicketDto.token})
|
||||||
|
.getOne();
|
||||||
|
}
|
||||||
|
if (tokenGame)
|
||||||
|
tokenGame.numberOfRegisteredUser++;
|
||||||
|
if (tokenGame && tokenGame.isGameIsWithInvitation === false)
|
||||||
|
{
|
||||||
|
this.tokenGameRepository.remove(tokenGame)
|
||||||
|
this.userRepository.update(tokenGame.playerOneUsername, {status : "In Game"})
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (tokenGame && tokenGame.isGameIsWithInvitation === true)
|
||||||
|
{
|
||||||
|
if (tokenGame.numberOfRegisteredUser === 2)
|
||||||
|
{
|
||||||
|
this.tokenGameRepository.remove(tokenGame)
|
||||||
|
this.userRepository.update(tokenGame.playerOneUsername, {status : "In Game"})
|
||||||
|
this.userRepository.update(tokenGame.playerTwousername, {status : "In Game"})
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
async findInvitations(user : User) {
|
||||||
|
const game = await this.tokenGameRepository.createQueryBuilder('tokengame')
|
||||||
|
.where('tokenGame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username})
|
||||||
|
.getMany();
|
||||||
|
if (!game)
|
||||||
|
return new HttpException( "No invitations !", HttpStatus.NOT_FOUND);
|
||||||
|
let partialGame : Partial<TokenGame>[] = [];
|
||||||
|
for (const gameToken of game) {
|
||||||
|
partialGame.push({
|
||||||
|
playerOneUsername : gameToken.playerOneUsername,
|
||||||
|
playerTwousername : gameToken.playerTwousername,
|
||||||
|
gameOptions : gameToken.gameOptions,
|
||||||
|
isGameIsWithInvitation : gameToken.isGameIsWithInvitation,
|
||||||
|
token : gameToken.token,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return partialGame;
|
||||||
|
}
|
||||||
|
|
||||||
|
async declineInvitation(validateTicketDto : ValidateTicketDto)
|
||||||
|
{
|
||||||
|
const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokenGame')
|
||||||
|
.where('tokenGame.playerOneUsername = :playerOneUsername', {playerOneUsername : validateTicketDto.playerOneUsername})
|
||||||
|
.andWhere('tokenGame.playerTwousername = :playerTwousername', {playerTwousername : validateTicketDto.playerTwousername})
|
||||||
|
.andWhere('tokenGame.gameOption = :gameOption', {gameOption : validateTicketDto.gameOptions})
|
||||||
|
.andWhere('tokenGame.isGameIsWithInvitation = :isGameIsWithInvitation', {isGameIsWithInvitation: validateTicketDto.isGameIsWithInvitation})
|
||||||
|
.andWhere('tokenGame.token = :token', {token : validateTicketDto.token})
|
||||||
|
.getOne();
|
||||||
|
return this.tokenGameRepository.remove(tokenGame);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
import {RedisService} from "@nestjs/redis"
|
|
||||||
@@ -83,7 +83,6 @@ export class UsersController {
|
|||||||
async update(@Req() req, @Body(new ValidationPipe()) usersUpdateDto: UpdateUsersDto, @Res() response : Response) {
|
async update(@Req() req, @Body(new ValidationPipe()) usersUpdateDto: UpdateUsersDto, @Res() response : Response) {
|
||||||
console.log("DANS PATCH USERS");
|
console.log("DANS PATCH USERS");
|
||||||
const user = await this.usersService.update(req.user.id, usersUpdateDto);
|
const user = await this.usersService.update(req.user.id, usersUpdateDto);
|
||||||
// const user : User = req.user;
|
|
||||||
if (user.isEnabledTwoFactorAuth === false && user.isTwoFactorAuthenticated === true)
|
if (user.isEnabledTwoFactorAuth === false && user.isTwoFactorAuthenticated === true)
|
||||||
this.usersService.setIsTwoFactorAuthenticatedWhenLogout(user.id);
|
this.usersService.setIsTwoFactorAuthenticatedWhenLogout(user.id);
|
||||||
console.log ("Enbale 2FA " + user.isEnabledTwoFactorAuth + " Is authenticated " + user.isTwoFactorAuthenticated);
|
console.log ("Enbale 2FA " + user.isEnabledTwoFactorAuth + " Is authenticated " + user.isTwoFactorAuthenticated);
|
||||||
|
|||||||
Reference in New Issue
Block a user