3 var Url = require('url');
4 var Code = require('code');
5 var Hawk = require('../lib');
6 var Lab = require('lab');
16 var lab = exports.lab = Lab.script();
17 var describe = lab.experiment;
19 var expect = Code.expect;
22 describe('Client', function () {
24 describe('header()', function () {
26 it('returns a valid authorization header (sha1)', function (done) {
34 var header = Hawk.client.header('http://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, ext: 'Bazinga!', timestamp: 1353809207, nonce: 'Ygvqdz', payload: 'something to write about' }).field;
35 expect(header).to.equal('Hawk id="123456", ts="1353809207", nonce="Ygvqdz", hash="bsvY3IfUllw6V5rvk4tStEvpBhE=", ext="Bazinga!", mac="qbf1ZPG/r/e06F4ht+T77LXi5vw="');
39 it('returns a valid authorization header (sha256)', function (done) {
47 var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, ext: 'Bazinga!', timestamp: 1353809207, nonce: 'Ygvqdz', payload: 'something to write about', contentType: 'text/plain' }).field;
48 expect(header).to.equal('Hawk id="123456", ts="1353809207", nonce="Ygvqdz", hash="2QfCt3GuY9HQnHWyWD3wX68ZOKbynqlfYmuO2ZBRqtY=", ext="Bazinga!", mac="q1CwFoSHzPZSkbIvl0oYlD+91rBUEvFk763nMjMndj8="');
52 it('returns a valid authorization header (no ext)', function (done) {
60 var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, timestamp: 1353809207, nonce: 'Ygvqdz', payload: 'something to write about', contentType: 'text/plain' }).field;
61 expect(header).to.equal('Hawk id="123456", ts="1353809207", nonce="Ygvqdz", hash="2QfCt3GuY9HQnHWyWD3wX68ZOKbynqlfYmuO2ZBRqtY=", mac="HTgtd0jPI6E4izx8e4OHdO36q00xFCU0FolNq3RiCYs="');
65 it('returns a valid authorization header (null ext)', function (done) {
73 var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, timestamp: 1353809207, nonce: 'Ygvqdz', payload: 'something to write about', contentType: 'text/plain', ext: null }).field;
74 expect(header).to.equal('Hawk id="123456", ts="1353809207", nonce="Ygvqdz", hash="2QfCt3GuY9HQnHWyWD3wX68ZOKbynqlfYmuO2ZBRqtY=", mac="HTgtd0jPI6E4izx8e4OHdO36q00xFCU0FolNq3RiCYs="');
78 it('returns a valid authorization header (empty payload)', function (done) {
86 var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, timestamp: 1353809207, nonce: 'Ygvqdz', payload: '', contentType: 'text/plain' }).field;
87 expect(header).to.equal('Hawk id=\"123456\", ts=\"1353809207\", nonce=\"Ygvqdz\", hash=\"q/t+NNAkQZNlq/aAD6PlexImwQTxwgT2MahfTa9XRLA=\", mac=\"U5k16YEzn3UnBHKeBzsDXn067Gu3R4YaY6xOt9PYRZM=\"');
91 it('returns a valid authorization header (pre hashed payload)', function (done) {
99 var options = { credentials: credentials, timestamp: 1353809207, nonce: 'Ygvqdz', payload: 'something to write about', contentType: 'text/plain' };
100 options.hash = Hawk.crypto.calculatePayloadHash(options.payload, credentials.algorithm, options.contentType);
101 var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', options).field;
102 expect(header).to.equal('Hawk id="123456", ts="1353809207", nonce="Ygvqdz", hash="2QfCt3GuY9HQnHWyWD3wX68ZOKbynqlfYmuO2ZBRqtY=", mac="HTgtd0jPI6E4izx8e4OHdO36q00xFCU0FolNq3RiCYs="');
106 it('errors on missing uri', function (done) {
108 var header = Hawk.client.header('', 'POST');
109 expect(header.field).to.equal('');
110 expect(header.err).to.equal('Invalid argument type');
114 it('errors on invalid uri', function (done) {
116 var header = Hawk.client.header(4, 'POST');
117 expect(header.field).to.equal('');
118 expect(header.err).to.equal('Invalid argument type');
122 it('errors on missing method', function (done) {
124 var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', '');
125 expect(header.field).to.equal('');
126 expect(header.err).to.equal('Invalid argument type');
130 it('errors on invalid method', function (done) {
132 var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 5);
133 expect(header.field).to.equal('');
134 expect(header.err).to.equal('Invalid argument type');
138 it('errors on missing options', function (done) {
140 var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST');
141 expect(header.field).to.equal('');
142 expect(header.err).to.equal('Invalid argument type');
146 it('errors on invalid credentials (id)', function (done) {
149 key: '2983d45yun89q',
153 var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, ext: 'Bazinga!', timestamp: 1353809207 });
154 expect(header.field).to.equal('');
155 expect(header.err).to.equal('Invalid credential object');
159 it('errors on missing credentials', function (done) {
161 var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { ext: 'Bazinga!', timestamp: 1353809207 });
162 expect(header.field).to.equal('');
163 expect(header.err).to.equal('Invalid credential object');
167 it('errors on invalid credentials', function (done) {
174 var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, ext: 'Bazinga!', timestamp: 1353809207 });
175 expect(header.field).to.equal('');
176 expect(header.err).to.equal('Invalid credential object');
180 it('errors on invalid algorithm', function (done) {
184 key: '2983d45yun89q',
185 algorithm: 'hmac-sha-0'
188 var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, payload: 'something, anything!', ext: 'Bazinga!', timestamp: 1353809207 });
189 expect(header.field).to.equal('');
190 expect(header.err).to.equal('Unknown algorithm');
195 describe('authenticate()', function () {
197 it('returns false on invalid header', function (done) {
201 'server-authorization': 'Hawk mac="abc", bad="xyz"'
205 expect(Hawk.client.authenticate(res, {})).to.equal(false);
209 it('returns false on invalid mac', function (done) {
213 'content-type': 'text/plain',
214 'server-authorization': 'Hawk mac="_IJRsMl/4oL+nn+vKoeVZPdCHXB4yJkNnBbTbHFZUYE=", hash="f9cDF/TDm7TkYRLnGwRMfeDzT6LixQVLvrIKhh0vgmM=", ext="response-specific"'
222 resource: '/resource/4?filter=a',
225 hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
226 ext: 'some-app-data',
229 mac: 'BlmSe8K+pbKIb6YsZCnt4E1GrYvY1AaYayNR82dGpIk=',
235 key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
240 expect(Hawk.client.authenticate(res, credentials, artifacts)).to.equal(false);
244 it('returns true on ignoring hash', function (done) {
248 'content-type': 'text/plain',
249 'server-authorization': 'Hawk mac="XIJRsMl/4oL+nn+vKoeVZPdCHXB4yJkNnBbTbHFZUYE=", hash="f9cDF/TDm7TkYRLnGwRMfeDzT6LixQVLvrIKhh0vgmM=", ext="response-specific"'
257 resource: '/resource/4?filter=a',
260 hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
261 ext: 'some-app-data',
264 mac: 'BlmSe8K+pbKIb6YsZCnt4E1GrYvY1AaYayNR82dGpIk=',
270 key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
275 expect(Hawk.client.authenticate(res, credentials, artifacts)).to.equal(true);
279 it('fails on invalid WWW-Authenticate header format', function (done) {
281 var header = 'Hawk ts="1362346425875", tsm="PhwayS28vtnn3qbv0mqRBYSXebN/zggEtucfeZ620Zo=", x="Stale timestamp"';
282 expect(Hawk.client.authenticate({ headers: { 'www-authenticate': header } }, {})).to.equal(false);
286 it('fails on invalid WWW-Authenticate header format', function (done) {
290 key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
295 var header = 'Hawk ts="1362346425875", tsm="hwayS28vtnn3qbv0mqRBYSXebN/zggEtucfeZ620Zo=", error="Stale timestamp"';
296 expect(Hawk.client.authenticate({ headers: { 'www-authenticate': header } }, credentials)).to.equal(false);
300 it('skips tsm validation when missing ts', function (done) {
302 var header = 'Hawk error="Stale timestamp"';
303 expect(Hawk.client.authenticate({ headers: { 'www-authenticate': header } }, {})).to.equal(true);
308 describe('message()', function () {
310 it('generates authorization', function (done) {
314 key: '2983d45yun89q',
318 var auth = Hawk.client.message('example.com', 80, 'I am the boodyman', { credentials: credentials, timestamp: 1353809207, nonce: 'abc123' });
319 expect(auth).to.exist();
320 expect(auth.ts).to.equal(1353809207);
321 expect(auth.nonce).to.equal('abc123');
325 it('errors on invalid host', function (done) {
329 key: '2983d45yun89q',
333 var auth = Hawk.client.message(5, 80, 'I am the boodyman', { credentials: credentials, timestamp: 1353809207, nonce: 'abc123' });
334 expect(auth).to.not.exist();
338 it('errors on invalid port', function (done) {
342 key: '2983d45yun89q',
346 var auth = Hawk.client.message('example.com', '80', 'I am the boodyman', { credentials: credentials, timestamp: 1353809207, nonce: 'abc123' });
347 expect(auth).to.not.exist();
351 it('errors on missing host', function (done) {
355 key: '2983d45yun89q',
359 var auth = Hawk.client.message('example.com', 0, 'I am the boodyman', { credentials: credentials, timestamp: 1353809207, nonce: 'abc123' });
360 expect(auth).to.not.exist();
364 it('errors on null message', function (done) {
368 key: '2983d45yun89q',
372 var auth = Hawk.client.message('example.com', 80, null, { credentials: credentials, timestamp: 1353809207, nonce: 'abc123' });
373 expect(auth).to.not.exist();
377 it('errors on missing message', function (done) {
381 key: '2983d45yun89q',
385 var auth = Hawk.client.message('example.com', 80, undefined, { credentials: credentials, timestamp: 1353809207, nonce: 'abc123' });
386 expect(auth).to.not.exist();
390 it('errors on invalid message', function (done) {
394 key: '2983d45yun89q',
398 var auth = Hawk.client.message('example.com', 80, 5, { credentials: credentials, timestamp: 1353809207, nonce: 'abc123' });
399 expect(auth).to.not.exist();
403 it('errors on missing options', function (done) {
407 key: '2983d45yun89q',
411 var auth = Hawk.client.message('example.com', 80, 'I am the boodyman');
412 expect(auth).to.not.exist();
416 it('errors on invalid credentials (id)', function (done) {
419 key: '2983d45yun89q',
423 var auth = Hawk.client.message('example.com', 80, 'I am the boodyman', { credentials: credentials, timestamp: 1353809207, nonce: 'abc123' });
424 expect(auth).to.not.exist();
428 it('errors on invalid credentials (key)', function (done) {
435 var auth = Hawk.client.message('example.com', 80, 'I am the boodyman', { credentials: credentials, timestamp: 1353809207, nonce: 'abc123' });
436 expect(auth).to.not.exist();