From d2620687ae857ab9344f29ecd6a91eda289a8b9b Mon Sep 17 00:00:00 2001 From: sebastian-rahlf <127388479+sebastian-rahlf@users.noreply.github.com> Date: Thu, 6 Jul 2023 13:21:12 +0200 Subject: [PATCH 1/3] Update _accessToken when it's created, updated or deleted It makes more sense to update the _accessToken property, which is needed by commonHeaders, whereever it is changed. --- homeconnect/lib/src/client/client_dart.dart | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/homeconnect/lib/src/client/client_dart.dart b/homeconnect/lib/src/client/client_dart.dart index d68d2b4..ca1529e 100644 --- a/homeconnect/lib/src/client/client_dart.dart +++ b/homeconnect/lib/src/client/client_dart.dart @@ -34,6 +34,7 @@ class HomeConnectApi { throw Exception('No authenticator provided'); } final token = await authenticator!.authorize(baseUrl, credentials); + _accessToken = token.accessToken; storage.setCredentials(token); } @@ -54,13 +55,12 @@ class HomeConnectApi { if (tokens == null) { throw Exception('Failed to refresh token'); } - // set token in storage + _accessToken = tokens.accessToken; await storage.setCredentials(tokens); } Future put({required String resource, required String body}) async { HomeConnectAuthCredentials? userCredentials = await checkTokenIntegrity(); - _accessToken = userCredentials!.accessToken; final uri = baseUrl.join('/api/homeappliances/$resource'); final response = await client.put(uri, headers: commonHeaders, body: body); if (response.statusCode >= 200 && response.statusCode < 300) { @@ -72,7 +72,6 @@ class HomeConnectApi { Future get(String resource) async { HomeConnectAuthCredentials? userCredentials = await checkTokenIntegrity(); - _accessToken = userCredentials!.accessToken; final uri = baseUrl.join('/api/homeappliances/$resource'); final response = await client.get( uri, @@ -87,7 +86,6 @@ class HomeConnectApi { Future delete(String resource) async { HomeConnectAuthCredentials? userCredentials = await checkTokenIntegrity(); - _accessToken = userCredentials!.accessToken; final uri = baseUrl.join('/api/homeappliances/$resource'); final response = await client.delete( uri, @@ -132,7 +130,6 @@ class HomeConnectApi { final uri = baseUrl.join("/api/homeappliances/${source.info.haId}/events"); HomeConnectAuthCredentials? userCredentials = await checkTokenIntegrity(); EventController eventController = EventController(); - _accessToken = userCredentials!.accessToken; try { EventSource eventSource = await EventSource.connect( @@ -170,6 +167,7 @@ class HomeConnectApi { } Future logout() async { + _accessToken = ''; await storage.clearCredentials(); } } From 3f59ea442e7f78fa88b7d0b6c0094f6778b86cc5 Mon Sep 17 00:00:00 2001 From: sebastian-rahlf <127388479+sebastian-rahlf@users.noreply.github.com> Date: Thu, 6 Jul 2023 15:09:24 +0200 Subject: [PATCH 2/3] Bugfix: After logout userCredentials can be null. --- homeconnect/lib/src/client/client_dart.dart | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/homeconnect/lib/src/client/client_dart.dart b/homeconnect/lib/src/client/client_dart.dart index ca1529e..4aa09ce 100644 --- a/homeconnect/lib/src/client/client_dart.dart +++ b/homeconnect/lib/src/client/client_dart.dart @@ -51,7 +51,10 @@ class HomeConnectApi { throw Exception('No authenticator provided'); } final userCredentials = await storage.getCredentials(); - final tokens = await authenticator?.refresh(baseUrl, userCredentials!.refreshToken); + if (userCredentials == null) { + throw Exception('Failed to refresh token'); + } + final tokens = await authenticator?.refresh(baseUrl, userCredentials.refreshToken); if (tokens == null) { throw Exception('Failed to refresh token'); } From cbad39c9fdd04be09f40f916b49b629395f7e9d7 Mon Sep 17 00:00:00 2001 From: Sebastian Rahlf Date: Thu, 6 Jul 2023 15:43:11 +0200 Subject: [PATCH 3/3] Remove unused variable assignments. --- homeconnect/lib/src/client/client_dart.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/homeconnect/lib/src/client/client_dart.dart b/homeconnect/lib/src/client/client_dart.dart index 4aa09ce..907bfd4 100644 --- a/homeconnect/lib/src/client/client_dart.dart +++ b/homeconnect/lib/src/client/client_dart.dart @@ -63,7 +63,7 @@ class HomeConnectApi { } Future put({required String resource, required String body}) async { - HomeConnectAuthCredentials? userCredentials = await checkTokenIntegrity(); + await checkTokenIntegrity(); final uri = baseUrl.join('/api/homeappliances/$resource'); final response = await client.put(uri, headers: commonHeaders, body: body); if (response.statusCode >= 200 && response.statusCode < 300) { @@ -74,7 +74,7 @@ class HomeConnectApi { } Future get(String resource) async { - HomeConnectAuthCredentials? userCredentials = await checkTokenIntegrity(); + await checkTokenIntegrity(); final uri = baseUrl.join('/api/homeappliances/$resource'); final response = await client.get( uri, @@ -88,7 +88,7 @@ class HomeConnectApi { } Future delete(String resource) async { - HomeConnectAuthCredentials? userCredentials = await checkTokenIntegrity(); + await checkTokenIntegrity(); final uri = baseUrl.join('/api/homeappliances/$resource'); final response = await client.delete( uri, @@ -131,7 +131,7 @@ class HomeConnectApi { Future openEventListenerChannel({required HomeDevice source}) async { final uri = baseUrl.join("/api/homeappliances/${source.info.haId}/events"); - HomeConnectAuthCredentials? userCredentials = await checkTokenIntegrity(); + await checkTokenIntegrity(); EventController eventController = EventController(); try {