]> git.basschouten.com Git - openhab-addons.git/commitdiff
[mercedesme] Remove the access token when the thing is removed (#14942)
authorlolodomo <lg.hc@free.fr>
Mon, 15 May 2023 21:56:00 +0000 (23:56 +0200)
committerGitHub <noreply@github.com>
Mon, 15 May 2023 21:56:00 +0000 (23:56 +0200)
* [mercedesme] 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.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/handler/AccountHandler.java
bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/server/CallbackServer.java

index 377b7b7a620bf4da2e3d6b81c892fad7d155d182..f47595d86ff0ba9556bac30f952a2adce4b895d1 100644 (file)
@@ -123,11 +123,20 @@ public class AccountHandler extends BaseBridgeHandler implements AccessTokenRefr
     @Override
     public void dispose() {
         if (!server.isEmpty()) {
-            server.get().stop();
+            CallbackServer serv = server.get();
+            serv.stop();
+            serv.dispose();
+            server = Optional.empty();
             Utils.removePort(config.get().callbackPort);
         }
     }
 
+    @Override
+    public void handleRemoval() {
+        server.ifPresent(s -> s.deleteOAuthServiceAndAccessToken());
+        super.handleRemoval();
+    }
+
     /**
      * https://next.openhab.org/javadoc/latest/org/openhab/core/auth/client/oauth2/package-summary.html
      */
index b10d886cfc224ee672df2aadd64a98ad6ea9de8b..cf9711acdb03f88a1e0fe503ac92c6c98208c793 100644 (file)
@@ -46,6 +46,8 @@ public class CallbackServer {
     private static final Map<Integer, CallbackServer> SERVER_MAP = new HashMap<Integer, CallbackServer>();
     private static final AccessTokenResponse INVALID_ACCESS_TOKEN = new AccessTokenResponse();
 
+    private final OAuthFactory oAuthFactory;
+
     private Optional<Server> server = Optional.empty();
     private AccessTokenRefreshListener listener;
     private AccountConfiguration config;
@@ -54,6 +56,7 @@ public class CallbackServer {
 
     public CallbackServer(AccessTokenRefreshListener l, HttpClient hc, OAuthFactory oAuthFactory,
             AccountConfiguration config, String callbackUrl) {
+        this.oAuthFactory = oAuthFactory;
         oacs = oAuthFactory.createOAuthClientService(config.clientId, Constants.MB_TOKEN_URL, Constants.MB_AUTH_URL,
                 config.clientId, config.clientSecret, config.getScope(), false);
         listener = l;
@@ -64,6 +67,16 @@ public class CallbackServer {
         INVALID_ACCESS_TOKEN.setAccessToken(Constants.EMPTY);
     }
 
+    public void dispose() {
+        oAuthFactory.ungetOAuthService(config.clientId);
+        AUTH_MAP.remove(Integer.valueOf(config.callbackPort));
+        SERVER_MAP.remove(Integer.valueOf(config.callbackPort));
+    }
+
+    public void deleteOAuthServiceAndAccessToken() {
+        oAuthFactory.deleteServiceAndAccessToken(config.clientId);
+    }
+
     public String getAuthorizationUrl() {
         try {
             return oacs.getAuthorizationUrl(callbackUrl, null, null);