Skip to content

Add OAuth support for Upload API #274

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cloudinary-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins {
task ciTest( type: Test )

dependencies {
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.4'
testImplementation "org.hamcrest:java-hamcrest:2.0.0.0"
testImplementation group: 'pl.pragmatists', name: 'JUnitParams', version: '1.0.5'
testImplementation group: 'junit', name: 'junit', version: '4.12'
Expand Down Expand Up @@ -108,4 +109,4 @@ if (hasProperty("ossrhPassword")) {
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import com.cloudinary.Cloudinary;
import com.cloudinary.ProgressCallback;
import com.cloudinary.Uploader;
import com.cloudinary.Util;
import com.cloudinary.utils.ObjectUtils;
import com.cloudinary.utils.StringUtils;
import org.cloudinary.json.JSONException;
import org.cloudinary.json.JSONObject;

import org.apache.http.client.methods.HttpPost;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -99,4 +102,21 @@ protected boolean requiresSigning(String action, Map options) {

return !unsigned && !deleteByToken;
}
}

protected HttpPost createPostMethod(String action, Map<String, Object> params, Map options) {
String oauthToken = ObjectUtils.asString(options.get("oauth_token"), cloudinary().config.oauthToken);
if (requiresSigning(action, options) && oauthToken == null) {
uploader.signRequestParams(params, options);
} else {
Util.clearEmpty(params);
}

String apiUrl = buildUploadUrl(action, options);

HttpPost postMethod = new HttpPost(apiUrl);
if (oauthToken != null) {
postMethod.setHeader("Authorization", "Bearer " + oauthToken);
}
return postMethod;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec

boolean returnError = ObjectUtils.asBoolean(options.get("return_error"), false);

if (requiresSigning(action, options)) {
uploader.signRequestParams(params, options);
} else {
Util.clearEmpty(params);
}

String apiUrl = buildUploadUrl(action, options);

ClientConnectionManager connectionManager = (ClientConnectionManager) this.uploader.cloudinary().config.properties.get("connectionManager");
HttpClient client = new DefaultHttpClient(connectionManager);

Expand All @@ -59,7 +51,7 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec
client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}

HttpPost postMethod = new HttpPost(apiUrl);
HttpPost postMethod = createPostMethod(action, params, options);
postMethod.setHeader("User-Agent", this.cloudinary().getUserAgent() + " ApacheHTTPComponents/4.2");

Map<String, String> extraHeaders = (Map<String, String>) options.get("extra_headers");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,7 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec

boolean returnError = ObjectUtils.asBoolean(options.get("return_error"), false);

if (requiresSigning(action, options)) {
uploader.signRequestParams(params, options);
} else {
Util.clearEmpty(params);
}

String apiUrl = buildUploadUrl(action, options);

HttpPost postMethod = new HttpPost(apiUrl);
HttpPost postMethod = createPostMethod(action, params, options);
ApiUtils.setTimeouts(postMethod, options);

Map<String, String> extraHeaders = (Map<String, String>) options.get("extra_headers");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,8 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec

boolean returnError = ObjectUtils.asBoolean(options.get("return_error"), false);

if (requiresSigning(action, options)) {
uploader.signRequestParams(params, options);
} else {
Util.clearEmpty(params);
}

String apiUrl = buildUploadUrl(action, options);
HttpPost postMethod = createPostMethod(action, params, options);

HttpPost postMethod = new HttpPost(apiUrl);
ApiUtils.setTimeouts(postMethod, options);

Map<String, String> extraHeaders = (Map<String, String>) options.get("extra_headers");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,7 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec

boolean returnError = ObjectUtils.asBoolean(options.get("return_error"), false);

if (requiresSigning(action, options)) {
uploader.signRequestParams(params, options);
} else {
Util.clearEmpty(params);
}

String apiUrl = buildUploadUrl(action, options);

HttpPost postMethod = new HttpPost(apiUrl);
HttpPost postMethod = createPostMethod(action, params, options);
ApiUtils.setTimeouts(postMethod, options);

Map<String, String> extraHeaders = (Map<String, String>) options.get("extra_headers");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.cloudinary.utils.ObjectUtils;
import com.cloudinary.utils.Rectangle;
import org.cloudinary.json.JSONArray;
import org.hamcrest.CoreMatchers;
import org.junit.*;
import org.junit.rules.TestName;

Expand Down Expand Up @@ -785,6 +786,32 @@ public void testAccessibilityAnalysisUpload() throws IOException {
assertNotNull(result.get("accessibility_analysis"));
}

@Test(expected = RuntimeException.class)
public void testOAuthInUploadOptions() throws Exception {
try {
cloudinary.config.oauthToken = "some_invalid_token";
cloudinary.uploader().upload(SRC_TEST_IMAGE, asMap("accessibility_analysis", true, "tags", Arrays.asList(SDK_TEST_TAG, UPLOADER_TAG), "oauth_token", "foo"));
} catch(RuntimeException e) {
assertThat(e.getMessage(), CoreMatchers.containsString("Invalid token"));
throw e;
} finally {
cloudinary.config.oauthToken = null;
}
}

@Test(expected = RuntimeException.class)
public void testOAuthInUploadConfig() throws Exception {
try {
cloudinary.config.oauthToken = "some_invalid_token";
cloudinary.uploader().upload(SRC_TEST_IMAGE, asMap("accessibility_analysis", true, "tags", Arrays.asList(SDK_TEST_TAG, UPLOADER_TAG)));
} catch(RuntimeException e) {
assertThat(e.getMessage(), CoreMatchers.containsString("Invalid token"));
throw e;
} finally {
cloudinary.config.oauthToken = null;
}
}

private void addToDeleteList(String type, String id) {
Set<String> ids = toDelete.get(type);
if (ids == null) {
Expand Down