Repository navigation
Enterprise RBAC / SSO 기반 정리 + Workspace 반응형 UX 보강 #190
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
Closed
Closed
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8dcffe2
docs: add plan for enterprise RBAC and responsive workspace
seonghobae 89c7186
fix(frontend): improve workspace responsiveness
seonghobae 942d477
feat(auth): add scoped RBAC foundation
seonghobae a5f764b
fix(auth): remove workspace override trust and align provider RBAC wi…
seonghobae 2f241bd
feat(auth): add enterprise role foundation and responsive workspace s…
seonghobae db10303
fix(auth): finalize org-scoped runner and workspace auth cleanup
seonghobae 25649d5
test(auth): scope dependency override cleanup to get_db only
seonghobae ba449bc
fix(auth): key runner configs by organization and align provider auth…
seonghobae File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,124 @@ | ||
| from dataclasses import dataclass | ||
| from typing import Literal, cast | ||
|
|
||
| from fastapi import Depends, Header, HTTPException | ||
|
|
||
| from core.config import settings | ||
|
|
||
| async def get_current_user(x_user_id: str | None = Header(None, alias="X-User-Id")) -> str: | ||
|
|
||
| RoleName = Literal["platform_admin", "organization_admin", "group_admin", "member"] | ||
| SCOPED_ROLES: set[str] = {"platform_admin", "organization_admin", "group_admin", "member"} | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class AuthContext: | ||
| user_id: str | ||
| role: RoleName | ||
| organization_id: str | None | ||
| group_ids: tuple[str, ...] | ||
| workspace_id: str | ||
|
|
||
|
|
||
| def _normalize_header_value(value: object) -> str | None: | ||
| if not isinstance(value, str): | ||
| return None | ||
| normalized = value.strip() | ||
| return normalized or None | ||
|
|
||
|
|
||
| def _derive_role(user_id: str, requested_role: str | None) -> RoleName: | ||
| if not (settings.DEBUG or settings.TRUST_DEV_HEADERS): | ||
| return "member" | ||
| if requested_role in SCOPED_ROLES: | ||
| return cast(RoleName, requested_role) | ||
| return "organization_admin" if user_id == "admin" else "member" | ||
|
|
||
|
|
||
| def _parse_group_ids(group_ids_header: str | None) -> tuple[str, ...]: | ||
| if not group_ids_header: | ||
| return () | ||
| return tuple(group_id.strip() for group_id in group_ids_header.split(",") if group_id.strip()) | ||
|
|
||
|
|
||
| def _derive_workspace_id( | ||
| user_id: str, | ||
| organization_id: str | None, | ||
| ) -> str: | ||
| if organization_id: | ||
| return f"workspace-{organization_id}" | ||
| return f"workspace-{user_id}" | ||
|
|
||
|
|
||
| def ensure_organization_access(auth_context: AuthContext, organization_id: str) -> None: | ||
| if auth_context.role == "platform_admin": | ||
| return | ||
| if auth_context.organization_id != organization_id: | ||
| raise HTTPException(status_code=403, detail="Resource belongs to a different organization") | ||
|
seonghobae marked this conversation as resolved.
Resolved
|
||
|
|
||
|
|
||
| async def get_auth_context( | ||
| x_user_id: str | None = Header(None, alias="X-User-Id"), | ||
| x_user_role: str | None = Header(None, alias="X-User-Role"), | ||
| x_organization_id: str | None = Header(None, alias="X-Organization-Id"), | ||
| x_group_ids: str | None = Header(None, alias="X-Group-Ids"), | ||
| ) -> AuthContext: | ||
| return build_auth_context( | ||
|
greptile-apps[bot] marked this conversation as resolved.
Resolved
|
||
| x_user_id=x_user_id, | ||
| x_user_role=x_user_role, | ||
| x_organization_id=x_organization_id, | ||
| x_group_ids=x_group_ids, | ||
| ) | ||
|
|
||
|
|
||
| def build_auth_context( | ||
| x_user_id: object, | ||
| x_user_role: object = None, | ||
| x_organization_id: object = None, | ||
| x_group_ids: object = None, | ||
| ) -> AuthContext: | ||
|
|
||
| """ | ||
| Extracts the user ID from the X-User-Id header. | ||
| In a real implementation, this should validate a JWT or Keycloak token. | ||
| For now, we enforce that it must be present. | ||
| Builds an auth context from the current request headers. | ||
| Today this still trusts local/dev headers, but the shape matches future | ||
| token-derived scope claims from Keycloak or Casdoor. | ||
| """ | ||
| if not x_user_id: | ||
| user_id = _normalize_header_value(x_user_id) | ||
| if user_id is None: | ||
| raise HTTPException(status_code=401, detail="Authentication required") | ||
| return x_user_id | ||
|
|
||
| organization_id = _normalize_header_value(x_organization_id) | ||
| role = _derive_role(user_id, _normalize_header_value(x_user_role)) | ||
| group_ids = _parse_group_ids(_normalize_header_value(x_group_ids)) | ||
| workspace_id = _derive_workspace_id(user_id, organization_id) | ||
|
|
||
| async def get_current_workspace_id(current_user: str = Depends(get_current_user)) -> str: | ||
| return f"workspace-{current_user}" | ||
| return AuthContext( | ||
| user_id=user_id, | ||
| role=role, | ||
| organization_id=organization_id, | ||
| group_ids=group_ids, | ||
| workspace_id=workspace_id, | ||
| ) | ||
|
|
||
|
|
||
| async def get_current_user( | ||
| x_user_id: str | None = Header(None, alias="X-User-Id"), | ||
| x_user_role: str | None = Header(None, alias="X-User-Role"), | ||
| x_organization_id: str | None = Header(None, alias="X-Organization-Id"), | ||
| x_group_ids: str | None = Header(None, alias="X-Group-Ids"), | ||
| ) -> str: | ||
| return build_auth_context( | ||
| x_user_id=x_user_id, | ||
| x_user_role=x_user_role, | ||
| x_organization_id=x_organization_id, | ||
| x_group_ids=x_group_ids, | ||
| ).user_id | ||
|
|
||
|
|
||
| async def get_current_workspace_id(auth_context: AuthContext = Depends(get_auth_context)) -> str: | ||
| return auth_context.workspace_id | ||
|
|
||
|
|
||
| async def get_current_user_role( | ||
| current_user: str = Depends(get_current_user), | ||
| auth_context: AuthContext = Depends(get_auth_context), | ||
| ) -> str: | ||
| if not (settings.DEBUG or settings.TRUST_DEV_HEADERS): | ||
| return "member" | ||
| return "organization_admin" if current_user == "admin" else "member" | ||
| return auth_context.role | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,71 @@ | ||
| import pytest | ||
| from fastapi import HTTPException | ||
| from api.auth import get_current_user | ||
| from api.auth import AuthContext, ensure_organization_access, get_auth_context, get_current_user | ||
| from core.config import settings | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def restore_auth_flags(): | ||
| previous_debug = settings.DEBUG | ||
| previous_trust = settings.TRUST_DEV_HEADERS | ||
| yield | ||
| settings.DEBUG = previous_debug | ||
| settings.TRUST_DEV_HEADERS = previous_trust | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_current_user_rejects_missing_auth(): | ||
| # It should raise HTTP 401 when no auth is provided, rather than defaulting to "default". | ||
| with pytest.raises(HTTPException) as exc: | ||
| await get_current_user(x_user_id=None) | ||
| assert exc.value.status_code == 401 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_auth_context_supports_scoped_enterprise_roles(): | ||
| settings.TRUST_DEV_HEADERS = True | ||
|
|
||
| context = await get_auth_context( | ||
| x_user_id="alice", | ||
| x_user_role="group_admin", | ||
| x_organization_id="org-acme", | ||
| x_group_ids="group-1,group-2", | ||
| ) | ||
|
|
||
| assert context == AuthContext( | ||
| user_id="alice", | ||
| role="group_admin", | ||
| organization_id="org-acme", | ||
| group_ids=("group-1", "group-2"), | ||
| workspace_id="workspace-org-acme", | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_auth_context_keeps_legacy_workspace_fallback_for_unscoped_dev_auth(): | ||
| settings.TRUST_DEV_HEADERS = True | ||
|
|
||
| context = await get_auth_context( | ||
| x_user_id="root", | ||
| x_user_role="platform_admin", | ||
| ) | ||
|
|
||
| assert context.role == "platform_admin" | ||
| assert context.organization_id is None | ||
| assert context.group_ids == () | ||
| assert context.workspace_id == "workspace-root" | ||
|
|
||
|
|
||
| def test_ensure_organization_access_rejects_cross_scope_resource(): | ||
| context = AuthContext( | ||
| user_id="alice", | ||
| role="organization_admin", | ||
| organization_id="org-acme", | ||
| group_ids=("group-1",), | ||
| workspace_id="workspace-org-acme", | ||
| ) | ||
|
|
||
| with pytest.raises(HTTPException) as exc: | ||
| ensure_organization_access(context, "org-other") | ||
|
|
||
| assert exc.value.status_code == 403 | ||
| assert exc.value.detail == "Resource belongs to a different organization" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.