1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | var crypto = require('crypto')
|
23 | , parse = require('url').parse
|
24 | ;
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | var keys =
|
31 | [ 'acl'
|
32 | , 'location'
|
33 | , 'logging'
|
34 | , 'notification'
|
35 | , 'partNumber'
|
36 | , 'policy'
|
37 | , 'requestPayment'
|
38 | , 'torrent'
|
39 | , 'uploadId'
|
40 | , 'uploads'
|
41 | , 'versionId'
|
42 | , 'versioning'
|
43 | , 'versions'
|
44 | , 'website'
|
45 | ]
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | function authorization (options) {
|
57 | return 'AWS ' + options.key + ':' + sign(options)
|
58 | }
|
59 |
|
60 | module.exports = authorization
|
61 | module.exports.authorization = authorization
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | function hmacSha1 (options) {
|
72 | return crypto.createHmac('sha1', options.secret).update(options.message).digest('base64')
|
73 | }
|
74 |
|
75 | module.exports.hmacSha1 = hmacSha1
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | function sign (options) {
|
86 | options.message = stringToSign(options)
|
87 | return hmacSha1(options)
|
88 | }
|
89 | module.exports.sign = sign
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | function signQuery (options) {
|
102 | options.message = queryStringToSign(options)
|
103 | return hmacSha1(options)
|
104 | }
|
105 | module.exports.signQuery= signQuery
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | function stringToSign (options) {
|
125 | var headers = options.amazonHeaders || ''
|
126 | if (headers) headers += '\n'
|
127 | var r =
|
128 | [ options.verb
|
129 | , options.md5
|
130 | , options.contentType
|
131 | , options.date ? options.date.toUTCString() : ''
|
132 | , headers + options.resource
|
133 | ]
|
134 | return r.join('\n')
|
135 | }
|
136 | module.exports.stringToSign = stringToSign
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | function queryStringToSign (options){
|
153 | return 'GET\n\n\n' + options.date + '\n' + options.resource
|
154 | }
|
155 | module.exports.queryStringToSign = queryStringToSign
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | function canonicalizeHeaders (headers) {
|
172 | var buf = []
|
173 | , fields = Object.keys(headers)
|
174 | ;
|
175 | for (var i = 0, len = fields.length; i < len; ++i) {
|
176 | var field = fields[i]
|
177 | , val = headers[field]
|
178 | , field = field.toLowerCase()
|
179 | ;
|
180 | if (0 !== field.indexOf('x-amz')) continue
|
181 | buf.push(field + ':' + val)
|
182 | }
|
183 | return buf.sort().join('\n')
|
184 | }
|
185 | module.exports.canonicalizeHeaders = canonicalizeHeaders
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | function canonicalizeResource (resource) {
|
199 | var url = parse(resource, true)
|
200 | , path = url.pathname
|
201 | , buf = []
|
202 | ;
|
203 |
|
204 | Object.keys(url.query).forEach(function(key){
|
205 | if (!~keys.indexOf(key)) return
|
206 | var val = '' == url.query[key] ? '' : '=' + encodeURIComponent(url.query[key])
|
207 | buf.push(key + val)
|
208 | })
|
209 |
|
210 | return path + (buf.length ? '?' + buf.sort().join('&') : '')
|
211 | }
|
212 | module.exports.canonicalizeResource = canonicalizeResource
|