Initial commit
[yaffs-website] / node_modules / hawk / test / index.js
1 // Load modules\r
2 \r
3 var Url = require('url');\r
4 var Code = require('code');\r
5 var Hawk = require('../lib');\r
6 var Lab = require('lab');\r
7 \r
8 \r
9 // Declare internals\r
10 \r
11 var internals = {};\r
12 \r
13 \r
14 // Test shortcuts\r
15 \r
16 var lab = exports.lab = Lab.script();\r
17 var describe = lab.experiment;\r
18 var it = lab.test;\r
19 var expect = Code.expect;\r
20 \r
21 \r
22 describe('Hawk', function () {\r
23 \r
24     var credentialsFunc = function (id, callback) {\r
25 \r
26         var credentials = {\r
27             id: id,\r
28             key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',\r
29             algorithm: (id === '1' ? 'sha1' : 'sha256'),\r
30             user: 'steve'\r
31         };\r
32 \r
33         return callback(null, credentials);\r
34     };\r
35 \r
36     it('generates a header then successfully parse it (configuration)', function (done) {\r
37 \r
38         var req = {\r
39             method: 'GET',\r
40             url: '/resource/4?filter=a',\r
41             host: 'example.com',\r
42             port: 8080\r
43         };\r
44 \r
45         credentialsFunc('123456', function (err, credentials1) {\r
46 \r
47             req.authorization = Hawk.client.header(Url.parse('http://example.com:8080/resource/4?filter=a'), req.method, { credentials: credentials1, ext: 'some-app-data' }).field;\r
48             expect(req.authorization).to.exist();\r
49 \r
50             Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {\r
51 \r
52                 expect(err).to.not.exist();\r
53                 expect(credentials2.user).to.equal('steve');\r
54                 expect(artifacts.ext).to.equal('some-app-data');\r
55                 done();\r
56             });\r
57         });\r
58     });\r
59 \r
60     it('generates a header then successfully parse it (node request)', function (done) {\r
61 \r
62         var req = {\r
63             method: 'POST',\r
64             url: '/resource/4?filter=a',\r
65             headers: {\r
66                 host: 'example.com:8080',\r
67                 'content-type': 'text/plain;x=y'\r
68             }\r
69         };\r
70 \r
71         var payload = 'some not so random text';\r
72 \r
73         credentialsFunc('123456', function (err, credentials1) {\r
74 \r
75             var reqHeader = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data', payload: payload, contentType: req.headers['content-type'] });\r
76             req.headers.authorization = reqHeader.field;\r
77 \r
78             Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {\r
79 \r
80                 expect(err).to.not.exist();\r
81                 expect(credentials2.user).to.equal('steve');\r
82                 expect(artifacts.ext).to.equal('some-app-data');\r
83                 expect(Hawk.server.authenticatePayload(payload, credentials2, artifacts, req.headers['content-type'])).to.equal(true);\r
84 \r
85                 var res = {\r
86                     headers: {\r
87                         'content-type': 'text/plain'\r
88                     }\r
89                 };\r
90 \r
91                 res.headers['server-authorization'] = Hawk.server.header(credentials2, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });\r
92                 expect(res.headers['server-authorization']).to.exist();\r
93 \r
94                 expect(Hawk.client.authenticate(res, credentials2, artifacts, { payload: 'some reply' })).to.equal(true);\r
95                 done();\r
96             });\r
97         });\r
98     });\r
99 \r
100     it('generates a header then successfully parse it (absolute request uri)', function (done) {\r
101 \r
102         var req = {\r
103             method: 'POST',\r
104             url: 'http://example.com:8080/resource/4?filter=a',\r
105             headers: {\r
106                 host: 'example.com:8080',\r
107                 'content-type': 'text/plain;x=y'\r
108             }\r
109         };\r
110 \r
111         var payload = 'some not so random text';\r
112 \r
113         credentialsFunc('123456', function (err, credentials1) {\r
114 \r
115             var reqHeader = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data', payload: payload, contentType: req.headers['content-type'] });\r
116             req.headers.authorization = reqHeader.field;\r
117 \r
118             Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {\r
119 \r
120                 expect(err).to.not.exist();\r
121                 expect(credentials2.user).to.equal('steve');\r
122                 expect(artifacts.ext).to.equal('some-app-data');\r
123                 expect(Hawk.server.authenticatePayload(payload, credentials2, artifacts, req.headers['content-type'])).to.equal(true);\r
124 \r
125                 var res = {\r
126                     headers: {\r
127                         'content-type': 'text/plain'\r
128                     }\r
129                 };\r
130 \r
131                 res.headers['server-authorization'] = Hawk.server.header(credentials2, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });\r
132                 expect(res.headers['server-authorization']).to.exist();\r
133 \r
134                 expect(Hawk.client.authenticate(res, credentials2, artifacts, { payload: 'some reply' })).to.equal(true);\r
135                 done();\r
136             });\r
137         });\r
138     });\r
139 \r
140     it('generates a header then successfully parse it (no server header options)', function (done) {\r
141 \r
142         var req = {\r
143             method: 'POST',\r
144             url: '/resource/4?filter=a',\r
145             headers: {\r
146                 host: 'example.com:8080',\r
147                 'content-type': 'text/plain;x=y'\r
148             }\r
149         };\r
150 \r
151         var payload = 'some not so random text';\r
152 \r
153         credentialsFunc('123456', function (err, credentials1) {\r
154 \r
155             var reqHeader = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data', payload: payload, contentType: req.headers['content-type'] });\r
156             req.headers.authorization = reqHeader.field;\r
157 \r
158             Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {\r
159 \r
160                 expect(err).to.not.exist();\r
161                 expect(credentials2.user).to.equal('steve');\r
162                 expect(artifacts.ext).to.equal('some-app-data');\r
163                 expect(Hawk.server.authenticatePayload(payload, credentials2, artifacts, req.headers['content-type'])).to.equal(true);\r
164 \r
165                 var res = {\r
166                     headers: {\r
167                         'content-type': 'text/plain'\r
168                     }\r
169                 };\r
170 \r
171                 res.headers['server-authorization'] = Hawk.server.header(credentials2, artifacts);\r
172                 expect(res.headers['server-authorization']).to.exist();\r
173 \r
174                 expect(Hawk.client.authenticate(res, credentials2, artifacts)).to.equal(true);\r
175                 done();\r
176             });\r
177         });\r
178     });\r
179 \r
180     it('generates a header then fails to parse it (missing server header hash)', function (done) {\r
181 \r
182         var req = {\r
183             method: 'POST',\r
184             url: '/resource/4?filter=a',\r
185             headers: {\r
186                 host: 'example.com:8080',\r
187                 'content-type': 'text/plain;x=y'\r
188             }\r
189         };\r
190 \r
191         var payload = 'some not so random text';\r
192 \r
193         credentialsFunc('123456', function (err, credentials1) {\r
194 \r
195             var reqHeader = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data', payload: payload, contentType: req.headers['content-type'] });\r
196             req.headers.authorization = reqHeader.field;\r
197 \r
198             Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {\r
199 \r
200                 expect(err).to.not.exist();\r
201                 expect(credentials2.user).to.equal('steve');\r
202                 expect(artifacts.ext).to.equal('some-app-data');\r
203                 expect(Hawk.server.authenticatePayload(payload, credentials2, artifacts, req.headers['content-type'])).to.equal(true);\r
204 \r
205                 var res = {\r
206                     headers: {\r
207                         'content-type': 'text/plain'\r
208                     }\r
209                 };\r
210 \r
211                 res.headers['server-authorization'] = Hawk.server.header(credentials2, artifacts);\r
212                 expect(res.headers['server-authorization']).to.exist();\r
213 \r
214                 expect(Hawk.client.authenticate(res, credentials2, artifacts, { payload: 'some reply' })).to.equal(false);\r
215                 done();\r
216             });\r
217         });\r
218     });\r
219 \r
220     it('generates a header then successfully parse it (with hash)', function (done) {\r
221 \r
222         var req = {\r
223             method: 'GET',\r
224             url: '/resource/4?filter=a',\r
225             host: 'example.com',\r
226             port: 8080\r
227         };\r
228 \r
229         credentialsFunc('123456', function (err, credentials1) {\r
230 \r
231             req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, payload: 'hola!', ext: 'some-app-data' }).field;\r
232             Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {\r
233 \r
234                 expect(err).to.not.exist();\r
235                 expect(credentials2.user).to.equal('steve');\r
236                 expect(artifacts.ext).to.equal('some-app-data');\r
237                 done();\r
238             });\r
239         });\r
240     });\r
241 \r
242     it('generates a header then successfully parse it then validate payload', function (done) {\r
243 \r
244         var req = {\r
245             method: 'GET',\r
246             url: '/resource/4?filter=a',\r
247             host: 'example.com',\r
248             port: 8080\r
249         };\r
250 \r
251         credentialsFunc('123456', function (err, credentials1) {\r
252 \r
253             req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, payload: 'hola!', ext: 'some-app-data' }).field;\r
254             Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {\r
255 \r
256                 expect(err).to.not.exist();\r
257                 expect(credentials2.user).to.equal('steve');\r
258                 expect(artifacts.ext).to.equal('some-app-data');\r
259                 expect(Hawk.server.authenticatePayload('hola!', credentials2, artifacts)).to.be.true();\r
260                 expect(Hawk.server.authenticatePayload('hello!', credentials2, artifacts)).to.be.false();\r
261                 done();\r
262             });\r
263         });\r
264     });\r
265 \r
266     it('generates a header then successfully parses and validates payload', function (done) {\r
267 \r
268         var req = {\r
269             method: 'GET',\r
270             url: '/resource/4?filter=a',\r
271             host: 'example.com',\r
272             port: 8080\r
273         };\r
274 \r
275         credentialsFunc('123456', function (err, credentials1) {\r
276 \r
277             req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, payload: 'hola!', ext: 'some-app-data' }).field;\r
278             Hawk.server.authenticate(req, credentialsFunc, { payload: 'hola!' }, function (err, credentials2, artifacts) {\r
279 \r
280                 expect(err).to.not.exist();\r
281                 expect(credentials2.user).to.equal('steve');\r
282                 expect(artifacts.ext).to.equal('some-app-data');\r
283                 done();\r
284             });\r
285         });\r
286     });\r
287 \r
288     it('generates a header then successfully parse it (app)', function (done) {\r
289 \r
290         var req = {\r
291             method: 'GET',\r
292             url: '/resource/4?filter=a',\r
293             host: 'example.com',\r
294             port: 8080\r
295         };\r
296 \r
297         credentialsFunc('123456', function (err, credentials1) {\r
298 \r
299             req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data', app: 'asd23ased' }).field;\r
300             Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {\r
301 \r
302                 expect(err).to.not.exist();\r
303                 expect(credentials2.user).to.equal('steve');\r
304                 expect(artifacts.ext).to.equal('some-app-data');\r
305                 expect(artifacts.app).to.equal('asd23ased');\r
306                 done();\r
307             });\r
308         });\r
309     });\r
310 \r
311     it('generates a header then successfully parse it (app, dlg)', function (done) {\r
312 \r
313         var req = {\r
314             method: 'GET',\r
315             url: '/resource/4?filter=a',\r
316             host: 'example.com',\r
317             port: 8080\r
318         };\r
319 \r
320         credentialsFunc('123456', function (err, credentials1) {\r
321 \r
322             req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data', app: 'asd23ased', dlg: '23434szr3q4d' }).field;\r
323             Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {\r
324 \r
325                 expect(err).to.not.exist();\r
326                 expect(credentials2.user).to.equal('steve');\r
327                 expect(artifacts.ext).to.equal('some-app-data');\r
328                 expect(artifacts.app).to.equal('asd23ased');\r
329                 expect(artifacts.dlg).to.equal('23434szr3q4d');\r
330                 done();\r
331             });\r
332         });\r
333     });\r
334 \r
335     it('generates a header then fail authentication due to bad hash', function (done) {\r
336 \r
337         var req = {\r
338             method: 'GET',\r
339             url: '/resource/4?filter=a',\r
340             host: 'example.com',\r
341             port: 8080\r
342         };\r
343 \r
344         credentialsFunc('123456', function (err, credentials1) {\r
345 \r
346             req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, payload: 'hola!', ext: 'some-app-data' }).field;\r
347             Hawk.server.authenticate(req, credentialsFunc, { payload: 'byebye!' }, function (err, credentials2, artifacts) {\r
348 \r
349                 expect(err).to.exist();\r
350                 expect(err.output.payload.message).to.equal('Bad payload hash');\r
351                 done();\r
352             });\r
353         });\r
354     });\r
355 \r
356     it('generates a header for one resource then fail to authenticate another', function (done) {\r
357 \r
358         var req = {\r
359             method: 'GET',\r
360             url: '/resource/4?filter=a',\r
361             host: 'example.com',\r
362             port: 8080\r
363         };\r
364 \r
365         credentialsFunc('123456', function (err, credentials1) {\r
366 \r
367             req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data' }).field;\r
368             req.url = '/something/else';\r
369 \r
370             Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {\r
371 \r
372                 expect(err).to.exist();\r
373                 expect(credentials2).to.exist();\r
374                 done();\r
375             });\r
376         });\r
377     });\r
378 });\r