Skip to content

Commit 0a0080e

Browse files
author
Amir Tocker
committed
Refactor generate_auth_token
1 parent 1182e0a commit 0a0080e

File tree

5 files changed

+48
-28
lines changed

5 files changed

+48
-28
lines changed

lib/utils.js

Lines changed: 14 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/utils.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/auth_token.coffee

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ crypto = require('crypto')
66
config = require('./config')
77

88
digest = (message, key) ->
9-
crypto.createHmac("sha256", new Buffer(key, "hex"))
10-
.update message
11-
.digest 'hex'
9+
crypto.createHmac("sha256", new Buffer(key, "hex")).update( message).digest('hex')
10+
11+
###*
12+
* Escape url using lowercase hex code
13+
* @param {string} url a url string
14+
* @return escaped url
15+
###
16+
escape_to_lower = (url) ->
17+
encodeURIComponent(url).replace(/%../g, (match)-> match.toLowerCase())
1218

1319
###*
1420
* Generate an authorization token
@@ -23,25 +29,24 @@ digest = (message, key) ->
2329
* @returns {string} the authorization token
2430
###
2531
module.exports = (options)->
26-
params = Object.assign {}, config().auth_token, options
27-
tokenName = params.token_name ? "__cld_token__"
32+
tokenName = options.token_name ? "__cld_token__"
2833

29-
unless params.expiration?
30-
if params.duration?
31-
start = params.start_time ? Math.round(Date.now() / 1000)
32-
params.expiration = start + params.duration
34+
unless options.expiration?
35+
if options.duration?
36+
start = options.start_time ? Math.round(Date.now() / 1000)
37+
options.expiration = start + options.duration
3338
else
3439
throw new Error( "Must provide either expiration or duration")
3540

3641
tokenParts = []
37-
tokenParts.push("ip=#{params.ip}") if params.ip?
38-
tokenParts.push("st=#{params.start_time}") if params.start_time?
39-
tokenParts.push("exp=#{params.expiration}")
40-
tokenParts.push("acl=#{params.acl}") if params.acl?
42+
tokenParts.push("ip=#{options.ip}") if options.ip?
43+
tokenParts.push("st=#{options.start_time}") if options.start_time?
44+
tokenParts.push("exp=#{options.expiration}")
45+
tokenParts.push("acl=#{escape_to_lower(options.acl)}") if options.acl?
4146
toSign = (part for part in tokenParts)
42-
if params.url
43-
url = encodeURIComponent(params.url).replace(/%../g, (match)-> match.toLowerCase())
47+
if options.url
48+
url = escape_to_lower(options.url)
4449
toSign.push "url=#{url}"
45-
auth = digest(toSign.join("~"), params.key)
50+
auth = digest(toSign.join("~"), options.key)
4651
tokenParts.push("hmac=#{auth}")
4752
"#{tokenName}=#{tokenParts.join('~')}"

src/utils.coffee

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ querystring = require('querystring')
1111
url = require('url')
1212

1313
utils = exports
14-
exports.generate_auth_token = require("./auth_token")
14+
generate_token = require("./auth_token")
15+
exports.generate_auth_token = (options)->
16+
token_options = Object.assign {}, config().auth_token, options
17+
generate_token token_options
18+
1519
exports.CF_SHARED_CDN = "d3jpl91pxevbkh.cloudfront.net"
1620
exports.OLD_AKAMAI_SHARED_CDN = "cloudinary-a.akamaihd.net"
1721
exports.AKAMAI_SHARED_CDN = "res.cloudinary.com"
@@ -396,7 +400,9 @@ exports.url = (public_id, options = {}) ->
396400
api_secret = utils.option_consume(options, "api_secret", config().api_secret)
397401
url_suffix = utils.option_consume(options, "url_suffix")
398402
use_root_path = utils.option_consume(options, "use_root_path", config().use_root_path)
399-
auth_token = if options.auth_token == false then false else exports.merge config().auth_token, utils.option_consume(options, "auth_token")
403+
auth_token = utils.option_consume(options, "auth_token")
404+
if auth_token != false
405+
auth_token = exports.merge config().auth_token, auth_token
400406

401407
preloaded = /^(image|raw)\/([a-z0-9_]+)\/v(\d+)\/([^#]+)$/.exec(public_id)
402408
if preloaded
@@ -436,7 +442,8 @@ exports.url = (public_id, options = {}) ->
436442
resultUrl = [prefix, resource_type, type, signature, transformation, version,
437443
public_id].filter((part) -> part? && part != '').join('/')
438444
if sign_url && !_.isEmpty(auth_token)
439-
token = utils.generate_auth_token exports.merge(url: url.parse(resultUrl).path, auth_token)
445+
auth_token.url = url.parse(resultUrl).path
446+
token = generate_token( auth_token)
440447
resultUrl += "?#{token}"
441448
resultUrl
442449

test/authtoken_spec.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe "authToken", ->
1919

2020
it "should generate with start and window", ->
2121
token = utils.generate_auth_token start_time: 1111111111, acl: "/image/*", duration: 300
22-
expect(token).to.eql '__cld_token__=st=1111111111~exp=1111111411~acl=/image/*~hmac=0854e8b6b6a46471a80b2dc28c69bd352d977a67d031755cc6f3486c121b43af'
22+
expect(token).to.eql "__cld_token__=st=1111111111~exp=1111111411~acl=%2fimage%2f*~hmac=1751370bcc6cfe9e03f30dd1a9722ba0f2cdca283fa3e6df3342a00a7528cc51"
2323

2424
describe "authenticated url", ->
2525
beforeEach ->
@@ -56,7 +56,7 @@ describe "authToken", ->
5656
tokenOptions = {key: KEY, duration: 300, acl: "/*/t_#{user}"}
5757
tokenOptions.start_time = 222222222 # we can't rely on the default "now" value in tests
5858
cookieToken = utils.generate_auth_token tokenOptions
59-
expect(cookieToken).to.eql("__cld_token__=st=222222222~exp=222222522~acl=/*/t_foobar~hmac=eb5e2266c8ec9573f696025f075b92998080347e1c12ac39a26c94d7d712704a")
59+
expect(cookieToken).to.eql("__cld_token__=st=222222222~exp=222222522~acl=%2f*%2ft_foobar~hmac=8e39600cc18cec339b21fe2b05fcb64b98de373355f8ce732c35710d8b10259f")
6060

6161
it "should add token to an image tag url", ->
6262
tag = cloudinary.image "sample.jpg", sign_url: true, type: "authenticated", version: "1486020273"

0 commit comments

Comments
 (0)