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
"""Pipeline IS the auth scheme.
|
|
|
|
`Pipeline.guard(scope=…)` is the only attachment point for endpoints —
|
|
that is the design lock-in: forgetting an auth layer is structurally
|
|
impossible because there is no "sometimes wrap, sometimes don't" choice.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from functools import wraps
|
|
|
|
from flask import request
|
|
|
|
from controllers.openapi.auth.context import Context, Step
|
|
|
|
|
|
class Pipeline:
|
|
def __init__(self, *steps: Step) -> None:
|
|
self._steps = steps
|
|
|
|
def run(self, ctx: Context) -> None:
|
|
for step in self._steps:
|
|
step(ctx)
|
|
|
|
def guard(self, *, scope: str):
|
|
def decorator(view):
|
|
@wraps(view)
|
|
def decorated(*args, **kwargs):
|
|
ctx = Context(request=request, required_scope=scope)
|
|
self.run(ctx)
|
|
kwargs.update(
|
|
app_model=ctx.app,
|
|
caller=ctx.caller,
|
|
caller_kind=ctx.caller_kind,
|
|
)
|
|
return view(*args, **kwargs)
|
|
|
|
return decorated
|
|
|
|
return decorator
|