Skip to content

Commit 1ce6675

Browse files
authored
Add oauth support to Admin Api calls. (#230)
Fix `timeout` config value on copy.
1 parent 16f8e21 commit 1ce6675

File tree

6 files changed

+60
-17
lines changed

6 files changed

+60
-17
lines changed

cloudinary-core/src/main/java/com/cloudinary/Configuration.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class Configuration {
4747
public boolean forceVersion = true;
4848
public boolean longUrlSignature = DEFAULT_IS_LONG_SIGNATURE;
4949
public SignatureAlgorithm signatureAlgorithm = DEFAULT_SIGNATURE_ALGORITHM;
50+
public String oauthToken = null;
5051

5152
public Configuration() {
5253
}
@@ -71,7 +72,8 @@ private Configuration(
7172
boolean loadStrategies,
7273
boolean forceVersion,
7374
boolean longUrlSignature,
74-
SignatureAlgorithm signatureAlgorithm) {
75+
SignatureAlgorithm signatureAlgorithm,
76+
String oauthToken) {
7577
this.cloudName = cloudName;
7678
this.apiKey = apiKey;
7779
this.apiSecret = apiSecret;
@@ -87,11 +89,12 @@ private Configuration(
8789
this.proxyPort = proxyPort;
8890
this.secureCdnSubdomain = secureCdnSubdomain;
8991
this.useRootPath = useRootPath;
90-
this.timeout = 0;
92+
this.timeout = timeout;
9193
this.loadStrategies = loadStrategies;
9294
this.forceVersion = forceVersion;
9395
this.longUrlSignature = longUrlSignature;
9496
this.signatureAlgorithm = signatureAlgorithm;
97+
this.oauthToken = oauthToken;
9598
}
9699

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

135139
@SuppressWarnings("rawtypes")
@@ -160,6 +164,7 @@ public Map<String, Object> asMap() {
160164
map.put("properties", new HashMap<String,Object>(properties));
161165
map.put("long_url_signature", longUrlSignature);
162166
map.put(CONFIG_PROP_SIGNATURE_ALGORITHM, signatureAlgorithm.toString());
167+
map.put("oauth_token", oauthToken);
163168
return map;
164169
}
165170

@@ -190,6 +195,7 @@ public Configuration(Configuration other) {
190195
this.properties.putAll(other.properties);
191196
this.longUrlSignature = other.longUrlSignature;
192197
this.signatureAlgorithm = other.signatureAlgorithm;
198+
this.oauthToken = other.oauthToken;
193199
}
194200

195201
/**
@@ -301,6 +307,7 @@ public static class Builder {
301307
private boolean forceVersion = true;
302308
private boolean longUrlSignature = DEFAULT_IS_LONG_SIGNATURE;
303309
private SignatureAlgorithm signatureAlgorithm = DEFAULT_SIGNATURE_ALGORITHM;
310+
private String oauthToken = null;
304311

305312
/**
306313
* Set the HTTP connection timeout.
@@ -337,7 +344,8 @@ public Configuration build() {
337344
loadStrategies,
338345
forceVersion,
339346
longUrlSignature,
340-
signatureAlgorithm);
347+
signatureAlgorithm,
348+
oauthToken);
341349
configuration.clientHints = clientHints;
342350
return configuration;
343351
}
@@ -466,6 +474,11 @@ public Builder setSignatureAlgorithm(SignatureAlgorithm signatureAlgorithm) {
466474
return this;
467475
}
468476

477+
public Builder setOAuthToken(String oauthToken) {
478+
this.oauthToken = oauthToken;
479+
return this;
480+
}
481+
469482
/**
470483
* Initialize builder from existing {@link Configuration}
471484
*
@@ -495,6 +508,7 @@ public Builder from(Configuration other) {
495508
this.forceVersion = other.forceVersion;
496509
this.longUrlSignature = other.longUrlSignature;
497510
this.signatureAlgorithm = other.signatureAlgorithm;
511+
this.oauthToken = other.oauthToken;
498512
return this;
499513
}
500514
}

cloudinary-core/src/main/java/com/cloudinary/strategies/AbstractApiStrategy.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.cloudinary.Api.HttpMethod;
55
import com.cloudinary.SmartUrlEncoder;
66
import com.cloudinary.api.ApiResponse;
7+
import com.cloudinary.utils.Base64Coder;
78
import com.cloudinary.utils.StringUtils;
89
import java.util.Arrays;
910
import java.util.Map;
@@ -31,4 +32,19 @@ protected String createApiUrl (Iterable<String> uri, String prefix, String cloud
3132
public abstract ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String, ? extends Object> params, Map options) throws Exception;
3233

3334
public abstract ApiResponse callAccountApi(HttpMethod method, Iterable<String> uri, Map<String, ? extends Object> params, Map options) throws Exception;
35+
36+
protected String getAuthorizationHeaderValue(String apiKey, String apiSecret, String oauthToken) {
37+
if (oauthToken != null){
38+
return "Bearer " + oauthToken;
39+
} else {
40+
return "Basic " + Base64Coder.encodeString(apiKey + ":" + apiSecret);
41+
}
42+
}
43+
44+
protected void validateAuthorization(String apiKey, String apiSecret, String oauthToken) {
45+
if (oauthToken == null) {
46+
if (apiKey == null) throw new IllegalArgumentException("Must supply api_key");
47+
if (apiSecret == null) throw new IllegalArgumentException("Must supply api_secret");
48+
}
49+
}
3450
}

cloudinary-http42/src/main/java/com/cloudinary/http42/ApiStrategy.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
3838
String cloudName = ObjectUtils.asString(options.get("cloud_name"), this.api.cloudinary.config.cloudName);
3939
if (cloudName == null) throw new IllegalArgumentException("Must supply cloud_name");
4040
String apiKey = ObjectUtils.asString(options.get("api_key"), this.api.cloudinary.config.apiKey);
41-
if (apiKey == null) throw new IllegalArgumentException("Must supply api_key");
4241
String apiSecret = ObjectUtils.asString(options.get("api_secret"), this.api.cloudinary.config.apiSecret);
43-
if (apiSecret == null) throw new IllegalArgumentException("Must supply api_secret");
42+
String oauthToken = ObjectUtils.asString(options.get("oauth_token"), this.api.cloudinary.config.oauthToken);
4443
String contentType = ObjectUtils.asString(options.get("content_type"), "urlencoded");
4544
int timeout = ObjectUtils.asInteger(options.get("timeout"), this.api.cloudinary.config.timeout);
45+
validateAuthorization(apiKey, apiSecret, oauthToken);
46+
4647
String apiUrl = createApiUrl(uri, prefix, cloudName);
4748

48-
return getApiResponse(method, params, apiKey, apiSecret, contentType, timeout, apiUrl);
49+
return getApiResponse(method, params, apiKey, apiSecret, oauthToken, contentType, timeout, apiUrl);
4950
}
5051

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

66-
return getApiResponse(method, params, apiKey, apiSecret, contentType, timeout, apiUrl);
67+
return getApiResponse(method, params, apiKey, apiSecret, null, contentType, timeout, apiUrl);
6768
}
6869

69-
private ApiResponse getApiResponse(HttpMethod method, Map<String, ?> params, String apiKey, String apiSecret, String contentType, int timeout, String apiUrl) throws Exception {
70+
private ApiResponse getApiResponse(HttpMethod method, Map<String, ?> params, String apiKey, String apiSecret, String oauthToken, String contentType, int timeout, String apiUrl) throws Exception {
7071
URIBuilder apiUrlBuilder = new URIBuilder(apiUrl);
7172
if (!contentType.equals("json")) {
7273
for (Map.Entry<String, ? extends Object> param : params.entrySet()) {
@@ -105,7 +106,7 @@ private ApiResponse getApiResponse(HttpMethod method, Map<String, ?> params, Str
105106
request = new HttpDelete(apiUri);
106107
break;
107108
}
108-
request.setHeader("Authorization", "Basic " + Base64Coder.encodeString(apiKey + ":" + apiSecret));
109+
request.setHeader("Authorization", getAuthorizationHeaderValue(apiKey, apiSecret, oauthToken));
109110
request.setHeader("User-Agent", Cloudinary.USER_AGENT + " ApacheHTTPComponents/4.2");
110111
if (contentType.equals("json")) {
111112
JSONObject asJSON = ObjectUtils.toJSON(params);

cloudinary-http43/src/main/java/com/cloudinary/http43/ApiStrategy.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
7979
String cloudName = ObjectUtils.asString(options.get("cloud_name"), this.api.cloudinary.config.cloudName);
8080
if (cloudName == null) throw new IllegalArgumentException("Must supply cloud_name");
8181
String apiKey = ObjectUtils.asString(options.get("api_key"), this.api.cloudinary.config.apiKey);
82-
if (apiKey == null) throw new IllegalArgumentException("Must supply api_key");
8382
String apiSecret = ObjectUtils.asString(options.get("api_secret"), this.api.cloudinary.config.apiSecret);
84-
if (apiSecret == null) throw new IllegalArgumentException("Must supply api_secret");
83+
String oauthToken = ObjectUtils.asString(options.get("oauth_token"), this.api.cloudinary.config.oauthToken);
8584

85+
validateAuthorization(apiKey, apiSecret, oauthToken);
8686

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

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

9292
return getApiResponse(request);
9393
}

cloudinary-http44/src/main/java/com/cloudinary/http44/ApiStrategy.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
8181
String cloudName = ObjectUtils.asString(options.get("cloud_name"), this.api.cloudinary.config.cloudName);
8282
if (cloudName == null) throw new IllegalArgumentException("Must supply cloud_name");
8383
String apiKey = ObjectUtils.asString(options.get("api_key"), this.api.cloudinary.config.apiKey);
84-
if (apiKey == null) throw new IllegalArgumentException("Must supply api_key");
8584
String apiSecret = ObjectUtils.asString(options.get("api_secret"), this.api.cloudinary.config.apiSecret);
86-
if (apiSecret == null) throw new IllegalArgumentException("Must supply api_secret");
85+
String oauthToken = ObjectUtils.asString(options.get("oauth_token"), this.api.cloudinary.config.oauthToken);
8786

87+
validateAuthorization(apiKey, apiSecret, oauthToken);
8888

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

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

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

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

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

151151
return getApiResponse(request);
152152
}

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractApiTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ public void test04ResourcesByType() throws Exception {
188188
assertTrue(resources.size() > 0);
189189
}
190190

191+
@Test
192+
public void testOAuthToken() {
193+
String message = "";
194+
try {
195+
api.resource(API_TEST, Collections.singletonMap("oauth_token", "not_a_real_token"));
196+
} catch (Exception e) {
197+
message = e.getMessage();
198+
}
199+
200+
assertTrue(message.contains("Invalid token"));
201+
}
202+
191203
@Test
192204
public void test05ResourcesByPrefix() throws Exception {
193205
// should allow listing resources by prefix
@@ -996,7 +1008,7 @@ public void testCinemagraphAnalysisResource() throws Exception {
9961008
ApiResponse res = api.resource(API_TEST, Collections.singletonMap("cinemagraph_analysis", true));
9971009
assertNotNull(res.get("cinemagraph_analysis"));
9981010
}
999-
1011+
10001012
@Test
10011013
public void testAccessibilityAnalysisResource() throws Exception {
10021014
ApiResponse res = api.resource(API_TEST, Collections.singletonMap("accessibility_analysis", true));

0 commit comments

Comments
 (0)