Skip to content
Merged
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
20 changes: 17 additions & 3 deletions cloudinary-core/src/main/java/com/cloudinary/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class Configuration {
public boolean forceVersion = true;
public boolean longUrlSignature = DEFAULT_IS_LONG_SIGNATURE;
public SignatureAlgorithm signatureAlgorithm = DEFAULT_SIGNATURE_ALGORITHM;
public String oauthToken = null;

public Configuration() {
}
Expand All @@ -71,7 +72,8 @@ private Configuration(
boolean loadStrategies,
boolean forceVersion,
boolean longUrlSignature,
SignatureAlgorithm signatureAlgorithm) {
SignatureAlgorithm signatureAlgorithm,
String oauthToken) {
this.cloudName = cloudName;
this.apiKey = apiKey;
this.apiSecret = apiSecret;
Expand All @@ -87,11 +89,12 @@ private Configuration(
this.proxyPort = proxyPort;
this.secureCdnSubdomain = secureCdnSubdomain;
this.useRootPath = useRootPath;
this.timeout = 0;
this.timeout = timeout;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was that changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bugfix (will be in the commit message on merge).

this.loadStrategies = loadStrategies;
this.forceVersion = forceVersion;
this.longUrlSignature = longUrlSignature;
this.signatureAlgorithm = signatureAlgorithm;
this.oauthToken = oauthToken;
}

@SuppressWarnings("rawtypes")
Expand Down Expand Up @@ -130,6 +133,7 @@ public void update(Map config) {
}
this.longUrlSignature = ObjectUtils.asBoolean(config.get("long_url_signature"), DEFAULT_IS_LONG_SIGNATURE);
this.signatureAlgorithm = SignatureAlgorithm.valueOf(ObjectUtils.asString(config.get(CONFIG_PROP_SIGNATURE_ALGORITHM), DEFAULT_SIGNATURE_ALGORITHM.name()));
this.oauthToken = (String) config.get("oauth_token");
}

@SuppressWarnings("rawtypes")
Expand Down Expand Up @@ -160,6 +164,7 @@ public Map<String, Object> asMap() {
map.put("properties", new HashMap<String,Object>(properties));
map.put("long_url_signature", longUrlSignature);
map.put(CONFIG_PROP_SIGNATURE_ALGORITHM, signatureAlgorithm.toString());
map.put("oauth_token", oauthToken);
return map;
}

Expand Down Expand Up @@ -190,6 +195,7 @@ public Configuration(Configuration other) {
this.properties.putAll(other.properties);
this.longUrlSignature = other.longUrlSignature;
this.signatureAlgorithm = other.signatureAlgorithm;
this.oauthToken = other.oauthToken;
}

/**
Expand Down Expand Up @@ -301,6 +307,7 @@ public static class Builder {
private boolean forceVersion = true;
private boolean longUrlSignature = DEFAULT_IS_LONG_SIGNATURE;
private SignatureAlgorithm signatureAlgorithm = DEFAULT_SIGNATURE_ALGORITHM;
private String oauthToken = null;

/**
* Set the HTTP connection timeout.
Expand Down Expand Up @@ -337,7 +344,8 @@ public Configuration build() {
loadStrategies,
forceVersion,
longUrlSignature,
signatureAlgorithm);
signatureAlgorithm,
oauthToken);
configuration.clientHints = clientHints;
return configuration;
}
Expand Down Expand Up @@ -466,6 +474,11 @@ public Builder setSignatureAlgorithm(SignatureAlgorithm signatureAlgorithm) {
return this;
}

public Builder setOAuthToken(String oauthToken) {
this.oauthToken = oauthToken;
return this;
}

/**
* Initialize builder from existing {@link Configuration}
*
Expand Down Expand Up @@ -495,6 +508,7 @@ public Builder from(Configuration other) {
this.forceVersion = other.forceVersion;
this.longUrlSignature = other.longUrlSignature;
this.signatureAlgorithm = other.signatureAlgorithm;
this.oauthToken = other.oauthToken;
return this;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.cloudinary.Api.HttpMethod;
import com.cloudinary.SmartUrlEncoder;
import com.cloudinary.api.ApiResponse;
import com.cloudinary.utils.Base64Coder;
import com.cloudinary.utils.StringUtils;
import java.util.Arrays;
import java.util.Map;
Expand Down Expand Up @@ -31,4 +32,19 @@ protected String createApiUrl (Iterable<String> uri, String prefix, String cloud
public abstract ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String, ? extends Object> params, Map options) throws Exception;

public abstract ApiResponse callAccountApi(HttpMethod method, Iterable<String> uri, Map<String, ? extends Object> params, Map options) throws Exception;

protected String getAuthorizationHeaderValue(String apiKey, String apiSecret, String oauthToken) {
if (oauthToken != null){
return "Bearer " + oauthToken;
} else {
return "Basic " + Base64Coder.encodeString(apiKey + ":" + apiSecret);
}
}

protected void validateAuthorization(String apiKey, String apiSecret, String oauthToken) {
if (oauthToken == null) {
if (apiKey == null) throw new IllegalArgumentException("Must supply api_key");
if (apiSecret == null) throw new IllegalArgumentException("Must supply api_secret");
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if all of them are null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If all are null then this is thrown - `IllegalArgumentException("Must supply api_key"). Same behaviour as before for people not using oauth so it doesn't break anything.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
String cloudName = ObjectUtils.asString(options.get("cloud_name"), this.api.cloudinary.config.cloudName);
if (cloudName == null) throw new IllegalArgumentException("Must supply cloud_name");
String apiKey = ObjectUtils.asString(options.get("api_key"), this.api.cloudinary.config.apiKey);
if (apiKey == null) throw new IllegalArgumentException("Must supply api_key");
String apiSecret = ObjectUtils.asString(options.get("api_secret"), this.api.cloudinary.config.apiSecret);
if (apiSecret == null) throw new IllegalArgumentException("Must supply api_secret");
String oauthToken = ObjectUtils.asString(options.get("oauth_token"), this.api.cloudinary.config.oauthToken);
String contentType = ObjectUtils.asString(options.get("content_type"), "urlencoded");
int timeout = ObjectUtils.asInteger(options.get("timeout"), this.api.cloudinary.config.timeout);
validateAuthorization(apiKey, apiSecret, oauthToken);

String apiUrl = createApiUrl(uri, prefix, cloudName);

return getApiResponse(method, params, apiKey, apiSecret, contentType, timeout, apiUrl);
return getApiResponse(method, params, apiKey, apiSecret, oauthToken, contentType, timeout, apiUrl);
}

@Override
Expand All @@ -63,10 +64,10 @@ public ApiResponse callAccountApi(HttpMethod method, Iterable<String> uri, Map<S
apiUrl = apiUrl + "/" + component;
}

return getApiResponse(method, params, apiKey, apiSecret, contentType, timeout, apiUrl);
return getApiResponse(method, params, apiKey, apiSecret, null, contentType, timeout, apiUrl);
}

private ApiResponse getApiResponse(HttpMethod method, Map<String, ?> params, String apiKey, String apiSecret, String contentType, int timeout, String apiUrl) throws Exception {
private ApiResponse getApiResponse(HttpMethod method, Map<String, ?> params, String apiKey, String apiSecret, String oauthToken, String contentType, int timeout, String apiUrl) throws Exception {
URIBuilder apiUrlBuilder = new URIBuilder(apiUrl);
if (!contentType.equals("json")) {
for (Map.Entry<String, ? extends Object> param : params.entrySet()) {
Expand Down Expand Up @@ -105,7 +106,7 @@ private ApiResponse getApiResponse(HttpMethod method, Map<String, ?> params, Str
request = new HttpDelete(apiUri);
break;
}
request.setHeader("Authorization", "Basic " + Base64Coder.encodeString(apiKey + ":" + apiSecret));
request.setHeader("Authorization", getAuthorizationHeaderValue(apiKey, apiSecret, oauthToken));
request.setHeader("User-Agent", Cloudinary.USER_AGENT + " ApacheHTTPComponents/4.2");
if (contentType.equals("json")) {
JSONObject asJSON = ObjectUtils.toJSON(params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
String cloudName = ObjectUtils.asString(options.get("cloud_name"), this.api.cloudinary.config.cloudName);
if (cloudName == null) throw new IllegalArgumentException("Must supply cloud_name");
String apiKey = ObjectUtils.asString(options.get("api_key"), this.api.cloudinary.config.apiKey);
if (apiKey == null) throw new IllegalArgumentException("Must supply api_key");
String apiSecret = ObjectUtils.asString(options.get("api_secret"), this.api.cloudinary.config.apiSecret);
if (apiSecret == null) throw new IllegalArgumentException("Must supply api_secret");
String oauthToken = ObjectUtils.asString(options.get("oauth_token"), this.api.cloudinary.config.oauthToken);

validateAuthorization(apiKey, apiSecret, oauthToken);

String apiUrl = createApiUrl(uri, prefix, cloudName);
HttpUriRequest request = prepareRequest(method, apiUrl, params, options);

request.setHeader("Authorization", "Basic " + Base64Coder.encodeString(apiKey + ":" + apiSecret));
request.setHeader("Authorization", getAuthorizationHeaderValue(apiKey, apiSecret, oauthToken));

return getApiResponse(request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
String cloudName = ObjectUtils.asString(options.get("cloud_name"), this.api.cloudinary.config.cloudName);
if (cloudName == null) throw new IllegalArgumentException("Must supply cloud_name");
String apiKey = ObjectUtils.asString(options.get("api_key"), this.api.cloudinary.config.apiKey);
if (apiKey == null) throw new IllegalArgumentException("Must supply api_key");
String apiSecret = ObjectUtils.asString(options.get("api_secret"), this.api.cloudinary.config.apiSecret);
if (apiSecret == null) throw new IllegalArgumentException("Must supply api_secret");
String oauthToken = ObjectUtils.asString(options.get("oauth_token"), this.api.cloudinary.config.oauthToken);

validateAuthorization(apiKey, apiSecret, oauthToken);

String apiUrl = createApiUrl(uri, prefix, cloudName);
HttpUriRequest request = prepareRequest(method, apiUrl, params, options);

request.setHeader("Authorization", "Basic " + Base64Coder.encodeString(apiKey + ":" + apiSecret));
request.setHeader("Authorization", getAuthorizationHeaderValue(apiKey, apiSecret, oauthToken));

return getApiResponse(request);
}
Expand Down Expand Up @@ -146,7 +146,7 @@ public ApiResponse callAccountApi(HttpMethod method, Iterable<String> uri, Map<S

HttpUriRequest request = prepareRequest(method, apiUrl, params, options);

request.setHeader("Authorization", "Basic " + Base64Coder.encodeString(apiKey + ":" + apiSecret));
request.setHeader("Authorization", getAuthorizationHeaderValue(apiKey, apiSecret, null));

return getApiResponse(request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ public void test04ResourcesByType() throws Exception {
assertTrue(resources.size() > 0);
}

@Test
public void testOAuthToken() {
String message = "";
try {
api.resource(API_TEST, Collections.singletonMap("oauth_token", "not_a_real_token"));
} catch (Exception e) {
message = e.getMessage();
}

assertTrue(message.contains("Invalid token"));
}

@Test
public void test05ResourcesByPrefix() throws Exception {
// should allow listing resources by prefix
Expand Down Expand Up @@ -996,7 +1008,7 @@ public void testCinemagraphAnalysisResource() throws Exception {
ApiResponse res = api.resource(API_TEST, Collections.singletonMap("cinemagraph_analysis", true));
assertNotNull(res.get("cinemagraph_analysis"));
}

@Test
public void testAccessibilityAnalysisResource() throws Exception {
ApiResponse res = api.resource(API_TEST, Collections.singletonMap("accessibility_analysis", true));
Expand Down