|
| 1 | +package com.cloudinary; |
| 2 | + |
| 3 | +import com.cloudinary.utils.StringUtils; |
| 4 | + |
| 5 | +import javax.crypto.Mac; |
| 6 | +import javax.crypto.spec.SecretKeySpec; |
| 7 | +import javax.xml.bind.DatatypeConverter; |
| 8 | +import java.security.InvalidKeyException; |
| 9 | +import java.security.NoSuchAlgorithmException; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Calendar; |
| 12 | +import java.util.TimeZone; |
| 13 | + |
| 14 | +/** |
| 15 | + * Token generator for Akamai authentication |
| 16 | + */ |
| 17 | +public class AkamaiToken { |
| 18 | + public String tokenName = Cloudinary.AKAMAI_TOKEN_NAME; |
| 19 | + public String key; |
| 20 | + public long startTime; |
| 21 | + public long endTime; |
| 22 | + public String ip; |
| 23 | + public String acl; |
| 24 | + public long window; |
| 25 | + |
| 26 | + public AkamaiToken(String key) { |
| 27 | + this.key = key; |
| 28 | + } |
| 29 | + |
| 30 | + public AkamaiToken setTokenName(String tokenName) { |
| 31 | + this.tokenName = tokenName; |
| 32 | + return this; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Set the start time of the token. Defaults to now. |
| 37 | + * |
| 38 | + * @param startTime in seconds since epoch |
| 39 | + * @return |
| 40 | + */ |
| 41 | + public AkamaiToken setStartTime(long startTime) { |
| 42 | + this.startTime = startTime; |
| 43 | + return this; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Set the end time (expiration) of the token |
| 48 | + * |
| 49 | + * @param endTime in seconds since epoch |
| 50 | + * @return |
| 51 | + */ |
| 52 | + public AkamaiToken setEndTime(long endTime) { |
| 53 | + this.endTime = endTime; |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + public AkamaiToken setIp(String ip) { |
| 58 | + this.ip = ip; |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + public AkamaiToken setAcl(String acl) { |
| 63 | + this.acl = acl; |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * The duration of the token in seconds. This value is used to calculate the expiration of the token. |
| 69 | + * It is ignored if endTime is provided. |
| 70 | + * |
| 71 | + * @param window |
| 72 | + * @return |
| 73 | + */ |
| 74 | + public AkamaiToken setWindow(long window) { |
| 75 | + this.window = window; |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Generate the authentication token |
| 81 | + * |
| 82 | + * @return a signed token |
| 83 | + */ |
| 84 | + public String generate() { |
| 85 | + long expiration = endTime; |
| 86 | + if (expiration == 0) { |
| 87 | + if (window > 0) { |
| 88 | + final long start = startTime > 0 ? startTime : Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTimeInMillis() / 1000L; |
| 89 | + expiration = start + window; |
| 90 | + } else { |
| 91 | + throw new IllegalArgumentException("Must provide either endTime or window"); |
| 92 | + } |
| 93 | + } |
| 94 | + ArrayList<String> tokenParts = new ArrayList<String>(); |
| 95 | + if (ip != null) { |
| 96 | + tokenParts.add("ip=" + ip); |
| 97 | + } |
| 98 | + if (startTime > 0) { |
| 99 | + tokenParts.add("st=" + startTime); |
| 100 | + } |
| 101 | + tokenParts.add("exp=" + expiration); |
| 102 | + tokenParts.add("acl=" + acl); |
| 103 | + String auth = digest(StringUtils.join(tokenParts, "~")); |
| 104 | + tokenParts.add("hmac=" + auth); |
| 105 | + return tokenName + "=" + StringUtils.join(tokenParts, "~"); |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + private String digest(String message) { |
| 110 | + byte[] binKey = DatatypeConverter.parseHexBinary(key); |
| 111 | + try { |
| 112 | + Mac hmac = Mac.getInstance("HmacSHA256"); |
| 113 | + SecretKeySpec secret = new SecretKeySpec(binKey, "HmacSHA256"); |
| 114 | + hmac.init(secret); |
| 115 | + final byte[] bytes = message.getBytes(); |
| 116 | + return DatatypeConverter.printHexBinary(hmac.doFinal(bytes)).toLowerCase(); |
| 117 | + } catch (NoSuchAlgorithmException e) { |
| 118 | + e.printStackTrace(); |
| 119 | + } catch (InvalidKeyException e) { |
| 120 | + e.printStackTrace(); |
| 121 | + } |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | +} |
0 commit comments