mirror of
https://github.com/langgenius/dify.git
synced 2026-05-20 08:46:57 +08:00
Ports service_api/app/{completion,workflow}.py to bearer-authed
/openapi/v1/apps/<app_id>/{info,chat-messages,completion-messages,workflows/run}.
Architecture:
- New controllers/openapi/auth/ package: Pipeline + Step protocol over
one mutable Context. Endpoints attach via @APP_PIPELINE.guard(scope=...)
— single attachment point; forgetting auth is structurally impossible.
- Pipeline order: BearerCheck -> ScopeCheck -> AppResolver -> AppAuthzCheck
-> CallerMount.
- Strategies vary along independent axes: AclStrategy (EE webapp-auth inner
API) vs MembershipStrategy (CE TenantAccountJoin); AccountMounter vs
EndUserMounter dispatched by SubjectType.
- App is in URL path (not header). Each non-GET has typed Pydantic Request;
each non-SSE response has typed Pydantic Response. Bearer-as-identity:
body 'user' field stripped, ignored if present.
Adds InvokeFrom.OPENAPI enum variant. Emits app.run.openapi audit log
on successful invocation via standard logger extra={"audit": True, ...}
convention.
40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
"""Mutable per-request context for the openapi auth pipeline.
|
|
|
|
Every field starts None / empty and is filled in by a step. The pipeline
|
|
is the only thing that should construct or mutate Context — handlers
|
|
read populated values via the decorator's kwargs unpacking.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
from datetime import datetime
|
|
from typing import Literal, Protocol
|
|
|
|
from flask import Request
|
|
|
|
from libs.oauth_bearer import SubjectType
|
|
|
|
|
|
@dataclass
|
|
class Context:
|
|
request: Request
|
|
required_scope: str
|
|
subject_type: SubjectType | None = None
|
|
subject_email: str | None = None
|
|
subject_issuer: str | None = None
|
|
account_id: str | None = None
|
|
scopes: frozenset[str] = field(default_factory=frozenset)
|
|
token_id: str | None = None
|
|
source: str | None = None
|
|
expires_at: datetime | None = None
|
|
app: object | None = None
|
|
tenant: object | None = None
|
|
caller: object | None = None
|
|
caller_kind: Literal["account", "end_user"] | None = None
|
|
|
|
|
|
class Step(Protocol):
|
|
"""One responsibility. Mutate ctx or raise to short-circuit."""
|
|
|
|
def __call__(self, ctx: Context) -> None: ...
|