Repository navigation
Let TokenExpiredError propagate from _async_request (fixes #5) - #7
YeomansIII wants to merge 4 commits into
Conversation
_async_request() re-raised (InvalidAuthError, ApiError, RateLimitError) but omitted TokenExpiredError, which async_refresh_tokens() raises on a 401 from the refresh endpoint. TokenExpiredError subclasses AuthenticationError, not ApiError, so it matched only the bare `except Exception` and was rewrapped as CannotConnectError. auth.py already guards this exception deliberately; client.py undid that one frame later. Downstream, ha-dominion-energy's coordinator raised UpdateFailed instead of ConfigEntryAuthFailed, so Home Assistant retried forever and never created a reauth flow. Catch the AuthenticationError base class instead, so InvalidAuthError, TokenExpiredError, BrowserAuthRequiredError, and any future auth error keep their type. Bump version to 0.2.3.
ruff is unpinned in the dev extra, so CI now installs 0.16.4, which formats Python code blocks inside Markdown. This breaks 'ruff format --check .' on main as well. Cosmetic only - no code or documented behavior changes.
|
Note on the second commit (README formatting): the first CI run failed on Commit 14d0343 just applies that formatter to the README so this PR can merge green. Cosmetic only. Worth pinning |
The dev extra used open ranges (ruff>=0.6.0, mypy>=1.11.0), so CI installed whatever was newest at the time. ruff 0.16 began formatting Python code blocks inside Markdown, which broke 'ruff format --check .' on main and on every new PR, with no repo change to cause it. Pin ruff==0.16.4 and mypy==2.3.1, and move .pre-commit-config.yaml to the same versions so local hooks, uv.lock and CI agree. Both are the versions CI has already run green.
Dominion sits behind an Incapsula WAF, which fingerprints the User-Agent header. Both strings in the library were wrong in ways a real browser never is: const.py sent AppleWebKit/537.00 and Safari/537.00. Every Chrome build reports 537.36. It also sent macOS 10_15_0, where Chrome always reports 10_15_7, and Chrome 142, which is ten releases behind stable. gigya_auth.py sent a truncated string that stopped after AppleWebKit/537.36, with no Chrome or Safari token at all. This is the header on the request that fetches the WAF cookies. Move the string to a single USER_AGENT constant, correct it to match Chrome 152 on macOS, and use it in both places. Add tests that lock the format and set a version floor. This may relate to #4, where every endpoint returns 403 with an HTML error page and a valid token. That is what a WAF block looks like. Not confirmed - the reporter has not said which state or utility they use.
|
Hit this in production: an expired refresh token showed up as |
Fixes #5.
1. TokenExpiredError rewrapped as CannotConnectError (#5)
_async_request()re-raised(InvalidAuthError, ApiError, RateLimitError), but omittedTokenExpiredError— the exceptionasync_refresh_tokens()raises when the refresh endpoint returns 401.Per
exceptions.py,TokenExpiredErrorsubclassesAuthenticationError, notApiError. So it matched only the bareexcept Exceptionand was rewrapped asCannotConnectError.auth.py:173-179guards this exception deliberately.client.pyundid that one frame later.Downstream, consumers could not tell "credentials expired, the user must re-authenticate" apart from "the network is down". In
ha-dominion-energy, the coordinator raisedUpdateFailedinstead ofConfigEntryAuthFailed, so Home Assistant retried forever and never created a reauth flow. Same symptom as YeomansIII/ha-dominion-energy#16, fixed for theApiError401/403 path in v0.2.2 but not for this one.Fix: catch the
AuthenticationErrorbase class, which coversInvalidAuthError,TokenExpiredError,BrowserAuthRequiredError, and anything added later.Two tests.
test_expired_refresh_token_propagatesmocks the API returning 401, then the refresh endpoint returning 401, and assertsTokenExpiredErrorreaches the caller. Onmainit fails with the exact text from the issue report:test_connection_failure_still_raises_cannot_connectconfirms a realClientConnectorErrorstill surfaces asCannotConnectError, so the widerexceptdoes not swallow transport errors.2. Pin ruff and mypy
The dev extra used open ranges, so CI installed whatever was newest. ruff 0.16 began formatting Python code blocks inside Markdown, which broke
ruff format --check .onmainand on every new PR with no repo change to cause it. Reproduced withuvx ruff@0.16.4 format --check .on a cleanmainworktree.Pinned
ruff==0.16.4andmypy==2.3.1, and moved.pre-commit-config.yamlto matching tags so local hooks,uv.lockand CI agree. One commit applies that formatter toREADME.md; it is cosmetic.This also unblocks #6, whose only CI failure is the same README check.
3. Fix and centralize the user agent
Dominion sits behind an Incapsula WAF (see
_async_load_login_page), which fingerprintsUser-Agent. Both strings in the library were wrong in ways a real browser never is:const.pysentAppleWebKit/537.00andSafari/537.00. Every Chrome build reports537.36. It also sent macOS10_15_0, where Chrome always reports10_15_7, and Chrome 142, ten releases behind stable.gigya_auth.pysent a truncated string that stopped afterAppleWebKit/537.36, with no Chrome or Safari token at all — on the request that fetches the WAF cookies.Moved to a single
USER_AGENTconstant matching Chrome 152 on macOS, used in both places, with tests that lock the format and set a version floor.This may relate to #4, where every endpoint returns 403 with an HTML error page despite a valid token. That is what a WAF block looks like. Not confirmed — the reporter has not said which state or utility they use.
Verification
44 tests pass.
ruff check,ruff format --checkandmypyare clean. CI green on 3.12 and 3.13.Follow-up
ha-dominion-energyneeds adompower>=0.2.3bump in its manifest once this is released. Its coordinator already handlesTokenExpiredErrorcorrectly.