From: lolodomo Date: Sat, 13 May 2023 17:06:41 +0000 (+0200) Subject: [nest] Remove the access token when the thing is removed (#14940) X-Git-Url: https://git.basschouten.com/?a=commitdiff_plain;h=c10f4f40514ba4c481581165d2f863b6bc3382fa;p=openhab-addons.git [nest] Remove the access token when the thing is removed (#14940) Related to #14818 Signed-off-by: Laurent Garnier --- diff --git a/bundles/org.openhab.binding.nest/src/main/java/org/openhab/binding/nest/internal/sdm/api/PubSubAPI.java b/bundles/org.openhab.binding.nest/src/main/java/org/openhab/binding/nest/internal/sdm/api/PubSubAPI.java index d410226529..8dbda72da4 100644 --- a/bundles/org.openhab.binding.nest/src/main/java/org/openhab/binding/nest/internal/sdm/api/PubSubAPI.java +++ b/bundles/org.openhab.binding.nest/src/main/java/org/openhab/binding/nest/internal/sdm/api/PubSubAPI.java @@ -144,7 +144,9 @@ public class PubSubAPI { private final Logger logger = LoggerFactory.getLogger(PubSubAPI.class); private final HttpClient httpClient; + private final OAuthFactory oAuthFactory; private final OAuthClientService oAuthService; + private final String oAuthServiceHandleId; private final String projectId; private final ScheduledThreadPoolExecutor scheduler; private final Map> subscriptionListeners = new HashMap<>(); @@ -153,14 +155,21 @@ public class PubSubAPI { String clientId, String clientSecret) { this.httpClient = httpClientFactory.getCommonHttpClient(); this.projectId = projectId; - this.oAuthService = oAuthFactory.createOAuthClientService(String.format(PUBSUB_HANDLE_FORMAT, ownerId), - TOKEN_URL, AUTH_URL, clientId, clientSecret, PUBSUB_SCOPE, false); + this.oAuthFactory = oAuthFactory; + this.oAuthServiceHandleId = String.format(PUBSUB_HANDLE_FORMAT, ownerId); + this.oAuthService = oAuthFactory.createOAuthClientService(oAuthServiceHandleId, TOKEN_URL, AUTH_URL, clientId, + clientSecret, PUBSUB_SCOPE, false); scheduler = new ScheduledThreadPoolExecutor(3, new NamedThreadFactory(ownerId, true)); } public void dispose() { subscriptionListeners.clear(); scheduler.shutdownNow(); + oAuthFactory.ungetOAuthService(oAuthServiceHandleId); + } + + public void deleteOAuthServiceAndAccessToken() { + oAuthFactory.deleteServiceAndAccessToken(oAuthServiceHandleId); } public void authorizeClient(String authorizationCode) throws InvalidPubSubAuthorizationCodeException, IOException { diff --git a/bundles/org.openhab.binding.nest/src/main/java/org/openhab/binding/nest/internal/sdm/api/SDMAPI.java b/bundles/org.openhab.binding.nest/src/main/java/org/openhab/binding/nest/internal/sdm/api/SDMAPI.java index 9e2d3a6b3c..46deb27c95 100644 --- a/bundles/org.openhab.binding.nest/src/main/java/org/openhab/binding/nest/internal/sdm/api/SDMAPI.java +++ b/bundles/org.openhab.binding.nest/src/main/java/org/openhab/binding/nest/internal/sdm/api/SDMAPI.java @@ -84,7 +84,9 @@ public class SDMAPI { private final Logger logger = LoggerFactory.getLogger(SDMAPI.class); private final HttpClient httpClient; + private final OAuthFactory oAuthFactory; private final OAuthClientService oAuthService; + private final String oAuthServiceHandleId; private final String projectId; private final Set requestListeners = ConcurrentHashMap.newKeySet(); @@ -92,13 +94,20 @@ public class SDMAPI { public SDMAPI(HttpClientFactory httpClientFactory, OAuthFactory oAuthFactory, String ownerId, String projectId, String clientId, String clientSecret) { this.httpClient = httpClientFactory.getCommonHttpClient(); - this.oAuthService = oAuthFactory.createOAuthClientService(String.format(SDM_HANDLE_FORMAT, ownerId), TOKEN_URL, - AUTH_URL, clientId, clientSecret, SDM_SCOPE, false); + this.oAuthFactory = oAuthFactory; + this.oAuthServiceHandleId = String.format(SDM_HANDLE_FORMAT, ownerId); + this.oAuthService = oAuthFactory.createOAuthClientService(oAuthServiceHandleId, TOKEN_URL, AUTH_URL, clientId, + clientSecret, SDM_SCOPE, false); this.projectId = projectId; } public void dispose() { requestListeners.clear(); + oAuthFactory.ungetOAuthService(oAuthServiceHandleId); + } + + public void deleteOAuthServiceAndAccessToken() { + oAuthFactory.deleteServiceAndAccessToken(oAuthServiceHandleId); } public void authorizeClient(String authorizationCode) throws InvalidSDMAuthorizationCodeException, IOException { diff --git a/bundles/org.openhab.binding.nest/src/main/java/org/openhab/binding/nest/internal/sdm/handler/SDMAccountHandler.java b/bundles/org.openhab.binding.nest/src/main/java/org/openhab/binding/nest/internal/sdm/handler/SDMAccountHandler.java index 7fb14e4dfa..b61097b4b1 100644 --- a/bundles/org.openhab.binding.nest/src/main/java/org/openhab/binding/nest/internal/sdm/handler/SDMAccountHandler.java +++ b/bundles/org.openhab.binding.nest/src/main/java/org/openhab/binding/nest/internal/sdm/handler/SDMAccountHandler.java @@ -263,6 +263,19 @@ public class SDMAccountHandler extends BaseBridgeHandler { } } + @Override + public void handleRemoval() { + PubSubAPI localPubSubAPI = pubSubAPI; + if (localPubSubAPI != null) { + localPubSubAPI.deleteOAuthServiceAndAccessToken(); + } + SDMAPI localSDMAPI = sdmAPI; + if (localSDMAPI != null) { + localSDMAPI.deleteOAuthServiceAndAccessToken(); + } + super.handleRemoval(); + } + @Override public Collection> getServices() { return List.of(SDMDiscoveryService.class);