TokenValidator 类

使用 PyJWKClient 进行简化验证的 JWT 令牌验证程序。

初始化令牌验证程序。

构造函数

TokenValidator(jwt_validation_options: JwtValidationOptions)

参数

名称 说明
jwt_validation_options
必需

JWT 验证的配置

方法

for_entra

为Entra ID令牌创建验证程序。

for_service

为 Bot Framework 服务令牌创建验证程序。

参考:https://learn.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-connector-authentication

validate_token

验证 JWT 令牌。

for_entra

为Entra ID令牌创建验证程序。

for_entra(app_id: str, tenant_id: str | None, scope: str | None = None, application_id_uri: str | None = None, cloud: CloudEnvironment | None = None) -> TokenValidator

参数

名称 说明
app_id
必需

应用的Microsoft应用 ID(用于受众验证)

tenant_id
必需

Azure AD 租户 ID

scope

令牌中必须存在的可选范围

默认值: None
application_id_uri

Azure门户中的可选应用程序 ID URI。 匹配应用清单中的 webApplicationInfo.resource。

默认值: None
cloud

主权云支持的可选云环境

默认值: None

for_service

为 Bot Framework 服务令牌创建验证程序。

参考:https://learn.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-connector-authentication

for_service(app_id: str, service_url: str | None = None, cloud: CloudEnvironment | None = None) -> TokenValidator

参数

名称 说明
app_id
必需

机器人Microsoft应用 ID(用于受众验证)

service_url

用于根据令牌声明进行验证的可选服务 URL

默认值: None
cloud

主权云支持的可选云环境

默认值: None

validate_token

验证 JWT 令牌。

async validate_token(raw_token: str, service_url: str | None = None, scope: str | None = None) -> Dict[str, Any]

参数

名称 说明
raw_token
必需

原始 JWT 令牌字符串

service_url

用于根据令牌声明进行验证的可选服务 URL

默认值: None
scope

令牌中必须存在的可选范围

默认值: None

返回

类型 说明

验证成功时解码的 JWT 有效负载

例外

类型 说明
jwt.InvalidTokenError

令牌验证失败时