Skip to content

Commit

Permalink
[somfytahoma] add proper OAuth2 token refreshing (#17574)
Browse files Browse the repository at this point in the history
Signed-off-by: Ondrej Pecta <[email protected]>
  • Loading branch information
octa22 authored Oct 16, 2024
1 parent 970ca59 commit 926f680
Showing 1 changed file with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -270,7 +273,7 @@ public synchronized void login() {
return;
}
} else {
loginOAUTH();
loginTahoma();
}

if (thingConfig.isDevMode()) {
Expand Down Expand Up @@ -308,6 +311,12 @@ public synchronized void login() {
}
}

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");
Expand Down Expand Up @@ -587,13 +596,33 @@ private void getTahomaUpdates() {
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) {
processEvent(event);
}
}

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()) {
Expand Down Expand Up @@ -923,7 +952,7 @@ private String getCozytouchJWT()
}
}

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
Expand Down Expand Up @@ -954,7 +983,8 @@ private void loginOAUTH() throws InterruptedException, TimeoutException, Executi
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();
}
Expand Down

0 comments on commit 926f680

Please sign in to comment.