taolib.testing.oauth.providers#
OAuth 提供商注册表模块。
Submodules#
Classes#
OAuth 提供商协议。 |
|
GitHub OAuth 提供商。 |
|
Google OAuth 提供商。 |
|
OAuth 提供商注册表。 |
Package Contents#
- class taolib.testing.oauth.providers.OAuthProviderProtocol#
Bases:
ProtocolOAuth 提供商协议。
所有 OAuth 提供商实现必须遵循此协议接口。
- get_authorize_url(client_id: str, redirect_uri: str, state: str, scopes: list[str]) str#
生成 OAuth 授权 URL。
- 参数:
client_id -- 应用 Client ID
redirect_uri -- 回调 URI
state -- CSRF 防护 state 参数
scopes -- 请求的权限范围
- 返回:
完整的授权 URL
- async exchange_code(code: str, client_id: str, client_secret: str, redirect_uri: str) dict[str, Any]#
用授权码交换 Token。
- 参数:
code -- 授权码
client_id -- 应用 Client ID
client_secret -- 应用 Client Secret
redirect_uri -- 回调 URI
- 返回:
Token 响应字典,包含 access_token、refresh_token、expires_in 等
- async fetch_user_info(access_token: str) taolib.testing.oauth.models.profile.OAuthUserInfo#
获取用户信息。
- 参数:
access_token -- 访问令牌
- 返回:
标准化的用户信息
- class taolib.testing.oauth.providers.GitHubOAuthProvider#
GitHub OAuth 提供商。
- get_authorize_url(client_id: str, redirect_uri: str, state: str, scopes: list[str]) str#
生成 GitHub 授权 URL。
- 参数:
client_id -- GitHub Client ID
redirect_uri -- 回调 URI
state -- CSRF 防护 state 参数
scopes -- 请求的权限范围
- 返回:
完整的 GitHub 授权 URL
- async exchange_code(code: str, client_id: str, client_secret: str, redirect_uri: str) dict[str, Any]#
用授权码交换 GitHub Token。
- 参数:
code -- 授权码
client_id -- GitHub Client ID
client_secret -- GitHub Client Secret
redirect_uri -- 回调 URI
- 返回:
Token 响应字典
- 抛出:
OAuthCodeExchangeError -- 交换失败
- async fetch_user_info(access_token: str) taolib.testing.oauth.models.profile.OAuthUserInfo#
获取 GitHub 用户信息。
如果用户信息中没有邮箱,会额外调用邮箱 API 获取主邮箱。
- 参数:
access_token -- GitHub Access Token
- 返回:
标准化的用户信息
- 抛出:
OAuthUserInfoError -- 获取失败
- static _fetch_primary_email(client: httpx.AsyncClient, headers: dict[str, str]) str | None#
- Async:
获取 GitHub 用户的主邮箱。
- 参数:
client -- httpx 异步客户端
headers -- 请求头
- 返回:
主邮箱地址,获取失败返回 None
- class taolib.testing.oauth.providers.GoogleOAuthProvider#
Google OAuth 提供商。
- get_authorize_url(client_id: str, redirect_uri: str, state: str, scopes: list[str]) str#
生成 Google 授权 URL。
- 参数:
client_id -- Google Client ID
redirect_uri -- 回调 URI
state -- CSRF 防护 state 参数
scopes -- 请求的权限范围
- 返回:
完整的 Google 授权 URL
- async exchange_code(code: str, client_id: str, client_secret: str, redirect_uri: str) dict[str, Any]#
用授权码交换 Google Token。
- 参数:
code -- 授权码
client_id -- Google Client ID
client_secret -- Google Client Secret
redirect_uri -- 回调 URI
- 返回:
Token 响应字典
- 抛出:
OAuthCodeExchangeError -- 交换失败
- async fetch_user_info(access_token: str) taolib.testing.oauth.models.profile.OAuthUserInfo#
获取 Google 用户信息。
- 参数:
access_token -- Google Access Token
- 返回:
标准化的用户信息
- 抛出:
OAuthUserInfoError -- 获取失败
- class taolib.testing.oauth.providers.ProviderRegistry#
OAuth 提供商注册表。
管理所有已注册的 OAuth 提供商,支持动态注册和查找。
- _providers: dict[str, base.OAuthProviderProtocol]#
- register(provider: base.OAuthProviderProtocol) None#
注册一个 OAuth 提供商。
- 参数:
provider -- 实现 OAuthProviderProtocol 的提供商实例
- get(provider_name: str) base.OAuthProviderProtocol#
获取指定的提供商。
- 参数:
provider_name -- 提供商名称
- 返回:
提供商实例
- 抛出:
KeyError -- 如果提供商未注册