From c10f4f40514ba4c481581165d2f863b6bc3382fa Mon Sep 17 00:00:00 2001 From: lolodomo Date: Sat, 13 May 2023 19:06:41 +0200 Subject: [PATCH] [nest] Remove the access token when the thing is removed (#14940) Related to #14818 Signed-off-by: Laurent Garnier --- .../binding/nest/internal/sdm/api/PubSubAPI.java | 13 +++++++++++-- .../binding/nest/internal/sdm/api/SDMAPI.java | 13 +++++++++++-- .../internal/sdm/handler/SDMAccountHandler.java | 13 +++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) 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 d4102265297..8dbda72da43 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 9e2d3a6b3cd..46deb27c959 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 7fb14e4dfa5..b61097b4b11 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);