[mercedesme] Remove the access token when the thing is removed (#14942)

* [mercedesme] Remove the access token when the thing is removed

Related to #14818

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2023-05-15 23:56:00 +02:00 committed by GitHub
parent ab16c94ace
commit 353c154c12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -123,11 +123,20 @@ public class AccountHandler extends BaseBridgeHandler implements AccessTokenRefr
@Override @Override
public void dispose() { public void dispose() {
if (!server.isEmpty()) { if (!server.isEmpty()) {
server.get().stop(); CallbackServer serv = server.get();
serv.stop();
serv.dispose();
server = Optional.empty();
Utils.removePort(config.get().callbackPort); 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 * https://next.openhab.org/javadoc/latest/org/openhab/core/auth/client/oauth2/package-summary.html
*/ */

View File

@ -46,6 +46,8 @@ public class CallbackServer {
private static final Map<Integer, CallbackServer> SERVER_MAP = new HashMap<Integer, CallbackServer>(); private static final Map<Integer, CallbackServer> SERVER_MAP = new HashMap<Integer, CallbackServer>();
private static final AccessTokenResponse INVALID_ACCESS_TOKEN = new AccessTokenResponse(); private static final AccessTokenResponse INVALID_ACCESS_TOKEN = new AccessTokenResponse();
private final OAuthFactory oAuthFactory;
private Optional<Server> server = Optional.empty(); private Optional<Server> server = Optional.empty();
private AccessTokenRefreshListener listener; private AccessTokenRefreshListener listener;
private AccountConfiguration config; private AccountConfiguration config;
@ -54,6 +56,7 @@ public class CallbackServer {
public CallbackServer(AccessTokenRefreshListener l, HttpClient hc, OAuthFactory oAuthFactory, public CallbackServer(AccessTokenRefreshListener l, HttpClient hc, OAuthFactory oAuthFactory,
AccountConfiguration config, String callbackUrl) { AccountConfiguration config, String callbackUrl) {
this.oAuthFactory = oAuthFactory;
oacs = oAuthFactory.createOAuthClientService(config.clientId, Constants.MB_TOKEN_URL, Constants.MB_AUTH_URL, oacs = oAuthFactory.createOAuthClientService(config.clientId, Constants.MB_TOKEN_URL, Constants.MB_AUTH_URL,
config.clientId, config.clientSecret, config.getScope(), false); config.clientId, config.clientSecret, config.getScope(), false);
listener = l; listener = l;
@ -64,6 +67,16 @@ public class CallbackServer {
INVALID_ACCESS_TOKEN.setAccessToken(Constants.EMPTY); 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() { public String getAuthorizationUrl() {
try { try {
return oacs.getAuthorizationUrl(callbackUrl, null, null); return oacs.getAuthorizationUrl(callbackUrl, null, null);