googleapis / googleapis/google-cloud-python
Auth: Align async token endpoint request helper with google.auth.aio.transport spec
- 主要语言
- Python
- 星标
- 5.4k
- 派生
- 1.8k
- 平均合并
- 2 天 23 小时
- 30 天内合并 PR
- 123
描述
### Summary of the issue
#### **Problem Description**
Currently, the core asynchronous token endpoint helper `_token_endpoint_request_no_throw` inside `google/oauth2/_client_async.py` assumes that the async transport `Request` callable returns a legacy response object (such as `_aiohttp_requests.Response` or `_CombinedResponse`) that exposes `.status` (as a property) and `.content()` (as an awaitable coroutine returning bytes):
```python
# google/oauth2/_client_async.py
response_body1 = await response.content()
...
if response.status == http_client.OK:
```
However, the modern public asynchronous transport interface specification defined in **`google.auth.aio.transport.Response`** specifies the property **`status_code`** and the method **`async def read(self) -> bytes`** instead (it does not define `.status` or `.content()`).
If a developer passes a fully compliant modern `google.auth.aio` transport (exposing only `.status_code` and `.read()`), executing any token grant or refresh methods (e.g., `jwt_grant`, `id_token_jwt_grant`, or `refresh_grant`) will instantly raise an `AttributeError` on `status` or `content`, crashing the primary authentication flow.
---
#### **Proposed Solution**
Update `_token_endpoint_request_no_throw` inside `google/oauth2/_client_async.py` to defensively support both legacy and modern AIO transports by using `hasattr()` fallback checks:
1. **Status Code fallback**:
```python
status_code = (
response.status_code
if hasattr(response, "status_code")
else response.status
)
```
2. **Body Read fallback**:
```python
if hasattr(response, "read"):
response_body1 = await response.read()
else:
response_body1 = await response.content()
```
*(Note: To prevent unhandled connection/socket exceptions during asynchronous response body streaming, these `read()`/`content()` calls should also be securely enclosed within the method's primary `try...except` block).*
---
#### **Testing Requirements**
- Update the unit tests in `test__client_async.py` to verify this compatibility.
- Implement dedicated test cases to ensure that both legacy and modern transport specifications are cleanly exercised in CI/CD without regressions.
贡献指南
调研方向
从 google/oauth2/_client_async.py 中的 _token_endpoint_request_no_throw 开始,将其响应处理与 google.auth.aio.transport.Response 规范进行比较。然后更新 test__client_async.py 中的测试用例,以覆盖 legacy 和 modern 两种响应接口,包括读取 body 失败的情况。运行聚焦的 async client 测试;当 token 授权和刷新流程在两种 transport 形式下都能正常工作且不出现 AttributeError 时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- authentication
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 冷清
- 描述清晰度
- 描述清楚
- 新手友好度
- 72/100