1
- 'use strict' ;
2
1
3
- var crypto = require ( 'crypto' ) ;
4
- var is = require ( 'is-type-of' ) ;
2
+ const crypto = require ( 'crypto' ) ;
3
+ const is = require ( 'is-type-of' ) ;
5
4
6
5
/**
7
6
*
@@ -10,25 +9,29 @@ var is = require('is-type-of');
10
9
* @return
11
10
*/
12
11
exports . buildCanonicalizedResource = function buildCanonicalizedResource ( resourcePath , parameters ) {
13
- var canonicalizedResource = '' + resourcePath ;
14
- var separatorString = '?' ;
12
+ let canonicalizedResource = ` ${ resourcePath } ` ;
13
+ let separatorString = '?' ;
15
14
16
15
if ( is . string ( parameters ) && parameters . trim ( ) !== '' ) {
17
16
canonicalizedResource += separatorString + parameters ;
18
17
} else if ( is . array ( parameters ) ) {
19
18
parameters . sort ( ) ;
20
19
canonicalizedResource += separatorString + parameters . join ( '&' ) ;
21
20
} else if ( parameters ) {
22
- var keys = Object . keys ( parameters ) . sort ( ) ;
23
- for ( var key in keys ) {
24
- var paramKey = keys [ key ] ;
25
- canonicalizedResource += separatorString + paramKey ;
26
- var parameterValue = parameters [ paramKey ] ;
27
- if ( parameterValue ) {
28
- canonicalizedResource += '=' + parameterValue ;
21
+ const compareFunc = ( entry1 , entry2 ) => {
22
+ if ( entry1 [ 0 ] >= entry2 [ 0 ] ) {
23
+ return 1 ;
24
+ }
25
+ return 0 ;
26
+ } ;
27
+ const processFunc = ( [ key , value ] ) => {
28
+ canonicalizedResource += separatorString + key ;
29
+ if ( value ) {
30
+ canonicalizedResource += `=${ value } ` ;
29
31
}
30
32
separatorString = '&' ;
31
- }
33
+ } ;
34
+ Object . entries ( parameters ) . sort ( compareFunc ) . forEach ( processFunc ) ;
32
35
}
33
36
34
37
return canonicalizedResource ;
@@ -42,42 +45,49 @@ exports.buildCanonicalizedResource = function buildCanonicalizedResource(resourc
42
45
* @return {String } canonicalString
43
46
*/
44
47
exports . buildCanonicalString = function canonicalString ( method , resourcePath , request , expires ) {
45
- var HEADER_CONTENT_TYPE = 'content-type' ;
46
- var HEADER_CONTENT_MD5 = 'content-md5' ;
47
- var OSS_PREFIX = 'x-oss-' ;
48
+ request = request || { } ;
49
+ const headers = request . headers || { } ;
50
+ const HEADER_CONTENT_TYPE = 'content-type' ;
51
+ const HEADER_CONTENT_MD5 = 'content-md5' ;
52
+ const OSS_PREFIX = 'x-oss-' ;
53
+ const canonicalElements = [ method ] ;
54
+ const headersToSign = { } ;
48
55
49
- var headers = request . headers || { } ;
50
- var canonicalElements = [ method ] ;
51
-
52
- var headersToSign = { } ;
53
- for ( var key in headers ) {
54
- var lowerKey = key . toLowerCase ( ) ;
56
+ Object . entries ( headers ) . forEach ( ( [ key , value ] ) => {
57
+ const lowerKey = key . toLowerCase ( ) ;
55
58
if ( lowerKey === HEADER_CONTENT_TYPE
56
- || lowerKey === HEADER_CONTENT_MD5
57
- || lowerKey . indexOf ( OSS_PREFIX ) === 0 ) {
58
- headersToSign [ lowerKey ] = String ( headers [ key ] ) . trim ( ) ;
59
+ || lowerKey === HEADER_CONTENT_MD5
60
+ || lowerKey . indexOf ( OSS_PREFIX ) === 0 ) {
61
+ headersToSign [ lowerKey ] = String ( value ) . trim ( ) ;
59
62
}
60
- }
63
+ } ) ;
61
64
62
- if ( ! headersToSign . hasOwnProperty ( HEADER_CONTENT_MD5 ) ) {
65
+ if ( ! Object . prototype . hasOwnProperty . call ( headersToSign , HEADER_CONTENT_MD5 ) ) {
63
66
headersToSign . HEADER_CONTENT_MD5 = '' ;
64
67
}
65
68
66
- if ( ! headersToSign . hasOwnProperty ( HEADER_CONTENT_TYPE ) ) {
69
+ if ( ! Object . prototype . hasOwnProperty . call ( headersToSign , HEADER_CONTENT_TYPE ) ) {
67
70
headersToSign . HEADER_CONTENT_TYPE = '' ;
68
71
}
69
72
70
- var keys = Object . keys ( headersToSign ) . sort ( ) ;
71
- for ( var key in keys ) {
72
- var parameterName = keys [ key ] ;
73
- var parameterValue = headersToSign [ parameterName ] ;
74
- if ( parameterName . indexOf ( OSS_PREFIX ) !== 0 ) {
75
- canonicalElements . push ( parameterValue ) ;
73
+ const compareFunc = ( a , b ) => {
74
+ if ( a [ 0 ] >= b [ 0 ] ) {
75
+ return 1 ;
76
+ }
77
+ return 0 ;
78
+ } ;
79
+
80
+ const processFunc = ( [ key , value ] ) => {
81
+ if ( key . indexOf ( OSS_PREFIX ) !== 0 ) {
82
+ canonicalElements . push ( value ) ;
76
83
} else {
77
- canonicalElements . push ( parameterName + ':' + parameterValue ) ;
84
+ canonicalElements . push ( ` ${ key } : ${ value } ` ) ;
78
85
}
79
- }
80
- expires = expires || headersToSign [ "x-oss-date" ] ;
86
+ } ;
87
+
88
+ Object . entries ( headersToSign ) . sort ( compareFunc ) . forEach ( processFunc ) ;
89
+
90
+ expires = expires || headersToSign [ 'x-oss-date' ] ;
81
91
canonicalElements . splice ( 3 , 0 , expires ) ;
82
92
83
93
canonicalElements . push ( this . buildCanonicalizedResource ( resourcePath , request . parameters ) ) ;
@@ -90,7 +100,7 @@ exports.buildCanonicalString = function canonicalString(method, resourcePath, re
90
100
* @param {String } canonicalString
91
101
*/
92
102
exports . computeSignature = function computeSignature ( accessKeySecret , canonicalString ) {
93
- var signature = crypto . createHmac ( 'sha1' , accessKeySecret ) ;
103
+ const signature = crypto . createHmac ( 'sha1' , accessKeySecret ) ;
94
104
return signature . update ( new Buffer ( canonicalString , 'utf8' ) ) . digest ( 'base64' ) ;
95
105
} ;
96
106
@@ -100,5 +110,74 @@ exports.computeSignature = function computeSignature(accessKeySecret, canonicalS
100
110
* @param {String } canonicalString
101
111
*/
102
112
exports . authorization = function authorization ( accessKeyId , accessKeySecret , canonicalString ) {
103
- return 'OSS ' + accessKeyId + ':' + this . computeSignature ( accessKeySecret , canonicalString ) ;
113
+ return `OSS ${ accessKeyId } :${ this . computeSignature ( accessKeySecret , canonicalString ) } ` ;
114
+ } ;
115
+
116
+ /**
117
+ *
118
+ * @param {String } accessKeySecret
119
+ * @param {Object } options
120
+ * @param {String } resource
121
+ * @param {Number } expires
122
+ */
123
+ exports . _signatureForURL = function _signatureForURL ( accessKeySecret , options , resource , expires ) {
124
+ const headers = { } ;
125
+ const subResource = { } ;
126
+
127
+ if ( options . process ) {
128
+ const processKeyword = 'x-oss-process' ;
129
+ subResource [ processKeyword ] = options . process ;
130
+ }
131
+
132
+ if ( options . response ) {
133
+ Object . entries ( options . response ) . forEach ( ( [ k , value ] ) => {
134
+ const key = `response-${ k . toLowerCase ( ) } ` ;
135
+ subResource [ key ] = value ;
136
+ } ) ;
137
+ }
138
+
139
+ Object . entries ( options ) . forEach ( ( [ key , value ] ) => {
140
+ const lowerKey = key . toLowerCase ( ) ;
141
+ if ( lowerKey . indexOf ( 'x-oss-' ) === 0 ) {
142
+ headers [ lowerKey ] = value ;
143
+ } else if ( lowerKey !== 'expires' && lowerKey !== 'response' && lowerKey !== 'process' && lowerKey !== 'method' ) {
144
+ subResource [ lowerKey ] = value ;
145
+ }
146
+ } ) ;
147
+
148
+ if ( Object . prototype . hasOwnProperty . call ( options , 'security-token' ) ) {
149
+ subResource [ 'security-token' ] = options [ 'security-token' ] ;
150
+ }
151
+
152
+ if ( Object . prototype . hasOwnProperty . call ( options , 'callback' ) ) {
153
+ const json = {
154
+ callbackUrl : encodeURI ( options . callback . url ) ,
155
+ callbackBody : options . callback . body ,
156
+ } ;
157
+ if ( options . callback . host ) {
158
+ json . callbackHost = options . callback . host ;
159
+ }
160
+ if ( options . callback . contentType ) {
161
+ json . callbackBodyType = options . callback . contentType ;
162
+ }
163
+ subResource . callback = new Buffer ( JSON . stringify ( json ) ) . toString ( 'base64' ) ;
164
+
165
+ if ( options . callback . customValue ) {
166
+ const callbackVar = { } ;
167
+ Object . keys ( options . callback . customValue ) . forEach ( ( key ) => {
168
+ callbackVar [ `x:${ key } ` ] = options . callback . customValue [ key ] ;
169
+ } ) ;
170
+ subResource [ 'callback-var' ] = new Buffer ( JSON . stringify ( callbackVar ) ) . toString ( 'base64' ) ;
171
+ }
172
+ }
173
+
174
+ const canonicalString = this . buildCanonicalString ( options . method , resource , {
175
+ headers,
176
+ parameters : subResource ,
177
+ } , expires . toString ( ) ) ;
178
+
179
+ return {
180
+ Signature : this . computeSignature ( accessKeySecret , canonicalString ) ,
181
+ subResource,
182
+ } ;
104
183
} ;
0 commit comments