Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[somfytahoma] Add proper OAuth2 token refreshing #17574

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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