From da1a95fbddcfe22dddad354286d1728d9c124b47 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Sat, 6 May 2023 22:21:40 +0200 Subject: [PATCH] [hydrawise] Remove the access token when the thing is removed (#14945) Related to #14818 Signed-off-by: Laurent Garnier --- .../handler/HydrawiseAccountHandler.java | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.binding.hydrawise/src/main/java/org/openhab/binding/hydrawise/internal/handler/HydrawiseAccountHandler.java b/bundles/org.openhab.binding.hydrawise/src/main/java/org/openhab/binding/hydrawise/internal/handler/HydrawiseAccountHandler.java index ef711034d6e..71dd5476726 100644 --- a/bundles/org.openhab.binding.hydrawise/src/main/java/org/openhab/binding/hydrawise/internal/handler/HydrawiseAccountHandler.java +++ b/bundles/org.openhab.binding.hydrawise/src/main/java/org/openhab/binding/hydrawise/internal/handler/HydrawiseAccountHandler.java @@ -68,18 +68,18 @@ public class HydrawiseAccountHandler extends BaseBridgeHandler implements Access private static final String SCOPE = "all"; private final List controllerListeners = Collections .synchronizedList(new ArrayList()); - private final HydrawiseGraphQLClient apiClient; - private final OAuthClientService oAuthService; + private final HttpClient httpClient; + private final OAuthFactory oAuthFactory; + private @Nullable OAuthClientService oAuthService; + private @Nullable HydrawiseGraphQLClient apiClient; private @Nullable ScheduledFuture pollFuture; private @Nullable Customer lastData; private int refresh; public HydrawiseAccountHandler(final Bridge bridge, final HttpClient httpClient, final OAuthFactory oAuthFactory) { super(bridge); - this.oAuthService = oAuthFactory.createOAuthClientService(getThing().toString(), AUTH_URL, AUTH_URL, CLIENT_ID, - CLIENT_SECRET, SCOPE, false); - oAuthService.addAccessTokenRefreshListener(this); - this.apiClient = new HydrawiseGraphQLClient(httpClient, oAuthService); + this.httpClient = httpClient; + this.oAuthFactory = oAuthFactory; } @Override @@ -88,14 +88,34 @@ public class HydrawiseAccountHandler extends BaseBridgeHandler implements Access @Override public void initialize() { + OAuthClientService oAuthService = oAuthFactory.createOAuthClientService(getThing().toString(), AUTH_URL, + AUTH_URL, CLIENT_ID, CLIENT_SECRET, SCOPE, false); + this.oAuthService = oAuthService; + oAuthService.addAccessTokenRefreshListener(this); + this.apiClient = new HydrawiseGraphQLClient(httpClient, oAuthService); logger.debug("Handler initialized."); - scheduler.schedule(this::configure, 0, TimeUnit.SECONDS); + scheduler.schedule(() -> configure(oAuthService), 0, TimeUnit.SECONDS); } @Override public void dispose() { logger.debug("Handler disposed."); clearPolling(); + OAuthClientService oAuthService = this.oAuthService; + if (oAuthService != null) { + oAuthService.removeAccessTokenRefreshListener(this); + oAuthFactory.ungetOAuthService(getThing().toString()); + this.oAuthService = null; + } + } + + @Override + public void handleRemoval() { + OAuthClientService oAuthService = this.oAuthService; + if (oAuthService != null) { + oAuthFactory.deleteServiceAndAccessToken(getThing().toString()); + } + super.handleRemoval(); } @Override @@ -134,7 +154,7 @@ public class HydrawiseAccountHandler extends BaseBridgeHandler implements Access initPolling(delaySeconds, this.refresh); } - private void configure() { + private void configure(OAuthClientService oAuthService) { HydrawiseAccountConfiguration config = getConfig().as(HydrawiseAccountConfiguration.class); try { if (!config.userName.isEmpty() && !config.password.isEmpty()) {