页眉认证是一种使用页眉对用户进行身份验证的简单方法。它通常用于将身份验证委托给反向代理。

`header_auth_callback` 函数会在请求带上页眉时被调用。如果用户已通过身份验证,该函数应返回一个 `User` 对象;如果用户未通过身份验证,则应返回 `None`。回调函数(由用户定义)负责管理身份验证逻辑。

示例

from typing import Optional

import chainlit as cl


@cl.header_auth_callback
def header_auth_callback(headers: Dict) -> Optional[cl.User]:
  # Verify the signature of a token in the header (ex: jwt token)
  # or check that the value is matching a row from your database
  if headers.get("test-header") == "test-value":
    return cl.User(identifier="admin", metadata={"role": "admin", "provider": "header"})
  else:
    return None

使用此代码,除非在向应用发送任何请求时将页眉 `test-header` 设置为 `test-value`,否则您将无法访问该应用。