mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-04-25 11:18:22 +00:00
Add new application structure: - app/main.py - application entry point - app/plugins/ - plugin system with auth plugin: - api/ - REST API endpoints and schemas - authorization/ - auth policies, providers, hooks - domain/ - business logic (service, models, jwt, password) - injection/ - route injection and guards - ops/ - operational utilities - runtime/ - runtime configuration - security/ - middleware, CSRF, dependencies - storage/ - user repositories and models - app/static/ - static assets (scalar.js for API docs) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
"""Authentication helpers used by auth-plugin authorization decorators."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from fastapi import Request
|
|
|
|
from app.plugins.auth.authorization.providers import PermissionProvider, default_permission_provider
|
|
from app.plugins.auth.authorization.types import AuthContext
|
|
|
|
|
|
def get_auth_context(request: Request) -> AuthContext | None:
|
|
"""Get AuthContext, preferring Starlette-style request.auth."""
|
|
|
|
auth = request.scope.get("auth")
|
|
if isinstance(auth, AuthContext):
|
|
return auth
|
|
return getattr(request.state, "auth", None)
|
|
|
|
|
|
def set_auth_context(request: Request, auth_context: AuthContext) -> AuthContext:
|
|
"""Persist AuthContext on the standard request surfaces."""
|
|
|
|
request.scope["auth"] = auth_context
|
|
request.state.auth = auth_context
|
|
return auth_context
|
|
|
|
|
|
async def authenticate_request(
|
|
request: Request,
|
|
*,
|
|
permission_provider: PermissionProvider = default_permission_provider,
|
|
) -> AuthContext:
|
|
"""Authenticate request and build AuthContext."""
|
|
|
|
from app.plugins.auth.security.dependencies import get_optional_user_from_request
|
|
|
|
user = await get_optional_user_from_request(request)
|
|
if user is None:
|
|
return AuthContext(user=None, permissions=[])
|
|
return AuthContext(user=user, permissions=permission_provider(user))
|
|
|
|
|
|
__all__ = ["authenticate_request", "get_auth_context", "set_auth_context"]
|