[somfytahoma] add proper OAuth2 token refreshing (#17574)

Signed-off-by: Ondrej Pecta <opecta@gmail.com>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Ondrej Pecta 2024-10-16 20:55:31 +02:00 committed by Ciprian Pascu
parent c1d7304cce
commit f5326ba224

View File

@ -146,6 +146,9 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler {
// Last login timestamp
private Instant lastLoginTimestamp = Instant.MIN;
// Token expiration time
private Instant tokenExpirationTime = Instant.MAX;
/**
* Our configuration
*/
@ -270,7 +273,7 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler {
return;
}
} else {
loginOAUTH();
loginTahoma();
}
if (thingConfig.isDevMode()) {
@ -308,6 +311,12 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler {
}
}
private void doOAuthLogin() throws ExecutionException, InterruptedException, TimeoutException {
lastLoginTimestamp = Instant.now();
loginTahoma();
}
private boolean loginCozyTouch()
throws ExecutionException, InterruptedException, TimeoutException, JsonSyntaxException {
logger.debug("CozyTouch Oauth2 authentication flow");
@ -587,6 +596,11 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler {
return;
}
if (tokenNeedsRefresh()) {
logger.debug("The access token expires soon, refreshing the cloud access token");
refreshToken();
}
List<SomfyTahomaEvent> events = getEvents();
logger.trace("Got total of {} events", events.size());
for (SomfyTahomaEvent event : events) {
@ -594,6 +608,21 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler {
}
}
private void refreshToken() {
try {
doOAuthLogin();
} catch (ExecutionException | TimeoutException e) {
logger.debug("Token refresh failed");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
private boolean tokenNeedsRefresh() {
return !thingConfig.getCloudPortal().equalsIgnoreCase(COZYTOUCH_PORTAL)
&& Instant.now().plusSeconds(thingConfig.getRefresh()).isAfter(tokenExpirationTime);
}
private void processEvent(SomfyTahomaEvent event) {
logger.debug("Got event: {}", event.getName());
switch (event.getName()) {
@ -923,7 +952,7 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler {
}
}
private void loginOAUTH() throws InterruptedException, TimeoutException, ExecutionException, JsonSyntaxException {
private void loginTahoma() throws InterruptedException, TimeoutException, ExecutionException, JsonSyntaxException {
String authBaseUrl = "https://" + SOMFY_OAUTH2_URL;
String urlParameters = "client_id=" + SOMFY_OAUTH2_CLIENT_ID + "&client_secret=" + SOMFY_OAUTH2_CLIENT_SECRET
@ -954,7 +983,8 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler {
SomfyTahomaOauth2Reponse oauth2response = gson.fromJson(response.getContentAsString(),
SomfyTahomaOauth2Reponse.class);
logger.debug("OAuth2 Access Token: {}", oauth2response.getAccessToken());
tokenExpirationTime = Instant.now().plusSeconds(oauth2response.getExpiresIn());
logger.debug("OAuth2 Access Token: {}, expires: {}", oauth2response.getAccessToken(), tokenExpirationTime);
accessToken = oauth2response.getAccessToken();
}