Refactor Cognito auth into an async, persistence-aware token lifecycle
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 35/100
- issue の種類
- リファクタリング
- 明瞭さ
- 明確に書かれている
- 活発さ
- 静か
- 技術スタック
- python
調査の方向性
まず evnex/api.py の認証フロー、特に 120-157、167-207、218-273、275-285、302-310 行を読み、次に tests/test_auth.py の 162-197 行と README の認証例を確認してください。完了とは、非同期認証、永続化されたトークン更新、401 に対する回数制限付きリカバリ、認証の一元化、新しい認証テストを含む、列挙された受け入れ条件がカバーされていることを意味します。
索引モデルが issue の本文から書いたものです。
説明
Context
MFA support in #85 / v0.6.0 adds the missing Cognito challenge flow and fixes several immediate reliability problems. The current API now works reasonably for a CLI or a single long-lived Evnex object.
It is still the wrong abstraction for a Home Assistant integration or another long-running application that must persist credentials safely across restarts.
Problems
1. Refresh-token resumption still requires and retains the password
Evnex.__init__() requires both username and password, even when a refresh token is supplied:
https://github.com/hardbyte/python-evnex/blob/main/evnex/api.py#L120-L157
The README says "the refresh token alone is enough", but callers still need to provide a real or dummy password:
https://github.com/hardbyte/python-evnex/blob/main/README.md#L104-L113
For Home Assistant, the password should ideally be used only during interactive bootstrap/reauthentication and then discarded. Normal startup should require only the durable refresh-token session.
2. Refreshed token state cannot be reliably persisted
pycognito mutates access_token, id_token, and refresh_token internally during authentication and refresh. python-evnex exposes properties for reading them, but provides no notification or async callback when the token set changes:
https://github.com/hardbyte/python-evnex/blob/main/evnex/api.py#L167-L207
https://github.com/hardbyte/python-evnex/blob/main/evnex/api.py#L275-L285
A caller can manually inspect the properties after a request, but it cannot reliably or atomically persist every replacement token set. This becomes a correctness issue if Cognito refresh-token rotation is enabled, and is awkward even when only access/ID tokens change.
Token persistence should be owned by the application, with the library explicitly publishing each newly issued token set before other requests proceed.
3. The public authentication API is synchronous
authenticate() and respond_to_mfa_challenge() perform network operations synchronously:
https://github.com/hardbyte/python-evnex/blob/main/evnex/api.py#L218-L273
Internal API calls correctly wrap Cognito operations in asyncio.to_thread(), but a Home Assistant config flow calling the documented public methods must know to add its own executor/thread boundary. This is easy to get wrong and inconsistent with the otherwise async client.
All public methods that can perform network I/O should be async.
4. Public consumers are coupled to pycognito
MFA is surfaced through pycognito exception classes and challenge.get_tokens():
https://github.com/hardbyte/python-evnex/blob/main/README.md#L62-L102
This leaks the implementation dependency into integrations and makes it harder to change the Cognito implementation later. It also models MFA as a special mode + code call rather than an explicit Cognito challenge containing the challenge name, opaque session, and parameters.
Even if Evnex currently uses only SMS and TOTP, python-evnex should expose its own typed challenge/result API.
5. Authentication retries are coupled to generic request retries
A 401 calls _ensure_valid_token(). If check_token() considers the JWT unexpired, the request fails without forcing one refresh. Conversely, if renewal keeps reporting success, TokenRefreshedError can consume the full five-attempt generic retry budget:
https://github.com/hardbyte/python-evnex/blob/main/evnex/api.py#L302-L310
https://github.com/hardbyte/python-evnex/blob/main/tests/test_auth.py#L162-L197
Authentication recovery should have independent semantics:
- Send with the current valid token.
- On 401, force one single-flight refresh.
- Retry the request once.
- Surface a reauthentication-required error if it still fails.
It should not share the generic transient-network retry counter, particularly for command endpoints that may not be safe to send repeatedly.
6. Authentication is applied separately to every API method
@refresh_token_if_expired is repeated across resource methods. This is easy to omit when adding a new endpoint and mixes transport/auth concerns into each operation.
Authentication and one-time 401 recovery should happen in one central request path or an injected httpx.Auth-like component.
Proposed direction
Introduce a separate async authentication component, while retaining a backwards-compatible convenience constructor initially.
For example:
@dataclass(frozen=True)
class TokenSet:
access_token: str
id_token: str | None
refresh_token: str
expires_at: datetime | None
@dataclass(frozen=True)
class AuthChallenge:
name: str
session: str
parameters: Mapping[str, str]
class EvnexAuth:
async def get_access_token(self) -> str: ...
async def force_refresh(self) -> TokenSet: ...
async def start_authentication(self, username: str, password: str) -> TokenSet | AuthChallenge: ...
async def respond_to_challenge(self, challenge: AuthChallenge, response: str) -> TokenSet | AuthChallenge: ...
The application should be able to supply an async token-update callback or token store:
async def save_tokens(tokens: TokenSet) -> None: ...
Evnex would then accept an auth object and an httpx.AsyncClient, without needing to retain a password itself.
Acceptance criteria
- A client can resume using a refresh token without supplying or retaining a password.
- Every successful authentication/refresh returns or publishes the complete current token set.
- Token updates can be persisted atomically by the caller.
- All public network operations, including initial auth and MFA responses, are async.
- Public exceptions and challenge objects do not expose
pycognitotypes. - Concurrent API calls perform at most one refresh.
- A 401 triggers at most one forced refresh and one request retry.
- Auth retry behaviour is separate from generic transient HTTP retries.
- Authentication is enforced through one central request path rather than per-method decorators.
- Tests cover refresh-token-only startup, token-update persistence, concurrent refresh, MFA challenge round-tripping, and bounded 401 recovery.
This does not need to block the current MFA release. It is a follow-up refactor to make the new support robust for Home Assistant rather than merely functional in a single-process example.
- 主要言語
- Python
- スター
- 13
- フォーク
- 8
- 平均マージ
- 5時間 58分
- マージ済み PR(30日)
- 2
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
hardbyte/python-evnex のほかの issue
-
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
hardbyte/python-evnex#129 · リアクション 2 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
hardbyte/python-evnex#119 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 52/100
hardbyte/python-evnex#109 · コメント 3 件 ·
hardbyte/python-evnex の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
anthropics/skills#1811 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
speaches-ai/speaches#678 ·
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
datalayer/mcp-compose#42 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
conda-forge/spacy-feedstock#177 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
UKGovernmentBEIS/inspect_evals#2523 ·