-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Permissions] Add userWorkspaceId to JWT token #9954
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
This PR adds userWorkspaceId to JWT tokens and request context to support role-based permission checking across the application.
- Added new
USER_WORKSPACE_NOT_FOUND
exception code and validation inJwtAuthStrategy
to handle cases where user workspace associations are missing - Added
AuthUserWorkspaceId
decorator in/decorators/auth/auth-user-workspace-id.decorator.ts
to extract workspace IDs from requests, though it lacks error handling - Extended
AuthContext
andJwtPayload
types in/types/auth-context.type.ts
to include optionaluserWorkspaceId
field - Modified
AccessTokenService
to include userWorkspaceId in token generation and validation flow - Added UserWorkspace entity to TypeORM configurations in both AuthModule and TokenModule for database operations
10 file(s) reviewed, 3 comment(s)
Edit PR Review Bot Settings | Greptile
const userWorkspace = await this.userWorkspaceRepository.findOne({ | ||
where: { | ||
id: payload.userWorkspaceId, | ||
}, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Consider also validating that the userWorkspace belongs to both the user and workspace being validated to prevent unauthorized access.
const userWorkspace = await this.userWorkspaceRepository.findOne({ | |
where: { | |
id: payload.userWorkspaceId, | |
}, | |
}); | |
const userWorkspace = await this.userWorkspaceRepository.findOne({ | |
where: { | |
id: payload.userWorkspaceId, | |
userId: user.id, | |
workspaceId: workspace.id, | |
}, | |
}); |
const userWorkspace = await this.userWorkspaceRepository.findOne({ | ||
where: { | ||
userId: user.id, | ||
workspaceId, | ||
}, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: No error handling if userWorkspace lookup fails. Consider throwing AuthException with USER_WORKSPACE_NOT_FOUND code since userWorkspaceId is required for permissions.
(data: unknown, ctx: ExecutionContext) => { | ||
const request = getRequest(ctx); | ||
|
||
return request.userWorkspaceId; | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: No error handling if userWorkspaceId is undefined. Could silently fail permission checks. Consider throwing AuthException with USER_WORKSPACE_NOT_FOUND code.
(data: unknown, ctx: ExecutionContext) => { | |
const request = getRequest(ctx); | |
return request.userWorkspaceId; | |
}, | |
(data: unknown, ctx: ExecutionContext) => { | |
const request = getRequest(ctx); | |
if (!request.userWorkspaceId) { | |
throw new UnauthorizedException('User workspace not found'); | |
} | |
return request.userWorkspaceId; | |
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you @ijreilly
This information will be used to fetch a user's role and check their permissions