Skip to content

Commit

Permalink
fix: Fixed restoring state without refresh token. (#1139)
Browse files Browse the repository at this point in the history
  • Loading branch information
antusus authored Jan 3, 2023
1 parent 57a4133 commit 3544709
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 35 deletions.
9 changes: 6 additions & 3 deletions src/main/java/com/box/sdk/BoxAPIConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -746,17 +746,20 @@ public void refresh() {
public void restore(String state) {
JsonObject json = Json.parse(state).asObject();
String accessToken = json.get("accessToken").asString();
String refreshToken = json.get("refreshToken").asString();
String refreshToken = getKeyValueOrDefault(json, "refreshToken", null);
long lastRefresh = json.get("lastRefresh").asLong();
long expires = json.get("expires").asLong();
String userAgent = json.get("userAgent").asString();
String tokenURL = getKeyValueOrDefault(json, "tokenURL", null);
String revokeURL = getKeyValueOrDefault(json, "revokeURL", null);
String baseURL = adoptBaseUrlWhenLoadingFromOldVersion(getKeyValueOrDefault(json, "baseURL", DEFAULT_BASE_URL));
String baseURL = adoptBaseUrlWhenLoadingFromOldVersion(
getKeyValueOrDefault(json, "baseURL", DEFAULT_BASE_URL)
);
String baseUploadURL = adoptUploadBaseUrlWhenLoadingFromOldVersion(
getKeyValueOrDefault(json, "baseUploadURL", DEFAULT_BASE_UPLOAD_URL)
);
String authorizationURL = getKeyValueOrDefault(json, "authorizationURL", DEFAULT_BASE_AUTHORIZATION_URL);
String authorizationURL =
getKeyValueOrDefault(json, "authorizationURL", DEFAULT_BASE_AUTHORIZATION_URL);
boolean autoRefresh = json.get("autoRefresh").asBoolean();

// Try to read deprecated value
Expand Down
36 changes: 4 additions & 32 deletions src/main/java/com/box/sdk/BoxCCGAPIConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,38 +126,10 @@ public String save() {

@Override
public void restore(String state) {
JsonObject json = Json.parse(state).asObject();
String accessToken = json.get("accessToken").asString();
long lastRefresh = json.get("lastRefresh").asLong();
long expires = json.get("expires").asLong();
String userAgent = json.get("userAgent").asString();
String revokeURL = getKeyValueOrDefault(json, "revokeURL", null);
String tokenURL = getKeyValueOrDefault(json, "tokenURL", null);
String authorizationURL = getKeyValueOrDefault(json, "authorizationURL", DEFAULT_BASE_AUTHORIZATION_URL);
String baseURL = json.get("baseURL").asString();
String baseUploadURL = json.get("baseUploadURL").asString();
boolean autoRefresh = json.get("autoRefresh").asBoolean();
String subjectType = json.get("subjectType").asString();
String subjectId = json.get("subjectId").asString();

int maxRetryAttempts = -1;
if (json.names().contains("maxRetryAttempts")) {
maxRetryAttempts = json.get("maxRetryAttempts").asInt();
}

setAccessToken(accessToken);
setLastRefresh(lastRefresh);
setExpires(expires);
setUserAgent(userAgent);
setTokenURL(tokenURL);
setRevokeURL(revokeURL);
setBaseAuthorizationURL(authorizationURL);
setBaseURL(baseURL);
setBaseUploadURL(baseUploadURL);
setAutoRefresh(autoRefresh);
setMaxRetryAttempts(maxRetryAttempts);
this.subjectType = subjectType;
this.subjectId = subjectId;
super.restore(state);

JsonObject json = Json.parse(state).asObject();
this.subjectType = json.get("subjectType").asString();
this.subjectId = json.get("subjectId").asString();
}
}
29 changes: 29 additions & 0 deletions src/test/java/com/box/sdk/BoxAPIConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -536,6 +537,34 @@ public void restoresProperUrlsWhenNotSet() {
assertThat("https://upload.box.com/api/2.0/", is(restoredApi.getBaseUploadURL()));
}

@Test
public void restoresWithEmptyRefreshToken() {
// given
String clientId = "some_client_id";
String clientSecret = "some_client_secret";
String state = "{\"accessToken\":\"some-access-token\","
+ "\"refreshToken\":null,"
+ "\"lastRefresh\":1672759898468,"
+ "\"expires\":3829000,"
+ "\"userAgent\":\"Box Java SDK v3.8.1 (Java 17.0.4)\","
+ "\"tokenURL\":null,"
+ "\"revokeURL\":null,"
+ "\"baseURL\":\"https://api.box.com/\","
+ "\"baseUploadURL\":\"https://upload.box.com/api/\","
+ "\"authorizationURL\":\"https://account.box.com/api/\","
+ "\"autoRefresh\":true,"
+ "\"maxRetryAttempts\":5}";

// when
BoxAPIConnection restoredApi = BoxAPIConnection.restore(clientId, clientSecret, state);

// then
assertThat("some-access-token", is(restoredApi.getAccessToken()));
assertNull(restoredApi.getRefreshToken());
assertThat("https://api.box.com/2.0/", is(restoredApi.getBaseURL()));
assertThat("https://upload.box.com/api/2.0/", is(restoredApi.getBaseUploadURL()));
}

@Test
public void restoresProperUrlsWhenDeprecatedUrlsAreSet() {
// given
Expand Down

0 comments on commit 3544709

Please sign in to comment.