Skip to content

Commit

Permalink
Fix InaccessibleObjectException (#3449)
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur authored Mar 16, 2023
1 parent c05fe8a commit 3e1c0b3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeParseException;
import java.util.Base64;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
Expand All @@ -43,6 +46,7 @@
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonSyntaxException;

/**
Expand All @@ -65,7 +69,15 @@ public class OAuthConnector {

public OAuthConnector(HttpClientFactory httpClientFactory, @Nullable String deserializerClassName) {
this.httpClientFactory = httpClientFactory;
GsonBuilder gsonBuilder = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
GsonBuilder gsonBuilder = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapter(Instant.class, (JsonDeserializer<Instant>) (json, typeOfT, context) -> {
try {
return Instant.parse(json.getAsString());
} catch (DateTimeParseException e) {
return LocalDateTime.parse(json.getAsString()).atZone(ZoneId.systemDefault()).toInstant();
}
});

if (deserializerClassName != null) {
try {
Class<?> deserializerClass = Class.forName(deserializerClassName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
Expand Down Expand Up @@ -221,12 +222,12 @@ protected synchronized void unsetStorageCipher(StorageCipher storageCipher) {
}
}

private boolean isExpired(@Nullable LocalDateTime lastUsed) {
private boolean isExpired(@Nullable Instant lastUsed) {
if (lastUsed == null) {
return false;
}
// (last used + 183 days < now) then it is expired
return lastUsed.plusDays(EXPIRE_DAYS).isBefore(LocalDateTime.now());
return lastUsed.plus(EXPIRE_DAYS, ChronoUnit.DAYS).isBefore(Instant.now());
}

/**
Expand Down Expand Up @@ -306,11 +307,11 @@ public Set<String> getAllHandlesFromIndex() {
}
} else if (LAST_USED.equals(recordType)) {
try {
LocalDateTime lastUsedDate = gson.fromJson(value, LocalDateTime.class);
Instant lastUsedDate = gson.fromJson(value, Instant.class);
return lastUsedDate;
} catch (Exception e) {
logger.info("Unable to deserialize json, reset LAST_USED to now. json:\n{}", value);
return LocalDateTime.now();
return Instant.now();
}
}
return null;
Expand All @@ -319,20 +320,6 @@ public Set<String> getAllHandlesFromIndex() {
}
}

public void put(String handle, @Nullable LocalDateTime lastUsed) {
storageLock.lock();
try {
if (lastUsed == null) {
storage.put(LAST_USED.getKey(handle), (String) null);
} else {
String gsonStr = gson.toJson(lastUsed);
storage.put(LAST_USED.getKey(handle), gsonStr);
}
} finally {
storageLock.unlock();
}
}

public void put(String handle, @Nullable AccessTokenResponse accessTokenResponse) {
storageLock.lock();
try {
Expand All @@ -341,7 +328,7 @@ public void put(String handle, @Nullable AccessTokenResponse accessTokenResponse
} else {
String gsonAccessTokenStr = gson.toJson(accessTokenResponse);
storage.put(ACCESS_TOKEN_RESPONSE.getKey(handle), gsonAccessTokenStr);
String gsonDateStr = gson.toJson(LocalDateTime.now());
String gsonDateStr = gson.toJson(Instant.now());
storage.put(LAST_USED.getKey(handle), gsonDateStr);

if (!allHandles.contains(handle)) {
Expand All @@ -363,7 +350,7 @@ public void put(String handle, @Nullable PersistedParams persistedParams) {
} else {
String gsonPersistedParamsStr = gson.toJson(persistedParams);
storage.put(SERVICE_CONFIGURATION.getKey(handle), gsonPersistedParamsStr);
String gsonDateStr = gson.toJson(LocalDateTime.now());
String gsonDateStr = gson.toJson(Instant.now());
storage.put(LAST_USED.getKey(handle), gsonDateStr);
if (!allHandles.contains(handle)) {
// update all handles index
Expand Down Expand Up @@ -412,7 +399,7 @@ public void close() {
if (handlesSSV != null) {
String[] handles = handlesSSV.trim().split(" ");
for (String handle : handles) {
LocalDateTime lastUsed = (LocalDateTime) get(handle, LAST_USED);
Instant lastUsed = (Instant) get(handle, LAST_USED);
if (isExpired(lastUsed)) {
removeByHandle(handle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.auth.oauth2client;
package org.openhab.core.auth.oauth2client.internal;

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -32,8 +32,6 @@
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
import org.openhab.core.auth.oauth2client.internal.OAuthStoreHandlerImpl;
import org.openhab.core.auth.oauth2client.internal.StorageRecordType;
import org.openhab.core.storage.Storage;
import org.openhab.core.storage.StorageService;
import org.openhab.core.test.storage.VolatileStorage;
Expand All @@ -55,14 +53,14 @@ public class OAuthStoreHandlerTest {
private @NonNullByDefault({}) OAuthStoreHandlerImpl storeHandler;

@BeforeEach
public void initialize() throws IOException {
void initialize() throws IOException {
storage = new VolatileStorage<>();
Mockito.doReturn(storage).when(storageService).getStorage(STORE_NAME);
storeHandler = new OAuthStoreHandlerImpl(storageService);
}

@Test
public void loadAccessTokenResponseWhenCreatedOnIsLocalDateTime() throws GeneralSecurityException {
void loadAccessTokenResponseWhenCreatedOnIsLocalDateTime() throws GeneralSecurityException {
final String handle = "test";
final String createdOn = "2022-08-14T21:21:05.568991";
final Instant expected = LocalDateTime.parse(createdOn).atZone(ZoneId.systemDefault()).toInstant();
Expand All @@ -77,7 +75,7 @@ public void loadAccessTokenResponseWhenCreatedOnIsLocalDateTime() throws General
}

@Test
public void loadAccessTokenResponseWhenCreatedOnIsInstant() throws GeneralSecurityException {
void loadAccessTokenResponseWhenCreatedOnIsInstant() throws GeneralSecurityException {
final String handle = "test";
final String createdOn = "2022-08-14T19:21:05.568991Z";
final Instant expected = Instant.parse(createdOn);
Expand All @@ -91,6 +89,13 @@ public void loadAccessTokenResponseWhenCreatedOnIsInstant() throws GeneralSecuri
}
}

@Test
void savePersistedParamsShouldNotThrow() {
final String handle = "test";

storeHandler.savePersistedParams(handle, new PersistedParams());
}

private String getJsonforCreatedOn(String createdOn) {
return "{\"accessToken\": \"x\", \"tokenType\": \"Bearer\", \"expiresIn\": 2592000, \"refreshToken\": \"x\", \"createdOn\": \""
+ createdOn + "\"}";
Expand Down

0 comments on commit 3e1c0b3

Please sign in to comment.