Files
dify/api/services/webapp_access_query_service.py
T
CodingOnStar 954ba55eb3 Merge remote-tracking branch 'origin/main' into song/snp-682-ip-allowlist
# Conflicts:
#	api/controllers/mcp/mcp.py
#	api/controllers/openapi/__init__.py
#	api/controllers/web/login.py
#	api/controllers/web/wraps.py
#	api/extensions/ext_application_services.py
#	api/openapi/markdown/console-openapi.md
#	api/services/webapp_access_query_service.py
#	api/tests/unit_tests/controllers/web/test_web_login.py
#	api/tests/unit_tests/controllers/web/test_wraps.py
#	api/tests/unit_tests/extensions/test_ext_application_services.py
#	packages/contracts/generated/api/console/apps/orpc.gen.ts
#	packages/contracts/generated/api/console/apps/types.gen.ts
#	web/app/(commonLayout)/app/(appDetailLayout)/[appId]/layout-main.tsx
#	web/app/components/app/access-point/index.tsx
#	web/app/components/base/chat/chat/hooks.ts
#	web/app/components/base/file-uploader/hooks.ts
#	web/app/components/base/image-uploader/__tests__/hooks.spec.ts
#	web/app/components/base/image-uploader/hooks.ts
#	web/app/components/explore/try-app/index.tsx
#	web/features/agent-v2/agent-detail/__tests__/layout.spec.tsx
#	web/i18n/ar-TN/common.json
#	web/i18n/de-DE/common.json
#	web/i18n/en-US/common.json
#	web/i18n/es-ES/common.json
#	web/i18n/fa-IR/common.json
#	web/i18n/fr-FR/common.json
#	web/i18n/hi-IN/common.json
#	web/i18n/id-ID/common.json
#	web/i18n/it-IT/common.json
#	web/i18n/ja-JP/common.json
#	web/i18n/ko-KR/common.json
#	web/i18n/lo-LA/common.json
#	web/i18n/nl-NL/common.json
#	web/i18n/pl-PL/common.json
#	web/i18n/pt-BR/common.json
#	web/i18n/ro-RO/common.json
#	web/i18n/ru-RU/common.json
#	web/i18n/sl-SI/common.json
#	web/i18n/th-TH/common.json
#	web/i18n/tr-TR/common.json
#	web/i18n/uk-UA/common.json
#	web/i18n/vi-VN/common.json
#	web/i18n/zh-Hans/common.json
#	web/i18n/zh-Hant/common.json
#	web/service/console/query-policies.ts
2026-09-29 16:53:53 +08:00

108 lines
4.4 KiB
Python

"""Application service for resolving web-app access."""
from collections.abc import Callable, Mapping, Sequence
from typing import Protocol
from enums import WebAppAccessMode
_PERMISSION_CHECK_MODES = frozenset({WebAppAccessMode.PRIVATE, WebAppAccessMode.PRIVATE_ALL})
class WebAppAccessQuery(Protocol):
def find_app_id_by_code(self, app_code: str) -> str | None: ...
def is_app_available(self, app_id: str) -> bool: ...
class WebAppAccessModesQuery(Protocol):
def __call__(self, *, app_ids: Sequence[str]) -> Mapping[str, WebAppAccessMode]: ...
class WebAppUserPermissionsQuery(Protocol):
def __call__(self, *, user_id: str, app_ids: Sequence[str]) -> Mapping[str, bool]: ...
class WebAppAccessReferenceRequiredError(ValueError):
"""Raised when neither an app ID nor an app code was provided."""
class WebAppAccessAppNotFoundError(LookupError):
"""Raised when an app code does not resolve to an app."""
class WebAppAccessUnavailableError(RuntimeError):
"""Raised when an access dependency cannot answer the query."""
class WebAppAccessQueryService:
def __init__(
self,
*,
access: WebAppAccessQuery,
webapp_auth_enabled: bool,
access_mode_for_app: Callable[[str], WebAppAccessMode],
is_user_allowed_for_app: Callable[[str, str], bool],
get_access_modes: WebAppAccessModesQuery,
get_user_permissions: WebAppUserPermissionsQuery,
) -> None:
self._access: WebAppAccessQuery = access
self._webapp_auth_enabled: bool = webapp_auth_enabled
self._access_mode_for_app: Callable[[str], WebAppAccessMode] = access_mode_for_app
self._is_user_allowed_for_app: Callable[[str, str], bool] = is_user_allowed_for_app
self._get_access_modes: WebAppAccessModesQuery = get_access_modes
self._get_user_permissions: WebAppUserPermissionsQuery = get_user_permissions
def get_app_id_by_code(self, app_code: str) -> str:
app_id = self._access.find_app_id_by_code(app_code)
if app_id is None:
raise WebAppAccessAppNotFoundError(f"App with code {app_code} not found")
return app_id
def resolve_app_id(self, *, app_id: str | None = None, app_code: str | None = None) -> str:
"""Resolve a published public App identity without tokens or user writes."""
if app_code:
app_id = self._access.find_app_id_by_code(app_code)
if app_id is None:
raise WebAppAccessAppNotFoundError
elif app_id and not self._access.is_app_available(app_id):
raise WebAppAccessAppNotFoundError
if not app_id:
raise WebAppAccessReferenceRequiredError("appId or appCode must be provided")
return app_id
def get_access_mode(self, *, app_id: str | None, app_code: str | None) -> WebAppAccessMode:
# Preserve the auth-disabled, reference-free bootstrap shortcut. Supplied
# references must still resolve to a live App regardless of auth settings.
if not self._webapp_auth_enabled and not app_id and not app_code:
return WebAppAccessMode.PUBLIC
app_id = self.resolve_app_id(app_id=app_id, app_code=app_code)
if not self._webapp_auth_enabled:
return WebAppAccessMode.PUBLIC
return self._access_mode_for_app(app_id)
def requires_permission_check(self, app_id: str) -> bool:
return self.is_permission_check_required(self._access_mode_for_app(app_id))
@staticmethod
def is_permission_check_required(access_mode: str) -> bool:
return access_mode in _PERMISSION_CHECK_MODES
def is_user_allowed(self, *, user_id: str, app_id: str) -> bool:
if not self._webapp_auth_enabled:
return True
return self._is_user_allowed_for_app(user_id, app_id)
def batch_get_access_modes(self, *, app_ids: Sequence[str]) -> Mapping[str, WebAppAccessMode]:
"""Return successfully parsed modes; missing IDs do not grant access."""
if not self._webapp_auth_enabled or not app_ids:
return dict.fromkeys(app_ids, WebAppAccessMode.PUBLIC)
return self._get_access_modes(app_ids=app_ids)
def batch_get_user_permissions(self, *, user_id: str, app_ids: Sequence[str]) -> Mapping[str, bool]:
if not self._webapp_auth_enabled or not app_ids:
return dict.fromkeys(app_ids, True)
return self._get_user_permissions(user_id=user_id, app_ids=app_ids)