19
19
LoginError ,
20
20
Response ,
21
21
UploadError ,
22
- UploadResponse ,
22
+ UploadResponse , WhoamiError , WhoamiResponse ,
23
23
)
24
24
import voluptuous as vol
25
25
@@ -166,7 +166,7 @@ def __init__(
166
166
self .hass = hass
167
167
168
168
self ._session_filepath = config_file
169
- self ._auth_tokens : JsonObjectType = {}
169
+ self ._access_tokens : JsonObjectType = {}
170
170
171
171
self ._homeserver = homeserver
172
172
self ._verify_tls = verify_ssl
@@ -192,7 +192,7 @@ async def stop_client(event: HassEvent) -> None:
192
192
193
193
async def handle_startup (event : HassEvent ) -> None :
194
194
"""Run once when Home Assistant finished startup."""
195
- self ._auth_tokens = await self ._get_auth_tokens ()
195
+ self ._access_tokens = await self ._get_auth_tokens ()
196
196
await self ._login ()
197
197
await self ._join_rooms ()
198
198
# Sync once so that we don't respond to past events.
@@ -283,44 +283,55 @@ async def _join_rooms(self) -> None:
283
283
await asyncio .wait (rooms )
284
284
285
285
async def _get_auth_tokens (self ) -> JsonObjectType :
286
- """Read sorted authentication tokens from disk."""
286
+ """Read sorted access tokens from disk."""
287
287
try :
288
288
return load_json_object (self ._session_filepath )
289
289
except HomeAssistantError as ex :
290
290
_LOGGER .warning (
291
- "Loading authentication tokens from file '%s' failed: %s" ,
291
+ "Loading access tokens from file '%s' failed: %s" ,
292
292
self ._session_filepath ,
293
293
str (ex ),
294
294
)
295
295
return {}
296
296
297
297
async def _store_auth_token (self , token : str ) -> None :
298
- """Store authentication token to session and persistent storage."""
299
- self ._auth_tokens [self ._mx_id ] = token
298
+ """Store access token to session and persistent storage."""
299
+ self ._access_tokens [self ._mx_id ] = token
300
300
301
301
await self .hass .async_add_executor_job (
302
- save_json , self ._session_filepath , self ._auth_tokens , True # private=True
302
+ save_json , self ._session_filepath , self ._access_tokens , True # private=True
303
303
)
304
304
305
305
async def _login (self ) -> None :
306
306
"""Log in to the Matrix homeserver.
307
307
308
- Attempts to use the stored authentication token.
308
+ Attempts to use the stored access token.
309
309
If that fails, then tries using the password.
310
310
If that also fails, raises LocalProtocolError.
311
311
"""
312
312
313
- # If we have an authentication token
314
- if (token := self ._auth_tokens .get (self ._mx_id )) is not None :
315
- response = await self ._client .login (token = token )
316
- _LOGGER .debug ("Logging in using stored token" )
317
-
318
- if isinstance (response , LoginError ):
313
+ # If we have an access token
314
+ if (token := self ._access_tokens .get (self ._mx_id )) is not None :
315
+ _LOGGER .debug ("Restoring login from stored access token" )
316
+ self ._client .restore_login (
317
+ user_id = self ._client .user_id ,
318
+ device_id = self ._client .device_id ,
319
+ access_token = token ,
320
+ )
321
+ response = await self ._client .whoami ()
322
+ if isinstance (response , WhoamiError ):
319
323
_LOGGER .warning (
320
- "Login by token failed: %s, %s" ,
324
+ "Restoring login from access token failed: %s, %s" ,
321
325
response .status_code ,
322
326
response .message ,
323
327
)
328
+ self ._client .access_token = "" # Force a soft-logout if the homeserver didn't.
329
+ elif isinstance (response , WhoamiResponse ):
330
+ _LOGGER .debug (
331
+ "Successfully restored login from access token: user_id '%s', device_id '%s'" ,
332
+ response .user_id ,
333
+ response .device_id ,
334
+ )
324
335
325
336
# If the token login did not succeed
326
337
if not self ._client .logged_in :
0 commit comments