diff --git a/backend/src/managers/auth/auth.service.ts b/backend/src/managers/auth/auth.service.ts index 0174d45..68210e8 100644 --- a/backend/src/managers/auth/auth.service.ts +++ b/backend/src/managers/auth/auth.service.ts @@ -13,7 +13,10 @@ export class AuthManagerService { async createToken(user: EUserBackend): Promise { const jwtData: JwtDataDto = plainToClass(JwtDataDto, { - user, + user: { + username: user.username, + roles: user.roles, + }, }); const errors = await strictValidate(jwtData); diff --git a/backend/src/managers/auth/guards/jwt.strategy.ts b/backend/src/managers/auth/guards/jwt.strategy.ts index a482fae..3b9c77c 100644 --- a/backend/src/managers/auth/guards/jwt.strategy.ts +++ b/backend/src/managers/auth/guards/jwt.strategy.ts @@ -1,8 +1,7 @@ import { Inject, Injectable, - Logger, - UnauthorizedException + Logger } from '@nestjs/common'; import { PassportStrategy } from '@nestjs/passport'; import { plainToClass } from 'class-transformer'; @@ -23,14 +22,14 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') { }); } - async validate(payload: any): Promise { + async validate(payload: any): Promise { const jwt = plainToClass(JwtDataDto, payload); const errors = await strictValidate(jwt); if (errors.length > 0) { this.logger.warn(errors); - throw new UnauthorizedException(); + return false; } return jwt.user; diff --git a/shared/src/dto/jwt.dto.ts b/shared/src/dto/jwt.dto.ts index f30704f..da96409 100644 --- a/shared/src/dto/jwt.dto.ts +++ b/shared/src/dto/jwt.dto.ts @@ -1,12 +1,12 @@ import { Type } from 'class-transformer'; import { IsDefined, IsInt, IsOptional, ValidateNested } from 'class-validator'; -import { EUser } from '../entities/user.entity'; +import { RoledUser } from '../entities/user.entity'; export class JwtDataDto { @IsDefined() @ValidateNested() - @Type(() => EUser) - user: EUser; + @Type(() => RoledUser) + user: RoledUser; @IsOptional() @IsInt() diff --git a/shared/src/entities/user.entity.ts b/shared/src/entities/user.entity.ts index e7d70f9..6cba91d 100644 --- a/shared/src/entities/user.entity.ts +++ b/shared/src/entities/user.entity.ts @@ -29,15 +29,18 @@ export class SimpleUser extends SimpleUsername { password: string; } -// Actual entity that goes in the db -export class EUser extends SimpleUsername { - @IsOptional() - @IsInt() - id?: number; - +// Add a user object with just the username and roles for jwt +export class RoledUser extends SimpleUsername { @IsArray() @IsString({ each: true }) roles: Roles; +} + +// Actual entity that goes in the db +export class EUser extends RoledUser { + @IsOptional() + @IsInt() + id?: number; @IsOptional() @Exclude()