Skip to content

Commit

Permalink
Use proxy when authenticating
Browse files Browse the repository at this point in the history
Update BoxAPIConnection so that the proxy configuration (along with
other connection-level settings) is applied when performing OAuth
requests.

Closes #142.
  • Loading branch information
Greg Curtis committed Jul 13, 2015
1 parent 9bb1fba commit 4a7418e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
10 changes: 4 additions & 6 deletions src/main/java/com/box/sdk/BoxAPIConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,8 @@ public void authenticate(String authCode) {
String urlParameters = String.format("grant_type=authorization_code&code=%s&client_id=%s&client_secret=%s",
authCode, this.clientID, this.clientSecret);

BoxAPIRequest request = new BoxAPIRequest(url, "POST");
request.addHeader("Content-Type", "application/x-www-form-urlencoded");
request.addHeader("User-Agent", this.getUserAgent());
BoxAPIRequest request = new BoxAPIRequest(this, url, "POST");
request.shouldAuthenticate(false);
request.setBody(urlParameters);

BoxJSONResponse response = (BoxJSONResponse) request.send();
Expand Down Expand Up @@ -433,9 +432,8 @@ public void refresh() {
String urlParameters = String.format("grant_type=refresh_token&refresh_token=%s&client_id=%s&client_secret=%s",
this.refreshToken, this.clientID, this.clientSecret);

BoxAPIRequest request = new BoxAPIRequest(url, "POST");
request.addHeader("Content-Type", "application/x-www-form-urlencoded");
request.addHeader("User-Agent", this.getUserAgent());
BoxAPIRequest request = new BoxAPIRequest(this, url, "POST");
request.shouldAuthenticate(false);
request.setBody(urlParameters);

String json;
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/box/sdk/BoxAPIRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class BoxAPIRequest {
private long bodyLength;
private Map<String, List<String>> requestProperties;
private int numRedirects;
private boolean shouldAuthenticate;

/**
* Constructs an unauthenticated BoxAPIRequest.
Expand All @@ -68,6 +69,7 @@ public BoxAPIRequest(BoxAPIConnection api, URL url, String method) {
this.method = method;
this.headers = new ArrayList<RequestHeader>();
this.backoffCounter = new BackoffCounter(new Time());
this.shouldAuthenticate = true;

this.addHeader("Accept-Encoding", "gzip");
this.addHeader("Accept-Charset", "utf-8");
Expand Down Expand Up @@ -347,7 +349,9 @@ private BoxAPIResponse trySend(ProgressListener listener) {
}

if (this.api != null) {
connection.addRequestProperty("Authorization", "Bearer " + this.api.lockAccessToken());
if (this.shouldAuthenticate) {
connection.addRequestProperty("Authorization", "Bearer " + this.api.lockAccessToken());
}
connection.setRequestProperty("User-Agent", this.api.getUserAgent());
if (this.api.getProxy() != null) {
if (this.api.getProxyUsername() != null && this.api.getProxyPassword() != null) {
Expand Down Expand Up @@ -393,7 +397,7 @@ private BoxAPIResponse trySend(ProgressListener listener) {
throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e);
}
} finally {
if (this.api != null) {
if (this.api != null && this.shouldAuthenticate) {
this.api.unlockAccessToken();
}
}
Expand Down Expand Up @@ -483,6 +487,10 @@ private HttpURLConnection createConnection() {
return connection;
}

void shouldAuthenticate(boolean shouldAuthenticate) {
this.shouldAuthenticate = shouldAuthenticate;
}

private static boolean isResponseRetryable(int responseCode) {
return (responseCode >= 500 || responseCode == 429);
}
Expand Down

0 comments on commit 4a7418e

Please sign in to comment.