Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds shared link downscoping and coverage #785

Merged
merged 11 commits into from
Jun 15, 2020
23 changes: 13 additions & 10 deletions src/main/java/com/box/sdk/BoxAPIConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ public void setRequestInterceptor(RequestInterceptor interceptor) {
* @param resource the resource for which the new token has to be obtained
PJSimon marked this conversation as resolved.
Show resolved Hide resolved
* @return scopedToken which has access token and other details
*/
public ScopedToken getLowerScopedToken(List<String> scopes, String resource) {
public ScopedToken getLowerScopedToken(List<String> scopes, String resource, String sharedLink) {
PJSimon marked this conversation as resolved.
Show resolved Hide resolved
assert (scopes != null);
assert (scopes.size() > 0);
URL url = null;
Expand All @@ -698,18 +698,21 @@ public ScopedToken getLowerScopedToken(List<String> scopes, String resource) {

String urlParameters = null;

if (resource != null) {
//this.getAccessToken() ensures we have a valid access token
if (resource == null && sharedLink == null) {
urlParameters = String.format("grant_type=urn:ietf:params:oauth:grant-type:token-exchange"
+ "&subject_token_type=urn:ietf:params:oauth:token-type:access_token&subject_token=%s"
+ "&scope=%s&resource=%s",
this.getAccessToken(), spaceSeparatedScopes, resource);
+ "&subject_token_type=urn:ietf:params:oauth:token-type:access_token&subject_token=%s"
+ "&scope=%s",
this.getAccessToken(), spaceSeparatedScopes);
} else if (resource != null) {
urlParameters = String.format("grant_type=urn:ietf:params:oauth:grant-type:token-exchange"
+ "&subject_token_type=urn:ietf:params:oauth:token-type:access_token&subject_token=%s"
+ "&scope=%s&resource=%s",
this.getAccessToken(), spaceSeparatedScopes, resource);
} else {
//this.getAccessToken() ensures we have a valid access token
urlParameters = String.format("grant_type=urn:ietf:params:oauth:grant-type:token-exchange"
+ "&subject_token_type=urn:ietf:params:oauth:token-type:access_token&subject_token=%s"
+ "&scope=%s",
this.getAccessToken(), spaceSeparatedScopes);
+ "&subject_token_type=urn:ietf:params:oauth:token-type:access_token&subject_token=%s"
+ "&scope=%s&box_shared_link=%s",
this.getAccessToken(), spaceSeparatedScopes, sharedLink);
}

BoxAPIRequest request = new BoxAPIRequest(this, url, "POST");
Expand Down
49 changes: 33 additions & 16 deletions src/test/java/com/box/sdk/BoxAPIConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -569,48 +569,65 @@ public void getLowerScopedTokenRefreshesTheTokenIfNeededbyCallingGetAccessToken(
List<String> scopes = new ArrayList<String>();
scopes.add("DummyScope");
String resource = "";
String sharedLink = null;

when(api.getTokenURL()).thenReturn("https://api.box.com/oauth2/token");
when(api.getLowerScopedToken(scopes, resource)).thenCallRealMethod();
when(api.getLowerScopedToken(scopes, resource, sharedLink)).thenCallRealMethod();
PJSimon marked this conversation as resolved.
Show resolved Hide resolved
try {
api.getLowerScopedToken(scopes, resource);
api.getLowerScopedToken(scopes, resource, sharedLink);
PJSimon marked this conversation as resolved.
Show resolved Hide resolved
} catch (RuntimeException e) {
//Ignore it
}
verify(api).getAccessToken();
}

@Test
@Category(UnitTest.class)
public void getLowerScopedTokenWithNullResource() {
BoxAPIConnection api = mock(BoxAPIConnection.class);
@Category(IntegrationTest.class)
public void getLowerScopedToken() {
final String originalAccessToken = TestConfig.getAccessToken();
BoxAPIConnection api = new BoxAPIConnection(originalAccessToken);

List<String> scopes = new ArrayList<String>();
scopes.add("DummyScope");
scopes.add("item_preview");
scopes.add("item_content_upload");
String resource = null;
String sharedLink = null;

when(api.getTokenURL()).thenReturn("https://api.box.com/oauth2/token");
when(api.getLowerScopedToken(scopes, resource)).thenCallRealMethod();
try {
api.getLowerScopedToken(scopes, resource);
} catch (RuntimeException e) {
//Ignore it
}
verify(api).getAccessToken();
ScopedToken token = api.getLowerScopedToken(scopes, resource, sharedLink);
assertThat(token, notNullValue());
assertThat(token.getAccessToken(), notNullValue());
}

@Test
@Category(IntegrationTest.class)
public void getLowerScopedTokenWorks() {
public void getLowerScopedTokenForResource() {
PJSimon marked this conversation as resolved.
Show resolved Hide resolved
final String originalAccessToken = TestConfig.getAccessToken();
BoxAPIConnection api = new BoxAPIConnection(originalAccessToken);

List<String> scopes = new ArrayList<String>();
scopes.add("item_preview");
scopes.add("item_content_upload");
String resource = "https://api.box.com/2.0/files/135906984991";
String sharedLink = null;

ScopedToken token = api.getLowerScopedToken(scopes, resource, sharedLink);
assertThat(token, notNullValue());
assertThat(token.getAccessToken(), notNullValue());
}

@Test
@Category(IntegrationTest.class)
public void getLowerScopedTokenForSharedLink() {
final String originalAccessToken = TestConfig.getAccessToken();
BoxAPIConnection api = new BoxAPIConnection(originalAccessToken);

List<String> scopes = new ArrayList<String>();
scopes.add("item_preview");
scopes.add("item_content_upload");
String resource = null;
String sharedLink = null;

ScopedToken token = api.getLowerScopedToken(scopes, resource);
ScopedToken token = api.getLowerScopedToken(scopes, resource, sharedLink);
assertThat(token, notNullValue());
assertThat(token.getAccessToken(), notNullValue());
}
Expand Down