]> git.basschouten.com Git - openhab-addons.git/commitdiff
[livisismarthome] Remove the access token when the thing is removed (#14946)
authorlolodomo <lg.hc@free.fr>
Sat, 6 May 2023 10:45:43 +0000 (12:45 +0200)
committerGitHub <noreply@github.com>
Sat, 6 May 2023 10:45:43 +0000 (12:45 +0200)
* [livisismarthome] Remove the access token when the thing is removed

Related to #14818

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
bundles/org.openhab.binding.livisismarthome/src/main/java/org/openhab/binding/livisismarthome/internal/handler/LivisiBridgeHandler.java

index 8609c285f59e710484875e78701f191355d8d9ba..5de2f8135c5489d621f2514fa4e65d5621833b6e 100644 (file)
@@ -114,7 +114,7 @@ public class LivisiBridgeHandler extends BaseBridgeHandler
     private @Nullable ScheduledFuture<?> reInitJob;
     private @Nullable ScheduledFuture<?> bridgeRefreshJob;
     private @NonNullByDefault({}) LivisiBridgeConfiguration bridgeConfiguration;
-    private @NonNullByDefault({}) OAuthClientService oAuthService;
+    private @Nullable OAuthClientService oAuthService;
     private String configVersion = "";
 
     /**
@@ -153,8 +153,9 @@ public class LivisiBridgeHandler extends BaseBridgeHandler
      */
     private void initializeClient() {
         String tokenURL = URLCreator.createTokenURL(bridgeConfiguration.host);
-        oAuthService = oAuthFactory.createOAuthClientService(thing.getUID().getAsString(), tokenURL, tokenURL,
-                "clientId", "clientPass", null, true);
+        OAuthClientService oAuthService = oAuthFactory.createOAuthClientService(thing.getUID().getAsString(), tokenURL,
+                tokenURL, "clientId", "clientPass", null, true);
+        this.oAuthService = oAuthService;
         client = createClient(oAuthService);
         deviceStructMan = new DeviceStructureManager(createFullDeviceManager(client));
         oAuthService.addAccessTokenRefreshListener(this);
@@ -349,6 +350,12 @@ public class LivisiBridgeHandler extends BaseBridgeHandler
         unregisterDeviceStatusListener(bridgeId);
         cancelJobs();
         stopWebSocket();
+        OAuthClientService oAuthService = this.oAuthService;
+        if (oAuthService != null) {
+            oAuthService.removeAccessTokenRefreshListener(this);
+            oAuthFactory.ungetOAuthService(thing.getUID().getAsString());
+            this.oAuthService = null;
+        }
         client = null;
         deviceStructMan = null;
 
@@ -356,6 +363,15 @@ public class LivisiBridgeHandler extends BaseBridgeHandler
         logger.debug("LIVISI SmartHome bridge handler shut down.");
     }
 
+    @Override
+    public void handleRemoval() {
+        OAuthClientService oAuthService = this.oAuthService;
+        if (oAuthService != null) {
+            oAuthFactory.deleteServiceAndAccessToken(thing.getUID().getAsString());
+        }
+        super.handleRemoval();
+    }
+
     private synchronized void cancelJobs() {
         if (cancelJob(reInitJob)) {
             reInitJob = null;
@@ -884,6 +900,10 @@ public class LivisiBridgeHandler extends BaseBridgeHandler
     }
 
     private void requestAccessToken() throws OAuthException, IOException, OAuthResponseException {
+        OAuthClientService oAuthService = this.oAuthService;
+        if (oAuthService == null) {
+            throw new OAuthException("OAuth service is not initialized");
+        }
         oAuthService.getAccessTokenByResourceOwnerPasswordCredentials(LivisiBindingConstants.USERNAME,
                 bridgeConfiguration.password, null);
     }