Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update auth docstrings #2977

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/docs/how-tos/auth/custom_auth.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# How to add custom authentication

Check notice on line 1 in docs/docs/how-tos/auth/custom_auth.md

View workflow job for this annotation

GitHub Actions / benchmark

Benchmark results

......................................... fanout_to_subgraph_10x: Mean +- std dev: 60.9 ms +- 0.9 ms ......................................... fanout_to_subgraph_10x_sync: Mean +- std dev: 53.0 ms +- 0.6 ms ......................................... fanout_to_subgraph_10x_checkpoint: Mean +- std dev: 74.2 ms +- 1.0 ms ......................................... fanout_to_subgraph_10x_checkpoint_sync: Mean +- std dev: 94.9 ms +- 1.0 ms ......................................... fanout_to_subgraph_100x: Mean +- std dev: 598 ms +- 22 ms ......................................... fanout_to_subgraph_100x_sync: Mean +- std dev: 515 ms +- 10 ms ......................................... fanout_to_subgraph_100x_checkpoint: Mean +- std dev: 742 ms +- 10 ms ......................................... fanout_to_subgraph_100x_checkpoint_sync: Mean +- std dev: 956 ms +- 17 ms ......................................... react_agent_10x: Mean +- std dev: 30.5 ms +- 0.7 ms ......................................... react_agent_10x_sync: Mean +- std dev: 22.7 ms +- 0.3 ms ......................................... react_agent_10x_checkpoint: Mean +- std dev: 37.8 ms +- 0.7 ms ......................................... react_agent_10x_checkpoint_sync: Mean +- std dev: 36.6 ms +- 0.3 ms ......................................... react_agent_100x: Mean +- std dev: 340 ms +- 6 ms ......................................... react_agent_100x_sync: Mean +- std dev: 269 ms +- 2 ms ......................................... react_agent_100x_checkpoint: Mean +- std dev: 631 ms +- 6 ms ......................................... react_agent_100x_checkpoint_sync: Mean +- std dev: 617 ms +- 6 ms ......................................... wide_state_25x300: Mean +- std dev: 23.0 ms +- 0.5 ms ......................................... wide_state_25x300_sync: Mean +- std dev: 15.2 ms +- 0.2 ms ......................................... wide_state_25x300_checkpoint: Mean +- std dev: 248 ms +- 13 ms ......................................... wide_state_25x300_checkpoint_sync: Mean +- std dev: 244 ms +- 13 ms ......................................... wide_state_15x600: Mean +- std dev: 27.0 ms +- 0.5 ms ......................................... wide_state_15x600_sync: Mean +- std dev: 17.6 ms +- 0.1 ms ......................................... wide_state_15x600_checkpoint: Mean +- std dev: 429 ms +- 14 ms ......................................... wide_state_15x600_checkpoint_sync: Mean +- std dev: 426 ms +- 14 ms ......................................... wide_state_9x1200: Mean +- std dev: 26.9 ms +- 0.5 ms ......................................... wide_state_9x1200_sync: Mean +- std dev: 17.7 ms +- 0.1 ms ......................................... wide_state_9x1200_checkpoint: Mean +- std dev: 280 ms +- 14 ms ......................................... wide_state_9x1200_checkpoint_sync: Mean +- std dev: 276 ms +- 13 ms

Check notice on line 1 in docs/docs/how-tos/auth/custom_auth.md

View workflow job for this annotation

GitHub Actions / benchmark

Comparison against main

