mirror of
https://github.com/danieldemus/openhab-addons
synced 2026-07-29 12:34:21 +02:00
Adapt to new Null annotations in AccessTokenResponse (#20366)
* Adapt to new Null annotations in AccessTokenResponse Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
+18
-11
@@ -81,12 +81,20 @@ public class AutomowerBridge {
|
||||
}
|
||||
}
|
||||
|
||||
private String getAccessToken() throws AutomowerCommunicationException {
|
||||
String token = authenticate().getAccessToken();
|
||||
if (token == null) {
|
||||
throw new AutomowerCommunicationException("Unable to authenticate, no access token available");
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return A result containing a list of mowers that are available for the current user
|
||||
* @throws AutomowerCommunicationException In case the query cannot be executed successfully
|
||||
*/
|
||||
public MowerListResult getAutomowers() throws AutomowerCommunicationException {
|
||||
return automowerApi.getMowers(appKey, authenticate().getAccessToken());
|
||||
return automowerApi.getMowers(appKey, getAccessToken());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -95,7 +103,7 @@ public class AutomowerBridge {
|
||||
* @throws AutomowerCommunicationException In case the query cannot be executed successfully
|
||||
*/
|
||||
public Mower getAutomowerStatus(String id) throws AutomowerCommunicationException {
|
||||
return automowerApi.getMower(appKey, authenticate().getAccessToken(), id).getData();
|
||||
return automowerApi.getMower(appKey, getAccessToken(), id).getData();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,7 +112,7 @@ public class AutomowerBridge {
|
||||
* @throws AutomowerCommunicationException In case the query cannot be executed successfully
|
||||
*/
|
||||
public MowerMessages getAutomowerMessages(String id) throws AutomowerCommunicationException {
|
||||
return automowerApi.getMowerMessages(appKey, authenticate().getAccessToken(), id).getData();
|
||||
return automowerApi.getMowerMessages(appKey, getAccessToken(), id).getData();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,7 +145,7 @@ public class AutomowerBridge {
|
||||
|
||||
MowerCommandRequest request = new MowerCommandRequest();
|
||||
request.setData(mowerCommand);
|
||||
automowerApi.sendCommand(appKey, authenticate().getAccessToken(), id, request);
|
||||
automowerApi.sendCommand(appKey, getAccessToken(), id, request);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -162,8 +170,7 @@ public class AutomowerBridge {
|
||||
MowerCalendardRequest calendarRequest = new MowerCalendardRequest();
|
||||
calendarRequest.setData(mowerCalendar);
|
||||
|
||||
automowerApi.sendCalendar(appKey, authenticate().getAccessToken(), id, hasWorkAreas, workAreaId,
|
||||
calendarRequest);
|
||||
automowerApi.sendCalendar(appKey, getAccessToken(), id, hasWorkAreas, workAreaId, calendarRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -182,7 +189,7 @@ public class AutomowerBridge {
|
||||
MowerSettingsRequest settingsRequest = new MowerSettingsRequest();
|
||||
settingsRequest.setData(mowerSettings);
|
||||
|
||||
automowerApi.sendSettings(appKey, authenticate().getAccessToken(), id, settingsRequest);
|
||||
automowerApi.sendSettings(appKey, getAccessToken(), id, settingsRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -192,7 +199,7 @@ public class AutomowerBridge {
|
||||
* @throws AutomowerCommunicationException In case the query cannot be executed successfully
|
||||
*/
|
||||
public void sendAutomowerConfirmError(String id) throws AutomowerCommunicationException {
|
||||
automowerApi.sendConfirmError(appKey, authenticate().getAccessToken(), id);
|
||||
automowerApi.sendConfirmError(appKey, getAccessToken(), id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -202,7 +209,7 @@ public class AutomowerBridge {
|
||||
* @throws AutomowerCommunicationException In case the query cannot be executed successfully
|
||||
*/
|
||||
public void sendAutomowerResetCuttingBladeUsageTime(String id) throws AutomowerCommunicationException {
|
||||
automowerApi.sendResetCuttingBladeUsageTime(appKey, authenticate().getAccessToken(), id);
|
||||
automowerApi.sendResetCuttingBladeUsageTime(appKey, getAccessToken(), id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -222,7 +229,7 @@ public class AutomowerBridge {
|
||||
MowerStayOutZoneRequest zoneRequest = new MowerStayOutZoneRequest();
|
||||
zoneRequest.setData(zoneData);
|
||||
|
||||
automowerApi.sendStayOutZone(appKey, authenticate().getAccessToken(), id, zoneId, zoneRequest);
|
||||
automowerApi.sendStayOutZone(appKey, getAccessToken(), id, zoneId, zoneRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -242,6 +249,6 @@ public class AutomowerBridge {
|
||||
MowerWorkAreaRequest workAreaRequest = new MowerWorkAreaRequest();
|
||||
workAreaRequest.setData(workAreaData);
|
||||
|
||||
automowerApi.sendWorkArea(appKey, authenticate().getAccessToken(), id, workAreaId, workAreaRequest);
|
||||
automowerApi.sendWorkArea(appKey, getAccessToken(), id, workAreaId, workAreaRequest);
|
||||
}
|
||||
}
|
||||
|
||||
+6
-1
@@ -192,7 +192,12 @@ public class GroupePSABridgeHandler extends BaseBridgeHandler {
|
||||
result = localOAuthService.getAccessTokenByResourceOwnerPasswordCredentials(userName, password,
|
||||
localVendorConstants.scope);
|
||||
}
|
||||
return result.getAccessToken();
|
||||
String token = result.getAccessToken();
|
||||
if (token == null) {
|
||||
throw new GroupePSACommunicationException("Unable to authenticate, no access token available");
|
||||
} else {
|
||||
return token;
|
||||
}
|
||||
} catch (OAuthException | IOException | OAuthResponseException e) {
|
||||
throw new GroupePSACommunicationException("Unable to authenticate: " + getRootCause(e).getMessage(), e);
|
||||
}
|
||||
|
||||
+8
-5
@@ -23,6 +23,7 @@ import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
@@ -107,14 +108,16 @@ public class HttpHelper {
|
||||
String lastToken = lastAccessToken;
|
||||
if (lastToken == null) {
|
||||
LoggerFactory.getLogger(HttpHelper.class).debug("The used access token was created at {}",
|
||||
LocalDateTime.ofInstant(accessTokenResponse.getCreatedOn(), ZoneId.systemDefault())
|
||||
LocalDateTime
|
||||
.ofInstant(Objects.requireNonNullElse(accessTokenResponse.getCreatedOn(),
|
||||
Instant.now()), ZoneId.systemDefault())
|
||||
.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
||||
} else if (!lastToken.equals(accessTokenResponse.getAccessToken())) {
|
||||
LoggerFactory.getLogger(HttpHelper.class)
|
||||
.debug("The access token changed. New one created at {}",
|
||||
LocalDateTime
|
||||
.ofInstant(accessTokenResponse.getCreatedOn(), ZoneId.systemDefault())
|
||||
.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
||||
.debug("The access token changed. New one created at {}", LocalDateTime
|
||||
.ofInstant(Objects.requireNonNullElse(accessTokenResponse.getCreatedOn(),
|
||||
Instant.now()), ZoneId.systemDefault())
|
||||
.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
||||
}
|
||||
lastAccessToken = accessTokenResponse.getAccessToken();
|
||||
|
||||
|
||||
+7
-3
@@ -128,8 +128,8 @@ public abstract class BridgeRemoteApiHandler extends BridgeRemoteBaseHandler {
|
||||
}
|
||||
|
||||
public String authorize(String redirectUri, String reqState, String reqCode) throws LinkyException {
|
||||
// Will work only in case of direct oAuth2 authentification to enedis
|
||||
// this is not the case in v1 as we go trough MyElectricalData
|
||||
// Will work only in case of direct oAuth2 authentication to enedis
|
||||
// this is not the case in v1 as we go through MyElectricalData
|
||||
|
||||
try {
|
||||
logger.debug("Make call to Enedis to get access token.");
|
||||
@@ -142,8 +142,12 @@ public abstract class BridgeRemoteApiHandler extends BridgeRemoteBaseHandler {
|
||||
.getAccessTokenByClientCredentials(LinkyBindingConstants.LINKY_SCOPES);
|
||||
|
||||
String accessToken = credentials.getAccessToken();
|
||||
if (accessToken == null) {
|
||||
throw new LinkyException("Unable to authenticate, no access token available");
|
||||
}
|
||||
|
||||
logger.debug("Acces token: {}", accessToken);
|
||||
// avoid logging the full access token for security reasons
|
||||
logger.debug("Access token: {}...", accessToken.substring(0, 3));
|
||||
return accessToken;
|
||||
} catch (RuntimeException | OAuthException | IOException e) {
|
||||
throw new LinkyException("Error during oAuth authorize :" + e.getMessage(), e);
|
||||
|
||||
+1
-1
@@ -269,7 +269,7 @@ public class LivisiBridgeHandler extends BaseBridgeHandler
|
||||
}
|
||||
|
||||
private static Optional<String> getAccessToken(LivisiClient client) throws IOException {
|
||||
return Optional.of(client.getAccessTokenResponse().getAccessToken());
|
||||
return Optional.ofNullable(client.getAccessTokenResponse().getAccessToken());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+3
-2
@@ -21,6 +21,7 @@ import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Instant;
|
||||
import java.util.Base64;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -118,7 +119,7 @@ public class Authorization {
|
||||
refreshToken();
|
||||
}
|
||||
}
|
||||
return token.getAccessToken();
|
||||
return Objects.requireNonNullElse(token.getAccessToken(), "");
|
||||
}
|
||||
|
||||
private void refreshToken() {
|
||||
@@ -163,7 +164,7 @@ public class Authorization {
|
||||
tokenResponseJson.createdOn = Instant.now().toString();
|
||||
// A refresh token is delivered optional. If not set in response take old one
|
||||
if (Constants.NOT_SET.equals(tokenResponseJson.refreshToken)) {
|
||||
tokenResponseJson.refreshToken = token.getRefreshToken();
|
||||
tokenResponseJson.refreshToken = Objects.requireNonNullElse(token.getRefreshToken(), "");
|
||||
}
|
||||
token = decodeToken(tokenResponseJson);
|
||||
if (authTokenIsValid()) {
|
||||
|
||||
+1
-1
@@ -123,7 +123,7 @@ public final class OpenHabOAuthTokenRefresher implements OAuthTokenRefresher {
|
||||
if (tokenResponse == null) {
|
||||
return Optional.empty();
|
||||
} else {
|
||||
return Optional.of(tokenResponse.getAccessToken());
|
||||
return Optional.ofNullable(tokenResponse.getAccessToken());
|
||||
}
|
||||
} catch (OAuthException | org.openhab.core.auth.client.oauth2.OAuthException | IOException
|
||||
| OAuthResponseException e) {
|
||||
|
||||
+5
-1
@@ -203,7 +203,11 @@ public final class OAuthAuthorizationHandlerImpl implements OAuthAuthorizationHa
|
||||
throw new OAuthException(
|
||||
"There is no access token in the persistent storage or it already expired and could not be refreshed");
|
||||
} else {
|
||||
return response.getAccessToken();
|
||||
String accessToken = response.getAccessToken();
|
||||
if (accessToken == null) {
|
||||
throw new OAuthException("Unable to authenticate, no access token available");
|
||||
}
|
||||
return accessToken;
|
||||
}
|
||||
} catch (org.openhab.core.auth.client.oauth2.OAuthException e) {
|
||||
throw new OAuthException("Failed to read access token from persistent storage: " + e.getMessage(), e);
|
||||
|
||||
+3
-2
@@ -16,6 +16,7 @@ import static org.openhab.binding.mspa.internal.MSpaConstants.*;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
@@ -152,12 +153,12 @@ public abstract class MSpaBaseAccount extends BaseBridgeHandler {
|
||||
*/
|
||||
public String getToken() {
|
||||
if (MSpaUtils.isTokenValid(token)) {
|
||||
return token.getAccessToken();
|
||||
return Objects.requireNonNullElse(token.getAccessToken(), UNKNOWN);
|
||||
} else {
|
||||
requestToken();
|
||||
// token shall be fine now.
|
||||
if (MSpaUtils.isTokenValid(token)) {
|
||||
return token.getAccessToken();
|
||||
return Objects.requireNonNullElse(token.getAccessToken(), UNKNOWN);
|
||||
} // else fall through to UNKNOWN
|
||||
}
|
||||
return UNKNOWN;
|
||||
|
||||
+3
-1
@@ -28,6 +28,7 @@ import java.util.Collection;
|
||||
import java.util.Deque;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
@@ -218,7 +219,8 @@ public class ApiBridgeHandler extends BaseBridgeHandler {
|
||||
return false;
|
||||
}
|
||||
|
||||
connectApi.setAccessToken(accessTokenResponse.getAccessToken(), accessTokenResponse.getScope());
|
||||
connectApi.setAccessToken(accessTokenResponse.getAccessToken(),
|
||||
Objects.requireNonNullElse(accessTokenResponse.getScope(), ""));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user