连接认证提供商¶
在上一个教程中,您添加了资源授权以向用户提供私有对话。但是,您仍然使用硬编码令牌进行认证,这不安全。现在,您将使用OAuth2将这些令牌替换为真实用户账户。
您将保留相同的Auth
对象和资源级别访问控制,但将认证升级为使用 Supabase 作为身份提供商。尽管本教程使用 Supabase,但这些概念适用于任何 OAuth2 提供商。您将学习如何
- 用真实的 JWT 令牌替换测试令牌
- 与 OAuth2 提供商集成,实现安全的用户认证
- 在保持现有授权逻辑的同时处理用户会话和元数据
背景¶
OAuth2 包含三个主要角色
- 授权服务器:处理用户认证并颁发令牌的身份提供商(例如 Supabase、Auth0、Google)
- 应用后端:您的 LangGraph 应用。它验证令牌并提供受保护资源(对话数据)
- 客户端应用:用户与您的服务交互的网页或移动应用
标准 OAuth2 流程大致如下
sequenceDiagram
participant User
participant Client
participant AuthServer
participant LangGraph Backend
User->>Client: Initiate login
User->>AuthServer: Enter credentials
AuthServer->>Client: Send tokens
Client->>LangGraph Backend: Request with token
LangGraph Backend->>AuthServer: Validate token
AuthServer->>LangGraph Backend: Token valid
LangGraph Backend->>Client: Serve request (e.g., run agent or graph)
先决条件¶
在开始本教程之前,请确保您已具备
- 第二个教程中的机器人无错误运行。
- 一个 Supabase 项目,用于使用其认证服务器。
1. 安装依赖¶
安装所需的依赖项。从您的 custom-auth
目录开始,并确保已安装 langgraph-cli
2. 设置认证提供商¶
接下来,获取您的认证服务器 URL 和认证私钥。由于您将使用 Supabase,您可以在 Supabase 控制台中完成此操作
- 在左侧边栏中,点击“⚙️ 项目设置”,然后点击“API”
-
复制您的项目 URL 并将其添加到您的
1. 复制您的服务角色密钥并将其添加到您的.env
文件中.env
文件中 1. 复制您的“匿名公共”密钥并记下。这将在稍后设置客户端代码时使用。
3. 实现令牌验证¶
在之前的教程中,您使用Auth
对象来验证硬编码令牌并添加资源所有权。
现在您将升级认证以验证来自 Supabase 的真实 JWT 令牌。主要更改都将在@auth.authenticate
装饰器函数中进行
- 您将不再对照硬编码的令牌列表进行检查,而是向 Supabase 发出 HTTP 请求以验证令牌。
- 您将从已验证的令牌中提取真实的用户信息(ID、电子邮件)。
- 现有的资源授权逻辑保持不变。
更新 src/security/auth.py
以实现此功能
import os
import httpx
from langgraph_sdk import Auth
auth = Auth()
# This is loaded from the `.env` file you created above
SUPABASE_URL = os.environ["SUPABASE_URL"]
SUPABASE_SERVICE_KEY = os.environ["SUPABASE_SERVICE_KEY"]
@auth.authenticate
async def get_current_user(authorization: str | None):
"""Validate JWT tokens and extract user information."""
assert authorization
scheme, token = authorization.split()
assert scheme.lower() == "bearer"
try:
# Verify token with auth provider
async with httpx.AsyncClient() as client:
response = await client.get(
f"{SUPABASE_URL}/auth/v1/user",
headers={
"Authorization": authorization,
"apiKey": SUPABASE_SERVICE_KEY,
},
)
assert response.status_code == 200
user = response.json()
return {
"identity": user["id"], # Unique user identifier
"email": user["email"],
"is_authenticated": True,
}
except Exception as e:
raise Auth.exceptions.HTTPException(status_code=401, detail=str(e))
# ... the rest is the same as before
# Keep our resource authorization from the previous tutorial
@auth.on
async def add_owner(ctx, value):
"""Make resources private to their creator using resource metadata."""
filters = {"owner": ctx.user.identity}
metadata = value.setdefault("metadata", {})
metadata.update(filters)
return filters
最重要的变化是,我们现在使用真实的认证服务器验证令牌。我们的认证处理程序拥有 Supabase 项目的私钥,我们可以使用它来验证用户的令牌并提取其信息。
4. 测试认证流程¶
让我们测试新的认证流程。您可以在文件或笔记本中运行以下代码。您需要提供
import os
import httpx
from getpass import getpass
from langgraph_sdk import get_client
# Get email from command line
email = getpass("Enter your email: ")
base_email = email.split("@")
password = "secure-password" # CHANGEME
email1 = f"{base_email[0]}+1@{base_email[1]}"
email2 = f"{base_email[0]}+2@{base_email[1]}"
SUPABASE_URL = os.environ.get("SUPABASE_URL")
if not SUPABASE_URL:
SUPABASE_URL = getpass("Enter your Supabase project URL: ")
# This is your PUBLIC anon key (which is safe to use client-side)
# Do NOT mistake this for the secret service role key
SUPABASE_ANON_KEY = os.environ.get("SUPABASE_ANON_KEY")
if not SUPABASE_ANON_KEY:
SUPABASE_ANON_KEY = getpass("Enter your public Supabase anon key: ")
async def sign_up(email: str, password: str):
"""Create a new user account."""
async with httpx.AsyncClient() as client:
response = await client.post(
f"{SUPABASE_URL}/auth/v1/signup",
json={"email": email, "password": password},
headers={"apiKey": SUPABASE_ANON_KEY},
)
assert response.status_code == 200
return response.json()
# Create two test users
print(f"Creating test users: {email1} and {email2}")
await sign_up(email1, password)
await sign_up(email2, password)
⚠️ 在继续之前:请检查您的电子邮件并点击两个确认链接。在您确认用户电子邮件之前,Supabase 将拒绝 /login
请求。
现在测试用户是否只能看到自己的数据。在继续之前,请确保服务器正在运行(运行 langgraph dev
)。以下代码片段需要您之前在设置认证提供商时从 Supabase 控制台复制的“匿名公共”密钥。
async def login(email: str, password: str):
"""Get an access token for an existing user."""
async with httpx.AsyncClient() as client:
response = await client.post(
f"{SUPABASE_URL}/auth/v1/token?grant_type=password",
json={
"email": email,
"password": password
},
headers={
"apikey": SUPABASE_ANON_KEY,
"Content-Type": "application/json"
},
)
assert response.status_code == 200
return response.json()["access_token"]
# Log in as user 1
user1_token = await login(email1, password)
user1_client = get_client(
url="http://localhost:2024", headers={"Authorization": f"Bearer {user1_token}"}
)
# Create a thread as user 1
thread = await user1_client.threads.create()
print(f"✅ User 1 created thread: {thread['thread_id']}")
# Try to access without a token
unauthenticated_client = get_client(url="http://localhost:2024")
try:
await unauthenticated_client.threads.create()
print("❌ Unauthenticated access should fail!")
except Exception as e:
print("✅ Unauthenticated access blocked:", e)
# Try to access user 1's thread as user 2
user2_token = await login(email2, password)
user2_client = get_client(
url="http://localhost:2024", headers={"Authorization": f"Bearer {user2_token}"}
)
try:
await user2_client.threads.get(thread["thread_id"])
print("❌ User 2 shouldn't see User 1's thread!")
except Exception as e:
print("✅ User 2 blocked from User 1's thread:", e)
✅ User 1 created thread: d6af3754-95df-4176-aa10-dbd8dca40f1a
✅ Unauthenticated access blocked: Client error '403 Forbidden' for url 'http://localhost:2024/threads'
✅ User 2 blocked from User 1's thread: Client error '404 Not Found' for url 'http://localhost:2024/threads/d6af3754-95df-4176-aa10-dbd8dca40f1a'
您的认证和授权正在协同工作
- 用户必须登录才能访问机器人
- 每个用户只能看到自己的会话
所有用户都由 Supabase 认证提供商管理,因此您无需实现任何额外的用户管理逻辑。
下一步¶
您已成功为您的 LangGraph 应用构建了一个生产就绪的认证系统!让我们回顾一下您所完成的工作
- 设置认证提供商(在本例中为 Supabase)
- 添加了带有电子邮件/密码认证的真实用户账户
- 将 JWT 令牌验证集成到您的 LangGraph 服务器中
- 实现了适当的授权,以确保用户只能访问自己的数据
- 打下了坚实的基础,为应对您的下一个认证挑战 🚀
既然您已拥有生产级认证,请考虑