Skip to content

Commit 3ffa4de

Browse files
committed
url signature fix
1 parent c817d94 commit 3ffa4de

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

libs/url/builder.js

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var crypto = require('crypto');
1010
Utils
1111
*/
1212
var transformationUtils = require('../../utils/transformation');
13+
var urlFormatter = require('../../utils/urlFormatter');
1314

1415
/*
1516
Variables
@@ -39,6 +40,8 @@ module.exports.buildURL = function(opts) {
3940
for(var i in opts.queryParameters) {
4041
queryParameters.set(i, opts.queryParameters[i]);
4142
}
43+
44+
4245

4346
//Initial URL Construction Object
4447
var urlObject = {host : "", pathname : "", search : ""};
@@ -68,8 +71,8 @@ module.exports.buildURL = function(opts) {
6871

6972

7073

71-
urlObject.host = removeTrailingSlash(urlObject.host);
72-
urlObject.pathname = addLeadingSlash(urlObject.pathname);
74+
urlObject.host = urlFormatter.removeTrailingSlash(urlObject.host);
75+
urlObject.pathname = urlFormatter.addLeadingSlash(urlObject.pathname);
7376
urlObject.search = queryParameters.toString();
7477

7578
// Signature String and Timestamp
@@ -129,21 +132,6 @@ function constructTransformationString(transformation) {
129132
return parsedTransforms.join(transformationUtils.getChainTransformDelimiter());
130133
}
131134

132-
function addLeadingSlash(str) {
133-
if(typeof str == "string" && str[0] != "/") {
134-
str = "/" + str;
135-
}
136-
137-
return str;
138-
}
139-
140-
function removeTrailingSlash(str) {
141-
if(typeof str == "string" && str[str.length - 1] == "/") {
142-
str = str.substring(0, str.length - 1);
143-
}
144-
145-
return str;
146-
}
147135

148136
function getSignatureTimestamp(seconds) {
149137
if(!seconds) return DEFAULT_TIMESTAMP;
@@ -157,6 +145,6 @@ function getSignatureTimestamp(seconds) {
157145

158146
function getSignature(opts) {
159147
if(!opts.privateKey || !opts.url || !opts.urlEndpoint) return "";
160-
161-
return crypto.createHmac('sha1', opts.privateKey).update(opts.url.replace(opts.urlEndpoint, "") + opts.expiryTimestamp).digest('hex');
148+
var stringToSign = opts.url.replace(urlFormatter.addTrailingSlash(opts.urlEndpoint), "") + opts.expiryTimestamp;
149+
return crypto.createHmac('sha1', opts.privateKey).update(stringToSign).digest('hex');
162150
}

libs/url/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var transformationUtils = require('../../utils/transformation');
1515

1616
module.exports = function(urlOpts, defaultOptions) {
1717
var opts = _.extend({}, defaultOptions, urlOpts);
18-
18+
1919
if(!validOptions(opts)) {
2020
return "";
2121
}

utils/urlFormatter.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module.exports.addLeadingQuestionMark = function(str) {
2+
if(typeof str == "string" && str[0] != "?") {
3+
str = "?" + str;
4+
}
5+
6+
return str;
7+
}
8+
9+
module.exports.addLeadingSlash = function(str) {
10+
if(typeof str == "string" && str[0] != "/") {
11+
str = "/" + str;
12+
}
13+
14+
return str;
15+
}
16+
17+
module.exports.removeLeadingSlash = function(str) {
18+
if(typeof str == "string" && str[0] == "/") {
19+
str = str.substring(1);
20+
}
21+
22+
return str;
23+
};
24+
25+
module.exports.removeTrailingSlash = function(str) {
26+
if(typeof str == "string" && str[str.length - 1] == "/") {
27+
str = str.substring(0, str.length - 1);
28+
}
29+
30+
return str;
31+
}
32+
33+
module.exports.addTrailingSlash = function(str) {
34+
if(typeof str == "string" && str[str.length - 1] != "/") {
35+
str = str + "/";
36+
}
37+
38+
return str;
39+
}

0 commit comments

Comments
 (0)