From 60232eadc4a2cc1dca3e76ac2cf53c83fca1cfd7 Mon Sep 17 00:00:00 2001 From: William Fu-Hinthorn <13333726+hinthornw@users.noreply.github.com> Date: Wed, 18 Dec 2024 11:13:28 -0800 Subject: [PATCH] [SDK] Add studio user object --- libs/sdk-py/langgraph_sdk/auth/types.py | 51 +++++++++++++++++++++++++ libs/sdk-py/pyproject.toml | 2 +- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/libs/sdk-py/langgraph_sdk/auth/types.py b/libs/sdk-py/langgraph_sdk/auth/types.py index cc47651d0..2320586b9 100644 --- a/libs/sdk-py/langgraph_sdk/auth/types.py +++ b/libs/sdk-py/langgraph_sdk/auth/types.py @@ -176,6 +176,57 @@ def permissions(self) -> Sequence[str]: ... +class StudioUser: + """A user object that's populated from authenticated requests from the LangGraph studio. + + Note: Studio auth can be disabled in your `langgraph.json` config. + + ```json + { + "auth": { + "disable_studio_auth": true + } + } + ``` + + You can use `isinstance` checks in your authorization handlers (`@auth.on`) to control access specifically + for developers accessing the instance from the LangGraph Studio UI. + + ???+ example "Examples" + ```python + @auth.on + async def allow_developers(ctx: Auth.types.AuthContext, value: Any) -> None: + if isinstance(ctx.user, Auth.types.StudioUser): + return None + ... + return False + ``` + """ + + __slots__ = ("username", "_is_authenticated", "_permissions") + + def __init__(self, username: str, is_authenticated: bool = False) -> None: + self.username = username + self._is_authenticated = is_authenticated + self._permissions = ["authenticated"] if is_authenticated else [] + + @property + def is_authenticated(self) -> bool: + return self._is_authenticated + + @property + def display_name(self) -> str: + return self.username + + @property + def identity(self) -> str: + return self.username + + @property + def permissions(self) -> Sequence[str]: + return self._permissions + + Authenticator = Callable[ ..., Awaitable[ diff --git a/libs/sdk-py/pyproject.toml b/libs/sdk-py/pyproject.toml index c73c8afa1..7ed7c49b1 100644 --- a/libs/sdk-py/pyproject.toml +++ b/libs/sdk-py/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "langgraph-sdk" -version = "0.1.47" +version = "0.1.48" description = "SDK for interacting with LangGraph API" authors = [] license = "MIT"