Skip to content

Commit

Permalink
Fix OAuth service handle
Browse files Browse the repository at this point in the history
Fixes #14783

Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur committed Feb 13, 2025
1 parent 5a57b9e commit c3c4d27
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ private I18NKeys() {
}

public static final String BRIDGE_STATUS_DESCRIPTION_ACCESS_TOKEN_NOT_CONFIGURED = "@text/mielecloud.bridge.status.access.token.not.configured";
public static final String BRIDGE_STATUS_DESCRIPTION_EMAIL_NOT_CONFIGURED = "@text/mielecloud.bridge.status.email.not.configured";
public static final String BRIDGE_STATUS_DESCRIPTION_ACCOUNT_NOT_AUTHORIZED = "@text/mielecloud.bridge.status.account.not.authorized";
public static final String BRIDGE_STATUS_DESCRIPTION_ACCESS_TOKEN_REFRESH_FAILED = "@text/mielecloud.bridge.status.access.token.refresh.failed";
public static final String BRIDGE_STATUS_DESCRIPTION_TRANSIENT_HTTP_ERROR = "@text/mielecloud.bridge.status.transient.http.error";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public synchronized void beginAuthorization(String clientId, String clientSecret
throw new OngoingAuthorizationException("There is already an ongoing authorization!", timerExpiryTimestamp);
}

this.oauthClientService = oauthFactory.createOAuthClientService(email, TOKEN_URL, AUTHORIZATION_URL, clientId,
clientSecret, null, false);
this.oauthClientService = oauthFactory.createOAuthClientService(bridgeUid.getAsString(), TOKEN_URL,
AUTHORIZATION_URL, clientId, clientSecret, null, false);
this.bridgeUid = bridgeUid;
this.email = email;
redirectUri = null;
Expand Down Expand Up @@ -136,6 +136,12 @@ public synchronized void completeAuthorization(String redirectUrlWithParameters)

// Although this method is called "get" it actually fetches and stores the token response as a side effect.
oauthClientService.getAccessTokenResponseByAuthorizationCode(authorizationCode, redirectUri);

// Remove any legacy OAuth service handle.
String email = this.email;
if (email != null) {
oauthFactory.deleteServiceAndAccessToken(email);
}
} catch (IOException e) {
throw new OAuthException("Network error while retrieving token response: " + e.getMessage(), e);
} catch (OAuthResponseException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class MieleBridgeHandler extends BaseBridgeHandler
private @Nullable CompletableFuture<@Nullable Void> logoutFuture;
private @Nullable MieleWebservice webService;
private @Nullable ThingDiscoveryService discoveryService;
private @Nullable String oAuthServiceHandle;

/**
* Creates a new {@link MieleBridgeHandler}.
Expand Down Expand Up @@ -109,7 +110,11 @@ public MieleWebservice getWebservice() {
}

private String getOAuthServiceHandle() {
return getConfig().get(MieleCloudBindingConstants.CONFIG_PARAM_EMAIL).toString();
String oAuthServiceHandle = this.oAuthServiceHandle;
if (oAuthServiceHandle == null) {
oAuthServiceHandle = this.oAuthServiceHandle = getThing().getUID().getAsString();
}
return oAuthServiceHandle;
}

@Override
Expand All @@ -132,13 +137,30 @@ public void initializeWebservice() {
try {
tokenRefresher.setRefreshListener(this, getOAuthServiceHandle());
} catch (OAuthException e) {
logger.debug("Could not initialize Miele Cloud bridge.", e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
I18NKeys.BRIDGE_STATUS_DESCRIPTION_ACCOUNT_NOT_AUTHORIZED);
// When the authorization takes place a new initialization will be triggered. Therefore, we can leave the
// bridge in this state.
return;
String oAuthServiceHandleFallback = getConfig().get(MieleCloudBindingConstants.CONFIG_PARAM_EMAIL)
.toString();

if (oAuthServiceHandleFallback == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
I18NKeys.BRIDGE_STATUS_DESCRIPTION_EMAIL_NOT_CONFIGURED);
return;
}

try {
tokenRefresher.setRefreshListener(this, oAuthServiceHandleFallback);
oAuthServiceHandle = oAuthServiceHandleFallback;

logger.warn("Using legacy OAuth service handle, please re-authorize to migrate");
} catch (OAuthException e2) {
logger.debug("Could not initialize Miele Cloud bridge.", e2);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
I18NKeys.BRIDGE_STATUS_DESCRIPTION_ACCOUNT_NOT_AUTHORIZED);
// When the authorization takes place a new initialization will be triggered. Therefore, we can leave
// the bridge in this state.
return;
}
}

languageProvider.setPrioritizedLanguageProvider(this);
tryInitializeWebservice();

Expand Down Expand Up @@ -169,6 +191,7 @@ public void disposeWebservice() {
getWebservice().disconnectSse();
languageProvider.unsetPrioritizedLanguageProvider();
tokenRefresher.unsetRefreshListener(getOAuthServiceHandle());
oAuthServiceHandle = null;

stopWebservice();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,9 @@ channel-type.mielecloud.battery_level.description=The battery level of the robot

# Error message texts
mielecloud.bridge.status.access.token.not.configured=The OAuth2 access token is not configured.
mielecloud.bridge.status.account.not.authorized=The account has not been authorized. Please consult the documentation on how to do that.
mielecloud.bridge.status.account.not.authorized=The account has not been authorized. Please authorize at http(s)://<YOUROPENHAB>:<YOURPORT>/mielecloud
mielecloud.bridge.status.access.token.refresh.failed=Failed to refresh the OAuth2 access token. Please authorize the account again.
mielecloud.bridge.status.email.not.configured=E-mail address is not configured.
mielecloud.bridge.status.transient.http.error=An unexpected HTTP error occurred. Check the logs if this error persists.
mielecloud.thing.status.webservice.missing=The Miele webservice cannot be accessed over the bridge. Check the bridge configuration.
mielecloud.thing.status.removed=This Miele device has been removed from the Miele@home account.
Expand Down

0 comments on commit c3c4d27

Please sign in to comment.