Skip to content

Commit

Permalink
[mercedesme] Remove the access token when the thing is removed (openh…
Browse files Browse the repository at this point in the history
…ab#14942)

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

Related to openhab#14818

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo authored May 15, 2023
1 parent ab16c94 commit 353c154
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 353c154

Please sign in to comment.