@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
*/
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;
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;
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);