Refactor WorkspaceAuthContext to use discriminated union types#17491
Refactor WorkspaceAuthContext to use discriminated union types#17491
Conversation
...ages/twenty-server/src/engine/core-modules/tool-provider/providers/database-tool.provider.ts
Show resolved
Hide resolved
Greptile OverviewGreptile SummaryThis PR refactors Key changes:
The refactoring improves type safety by making it explicit which fields are available in different authentication scenarios, eliminating runtime checks scattered throughout the codebase. Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Client
participant Middleware as WorkspaceAuthContextMiddleware
participant Builder as Builder Utils
participant Storage as WorkspaceAuthContextStorage
participant Consumer as Query Runners/Services
Client->>Middleware: HTTP Request with auth info
alt API Key Auth
Middleware->>Builder: buildApiKeyAuthContext(workspace, apiKey)
Builder-->>Middleware: ApiKeyWorkspaceAuthContext
else User Auth
Middleware->>Builder: buildUserAuthContext(workspace, user, ...)
Builder-->>Middleware: UserWorkspaceAuthContext
else Application Auth
Middleware->>Builder: buildApplicationAuthContext(workspace, app)
Builder-->>Middleware: ApplicationWorkspaceAuthContext
else No specific auth
Middleware->>Middleware: throw AuthException
end
Middleware->>Storage: withWorkspaceAuthContext(context, callback)
Storage->>Consumer: Execute with typed context
Consumer->>Consumer: Use type guards<br/>(isUserAuthContext, etc.)
Consumer->>Consumer: Access type-specific fields<br/>(user, apiKey, application)
Consumer-->>Storage: Return result
Storage-->>Middleware: Complete
Middleware-->>Client: Response
|
packages/twenty-server/src/engine/core-modules/auth/types/workspace-auth-context.type.ts
Outdated
Show resolved
Hide resolved
packages/twenty-server/src/engine/core-modules/auth/guards/is-system-auth-context.guard.ts
Show resolved
Hide resolved
packages/twenty-server/src/engine/api/mcp/services/mcp-protocol.service.ts
Outdated
Show resolved
Hide resolved
| } | ||
|
|
||
| const authContext: WorkspaceAuthContext = buildUserAuthContext({ | ||
| workspace: { id: context.workspaceId } as WorkspaceEntity, |
There was a problem hiding this comment.
I'm reusing the existing code but we should query the whole workspace entity from the cache here as well instead of type assertion
There was a problem hiding this comment.
1 issue found across 59 files
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="packages/twenty-server/src/engine/api/mcp/services/mcp-protocol.service.ts">
<violation number="1" location="packages/twenty-server/src/engine/api/mcp/services/mcp-protocol.service.ts:109">
P2: User lookup uses userWorkspaceId as the UserEntity id, but userWorkspaceId is a separate identifier. This will fail to resolve users and reject valid requests when ids differ.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
packages/twenty-server/src/engine/api/mcp/services/mcp-protocol.service.ts
Outdated
Show resolved
Hide resolved
packages/twenty-server/src/engine/api/rest/core/handlers/rest-api-base.handler.ts
Show resolved
Hide resolved
|
🚀 Preview Environment Ready! Your preview environment is available at: http://bore.pub:56300 This environment will automatically shut down when the PR is closed or after 5 hours. |
This reverts commit 8469968.
Context
The previous WorkspaceAuthContext was a single interface with many optional fields, making it unclear which fields are available in different authentication scenarios. This made the code harder to reason about and required runtime checks scattered throughout the codebase.
Changes
-> UserWorkspaceAuthContext - for authenticated users
-> ApiKeyWorkspaceAuthContext - for API key authentication
-> ApplicationWorkspaceAuthContext - for application-based auth
-> SystemWorkspaceAuthContext - for system/internal operations
-> PendingActivationUserWorkspaceAuthContext - for pending workspace creation, similar to UserWorkspaceAuthContext but without workspaceMember in it
Notes