Skip to content

Commit

Permalink
[spotify] Remove the access token when the thing is removed (openhab#…
Browse files Browse the repository at this point in the history
…14947)

Related to openhab#14818

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
Signed-off-by: Jørgen Austvik <jaustvik@acm.org>
  • Loading branch information
lolodomo authored and austvik committed Mar 27, 2024
1 parent 095b7e6 commit 46f20a5
Showing 1 changed file with 25 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,7 @@
*/
package org.openhab.binding.spotify.internal.handler;

import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_ACCESSTOKEN;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_CONFIG_IMAGE_INDEX;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_DEVICEACTIVE;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_DEVICEID;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_DEVICENAME;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_DEVICES;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_DEVICESHUFFLE;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_DEVICETYPE;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_DEVICEVOLUME;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_ALBUMHREF;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_ALBUMID;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_ALBUMIMAGE;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_ALBUMIMAGEURL;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_ALBUMNAME;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_ALBUMTYPE;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_ALBUMURI;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_ARTISTHREF;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_ARTISTID;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_ARTISTNAME;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_ARTISTTYPE;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_ARTISTURI;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_TRACKDISCNUMBER;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_TRACKDURATION_FMT;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_TRACKDURATION_MS;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_TRACKEXPLICIT;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_TRACKHREF;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_TRACKID;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_TRACKNAME;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_TRACKNUMBER;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_TRACKPOPULARITY;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_TRACKPROGRESS_FMT;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_TRACKPROGRESS_MS;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_TRACKTYPE;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYED_TRACKURI;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYLISTNAME;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYLISTS;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYLISTS_LIMIT;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_PLAYLISTS_OFFSET;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_TRACKPLAYER;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.CHANNEL_TRACKREPEAT;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.PROPERTY_SPOTIFY_USER;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.SPOTIFY_API_TOKEN_URL;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.SPOTIFY_AUTHORIZE_URL;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.SPOTIFY_SCOPES;
import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.*;

import java.io.IOException;
import java.math.BigDecimal;
Expand Down Expand Up @@ -163,7 +120,7 @@ public class SpotifyBridgeHandler extends BaseBridgeHandler

// Field members assigned in initialize method
private @NonNullByDefault({}) Future<?> pollingFuture;
private @NonNullByDefault({}) OAuthClientService oAuthService;
private @Nullable OAuthClientService oAuthService;
private @NonNullByDefault({}) SpotifyApi spotifyApi;
private @NonNullByDefault({}) SpotifyBridgeConfiguration configuration;
private @NonNullByDefault({}) SpotifyHandleCommands handleCommand;
Expand Down Expand Up @@ -229,11 +186,19 @@ public void handleCommand(ChannelUID channelUID, Command command) {
@Override
public void dispose() {
active = false;
cancelSchedulers();
OAuthClientService oAuthService = this.oAuthService;
if (oAuthService != null) {
oAuthService.removeAccessTokenRefreshListener(this);
oAuthFactory.ungetOAuthService(thing.getUID().getAsString());
this.oAuthService = null;
}
oAuthFactory.ungetOAuthService(thing.getUID().getAsString());
cancelSchedulers();
}

@Override
public void handleRemoval() {
oAuthFactory.deleteServiceAndAccessToken(thing.getUID().getAsString());
super.handleRemoval();
}

@Override
Expand All @@ -256,6 +221,7 @@ public boolean isAuthorized() {

private @Nullable AccessTokenResponse getAccessTokenResponse() {
try {
OAuthClientService oAuthService = this.oAuthService;
return oAuthService == null ? null : oAuthService.getAccessTokenResponse();
} catch (OAuthException | IOException | OAuthResponseException | RuntimeException e) {
logger.debug("Exception checking authorization: ", e);
Expand Down Expand Up @@ -286,6 +252,10 @@ public boolean equalsThingUID(String thingUID) {
@Override
public String formatAuthorizationUrl(String redirectUri) {
try {
OAuthClientService oAuthService = this.oAuthService;
if (oAuthService == null) {
throw new OAuthException("OAuth service is not initialized");
}
return oAuthService.getAuthorizationUrl(redirectUri, null, thing.getUID().getAsString());
} catch (final OAuthException e) {
logger.debug("Error constructing AuthorizationUrl: ", e);
Expand All @@ -296,6 +266,10 @@ public String formatAuthorizationUrl(String redirectUri) {
@Override
public String authorize(String redirectUri, String reqCode) {
try {
OAuthClientService oAuthService = this.oAuthService;
if (oAuthService == null) {
throw new OAuthException("OAuth service is not initialized");
}
logger.debug("Make call to Spotify to get access token.");
final AccessTokenResponse credentials = oAuthService.getAccessTokenResponseByAuthorizationCode(reqCode,
redirectUri);
Expand Down Expand Up @@ -329,8 +303,10 @@ public void initialize() {
updateStatus(ThingStatus.UNKNOWN);
active = true;
configuration = getConfigAs(SpotifyBridgeConfiguration.class);
oAuthService = oAuthFactory.createOAuthClientService(thing.getUID().getAsString(), SPOTIFY_API_TOKEN_URL,
SPOTIFY_AUTHORIZE_URL, configuration.clientId, configuration.clientSecret, SPOTIFY_SCOPES, true);
OAuthClientService oAuthService = oAuthFactory.createOAuthClientService(thing.getUID().getAsString(),
SPOTIFY_API_TOKEN_URL, SPOTIFY_AUTHORIZE_URL, configuration.clientId, configuration.clientSecret,
SPOTIFY_SCOPES, true);
this.oAuthService = oAuthService;
oAuthService.addAccessTokenRefreshListener(SpotifyBridgeHandler.this);
spotifyApi = new SpotifyApi(oAuthService, scheduler, httpClient);
handleCommand = new SpotifyHandleCommands(spotifyApi);
Expand Down

0 comments on commit 46f20a5

Please sign in to comment.