+-----------------------------------------+----------+-----------------------+ | Benchmark | main | changes | +=========================================+==========+=======================+ | fanout_to_subgraph_100x_checkpoint | 896 ms | 742 ms: 1.21x faster | +-----------------------------------------+----------+-----------------------+ | fanout_to_subgraph_100x | 681 ms | 598 ms: 1.14x faster | +-----------------------------------------+----------+-----------------------+ | react_agent_100x_checkpoint | 683 ms | 631 ms: 1.08x faster | +-----------------------------------------+----------+-----------------------+ | react_agent_100x_checkpoint_sync | 666 ms | 617 ms: 1.08x faster | +-----------------------------------------+----------+-----------------------+ | fanout_to_subgraph_100x_sync | 549 ms | 515 ms: 1.07x faster | +-----------------------------------------+----------+-----------------------+ | fanout_to_subgraph_100x_checkpoint_sync | 1.01 sec | 956 ms: 1.05x faster | +-----------------------------------------+----------+-----------------------+ | wide_state_25x300_checkpoint | 259 ms | 248 ms: 1.05x faster | +-----------------------------------------+----------+-----------------------+ | wide_state_15x600_sync | 18.4 ms | 17.6 ms: 1.05x faster | +-----------------------------------------+----------+-----------------------+ | wide_state_25x300 | 24.0 ms | 23.0 ms: 1.04x faster | +-----------------------------------------+----------+-----------------------+ | react_agent_10x_checkpoint_sync | 38.1 ms | 36.6 ms: 1.04x faster | +-----------------------------------------+----------+-----------------------+ | react_agent_10x_checkpoint | 39.2 ms | 37.8 ms: 1.04x faster | +-----------------------------------------+----------+-----------------------+ | wide_state_25x300_sync | 15.7 ms | 15.2 ms: 1.03x faster | +-----------------------------------------+----------+-----------------------+ | fanout_to_subgraph_10x_checkpoint_sync | 98.0 ms | 94.9 ms: 1.03x faster | +-----------------------------------------+----------+-----------------------+ | fanout_to_subgraph_10x_sync | 54.6 ms | 53.0 ms: 1.03x faster | +-----------------------------------------+----------+-----------------------+ | fanout_to_subgraph_10x_checkpoint | 76.6 ms | 74.2 ms: 1.03x faster | +-----------------------------------------+----------+-----------------------+ | wide_state_9x1200_sync | 18.2 ms | 17.7 ms: 1.03x faster | +-----------------------------------------+----------+-----------------------+ | wide_state_15x600 | 27.9 ms | 27.0 ms: 1.03x faster | +-----------------------------------------+----------+-----------------------+ | wide_state_25x300_checkpoint_sync | 252 ms | 244 ms: 1.03x faster | +-----------------------------------------+----------+-----------------------+ | wide_state_9x1200 | 27.7 ms | 26.9 ms: 1.03x faster | +-----------------------------------------+----------+-----------------------+ | react_agent_10x | 31.3 ms | 30.5 ms: 1.03x faster | +-----------------------------------------+----------+-----------------------+ | wide_state_15x600_checkpoint | 440 ms | 429 ms: 1.02x faster | +-----------------------------------------+----------+-----------------------+ | react_agent_10x_sync | 23.2 ms | 22.7 ms: 1.02x faster | +-----------------------------------------+----------+-----------------------+ | react_agent_100x_sync | 275 ms | 269 ms: 1.02x faster | +-----------------------------------------+----------+-----------------------+ | wide_state_9x1200_checkpoint | 286 ms | 280 ms: 1.02x faster | +-----------------------------------------+----------+-----------------------+ | fanout_to_subgraph_10x | 62.1 ms | 60.9 ms: 1.0

!!! tip "Prerequisites"

Expand Down Expand Up @@ -37,7 +37,7 @@
detail="Invalid token"
)

# Optional: Add authorization rules
# Add authorization rules to actually control access to resources
@my_auth.on
async def add_owner(
ctx: Auth.types.AuthContext,
Expand All @@ -48,6 +48,13 @@
metadata = value.setdefault("metadata", {})
metadata.update(filters)
return filters

# Assumes you organize information in store like (user_id, resource_type, resource_id)
@my_auth.on.store()
async def authorize_store(ctx: Auth.types.AuthContext, value: dict):
namespace: tuple = value["namespace"]
assert namespace[0] == ctx.user.identity, "Not authorized"

```

## 2. Update configuration
Expand Down
7 changes: 7 additions & 0 deletions docs/docs/tutorials/auth/resource_auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,13 @@ async def on_assistants(
status_code=403,
detail="User lacks the required permissions.",
)

# Assumes you organize information in store like (user_id, resource_type, resource_id)
@auth.on.store()
async def authorize_store(ctx: Auth.types.AuthContext, value: dict):
# The "namespace" field for each store item is a tuple you can think of as the directory of an item.
namespace: tuple = value["namespace"]
assert namespace[0] == ctx.user.identity, "Not authorized"
```

Notice that instead of one global handler, we now have specific handlers for:
Expand Down
13 changes: 13 additions & 0 deletions libs/sdk-py/langgraph_sdk/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ async def authorize_default(params: Auth.on.value):
async def authorize_thread_create(params: Auth.on.threads.create.value):
# Allow the allowed user to create a thread
assert params.get("metadata", {}).get("owner") == "allowed_user"

@auth.on.store
async def authorize_store(ctx: Auth.types.AuthContext, value: Auth.types.on):
assert ctx.user.identity in value["namespace"], "Not authorized"
```

???+ note "Request Processing Flow"
Expand Down Expand Up @@ -157,6 +161,15 @@ async def rate_limit_writes(ctx: AuthContext, value: Any) -> bool:
# Implement rate limiting for write operations
return await check_rate_limit(ctx.user.identity)
```

Auth for the `store` resource is a bit different since its structure is developer defined.
You typically want to enforce user creds in the namespace. Y
```python
@auth.on.store
async def check_store_access(ctx: AuthContext, value: Auth.types.on) -> bool:
# Assuming you structure your store like (store.aput((user_id, application_context), key, value))
assert value["namespace"][0] == ctx.user.identity
```
"""
# These are accessed by the API. Changes to their names or types is
# will be considered a breaking change.
Expand Down
Loading