Skip to content

Commit ae156d2

Browse files
committed
some more fixes for 403
1 parent d390d1a commit ae156d2

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

backend/src/auth/auth.service.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { Injectable, UnauthorizedException } from "@nestjs/common";
1+
import {
2+
ForbiddenException,
3+
Injectable,
4+
Logger,
5+
UnauthorizedException,
6+
} from "@nestjs/common";
27
import { ConfigService } from "@nestjs/config";
38
import { JwtService } from "@nestjs/jwt";
49
import jwksClient from "jwks-rsa";
@@ -61,20 +66,20 @@ export class AuthService {
6166
publicKey: key,
6267
algorithms: ["RS256"],
6368
}) as KeycloakUser;
64-
69+
console.info("payload", payload);
6570
// Validate audience claim
6671
await this.validateAudience(payload);
6772
// validate role
6873
if (!payload.client_roles) {
69-
throw new UnauthorizedException("Token missing client roles");
74+
throw new ForbiddenException("Token missing client roles");
7075
}
71-
if (this.hasRole(payload, "ai-poc-participant")) {
72-
throw new UnauthorizedException("User does not have the required role");
76+
if (!this.hasRole(payload, "ai-poc-participant")) {
77+
throw new ForbiddenException("User does not have the required role");
7378
}
7479
return payload;
7580
} catch (error) {
7681
console.error("Token validation error:", error);
77-
throw new UnauthorizedException("Invalid or expired token");
82+
throw error;
7883
}
7984
}
8085

@@ -116,6 +121,10 @@ export class AuthService {
116121

117122
hasRole(user: KeycloakUser, role: string): boolean {
118123
// Check realm roles
124+
if (!Array.isArray(user.client_roles)) {
125+
console.info("User has no client roles is not array");
126+
return false;
127+
}
119128
if (user.client_roles?.includes(role)) {
120129
return true;
121130
}

backend/src/auth/jwt-auth.guard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class JwtAuthGuard implements CanActivate {
3333
request.user = user;
3434
return true;
3535
} catch (error) {
36-
throw new UnauthorizedException("Invalid authorization token");
36+
throw error;
3737
}
3838
}
3939

0 commit comments

Comments
 (0)