Skip to content

Commit

Permalink
[livisismarthome] Remove the access token when the thing is removed (o…
Browse files Browse the repository at this point in the history
…penhab#14946)

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

Related to openhab#14818

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
Signed-off-by: Matt Myers <mmyers75@icloud.com>
  • Loading branch information
lolodomo authored and matchews committed Aug 9, 2023
1 parent 4921157 commit c903b7b
Showing 1 changed file with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public class LivisiBridgeHandler extends BaseBridgeHandler
private @Nullable ScheduledFuture<?> reInitJob;
private @Nullable ScheduledFuture<?> bridgeRefreshJob;
private @NonNullByDefault({}) LivisiBridgeConfiguration bridgeConfiguration;
private @NonNullByDefault({}) OAuthClientService oAuthService;
private @Nullable OAuthClientService oAuthService;
private String configVersion = "";

/**
Expand Down Expand Up @@ -153,8 +153,9 @@ public void initialize() {
*/
private void initializeClient() {
String tokenURL = URLCreator.createTokenURL(bridgeConfiguration.host);
oAuthService = oAuthFactory.createOAuthClientService(thing.getUID().getAsString(), tokenURL, tokenURL,
"clientId", "clientPass", null, true);
OAuthClientService oAuthService = oAuthFactory.createOAuthClientService(thing.getUID().getAsString(), tokenURL,
tokenURL, "clientId", "clientPass", null, true);
this.oAuthService = oAuthService;
client = createClient(oAuthService);
deviceStructMan = new DeviceStructureManager(createFullDeviceManager(client));
oAuthService.addAccessTokenRefreshListener(this);
Expand Down Expand Up @@ -349,13 +350,28 @@ public void dispose() {
unregisterDeviceStatusListener(bridgeId);
cancelJobs();
stopWebSocket();
OAuthClientService oAuthService = this.oAuthService;
if (oAuthService != null) {
oAuthService.removeAccessTokenRefreshListener(this);
oAuthFactory.ungetOAuthService(thing.getUID().getAsString());
this.oAuthService = null;
}
client = null;
deviceStructMan = null;

super.dispose();
logger.debug("LIVISI SmartHome bridge handler shut down.");
}

@Override
public void handleRemoval() {
OAuthClientService oAuthService = this.oAuthService;
if (oAuthService != null) {
oAuthFactory.deleteServiceAndAccessToken(thing.getUID().getAsString());
}
super.handleRemoval();
}

private synchronized void cancelJobs() {
if (cancelJob(reInitJob)) {
reInitJob = null;
Expand Down Expand Up @@ -884,6 +900,10 @@ private void refreshAccessToken() {
}

private void requestAccessToken() throws OAuthException, IOException, OAuthResponseException {
OAuthClientService oAuthService = this.oAuthService;
if (oAuthService == null) {
throw new OAuthException("OAuth service is not initialized");
}
oAuthService.getAccessTokenByResourceOwnerPasswordCredentials(LivisiBindingConstants.USERNAME,
bridgeConfiguration.password, null);
}
Expand Down

0 comments on commit c903b7b

Please sign in to comment.