Skip to content

Commit 9afbba8

Browse files
Merge pull request #573 from cloudinary/feat/mutliple-acls
Support multiple ACLs for generate_auth_token()
2 parents 7f308a1 + bbce2b2 commit 9afbba8

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

lib-es5/auth_token.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function escapeToLower(url) {
3535
* @property {number} start_time=now The start time of the token in seconds from epoch.
3636
* @property {string} expiration The expiration time of the token in seconds from epoch.
3737
* @property {string} duration The duration of the token (from start_time).
38-
* @property {string} acl The ACL for the token.
38+
* @property {string|Array<string>} acl The ACL(s) for the token.
3939
* @property {string} url The URL to authentication in case of a URL token.
4040
*
4141
*/
@@ -65,6 +65,9 @@ module.exports = function (options) {
6565
}
6666
tokenParts.push(`exp=${options.expiration}`);
6767
if (options.acl != null) {
68+
if (Array.isArray(options.acl) === true) {
69+
options.acl = options.acl.join("!");
70+
}
6871
tokenParts.push(`acl=${escapeToLower(options.acl)}`);
6972
}
7073
var toSign = [].concat(tokenParts);

lib/auth_token.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function escapeToLower(url) {
3333
* @property {number} start_time=now The start time of the token in seconds from epoch.
3434
* @property {string} expiration The expiration time of the token in seconds from epoch.
3535
* @property {string} duration The duration of the token (from start_time).
36-
* @property {string} acl The ACL for the token.
36+
* @property {string|Array<string>} acl The ACL(s) for the token.
3737
* @property {string} url The URL to authentication in case of a URL token.
3838
*
3939
*/
@@ -63,6 +63,9 @@ module.exports = function (options) {
6363
}
6464
tokenParts.push(`exp=${options.expiration}`);
6565
if (options.acl != null) {
66+
if (Array.isArray(options.acl) === true) {
67+
options.acl = options.acl.join("!");
68+
}
6669
tokenParts.push(`acl=${escapeToLower(options.acl)}`);
6770
}
6871
let toSign = [...tokenParts];

test/unit/authToken/authTokenUtils_spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,15 @@ describe("AuthToken tests using utils.generate_auth_token", function () {
6666
});
6767
}).not.to.throwError();
6868
});
69+
70+
it("should support multiple ACLs", function () {
71+
let token = utils.generate_auth_token({
72+
start_time: 222222222,
73+
key: "00112233FF99",
74+
duration: 300,
75+
acl: ["/*/t_foobar", "t_foobar/*/"]
76+
});
77+
78+
expect(token).to.eql("__cld_token__=st=222222222~exp=222222522~acl=%2f*%2ft_foobar!t_foobar%2f*%2f~hmac=45d51dd32dd26a3c2339155f454076c1d8fbd93c611965461569a0b4279bbdd5");
79+
});
6980
});

0 commit comments

Comments
 (0)