Skip to content

Commit 1f70ca2

Browse files
committed
Remove client ID and secret from connection state
This change removes the client ID and client secret parameters from the JSON state blob for BoxAPIConnections. It generally isn't a good security practice to store your client ID and secret along with access/refresh tokens, so the restore method was changed to take them as separate parameters which need to be provided by the application. There is also a new non-static `restore(String)` method that allows for a connection state to be restored into an existing connection. This method helps in situations where the saved state isn't immediately available when the API connection is constructed.
1 parent 15fcbb3 commit 1f70ca2

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

src/main/java/com/box/sdk/BoxAPIConnection.java

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -106,35 +106,14 @@ public BoxAPIConnection(String clientID, String clientSecret) {
106106
* Restores a BoxAPIConnection from a saved state.
107107
*
108108
* @see #save
109-
* @param state the saved state that was created with {@link #save}.
110-
* @return a restored API connection.
111-
*/
112-
public static BoxAPIConnection restore(String state) {
113-
JsonObject json = JsonObject.readFrom(state);
114-
String clientID = json.get("clientID").asString();
115-
String clientSecret = json.get("clientSecret").asString();
116-
String accessToken = json.get("accessToken").asString();
117-
String refreshToken = json.get("refreshToken").asString();
118-
long lastRefresh = json.get("lastRefresh").asLong();
119-
long expires = json.get("expires").asLong();
120-
String userAgent = json.get("userAgent").asString();
121-
String tokenURL = json.get("tokenURL").asString();
122-
String baseURL = json.get("baseURL").asString();
123-
String baseUploadURL = json.get("baseUploadURL").asString();
124-
boolean autoRefresh = json.get("autoRefresh").asBoolean();
125-
int maxRequestAttempts = json.get("maxRequestAttempts").asInt();
126-
127-
BoxAPIConnection api = new BoxAPIConnection(clientID, clientSecret, accessToken, refreshToken);
128-
api.accessToken = accessToken;
129-
api.refreshToken = refreshToken;
130-
api.lastRefresh = lastRefresh;
131-
api.expires = expires;
132-
api.userAgent = userAgent;
133-
api.tokenURL = tokenURL;
134-
api.baseURL = baseURL;
135-
api.baseUploadURL = baseUploadURL;
136-
api.autoRefresh = autoRefresh;
137-
api.maxRequestAttempts = maxRequestAttempts;
109+
* @param clientID the client ID to use with the connection.
110+
* @param clientSecret the client secret to use with the connection.
111+
* @param state the saved state that was created with {@link #save}.
112+
* @return a restored API connection.
113+
*/
114+
public static BoxAPIConnection restore(String clientID, String clientSecret, String state) {
115+
BoxAPIConnection api = new BoxAPIConnection(clientID, clientSecret);
116+
api.restore(state);
138117
return api;
139118
}
140119

@@ -427,6 +406,37 @@ public void refresh() {
427406
this.refreshLock.writeLock().unlock();
428407
}
429408

409+
/**
410+
* Restores a saved connection state into this BoxAPIConnection.
411+
*
412+
* @see #save
413+
* @param state the saved state that was created with {@link #save}.
414+
*/
415+
public void restore(String state) {
416+
JsonObject json = JsonObject.readFrom(state);
417+
String accessToken = json.get("accessToken").asString();
418+
String refreshToken = json.get("refreshToken").asString();
419+
long lastRefresh = json.get("lastRefresh").asLong();
420+
long expires = json.get("expires").asLong();
421+
String userAgent = json.get("userAgent").asString();
422+
String tokenURL = json.get("tokenURL").asString();
423+
String baseURL = json.get("baseURL").asString();
424+
String baseUploadURL = json.get("baseUploadURL").asString();
425+
boolean autoRefresh = json.get("autoRefresh").asBoolean();
426+
int maxRequestAttempts = json.get("maxRequestAttempts").asInt();
427+
428+
this.accessToken = accessToken;
429+
this.refreshToken = refreshToken;
430+
this.lastRefresh = lastRefresh;
431+
this.expires = expires;
432+
this.userAgent = userAgent;
433+
this.tokenURL = tokenURL;
434+
this.baseURL = baseURL;
435+
this.baseUploadURL = baseUploadURL;
436+
this.autoRefresh = autoRefresh;
437+
this.maxRequestAttempts = maxRequestAttempts;
438+
}
439+
430440
/**
431441
* Notifies a refresh event to all the listeners.
432442
*/
@@ -485,8 +495,6 @@ public void setRequestInterceptor(RequestInterceptor interceptor) {
485495
*/
486496
public String save() {
487497
JsonObject state = new JsonObject()
488-
.add("clientID", this.clientID)
489-
.add("clientSecret", this.clientSecret)
490498
.add("accessToken", this.accessToken)
491499
.add("refreshToken", this.refreshToken)
492500
.add("lastRefresh", this.lastRefresh)

src/test/java/com/box/sdk/BoxAPIConnectionTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void restoreConnectionThatDoesNotNeedRefresh() {
9191
api.setLastRefresh(System.currentTimeMillis());
9292
String state = api.save();
9393

94-
final BoxAPIConnection restoredAPI = BoxAPIConnection.restore(state);
94+
final BoxAPIConnection restoredAPI = BoxAPIConnection.restore("fake client ID", "fake client secret", state);
9595
restoredAPI.setRequestInterceptor(new RequestInterceptor() {
9696
@Override
9797
public BoxAPIResponse onRequest(BoxAPIRequest request) {
@@ -203,7 +203,8 @@ public void successfullySavesAndRestoresConnection() {
203203
originalAccessToken, originalRefreshToken);
204204
String state = api.save();
205205

206-
BoxAPIConnection restoredAPI = BoxAPIConnection.restore(state);
206+
BoxAPIConnection restoredAPI = BoxAPIConnection.restore(TestConfig.getClientID(), TestConfig.getClientSecret(),
207+
state);
207208
BoxFolder.Info rootFolderInfo = BoxFolder.getRootFolder(restoredAPI).getInfo();
208209

209210
TestConfig.setAccessToken(restoredAPI.getAccessToken());

0 commit comments

Comments
 (0)