Skip to content

Commit

Permalink
fix: Fix accessToken locking mechanism (#1270)
Browse files Browse the repository at this point in the history
Closes: SDK-4211
  • Loading branch information
lukaszsocha2 authored Nov 19, 2024
1 parent 6ea70f7 commit 5eb4c93
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 65 deletions.
65 changes: 34 additions & 31 deletions src/main/java/com/box/sdk/BoxAPIConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,12 @@ public String getAccessToken() {
}
}

return this.accessToken;
this.refreshLock.readLock().lock();
try {
return this.accessToken;
} finally {
this.refreshLock.readLock().unlock();
}
}

/**
Expand Down Expand Up @@ -760,10 +765,13 @@ public boolean needsRefresh() {
boolean needsRefresh;

this.refreshLock.readLock().lock();
long now = System.currentTimeMillis();
long tokenDuration = (now - this.lastRefresh);
needsRefresh = (tokenDuration >= this.expires - REFRESH_EPSILON);
this.refreshLock.readLock().unlock();
try {
long now = System.currentTimeMillis();
long tokenDuration = (now - this.lastRefresh);
needsRefresh = (tokenDuration >= this.expires - REFRESH_EPSILON);
} finally {
this.refreshLock.readLock().unlock();
}

return needsRefresh;
}
Expand All @@ -775,37 +783,32 @@ public boolean needsRefresh() {
*/
public void refresh() {
this.refreshLock.writeLock().lock();

if (!this.canRefresh()) {
this.refreshLock.writeLock().unlock();
throw new IllegalStateException("The BoxAPIConnection cannot be refreshed because it doesn't have a "
+ "refresh token.");
}

URL url;
try {
url = new URL(getTokenURL());
} catch (MalformedURLException e) {
this.refreshLock.writeLock().unlock();
assert false : "An invalid refresh URL indicates a bug in the SDK.";
throw new RuntimeException("An invalid refresh URL indicates a bug in the SDK.", e);
}
if (!this.canRefresh()) {
throw new IllegalStateException("The BoxAPIConnection cannot be refreshed because it doesn't have a "
+ "refresh token.");
}

BoxAPIRequest request = createTokenRequest(url);
URL url;
try {
url = new URL(getTokenURL());
} catch (MalformedURLException e) {
assert false : "An invalid refresh URL indicates a bug in the SDK.";
throw new RuntimeException("An invalid refresh URL indicates a bug in the SDK.", e);
}

String json;
try (BoxAPIResponse boxAPIResponse = request.send()) {
BoxJSONResponse response = (BoxJSONResponse) boxAPIResponse;
json = response.getJSON();
} catch (BoxAPIException e) {
this.refreshLock.writeLock().unlock();
this.notifyError(e);
throw e;
}
BoxAPIRequest request = createTokenRequest(url);

try {
extractTokens(Json.parse(json).asObject());
String json;
try (BoxAPIResponse boxAPIResponse = request.send()) {
BoxJSONResponse response = (BoxJSONResponse) boxAPIResponse;
json = response.getJSON();
} catch (BoxAPIException e) {
this.notifyError(e);
throw e;
}

extractTokens(Json.parse(json).asObject());
this.notifyRefresh();
} finally {
this.refreshLock.writeLock().unlock();
Expand Down
69 changes: 35 additions & 34 deletions src/main/java/com/box/sdk/BoxAPIRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -585,54 +585,55 @@ private BoxAPIResponse trySend(ProgressListener listener) {
}
}
}
long start = System.currentTimeMillis();
Request request = composeRequest(listener);
Response response;
if (this.followRedirects) {
response = api.execute(request);
} else {
response = api.executeWithoutRedirect(request);
}
logDebug(format("[trySend] connection.connect() took %dms%n", (System.currentTimeMillis() - start)));

Request.Builder requestBuilder = new Request.Builder().url(getUrl());
BoxAPIResponse result = BoxAPIResponse.toBoxResponse(response);
this.logRequest();
long getResponseStart = System.currentTimeMillis();
logDebug(format(
"[trySend] Get Response (read network) took %dms%n", System.currentTimeMillis() - getResponseStart
));
return result;
}

private Request composeRequest(ProgressListener listener) {
Request.Builder requestBuilder = new Request.Builder().url(getUrl());
if (this.shouldAuthenticate) {
requestBuilder.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + this.api.lockAccessToken());
}
requestBuilder.addHeader("User-Agent", this.api.getUserAgent());
requestBuilder.addHeader("X-Box-UA", this.api.getBoxUAHeader());
headers.forEach(h -> {
requestBuilder.removeHeader(h.getKey());
requestBuilder.addHeader(h.getKey(), h.getValue());
});

if (this.api instanceof SharedLinkAPIConnection) {
SharedLinkAPIConnection sharedItemAPI = (SharedLinkAPIConnection) this.api;
String boxAPIValue = BoxSharedLink.getSharedLinkHeaderValue(
sharedItemAPI.getSharedLink(),
sharedItemAPI.getSharedLinkPassword()
);
requestBuilder.addHeader("BoxApi", boxAPIValue);
}

try {
long start = System.currentTimeMillis();
writeMethodWithBody(requestBuilder, listener);
Request request = requestBuilder.build();
Response response;
if (this.followRedirects) {
response = api.execute(request);
} else {
response = api.executeWithoutRedirect(request);
requestBuilder.addHeader("User-Agent", this.api.getUserAgent());
requestBuilder.addHeader("X-Box-UA", this.api.getBoxUAHeader());
headers.forEach(h -> {
requestBuilder.removeHeader(h.getKey());
requestBuilder.addHeader(h.getKey(), h.getValue());
});

if (this.api instanceof SharedLinkAPIConnection) {
SharedLinkAPIConnection sharedItemAPI = (SharedLinkAPIConnection) this.api;
String boxAPIValue = BoxSharedLink.getSharedLinkHeaderValue(
sharedItemAPI.getSharedLink(),
sharedItemAPI.getSharedLinkPassword()
);
requestBuilder.addHeader("BoxApi", boxAPIValue);
}
logDebug(format("[trySend] connection.connect() took %dms%n", (System.currentTimeMillis() - start)));

BoxAPIResponse result = BoxAPIResponse.toBoxResponse(response);
this.logRequest();
long getResponseStart = System.currentTimeMillis();
logDebug(format(
"[trySend] Get Response (read network) took %dms%n", System.currentTimeMillis() - getResponseStart
));
return result;

writeMethodWithBody(requestBuilder, listener);
return requestBuilder.build();
} finally {
if (this.shouldAuthenticate) {
this.api.unlockAccessToken();
}
}

}

protected void writeMethodWithBody(Request.Builder requestBuilder, ProgressListener listener) {
Expand Down

0 comments on commit 5eb4c93

Please sign in to comment.