Auth
object lets you register an authentication function, which LangGraph Platform uses to validate the bearer tokens in incoming requests. Now you’ll use it to register an authorization handler.
Authorization handlers are functions that run after authentication succeeds. These handlers can add metadata to resources (like who owns them) and filter what each user can see.
Update your src/security/auth.py
and add one authorization handler to run on every request:
ctx
(AuthContext): contains info about the current user
, the user’s permissions
, the resource
(“threads”, “crons”, “assistants”), and the action
being taken (“create”, “read”, “update”, “delete”, “search”, “create_run”)value
(dict
): data that is being created or accessed. The contents of this dict depend on the resource and action being accessed. See adding scoped authorization handlers below for information on how to get more tightly scoped access control.langgraph dev
):
@auth.on
handler matches on all authorization events. This is concise, but it means the contents of the value
dict are not well-scoped, and the same user-level access control is applied to every resource. If you want to be more fine-grained, you can also control specific actions on resources.
Update src/security/auth.py
to add handlers for specific resource types:
@auth.on.assistants
) matches any action on the assistants
resource. For each request, LangGraph will run the most specific handler that matches the resource and action being accessed. This means that the four handlers above will run rather than the broadly scoped “@auth.on
” handler.
Try adding the following test code to your test file: