oauth_state
feishu.auth.oauth_state
¶
OAuth 重定向流程的 state 防伪:HMAC 签名 + TTL + 发起者身份绑定,抵御 CSRF / state 伪造。
feishu.auth.oauth.OAuthNamespace.authorize_url 接受一个透传的 state 但不规定其生成与校验。
feishu.auth.oauth_state.OAuthStateSigner 生成一个自包含、经 HMAC-SHA256 签名、带签发时间的 state,
并在回调时校验签名与有效期;user_matches 进一步校验「完成授权的用户」与「发起授权的用户」一致。
注意:本签名器是无状态的(仅保证完整性、时效与身份绑定)。若需严格的「一次性」语义,调用方应另以一个一次性
nonce 存储记录已消费的 nonce,本签名器在每个 state 中携带 nonce 以便此类去重。
OAuthState
dataclass
¶
一个已校验的 OAuth state:发起授权的用户别名、申请的 scopes、租户、nonce 与签发时间。
源代码位于: feishu/auth/oauth_state.py
OAuthStateSigner
¶
生成与校验 HMAC 签名的 OAuth state。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
用于 HMAC-SHA256 的密钥,不能为空。 |
必需 |
|
int
|
|
600
|
|
str
|
版本标识,纳入签名载荷以便后续平滑升级。默认为 |
'v1'
|
示例:
源代码位于: feishu/auth/oauth_state.py
| Python | |
|---|---|
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
issue
¶
issue(*, user_keys: tuple[str, ...] = (), scopes: tuple[str, ...] = (), tenant_key: str | None = None, nonce: str | None = None) -> str
签发一个签名的 state 字符串,可绑定发起用户、scopes 与租户。
源代码位于: feishu/auth/oauth_state.py
consume
¶
consume(state: str | None) -> OAuthState | None
校验签名与有效期,成功返回 feishu.auth.oauth_state.OAuthState,否则返回 None。
源代码位于: feishu/auth/oauth_state.py
user_matches
¶
user_matches(state: OAuthState, callback_user: Mapping[str, Any]) -> bool
校验完成授权的回调用户是否就是发起授权的用户。
fail-closed:state 未绑定发起用户(user_keys 为空)时返回 False——无绑定就无法证明「完成授权
者即发起者」,按零信任视作校验失败而非放行(避免任意回调用户冒用一个无主 state)。发起方应始终绑定
用户(见 feishu.auth.oauth_state.OAuthStateSigner.issue 的 user_keys)。