Skip to content
Snippets Groups Projects
Commit 7ed2d801 authored by L4168's avatar L4168
Browse files

added check for authorization header

parent a1e7ac42
No related branches found
No related tags found
3 merge requests!59Development to master,!31Development,!23Faction tasks + Piirto
...@@ -18,7 +18,7 @@ export class RolesGuard implements CanActivate { ...@@ -18,7 +18,7 @@ export class RolesGuard implements CanActivate {
private readonly reflector: Reflector, private readonly reflector: Reflector,
@InjectRepository(Game_PersonEntity) @InjectRepository(Game_PersonEntity)
private game_PersonRepository: Repository<Game_PersonEntity>, private game_PersonRepository: Repository<Game_PersonEntity>,
) {} ) {}
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 // get roles that are allowed access, identified by @Roles('role') decorators in controllers
...@@ -27,9 +27,16 @@ export class RolesGuard implements CanActivate { ...@@ -27,9 +27,16 @@ export class RolesGuard implements CanActivate {
return true; return true;
} }
const request = context.switchToHttp().getRequest(); const request = context.switchToHttp().getRequest();
const gameId = request.params.id // check for authorization header
if (!request.headers.authorization) {
return false;
}
const gameId = request.params.id;
const user = await this.getUserObject(request.headers.authorization); const user = await this.getUserObject(request.headers.authorization);
const role = await this.game_PersonRepository.findOne({person: user['id'], game: gameId}) const role = await this.game_PersonRepository.findOne({
person: user['id'],
game: gameId,
});
// check that the role matches the criteria and that token is valid for this game // check that the role matches the criteria and that token is valid for this game
return role && roles.includes(role['role']); return role && roles.includes(role['role']);
} }
...@@ -42,7 +49,7 @@ export class RolesGuard implements CanActivate { ...@@ -42,7 +49,7 @@ export class RolesGuard implements CanActivate {
// get the token // get the token
const token = auth.split(' ')[1]; const token = auth.split(' ')[1];
try { try {
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}`;
throw new HttpException(message, HttpStatus.FORBIDDEN); throw new HttpException(message, HttpStatus.FORBIDDEN);
......
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