3 var Url = require('url');
4 var Code = require('code');
5 var Hawk = require('../lib');
6 var Hoek = require('hoek');
7 var Lab = require('lab');
17 var lab = exports.lab = Lab.script();
18 var describe = lab.experiment;
20 var expect = Code.expect;
23 describe('Server', function () {
25 var credentialsFunc = function (id, callback) {
29 key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
30 algorithm: (id === '1' ? 'sha1' : 'sha256'),
34 return callback(null, credentials);
37 describe('authenticate()', function () {
39 it('parses a valid authentication header (sha1)', function (done) {
43 url: '/resource/4?filter=a',
46 authorization: 'Hawk id="1", ts="1353788437", nonce="k3j4h2", mac="zy79QQ5/EYFmQqutVnYb73gAc/U=", ext="hello"'
49 Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
51 expect(err).to.not.exist();
52 expect(credentials.user).to.equal('steve');
57 it('parses a valid authentication header (sha256)', function (done) {
61 url: '/resource/1?b=1&a=2',
64 authorization: 'Hawk id="dh37fgj492je", ts="1353832234", nonce="j4h3g2", mac="m8r1rHbXN6NgO+KIIhjO7sFRyd78RNGVUwehe8Cp2dU=", ext="some-app-data"'
67 Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353832234000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
69 expect(err).to.not.exist();
70 expect(credentials.user).to.equal('steve');
75 it('parses a valid authentication header (host override)', function (done) {
79 url: '/resource/4?filter=a',
81 host: 'example1.com:8080',
82 authorization: 'Hawk id="1", ts="1353788437", nonce="k3j4h2", mac="zy79QQ5/EYFmQqutVnYb73gAc/U=", ext="hello"'
86 Hawk.server.authenticate(req, credentialsFunc, { host: 'example.com', localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
88 expect(err).to.not.exist();
89 expect(credentials.user).to.equal('steve');
94 it('parses a valid authentication header (host port override)', function (done) {
98 url: '/resource/4?filter=a',
100 host: 'example1.com:80',
101 authorization: 'Hawk id="1", ts="1353788437", nonce="k3j4h2", mac="zy79QQ5/EYFmQqutVnYb73gAc/U=", ext="hello"'
105 Hawk.server.authenticate(req, credentialsFunc, { host: 'example.com', port: 8080, localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
107 expect(err).to.not.exist();
108 expect(credentials.user).to.equal('steve');
113 it('parses a valid authentication header (POST with payload)', function (done) {
117 url: '/resource/4?filter=a',
120 authorization: 'Hawk id="123456", ts="1357926341", nonce="1AwuJD", hash="qAiXIVv+yjDATneWxZP2YCTa9aHRgQdnH9b3Wc+o3dg=", ext="some-app-data", mac="UeYcj5UoTVaAWXNvJfLVia7kU3VabxCqrccXP8sUGC4="'
123 Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1357926341000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
125 expect(err).to.not.exist();
126 expect(credentials.user).to.equal('steve');
131 it('errors on missing hash', function (done) {
135 url: '/resource/1?b=1&a=2',
138 authorization: 'Hawk id="dh37fgj492je", ts="1353832234", nonce="j4h3g2", mac="m8r1rHbXN6NgO+KIIhjO7sFRyd78RNGVUwehe8Cp2dU=", ext="some-app-data"'
141 Hawk.server.authenticate(req, credentialsFunc, { payload: 'body', localtimeOffsetMsec: 1353832234000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
143 expect(err).to.exist();
144 expect(err.output.payload.message).to.equal('Missing required payload hash');
149 it('errors on a stale timestamp', function (done) {
153 url: '/resource/4?filter=a',
156 authorization: 'Hawk id="123456", ts="1362337299", nonce="UzmxSs", ext="some-app-data", mac="wnNUxchvvryMH2RxckTdZ/gY3ijzvccx4keVvELC61w="'
159 Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
161 expect(err).to.exist();
162 expect(err.output.payload.message).to.equal('Stale timestamp');
163 var header = err.output.headers['WWW-Authenticate'];
164 var ts = header.match(/^Hawk ts\=\"(\d+)\"\, tsm\=\"([^\"]+)\"\, error=\"Stale timestamp\"$/);
165 var now = Hawk.utils.now();
166 expect(parseInt(ts[1], 10) * 1000).to.be.within(now - 1000, now + 1000);
170 'www-authenticate': header
174 expect(Hawk.client.authenticate(res, credentials, artifacts)).to.equal(true);
179 it('errors on a replay', function (done) {
183 url: '/resource/4?filter=a',
186 authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="bXx7a7p1h9QYQNZ8x7QhvDQym8ACgab4m3lVSFn4DBw=", ext="hello"'
189 var memoryCache = {};
191 localtimeOffsetMsec: 1353788437000 - Hawk.utils.now(),
192 nonceFunc: function (key, nonce, ts, callback) {
194 if (memoryCache[key + nonce]) {
195 return callback(new Error());
198 memoryCache[key + nonce] = true;
203 Hawk.server.authenticate(req, credentialsFunc, options, function (err, credentials1, artifacts1) {
205 expect(err).to.not.exist();
206 expect(credentials1.user).to.equal('steve');
208 Hawk.server.authenticate(req, credentialsFunc, options, function (err, credentials2, artifacts2) {
210 expect(err).to.exist();
211 expect(err.output.payload.message).to.equal('Invalid nonce');
217 it('does not error on nonce collision if keys differ', function (done) {
221 url: '/resource/4?filter=a',
224 authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="bXx7a7p1h9QYQNZ8x7QhvDQym8ACgab4m3lVSFn4DBw=", ext="hello"'
229 url: '/resource/4?filter=a',
232 authorization: 'Hawk id="456", ts="1353788437", nonce="k3j4h2", mac="LXfmTnRzrLd9TD7yfH+4se46Bx6AHyhpM94hLCiNia4=", ext="hello"'
235 var credentialsFuncion = function (id, callback) {
240 key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
241 algorithm: (id === '1' ? 'sha1' : 'sha256'),
246 key: 'xrunpaw3489ruxnpa98w4rxnwerxhqb98rpaxn39848',
247 algorithm: (id === '1' ? 'sha1' : 'sha256'),
252 return callback(null, credentials[id]);
255 var memoryCache = {};
257 localtimeOffsetMsec: 1353788437000 - Hawk.utils.now(),
258 nonceFunc: function (key, nonce, ts, callback) {
260 if (memoryCache[key + nonce]) {
261 return callback(new Error());
264 memoryCache[key + nonce] = true;
269 Hawk.server.authenticate(reqSteve, credentialsFuncion, options, function (err, credentials1, artifacts1) {
271 expect(err).to.not.exist();
272 expect(credentials1.user).to.equal('steve');
274 Hawk.server.authenticate(reqBob, credentialsFuncion, options, function (err, credentials2, artifacts2) {
276 expect(err).to.not.exist();
277 expect(credentials2.user).to.equal('bob');
283 it('errors on an invalid authentication header: wrong scheme', function (done) {
287 url: '/resource/4?filter=a',
290 authorization: 'Basic asdasdasdasd'
293 Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
295 expect(err).to.exist();
296 expect(err.output.payload.message).to.not.exist();
301 it('errors on an invalid authentication header: no scheme', function (done) {
305 url: '/resource/4?filter=a',
311 Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
313 expect(err).to.exist();
314 expect(err.output.payload.message).to.equal('Invalid header syntax');
319 it('errors on an missing authorization header', function (done) {
323 url: '/resource/4?filter=a',
328 Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
330 expect(err).to.exist();
331 expect(err.isMissing).to.equal(true);
336 it('errors on an missing host header', function (done) {
340 url: '/resource/4?filter=a',
342 authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
346 Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
348 expect(err).to.exist();
349 expect(err.output.payload.message).to.equal('Invalid Host header');
354 it('errors on an missing authorization attribute (id)', function (done) {
358 url: '/resource/4?filter=a',
361 authorization: 'Hawk ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
364 Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
366 expect(err).to.exist();
367 expect(err.output.payload.message).to.equal('Missing attributes');
372 it('errors on an missing authorization attribute (ts)', function (done) {
376 url: '/resource/4?filter=a',
379 authorization: 'Hawk id="123", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
382 Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
384 expect(err).to.exist();
385 expect(err.output.payload.message).to.equal('Missing attributes');
390 it('errors on an missing authorization attribute (nonce)', function (done) {
394 url: '/resource/4?filter=a',
397 authorization: 'Hawk id="123", ts="1353788437", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
400 Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
402 expect(err).to.exist();
403 expect(err.output.payload.message).to.equal('Missing attributes');
408 it('errors on an missing authorization attribute (mac)', function (done) {
412 url: '/resource/4?filter=a',
415 authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", ext="hello"'
418 Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
420 expect(err).to.exist();
421 expect(err.output.payload.message).to.equal('Missing attributes');
426 it('errors on an unknown authorization attribute', function (done) {
430 url: '/resource/4?filter=a',
433 authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", x="3", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
436 Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
438 expect(err).to.exist();
439 expect(err.output.payload.message).to.equal('Unknown attribute: x');
444 it('errors on an bad authorization header format', function (done) {
448 url: '/resource/4?filter=a',
451 authorization: 'Hawk id="123\\", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
454 Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
456 expect(err).to.exist();
457 expect(err.output.payload.message).to.equal('Bad header format');
462 it('errors on an bad authorization attribute value', function (done) {
466 url: '/resource/4?filter=a',
469 authorization: 'Hawk id="\t", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
472 Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
474 expect(err).to.exist();
475 expect(err.output.payload.message).to.equal('Bad attribute value: id');
480 it('errors on an empty authorization attribute value', function (done) {
484 url: '/resource/4?filter=a',
487 authorization: 'Hawk id="", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
490 Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
492 expect(err).to.exist();
493 expect(err.output.payload.message).to.equal('Bad attribute value: id');
498 it('errors on duplicated authorization attribute key', function (done) {
502 url: '/resource/4?filter=a',
505 authorization: 'Hawk id="123", id="456", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
508 Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
510 expect(err).to.exist();
511 expect(err.output.payload.message).to.equal('Duplicate attribute: id');
516 it('errors on an invalid authorization header format', function (done) {
520 url: '/resource/4?filter=a',
523 authorization: 'Hawk'
526 Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
528 expect(err).to.exist();
529 expect(err.output.payload.message).to.equal('Invalid header syntax');
534 it('errors on an bad host header (missing host)', function (done) {
538 url: '/resource/4?filter=a',
541 authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
545 Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
547 expect(err).to.exist();
548 expect(err.output.payload.message).to.equal('Invalid Host header');
553 it('errors on an bad host header (pad port)', function (done) {
557 url: '/resource/4?filter=a',
559 host: 'example.com:something',
560 authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
564 Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
566 expect(err).to.exist();
567 expect(err.output.payload.message).to.equal('Invalid Host header');
572 it('errors on credentialsFunc error', function (done) {
576 url: '/resource/4?filter=a',
579 authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
582 var credentialsFuncion = function (id, callback) {
584 return callback(new Error('Unknown user'));
587 Hawk.server.authenticate(req, credentialsFuncion, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
589 expect(err).to.exist();
590 expect(err.message).to.equal('Unknown user');
595 it('errors on credentialsFunc error (with credentials)', function (done) {
599 url: '/resource/4?filter=a',
602 authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
605 var credentialsFuncion = function (id, callback) {
607 return callback(new Error('Unknown user'), { some: 'value' });
610 Hawk.server.authenticate(req, credentialsFuncion, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
612 expect(err).to.exist();
613 expect(err.message).to.equal('Unknown user');
614 expect(credentials.some).to.equal('value');
619 it('errors on missing credentials', function (done) {
623 url: '/resource/4?filter=a',
626 authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
629 var credentialsFuncion = function (id, callback) {
631 return callback(null, null);
634 Hawk.server.authenticate(req, credentialsFuncion, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
636 expect(err).to.exist();
637 expect(err.output.payload.message).to.equal('Unknown credentials');
642 it('errors on invalid credentials (id)', function (done) {
646 url: '/resource/4?filter=a',
649 authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
652 var credentialsFuncion = function (id, callback) {
655 key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
659 return callback(null, credentials);
662 Hawk.server.authenticate(req, credentialsFuncion, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
664 expect(err).to.exist();
665 expect(err.message).to.equal('Invalid credentials');
666 expect(err.output.payload.message).to.equal('An internal server error occurred');
671 it('errors on invalid credentials (key)', function (done) {
675 url: '/resource/4?filter=a',
678 authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
681 var credentialsFuncion = function (id, callback) {
684 id: '23434d3q4d5345d',
688 return callback(null, credentials);
691 Hawk.server.authenticate(req, credentialsFuncion, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
693 expect(err).to.exist();
694 expect(err.message).to.equal('Invalid credentials');
695 expect(err.output.payload.message).to.equal('An internal server error occurred');
700 it('errors on unknown credentials algorithm', function (done) {
704 url: '/resource/4?filter=a',
707 authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
710 var credentialsFuncion = function (id, callback) {
713 key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
714 algorithm: 'hmac-sha-0',
718 return callback(null, credentials);
721 Hawk.server.authenticate(req, credentialsFuncion, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
723 expect(err).to.exist();
724 expect(err.message).to.equal('Unknown algorithm');
725 expect(err.output.payload.message).to.equal('An internal server error occurred');
730 it('errors on unknown bad mac', function (done) {
734 url: '/resource/4?filter=a',
737 authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcU4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
740 var credentialsFuncion = function (id, callback) {
743 key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
748 return callback(null, credentials);
751 Hawk.server.authenticate(req, credentialsFuncion, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
753 expect(err).to.exist();
754 expect(err.output.payload.message).to.equal('Bad mac');
760 describe('header()', function () {
762 it('generates header', function (done) {
766 key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
775 resource: '/resource/4?filter=a',
778 hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
779 ext: 'some-app-data',
780 mac: 'dvIvMThwi28J61Jc3P0ryAhuKpanU63GXdx6hkmQkJA=',
784 var header = Hawk.server.header(credentials, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });
785 expect(header).to.equal('Hawk mac=\"n14wVJK4cOxAytPUMc5bPezQzuJGl5n7MYXhFQgEKsE=\", hash=\"f9cDF/TDm7TkYRLnGwRMfeDzT6LixQVLvrIKhh0vgmM=\", ext=\"response-specific\"');
789 it('generates header (empty payload)', function (done) {
793 key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
802 resource: '/resource/4?filter=a',
805 hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
806 ext: 'some-app-data',
807 mac: 'dvIvMThwi28J61Jc3P0ryAhuKpanU63GXdx6hkmQkJA=',
811 var header = Hawk.server.header(credentials, artifacts, { payload: '', contentType: 'text/plain', ext: 'response-specific' });
812 expect(header).to.equal('Hawk mac=\"i8/kUBDx0QF+PpCtW860kkV/fa9dbwEoe/FpGUXowf0=\", hash=\"q/t+NNAkQZNlq/aAD6PlexImwQTxwgT2MahfTa9XRLA=\", ext=\"response-specific\"');
816 it('generates header (pre calculated hash)', function (done) {
820 key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
829 resource: '/resource/4?filter=a',
832 hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
833 ext: 'some-app-data',
834 mac: 'dvIvMThwi28J61Jc3P0ryAhuKpanU63GXdx6hkmQkJA=',
838 var options = { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' };
839 options.hash = Hawk.crypto.calculatePayloadHash(options.payload, credentials.algorithm, options.contentType);
840 var header = Hawk.server.header(credentials, artifacts, options);
841 expect(header).to.equal('Hawk mac=\"n14wVJK4cOxAytPUMc5bPezQzuJGl5n7MYXhFQgEKsE=\", hash=\"f9cDF/TDm7TkYRLnGwRMfeDzT6LixQVLvrIKhh0vgmM=\", ext=\"response-specific\"');
845 it('generates header (null ext)', function (done) {
849 key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
858 resource: '/resource/4?filter=a',
861 hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
862 mac: 'dvIvMThwi28J61Jc3P0ryAhuKpanU63GXdx6hkmQkJA=',
866 var header = Hawk.server.header(credentials, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: null });
867 expect(header).to.equal('Hawk mac=\"6PrybJTJs20jsgBw5eilXpcytD8kUbaIKNYXL+6g0ns=\", hash=\"f9cDF/TDm7TkYRLnGwRMfeDzT6LixQVLvrIKhh0vgmM=\"');
871 it('errors on missing artifacts', function (done) {
875 key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
880 var header = Hawk.server.header(credentials, null, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });
881 expect(header).to.equal('');
885 it('errors on invalid artifacts', function (done) {
889 key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
894 var header = Hawk.server.header(credentials, 5, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });
895 expect(header).to.equal('');
899 it('errors on missing credentials', function (done) {
905 resource: '/resource/4?filter=a',
908 hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
909 ext: 'some-app-data',
910 mac: 'dvIvMThwi28J61Jc3P0ryAhuKpanU63GXdx6hkmQkJA=',
914 var header = Hawk.server.header(null, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });
915 expect(header).to.equal('');
919 it('errors on invalid credentials (key)', function (done) {
931 resource: '/resource/4?filter=a',
934 hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
935 ext: 'some-app-data',
936 mac: 'dvIvMThwi28J61Jc3P0ryAhuKpanU63GXdx6hkmQkJA=',
940 var header = Hawk.server.header(credentials, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });
941 expect(header).to.equal('');
945 it('errors on invalid algorithm', function (done) {
949 key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
958 resource: '/resource/4?filter=a',
961 hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
962 ext: 'some-app-data',
963 mac: 'dvIvMThwi28J61Jc3P0ryAhuKpanU63GXdx6hkmQkJA=',
967 var header = Hawk.server.header(credentials, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });
968 expect(header).to.equal('');
973 describe('authenticateBewit()', function () {
975 it('errors on uri too long', function (done) {
978 for (var i = 0; i < 5000; ++i) {
987 authorization: 'Hawk id="1", ts="1353788437", nonce="k3j4h2", mac="zy79QQ5/EYFmQqutVnYb73gAc/U=", ext="hello"'
990 Hawk.server.authenticateBewit(req, credentialsFunc, {}, function (err, credentials, bewit) {
992 expect(err).to.exist();
993 expect(err.output.statusCode).to.equal(400);
994 expect(err.message).to.equal('Resource path exceeds max length');
1000 describe('authenticateMessage()', function () {
1002 it('errors on invalid authorization (ts)', function (done) {
1004 credentialsFunc('123456', function (err, credentials1) {
1006 var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
1009 Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, {}, function (err, credentials2) {
1011 expect(err).to.exist();
1012 expect(err.message).to.equal('Invalid authorization');
1018 it('errors on invalid authorization (nonce)', function (done) {
1020 credentialsFunc('123456', function (err, credentials1) {
1022 var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
1025 Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, {}, function (err, credentials2) {
1027 expect(err).to.exist();
1028 expect(err.message).to.equal('Invalid authorization');
1034 it('errors on invalid authorization (hash)', function (done) {
1036 credentialsFunc('123456', function (err, credentials1) {
1038 var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
1041 Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, {}, function (err, credentials2) {
1043 expect(err).to.exist();
1044 expect(err.message).to.equal('Invalid authorization');
1050 it('errors with credentials', function (done) {
1052 credentialsFunc('123456', function (err, credentials1) {
1054 var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
1056 Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, function (id, callback) {
1058 callback(new Error('something'), { some: 'value' });
1059 }, {}, function (err, credentials2) {
1061 expect(err).to.exist();
1062 expect(err.message).to.equal('something');
1063 expect(credentials2.some).to.equal('value');
1069 it('errors on nonce collision', function (done) {
1071 credentialsFunc('123456', function (err, credentials1) {
1073 var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
1074 Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, {
1075 nonceFunc: function (key, nonce, ts, nonceCallback) {
1077 nonceCallback(true);
1079 }, function (err, credentials2) {
1081 expect(err).to.exist();
1082 expect(err.message).to.equal('Invalid nonce');
1088 it('should generate an authorization then successfully parse it', function (done) {
1090 credentialsFunc('123456', function (err, credentials1) {
1092 var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
1093 expect(auth).to.exist();
1095 Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, {}, function (err, credentials2) {
1097 expect(err).to.not.exist();
1098 expect(credentials2.user).to.equal('steve');
1104 it('should fail authorization on mismatching host', function (done) {
1106 credentialsFunc('123456', function (err, credentials1) {
1108 var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
1109 expect(auth).to.exist();
1111 Hawk.server.authenticateMessage('example1.com', 8080, 'some message', auth, credentialsFunc, {}, function (err, credentials2) {
1113 expect(err).to.exist();
1114 expect(err.message).to.equal('Bad mac');
1120 it('should fail authorization on stale timestamp', function (done) {
1122 credentialsFunc('123456', function (err, credentials1) {
1124 var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
1125 expect(auth).to.exist();
1127 Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, { localtimeOffsetMsec: 100000 }, function (err, credentials2) {
1129 expect(err).to.exist();
1130 expect(err.message).to.equal('Stale timestamp');
1136 it('overrides timestampSkewSec', function (done) {
1138 credentialsFunc('123456', function (err, credentials1) {
1140 var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1, localtimeOffsetMsec: 100000 });
1141 expect(auth).to.exist();
1143 Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, { timestampSkewSec: 500 }, function (err, credentials2) {
1145 expect(err).to.not.exist();
1151 it('should fail authorization on invalid authorization', function (done) {
1153 credentialsFunc('123456', function (err, credentials1) {
1155 var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
1156 expect(auth).to.exist();
1159 Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, {}, function (err, credentials2) {
1161 expect(err).to.exist();
1162 expect(err.message).to.equal('Invalid authorization');
1168 it('should fail authorization on bad hash', function (done) {
1170 credentialsFunc('123456', function (err, credentials1) {
1172 var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
1173 expect(auth).to.exist();
1175 Hawk.server.authenticateMessage('example.com', 8080, 'some message1', auth, credentialsFunc, {}, function (err, credentials2) {
1177 expect(err).to.exist();
1178 expect(err.message).to.equal('Bad message hash');
1184 it('should fail authorization on nonce error', function (done) {
1186 credentialsFunc('123456', function (err, credentials1) {
1188 var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
1189 expect(auth).to.exist();
1191 Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, {
1192 nonceFunc: function (key, nonce, ts, callback) {
1194 callback(new Error('kaboom'));
1196 }, function (err, credentials2) {
1198 expect(err).to.exist();
1199 expect(err.message).to.equal('Invalid nonce');
1205 it('should fail authorization on credentials error', function (done) {
1207 credentialsFunc('123456', function (err, credentials1) {
1209 var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
1210 expect(auth).to.exist();
1212 var errFunc = function (id, callback) {
1214 callback(new Error('kablooey'));
1217 Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, errFunc, {}, function (err, credentials2) {
1219 expect(err).to.exist();
1220 expect(err.message).to.equal('kablooey');
1226 it('should fail authorization on missing credentials', function (done) {
1228 credentialsFunc('123456', function (err, credentials1) {
1230 var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
1231 expect(auth).to.exist();
1233 var errFunc = function (id, callback) {
1238 Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, errFunc, {}, function (err, credentials2) {
1240 expect(err).to.exist();
1241 expect(err.message).to.equal('Unknown credentials');
1247 it('should fail authorization on invalid credentials', function (done) {
1249 credentialsFunc('123456', function (err, credentials1) {
1251 var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
1252 expect(auth).to.exist();
1254 var errFunc = function (id, callback) {
1259 Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, errFunc, {}, function (err, credentials2) {
1261 expect(err).to.exist();
1262 expect(err.message).to.equal('Invalid credentials');
1268 it('should fail authorization on invalid credentials algorithm', function (done) {
1270 credentialsFunc('123456', function (err, credentials1) {
1272 var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
1273 expect(auth).to.exist();
1275 var errFunc = function (id, callback) {
1277 callback(null, { key: '123', algorithm: '456' });
1280 Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, errFunc, {}, function (err, credentials2) {
1282 expect(err).to.exist();
1283 expect(err.message).to.equal('Unknown algorithm');
1289 it('should fail on missing host', function (done) {
1291 credentialsFunc('123456', function (err, credentials) {
1293 var auth = Hawk.client.message(null, 8080, 'some message', { credentials: credentials });
1294 expect(auth).to.not.exist();
1299 it('should fail on missing credentials', function (done) {
1301 var auth = Hawk.client.message('example.com', 8080, 'some message', {});
1302 expect(auth).to.not.exist();
1306 it('should fail on invalid algorithm', function (done) {
1308 credentialsFunc('123456', function (err, credentials) {
1310 var creds = Hoek.clone(credentials);
1311 creds.algorithm = 'blah';
1312 var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: creds });
1313 expect(auth).to.not.exist();
1319 describe('authenticatePayloadHash()', function () {
1321 it('checks payload hash', function (done) {
1323 expect(Hawk.server.authenticatePayloadHash('abcdefg', { hash: 'abcdefg' })).to.equal(true);
1324 expect(Hawk.server.authenticatePayloadHash('1234567', { hash: 'abcdefg' })).to.equal(false);