@@ -10,6 +10,7 @@ var crypto = require('crypto');
1010 Utils
1111*/
1212var 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
148136function getSignatureTimestamp ( seconds ) {
149137 if ( ! seconds ) return DEFAULT_TIMESTAMP ;
@@ -157,6 +145,6 @@ function getSignatureTimestamp(seconds) {
157145
158146function 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}
0 commit comments