Skip to content

Fix external auth reconnection loop if connection lost after refresh token expiration#8279

Merged
bramkragten merged 3 commits intohome-assistant:devfrom
chriss158:dev
Jan 29, 2021
Merged

Fix external auth reconnection loop if connection lost after refresh token expiration#8279
bramkragten merged 3 commits intohome-assistant:devfrom
chriss158:dev

Conversation

@chriss158
Copy link
Copy Markdown
Contributor

@chriss158 chriss158 commented Jan 29, 2021

Breaking change

Proposed change

This PR fixes the bug described in the issue #7732. This bug was introduced with PR #7236.

The external auth module (which is used by the companion apps (iOS/Android)) doesn't survive a lost connection if the refresh token is expired. The process is fully described in the issue #7732

The problem occurs frequently more on devices which doesn't close/suspend the app if not used and therefore doesn't force close the connection by closing/suspending the app.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example configuration

For better debugging, I lowered the refresh token expiration from 30 minutes to 1 minute in HA here:
https://github.com/home-assistant/core/blob/ac3a6aaa8cdb005b3c900f9d9671e5a859351060/homeassistant/auth/const.py#L4

To reproduce this problem you need to disable "Close connection automatically" in the profile settings (only if you didn't lower the refresh token expiration time < 5 minutes) and use the Home Assistant Companion App (Then the external auth module is used)

  1. Open the app and let it open and wait for the refresh token timeout (30 minutes or if lowered the ACCESS_TOKE_EXPIRATION then X Minutes)
  2. Right after the refresh token is expired, stop HA
  3. Now wait till the App tries to reconnect and show the "Connection unsuccessful" popup with the "Wait/Retry/Settings" buttons.
    3a. Alternatively you can try to trigger a fetch from HA with auth (which needs a new refresh token), by opening e.g. the history tab.
  4. Now start HA again.

Now we are in a reconnection loop and the frontend cannot connect anymore.
You will see in the Home Assistant log continuously following messages (Be sure to enable the websocket debug logs in HA)

logger:
  default: info
  logs:
    homeassistant.components.websocket_api: debug
2020-11-16 17:31:00 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.140600942755264] Connected from ipaddress
2020-11-16 17:31:00 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.140600942755264] Sending {'type': 'auth_required', 'ha_version': '0.117.6'}
2020-11-16 17:31:00 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.140600942755264] Disconnected

Additional information

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

* This happens when the refresh token is expired
* Maybe the data is also helpful on a reject, therefore reject uses the data object
@chriss158 chriss158 requested a review from bramkragten January 29, 2021 12:12
@bramkragten bramkragten merged commit bd59c4f into home-assistant:dev Jan 29, 2021
bramkragten pushed a commit that referenced this pull request Jan 29, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Jan 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reconnecting loop if connection is broken after 30 minutes

3 participants