diff --git a/bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/handler/AccountHandler.java b/bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/handler/AccountHandler.java index 377b7b7a620bf..f47595d86ff0b 100644 --- a/bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/handler/AccountHandler.java +++ b/bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/handler/AccountHandler.java @@ -123,11 +123,20 @@ private String configValid() { @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 */ diff --git a/bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/server/CallbackServer.java b/bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/server/CallbackServer.java index b10d886cfc224..cf9711acdb03f 100644 --- a/bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/server/CallbackServer.java +++ b/bundles/org.openhab.binding.mercedesme/src/main/java/org/openhab/binding/mercedesme/internal/server/CallbackServer.java @@ -46,6 +46,8 @@ public class CallbackServer { private static final Map SERVER_MAP = new HashMap(); private static final AccessTokenResponse INVALID_ACCESS_TOKEN = new AccessTokenResponse(); + private final OAuthFactory oAuthFactory; + private Optional 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 CallbackServer(AccessTokenRefreshListener l, HttpClient hc, OAuthFactory 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);