taolib.testing.oauth.cache.state_store#

OAuth CSRF State 存储模块。

使用 Redis 存储和验证 OAuth 流程中的 CSRF state 参数。

Classes#

OAuthStateStore

OAuth CSRF State 存储。

Module Contents#

class taolib.testing.oauth.cache.state_store.OAuthStateStore(redis_client, ttl_seconds: int = 600)#

OAuth CSRF State 存储。

基于 Redis 的 state 参数管理,支持一次性消费和自动过期。

参数:
  • redis_client -- Redis 异步客户端

  • ttl_seconds -- state 有效期(秒),默认 600(10 分钟)

_redis#
_ttl = 600#
async create_state(extra_data: dict[str, Any] | None = None) str#

创建新的 CSRF state token。

参数:

extra_data -- 附加数据(如 return_url, link_to_user_id)

返回:

state token 字符串

async validate_and_consume(state: str) dict[str, Any] | None#

验证并消费 state token(一次性使用)。

原子性地读取并删除 state,防止重放攻击。

参数:

state -- 待验证的 state token

返回:

附加数据字典,如果 state 无效/已过期/已消费则返回 None