核心概念
身份验证 vs 授权
虽然经常互换使用,但这些术语代表不同的安全概念: 在 LangSmith 中,身份验证由您的@auth.authenticate 处理程序处理,授权由您的 @auth.on 处理程序处理。
默认安全模型
LangSmith 提供不同的安全默认设置:LangSmith
- 默认使用 LangSmith API 密钥
- 需要
x-api-key头中的有效 API 密钥 - 可以使用您的身份验证处理程序自定义
自定义身份验证
LangSmith 的所有计划都支持自定义身份验证。
自托管
- 没有默认身份验证
- 完全灵活地实现您的安全模型
- 您控制身份验证和授权的所有方面
系统架构
典型的身份验证设置涉及三个主要组件:- 身份验证提供商(身份提供商/IdP)
- 管理用户身份和凭据的专用服务
- 处理用户注册、登录、密码重置等
- 在成功身份验证后颁发令牌(JWT、会话令牌等)
- 示例:Auth0、Supabase Auth、Okta 或您自己的身份验证服务器
- LangGraph 后端(资源服务器)
- 包含业务逻辑和受保护资源的 LangGraph 应用程序
- 使用身份验证提供商验证令牌
- 根据用户身份和权限强制执行访问控制
- 不直接存储用户凭据
- 客户端应用程序(前端)
- Web 应用程序、移动应用程序或 API 客户端
- 收集具有时效性的用户凭据并发送到身份验证提供商
- 从身份验证提供商接收令牌
- 在对 LangGraph 后端的请求中包含这些令牌
@auth.authenticate 处理程序处理步骤 4-6,而您的 @auth.on 处理程序实现步骤 7。
身份验证
LangGraph 中的身份验证在每个请求上作为中间件运行。您的@auth.authenticate 处理程序接收请求信息并应该:
- To your authorization handlers via
ctx.user - In your application via
config["configuration"]["langgraph_auth_user"]
Supported Parameters
Supported Parameters
The
@auth.authenticate handler can accept any of the following parameters by name:- request (Request): The raw ASGI request object
- path (str): The request path, e.g.,
"/threads/abcd-1234-abcd-1234/runs/abcd-1234-abcd-1234/stream" - method (str): The HTTP method, e.g.,
"GET" - path_params (dict[str, str]): URL path parameters, e.g.,
{"thread_id": "abcd-1234-abcd-1234", "run_id": "abcd-1234-abcd-1234"} - query_params (dict[str, str]): URL query parameters, e.g.,
{"stream": "true"} - headers (dict[bytes, bytes]): Request headers
- authorization (str | None): The Authorization header value (e.g.,
"Bearer <token>")
智能体身份验证
自定义身份验证允许委托访问。您在@auth.authenticate 中返回的值被添加到运行上下文中,为智能体提供用户范围的凭据,使它们能够代表用户访问资源。
身份验证后,平台创建一个特殊的配置对象,该对象通过可配置的上下文传递给您的图和所有节点。
此对象包含有关当前用户的信息,包括您从 @auth.authenticate 处理程序返回的任何自定义字段。
要使智能体能够代表用户执行操作,请使用自定义身份验证中间件。这将允许智能体代表用户与外部系统(如 MCP 服务器、外部数据库,甚至其他智能体)进行交互。
有关更多信息,请参阅使用自定义身份验证指南。
使用 MCP 的智能体身份验证
有关如何将智能体验证到 MCP 服务器的信息,请参阅 MCP 概念指南。授权
身份验证后,LangGraph 调用您的@auth.on 处理程序来控制对特定资源(例如,线程、助手、定时任务)的访问。这些处理程序可以:
- 通过直接修改
value["metadata"]字典,在资源创建期间添加要保存的元数据。有关每个操作值可以采用的类型列表,请参阅支持的操作表。 - 在搜索/列表或读取操作期间通过返回过滤字典按元数据过滤资源。
- 如果拒绝访问,抛出 HTTP 异常。
@auth.on 处理程序。如果您想根据资源和操作进行不同的控制,可以使用特定于资源的处理程序。有关支持访问控制的资源的完整列表,请参阅支持的资源部分。
Resource-specific handlers
You can register handlers for specific resources and actions by chaining the resource and action names together with the@auth.on decorator.
When a request is made, the most specific handler that matches that resource and action is called. Below is an example of how to register handlers for specific resources and actions. For the following setup:
- Authenticated users are able to create threads, read threads, and create runs on threads
- Only users with the “assistants:create” permission are allowed to create new assistants
- All other endpoints (e.g., e.g., delete assistant, crons, store) are disabled for all users.
thread would match the on_thread_create handler but NOT the reject_unhandled_requests handler. A request to update a thread, however would be handled by the global handler, since we don’t have a more specific handler for that resource and action.
Filter operations
Authorization handlers can returnNone, a boolean, or a filter dictionary.
NoneandTruemean “authorize access to all underling resources”Falsemeans “deny access to all underling resources (raises a 403 exception)”- A metadata filter dictionary will restrict access to resources
- The default value is a shorthand for exact match, or “$eq”, below. For example,
{"owner": user_id}will include only resources with metadata containing{"owner": user_id} $eq: Exact match (e.g.,{"owner": {"$eq": user_id}}) - this is equivalent to the shorthand above,{"owner": user_id}$contains: List membership (e.g.,{"allowed_users": {"$contains": user_id}}) or list containment (e.g.,{"allowed_users": {"$contains": [user_id_1, user_id_2]}}). The value here must be an element of the list or a subset of the elements of the list, respectively. The metadata in the stored resource must be a list/container type.
AND filter. For example, {"owner": org_id, "allowed_users": {"$contains": user_id}} will only match resources with metadata whose “owner” is org_id and whose “allowed_users” list contains user_id.
See the reference here for more information.
Common access patterns
Here are some typical authorization patterns:Single-owner resources
This common pattern lets you scope all threads, assistants, crons, and runs to a single user. It’s useful for common single-user use cases like regular chatbot-style apps.Permission-based access
This pattern lets you control access based on permissions. It’s useful if you want certain roles to have broader or more restricted access to resources.Supported resources
LangGraph provides three levels of authorization handlers, from most general to most specific:- Global Handler (
@auth.on): Matches all resources and actions - Resource Handler (e.g.,
@auth.on.threads,@auth.on.assistants,@auth.on.crons): Matches all actions for a specific resource - Action Handler (e.g.,
@auth.on.threads.create,@auth.on.threads.read): Matches a specific action on a specific resource
@auth.on.threads.create takes precedence over @auth.on.threads for thread creation.
If a more specific handler is registered, the more general handler will not be called for that resource and action.
Supported actions and types
Here are all the supported action handlers:| Resource | Handler | Description | Value Type |
|---|---|---|---|
| Threads | @auth.on.threads.create | Thread creation | ThreadsCreate |
@auth.on.threads.read | Thread retrieval | ThreadsRead | |
@auth.on.threads.update | Thread updates | ThreadsUpdate | |
@auth.on.threads.delete | Thread deletion | ThreadsDelete | |
@auth.on.threads.search | Listing threads | ThreadsSearch | |
@auth.on.threads.create_run | Creating or updating a run | RunsCreate | |
| Assistants | @auth.on.assistants.create | Assistant creation | AssistantsCreate |
@auth.on.assistants.read | Assistant retrieval | AssistantsRead | |
@auth.on.assistants.update | Assistant updates | AssistantsUpdate | |
@auth.on.assistants.delete | Assistant deletion | AssistantsDelete | |
@auth.on.assistants.search | Listing assistants | AssistantsSearch | |
| Crons | @auth.on.crons.create | Cron job creation | CronsCreate |
@auth.on.crons.read | Cron job retrieval | CronsRead | |
@auth.on.crons.update | Cron job updates | CronsUpdate | |
@auth.on.crons.delete | Cron job deletion | CronsDelete | |
@auth.on.crons.search | Listing cron jobs | CronsSearch |
“About Runs”Runs are scoped to their parent thread for access control. This means permissions are typically inherited from the thread, reflecting the conversational nature of the data model. All run operations (reading, listing) except creation are controlled by the thread’s handlers.
There is a specific
create_run handler for creating new runs because it had more arguments that you can view in the handler.Next steps
For implementation details:- Check out the introductory tutorial on setting up authentication
- See the how-to guide on implementing a custom auth handlers