Skip to content

Commit 12fc1c1

Browse files
author
Amir Tocker
committed
Rename AuthToken. Add url parameter.
1 parent 8514773 commit 12fc1c1

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

cloudinary-core/src/main/java/com/cloudinary/AkamaiToken.java renamed to cloudinary-core/src/main/java/com/cloudinary/AuthToken.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
import java.util.TimeZone;
1313

1414
/**
15-
* Token generator for Akamai authentication
15+
* Authentication Token generator
1616
*/
17-
public class AkamaiToken {
17+
public class AuthToken {
1818
public String tokenName = Cloudinary.AKAMAI_TOKEN_NAME;
1919
public String key;
2020
public long startTime;
@@ -23,11 +23,11 @@ public class AkamaiToken {
2323
public String acl;
2424
public long window;
2525

26-
public AkamaiToken(String key) {
26+
public AuthToken(String key) {
2727
this.key = key;
2828
}
2929

30-
public AkamaiToken setTokenName(String tokenName) {
30+
public AuthToken setTokenName(String tokenName) {
3131
this.tokenName = tokenName;
3232
return this;
3333
}
@@ -38,7 +38,7 @@ public AkamaiToken setTokenName(String tokenName) {
3838
* @param startTime in seconds since epoch
3939
* @return
4040
*/
41-
public AkamaiToken setStartTime(long startTime) {
41+
public AuthToken setStartTime(long startTime) {
4242
this.startTime = startTime;
4343
return this;
4444
}
@@ -49,17 +49,17 @@ public AkamaiToken setStartTime(long startTime) {
4949
* @param endTime in seconds since epoch
5050
* @return
5151
*/
52-
public AkamaiToken setEndTime(long endTime) {
52+
public AuthToken setEndTime(long endTime) {
5353
this.endTime = endTime;
5454
return this;
5555
}
5656

57-
public AkamaiToken setIp(String ip) {
57+
public AuthToken setIp(String ip) {
5858
this.ip = ip;
5959
return this;
6060
}
6161

62-
public AkamaiToken setAcl(String acl) {
62+
public AuthToken setAcl(String acl) {
6363
this.acl = acl;
6464
return this;
6565
}
@@ -71,7 +71,7 @@ public AkamaiToken setAcl(String acl) {
7171
* @param window
7272
* @return
7373
*/
74-
public AkamaiToken setWindow(long window) {
74+
public AuthToken setWindow(long window) {
7575
this.window = window;
7676
return this;
7777
}
@@ -82,6 +82,10 @@ public AkamaiToken setWindow(long window) {
8282
* @return a signed token
8383
*/
8484
public String generate() {
85+
return generate(null);
86+
}
87+
88+
public String generate(String url) {
8589
long expiration = endTime;
8690
if (expiration == 0) {
8791
if (window > 0) {
@@ -99,12 +103,18 @@ public String generate() {
99103
tokenParts.add("st=" + startTime);
100104
}
101105
tokenParts.add("exp=" + expiration);
102-
tokenParts.add("acl=" + acl);
106+
if (url != null) {
107+
tokenParts.add("url=" + url);
108+
} else if (acl != null) {
109+
tokenParts.add("acl=" + acl);
110+
} else {
111+
throw new IllegalArgumentException("Must provide either url or acl");
112+
}
103113
String auth = digest(StringUtils.join(tokenParts, "~"));
104114
tokenParts.add("hmac=" + auth);
105115
return tokenName + "=" + StringUtils.join(tokenParts, "~");
106-
}
107116

117+
}
108118

109119
private String digest(String message) {
110120
byte[] binKey = DatatypeConverter.parseHexBinary(key);

cloudinary-core/src/test/java/com/cloudinary/AkamaiTokenTest.java renamed to cloudinary-core/src/test/java/com/cloudinary/AuthTokenTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
import static org.junit.Assert.*;
1212

13-
public class AkamaiTokenTest {
13+
public class AuthTokenTest {
1414
public static final String KEY = "00112233FF99";
1515

1616
@Test
1717
public void generateWithStartAndWindow() throws Exception {
18-
AkamaiToken t = new AkamaiToken(KEY);
18+
AuthToken t = new AuthToken(KEY);
1919
t.setStartTime(1111111111).setAcl("/image/*").setWindow(300);
2020
assertEquals("should generate an Akamai token with startTime and window", "__cld_token__=st=1111111111~exp=1111111411~acl=/image/*~hmac=0854e8b6b6a46471a80b2dc28c69bd352d977a67d031755cc6f3486c121b43af", t.generate());
2121
}
@@ -24,7 +24,7 @@ public void generateWithStartAndWindow() throws Exception {
2424
public void generateWithWindow() throws Exception {
2525
long firstExp = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTimeInMillis() / 1000L + 300;
2626
Thread.sleep(1200);
27-
String token = new AkamaiToken(KEY).setAcl("*").setWindow(300).generate();
27+
String token = new AuthToken(KEY).setAcl("*").setWindow(300).generate();
2828
Thread.sleep(1200);
2929
long secondExp = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTimeInMillis() / 1000L + 300;
3030
Matcher m = Pattern.compile("exp=(\\d+)").matcher(token);
@@ -33,12 +33,12 @@ public void generateWithWindow() throws Exception {
3333
final long actual = Long.parseLong(expString);
3434
assertThat(actual, Matchers.greaterThanOrEqualTo(firstExp));
3535
assertThat(actual, Matchers.lessThanOrEqualTo(secondExp));
36-
assertEquals(token, new AkamaiToken(KEY).setAcl("*").setEndTime(actual).generate());
36+
assertEquals(token, new AuthToken(KEY).setAcl("*").setEndTime(actual).generate());
3737
}
3838

3939
@Test(expected = IllegalArgumentException.class)
4040
public void testMustProvideEndTimeOrWindow(){
41-
new AkamaiToken(KEY).setAcl("*").generate();
41+
new AuthToken(KEY).setAcl("*").generate();
4242
}
4343

4444

0 commit comments

Comments
 (0)