Skip to content
Snippets Groups Projects
Commit 9254070b authored by L4168's avatar L4168
Browse files

added gameId to jwt to deny admin exploit

parent 211faef1
No related branches found
No related tags found
2 merge requests!59Development to master,!18Join game
...@@ -75,10 +75,11 @@ export class Game_PersonEntity { ...@@ -75,10 +75,11 @@ export class Game_PersonEntity {
return gametoken; return gametoken;
} }
private get gametoken() { private get gametoken() {
const { gamepersonId, role } = this; const { gamepersonId, game, role } = this;
return jwt.sign( return jwt.sign(
{ {
gamepersonId, gamepersonId,
game,
role, role,
}, },
process.env.SECRET, process.env.SECRET,
......
...@@ -5,9 +5,7 @@ import { ...@@ -5,9 +5,7 @@ import {
HttpException, HttpException,
HttpStatus, HttpStatus,
} from '@nestjs/common'; } from '@nestjs/common';
import { Observable } from 'rxjs';
import { Reflector } from '@nestjs/core'; import { Reflector } from '@nestjs/core';
import * as jwt from 'jsonwebtoken'; import * as jwt from 'jsonwebtoken';
@Injectable() @Injectable()
...@@ -15,13 +13,16 @@ export class RolesGuard implements CanActivate { ...@@ -15,13 +13,16 @@ export class RolesGuard implements CanActivate {
constructor(private readonly reflector: Reflector) {} constructor(private readonly reflector: Reflector) {}
async canActivate(context: ExecutionContext): Promise<boolean> { async canActivate(context: ExecutionContext): Promise<boolean> {
// get roles that are allowed access, identified by @Roles('role') decorators in controllers
const roles = this.reflector.get<string[]>('roles', context.getHandler()); const roles = this.reflector.get<string[]>('roles', context.getHandler());
if (!roles) { if (!roles) {
return true; return true;
} }
const request = context.switchToHttp().getRequest(); const request = context.switchToHttp().getRequest();
const gameId = request.params.id
const role = await this.checkRole(request.headers.authorization); const role = await this.checkRole(request.headers.authorization);
return roles.includes(role['role']) // check that the role matches the criteria and that token is valid for this game
return roles.includes(role['role']) && role['game']['id'] === gameId;
} }
async checkRole(auth: string) { async checkRole(auth: string) {
...@@ -32,7 +33,6 @@ export class RolesGuard implements CanActivate { ...@@ -32,7 +33,6 @@ export class RolesGuard implements CanActivate {
// get the token // get the token
const token = auth.split(' ')[1]; const token = auth.split(' ')[1];
try { try {
console.log(jwt.verify(token, process.env.SECRET))
return await jwt.verify(token, process.env.SECRET) return await jwt.verify(token, process.env.SECRET)
} catch (err) { } catch (err) {
const message = `Token error: ${err.message || err.name}`; const message = `Token error: ${err.message || err.name}`;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment