Skip to content

Commit

Permalink
fix: Fix accessToken locking mechanism
Browse files Browse the repository at this point in the history
Closes: SDK-4211
  • Loading branch information
lukaszsocha2 committed Nov 6, 2024
1 parent 6ea70f7 commit 48c2ae4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 47 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
32 changes: 16 additions & 16 deletions src/main/java/com/box/sdk/BoxAPIRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -591,23 +591,23 @@ private BoxAPIResponse trySend(ProgressListener listener) {
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 {
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);
}

long start = System.currentTimeMillis();
writeMethodWithBody(requestBuilder, listener);
Request request = requestBuilder.build();
Expand Down

0 comments on commit 48c2ae4

Please sign in to comment.