Fix OAuth cookie persistence and HTTP 202 for 2FA#1229
Open
knightwebdevelopmentservices-ops wants to merge 1 commit into
Open
Conversation
Two bugs fixed: 1. Cookie persistence: Use CookieJar(unsafe=True) for aiohttp session. The OAuth flow requires cookies to persist across redirects between different subdomains (api.oauth.blink.com → rest-*.immedia-semi.com). aiohttp's default CookieJar drops cookies on cross-domain redirects, causing 'empty_cookies' errors during the 2FA step. 2. HTTP 202 status: Accept HTTP 202 (Accepted) as a 2FA-required response in addition to HTTP 412 (Precondition Failed). Some Blink regions/versions return 202 instead of 412 to indicate 2FA verification is needed. Fixes: fronzbot#1217 Related: home-assistant/core#168029
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes two bugs that prevent Blink OAuth v2 login with 2FA from working.
Bug 1: Cookie persistence (root cause of
empty_cookieserror)The OAuth v2 flow requires cookies to persist across multiple HTTP requests to
api.oauth.blink.com(authorize → signin page → submit credentials → 2FA verify → get auth code).aiohttp's defaultCookieJaruses strict cookie domain matching, which drops cookies when the OAuth flow redirects or when cookies need to be sent back to the same domain across requests. This causes the Blink API to return:{"error":"bad_request","error_cause":"empty_cookies","error_description":"Empty Cookies."}Fix: Create the
ClientSessionwithCookieJar(unsafe=True), which allows cookies to be properly stored and sent across all requests in the OAuth flow.Bug 2: HTTP 202 status not recognized as 2FA-required
The current code only checks for HTTP 412 (Precondition Failed) to detect when 2FA is required. However, some Blink regions/versions return HTTP 202 (Accepted) instead:
{"next_time_in_secs":60,"phone":"+61xxxxxxxx40","tsv_methods":["sms","whatsapp","voice"],"tsv_state":"sms","user_id":188584242}Fix: Accept both 412 and 202 as 2FA-required responses in
oauth_signin().Testing
test_oauth_signin_2fa_required_202to cover the 202 status code casetest_auth_uses_unsafe_cookie_jarto verify cookie jar configurationRelated Issues
Files Changed
blinkpy/auth.py— UseCookieJar(unsafe=True)for session creationblinkpy/api.py— Accept HTTP 202 as 2FA-required, improve error loggingtests/test_oauth.py— New tests for both fixes