| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838 | // Load modulesvar Http = require('http');var Url = require('url');var Code = require('code');var Hawk = require('../lib');var Hoek = require('hoek');var Lab = require('lab');// Declare internalsvar internals = {};// Test shortcutsvar lab = exports.lab = Lab.script();var describe = lab.experiment;var it = lab.test;var expect = Code.expect;describe('Uri', function () {    var credentialsFunc = function (id, callback) {        var credentials = {            id: id,            key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',            algorithm: (id === '1' ? 'sha1' : 'sha256'),            user: 'steve'        };        return callback(null, credentials);    };    it('should generate a bewit then successfully authenticate it', function (done) {        var req = {            method: 'GET',            url: '/resource/4?a=1&b=2',            host: 'example.com',            port: 80        };        credentialsFunc('123456', function (err, credentials1) {            var bewit = Hawk.uri.getBewit('http://example.com/resource/4?a=1&b=2', { credentials: credentials1, ttlSec: 60 * 60 * 24 * 365 * 100, ext: 'some-app-data' });            req.url += '&bewit=' + bewit;            Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials2, attributes) {                expect(err).to.not.exist();                expect(credentials2.user).to.equal('steve');                expect(attributes.ext).to.equal('some-app-data');                done();            });        });    });    it('should generate a bewit then successfully authenticate it (no ext)', function (done) {        var req = {            method: 'GET',            url: '/resource/4?a=1&b=2',            host: 'example.com',            port: 80        };        credentialsFunc('123456', function (err, credentials1) {            var bewit = Hawk.uri.getBewit('http://example.com/resource/4?a=1&b=2', { credentials: credentials1, ttlSec: 60 * 60 * 24 * 365 * 100 });            req.url += '&bewit=' + bewit;            Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials2, attributes) {                expect(err).to.not.exist();                expect(credentials2.user).to.equal('steve');                done();            });        });    });    it('should successfully authenticate a request (last param)', function (done) {        var req = {            method: 'GET',            url: '/resource/4?a=1&b=2&bewit=MTIzNDU2XDQ1MTE0ODQ2MjFcMzFjMmNkbUJFd1NJRVZDOVkva1NFb2c3d3YrdEVNWjZ3RXNmOGNHU2FXQT1cc29tZS1hcHAtZGF0YQ',            host: 'example.com',            port: 8080        };        Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {            expect(err).to.not.exist();            expect(credentials.user).to.equal('steve');            expect(attributes.ext).to.equal('some-app-data');            done();        });    });    it('should successfully authenticate a request (first param)', function (done) {        var req = {            method: 'GET',            url: '/resource/4?bewit=MTIzNDU2XDQ1MTE0ODQ2MjFcMzFjMmNkbUJFd1NJRVZDOVkva1NFb2c3d3YrdEVNWjZ3RXNmOGNHU2FXQT1cc29tZS1hcHAtZGF0YQ&a=1&b=2',            host: 'example.com',            port: 8080        };        Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {            expect(err).to.not.exist();            expect(credentials.user).to.equal('steve');            expect(attributes.ext).to.equal('some-app-data');            done();        });    });    it('should successfully authenticate a request (only param)', function (done) {        var req = {            method: 'GET',            url: '/resource/4?bewit=MTIzNDU2XDQ1MTE0ODQ2NDFcZm1CdkNWT3MvcElOTUUxSTIwbWhrejQ3UnBwTmo4Y1VrSHpQd3Q5OXJ1cz1cc29tZS1hcHAtZGF0YQ',            host: 'example.com',            port: 8080        };        Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {            expect(err).to.not.exist();            expect(credentials.user).to.equal('steve');            expect(attributes.ext).to.equal('some-app-data');            done();        });    });    it('should fail on multiple authentication', function (done) {        var req = {            method: 'GET',            url: '/resource/4?bewit=MTIzNDU2XDQ1MTE0ODQ2NDFcZm1CdkNWT3MvcElOTUUxSTIwbWhrejQ3UnBwTmo4Y1VrSHpQd3Q5OXJ1cz1cc29tZS1hcHAtZGF0YQ',            host: 'example.com',            port: 8080,            authorization: 'Basic asdasdasdasd'        };        Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {            expect(err).to.exist();            expect(err.output.payload.message).to.equal('Multiple authentications');            done();        });    });    it('should fail on method other than GET', function (done) {        credentialsFunc('123456', function (err, credentials1) {            var req = {                method: 'POST',                url: '/resource/4?filter=a',                host: 'example.com',                port: 8080            };            var exp = Math.floor(Hawk.utils.now() / 1000) + 60;            var ext = 'some-app-data';            var mac = Hawk.crypto.calculateMac('bewit', credentials1, {                timestamp: exp,                nonce: '',                method: req.method,                resource: req.url,                host: req.host,                port: req.port,                ext: ext            });            var bewit = credentials1.id + '\\' + exp + '\\' + mac + '\\' + ext;            req.url += '&bewit=' + Hoek.base64urlEncode(bewit);            Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials2, attributes) {                expect(err).to.exist();                expect(err.output.payload.message).to.equal('Invalid method');                done();            });        });    });    it('should fail on invalid host header', function (done) {        var req = {            method: 'GET',            url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',            headers: {                host: 'example.com:something'            }        };        Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {            expect(err).to.exist();            expect(err.output.payload.message).to.equal('Invalid Host header');            done();        });    });    it('should fail on empty bewit', function (done) {        var req = {            method: 'GET',            url: '/resource/4?bewit=',            host: 'example.com',            port: 8080        };        Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {            expect(err).to.exist();            expect(err.output.payload.message).to.equal('Empty bewit');            expect(err.isMissing).to.not.exist();            done();        });    });    it('should fail on invalid bewit', function (done) {        var req = {            method: 'GET',            url: '/resource/4?bewit=*',            host: 'example.com',            port: 8080        };        Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {            expect(err).to.exist();            expect(err.output.payload.message).to.equal('Invalid bewit encoding');            expect(err.isMissing).to.not.exist();            done();        });    });    it('should fail on missing bewit', function (done) {        var req = {            method: 'GET',            url: '/resource/4',            host: 'example.com',            port: 8080        };        Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {            expect(err).to.exist();            expect(err.output.payload.message).to.not.exist();            expect(err.isMissing).to.equal(true);            done();        });    });    it('should fail on invalid bewit structure', function (done) {        var req = {            method: 'GET',            url: '/resource/4?bewit=abc',            host: 'example.com',            port: 8080        };        Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {            expect(err).to.exist();            expect(err.output.payload.message).to.equal('Invalid bewit structure');            done();        });    });    it('should fail on empty bewit attribute', function (done) {        var req = {            method: 'GET',            url: '/resource/4?bewit=YVxcY1xk',            host: 'example.com',            port: 8080        };        Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {            expect(err).to.exist();            expect(err.output.payload.message).to.equal('Missing bewit attributes');            done();        });    });    it('should fail on missing bewit id attribute', function (done) {        var req = {            method: 'GET',            url: '/resource/4?bewit=XDQ1NTIxNDc2MjJcK0JFbFhQMXhuWjcvd1Nrbm1ldGhlZm5vUTNHVjZNSlFVRHk4NWpTZVJ4VT1cc29tZS1hcHAtZGF0YQ',            host: 'example.com',            port: 8080        };        Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {            expect(err).to.exist();            expect(err.output.payload.message).to.equal('Missing bewit attributes');            done();        });    });    it('should fail on expired access', function (done) {        var req = {            method: 'GET',            url: '/resource/4?a=1&b=2&bewit=MTIzNDU2XDEzNTY0MTg1ODNcWk1wZlMwWU5KNHV0WHpOMmRucTRydEk3NXNXTjFjeWVITTcrL0tNZFdVQT1cc29tZS1hcHAtZGF0YQ',            host: 'example.com',            port: 8080        };        Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {            expect(err).to.exist();            expect(err.output.payload.message).to.equal('Access expired');            done();        });    });    it('should fail on credentials function error', function (done) {        var req = {            method: 'GET',            url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',            host: 'example.com',            port: 8080        };        Hawk.uri.authenticate(req, function (id, callback) {            callback(Hawk.error.badRequest('Boom'));        }, {}, function (err, credentials, attributes) {            expect(err).to.exist();            expect(err.output.payload.message).to.equal('Boom');            done();        });    });    it('should fail on credentials function error with credentials', function (done) {        var req = {            method: 'GET',            url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',            host: 'example.com',            port: 8080        };        Hawk.uri.authenticate(req, function (id, callback) {            callback(Hawk.error.badRequest('Boom'), { some: 'value' });        }, {}, function (err, credentials, attributes) {            expect(err).to.exist();            expect(err.output.payload.message).to.equal('Boom');            expect(credentials.some).to.equal('value');            done();        });    });    it('should fail on null credentials function response', function (done) {        var req = {            method: 'GET',            url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',            host: 'example.com',            port: 8080        };        Hawk.uri.authenticate(req, function (id, callback) {            callback(null, null);        }, {}, function (err, credentials, attributes) {            expect(err).to.exist();            expect(err.output.payload.message).to.equal('Unknown credentials');            done();        });    });    it('should fail on invalid credentials function response', function (done) {        var req = {            method: 'GET',            url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',            host: 'example.com',            port: 8080        };        Hawk.uri.authenticate(req, function (id, callback) {            callback(null, {});        }, {}, function (err, credentials, attributes) {            expect(err).to.exist();            expect(err.message).to.equal('Invalid credentials');            done();        });    });    it('should fail on invalid credentials function response (unknown algorithm)', function (done) {        var req = {            method: 'GET',            url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',            host: 'example.com',            port: 8080        };        Hawk.uri.authenticate(req, function (id, callback) {            callback(null, { key: 'xxx', algorithm: 'xxx' });        }, {}, function (err, credentials, attributes) {            expect(err).to.exist();            expect(err.message).to.equal('Unknown algorithm');            done();        });    });    it('should fail on expired access', function (done) {        var req = {            method: 'GET',            url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',            host: 'example.com',            port: 8080        };        Hawk.uri.authenticate(req, function (id, callback) {            callback(null, { key: 'xxx', algorithm: 'sha256' });        }, {}, function (err, credentials, attributes) {            expect(err).to.exist();            expect(err.output.payload.message).to.equal('Bad mac');            done();        });    });    describe('getBewit()', function () {        it('returns a valid bewit value', function (done) {            var credentials = {                id: '123456',                key: '2983d45yun89q',                algorithm: 'sha256'            };            var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow', { credentials: credentials, ttlSec: 300, localtimeOffsetMsec: 1356420407232 - Hawk.utils.now(), ext: 'xandyandz' });            expect(bewit).to.equal('MTIzNDU2XDEzNTY0MjA3MDdca3NjeHdOUjJ0SnBQMVQxekRMTlBiQjVVaUtJVTl0T1NKWFRVZEc3WDloOD1ceGFuZHlhbmR6');            done();        });        it('returns a valid bewit value (explicit port)', function (done) {            var credentials = {                id: '123456',                key: '2983d45yun89q',                algorithm: 'sha256'            };            var bewit = Hawk.uri.getBewit('https://example.com:8080/somewhere/over/the/rainbow', { credentials: credentials, ttlSec: 300, localtimeOffsetMsec: 1356420407232 - Hawk.utils.now(), ext: 'xandyandz' });            expect(bewit).to.equal('MTIzNDU2XDEzNTY0MjA3MDdcaFpiSjNQMmNLRW80a3kwQzhqa1pBa1J5Q1p1ZWc0V1NOYnhWN3ZxM3hIVT1ceGFuZHlhbmR6');            done();        });        it('returns a valid bewit value (null ext)', function (done) {            var credentials = {                id: '123456',                key: '2983d45yun89q',                algorithm: 'sha256'            };            var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow', { credentials: credentials, ttlSec: 300, localtimeOffsetMsec: 1356420407232 - Hawk.utils.now(), ext: null });            expect(bewit).to.equal('MTIzNDU2XDEzNTY0MjA3MDdcSUdZbUxnSXFMckNlOEN4dktQczRKbFdJQStValdKSm91d2dBUmlWaENBZz1c');            done();        });        it('returns a valid bewit value (parsed uri)', function (done) {            var credentials = {                id: '123456',                key: '2983d45yun89q',                algorithm: 'sha256'            };            var bewit = Hawk.uri.getBewit(Url.parse('https://example.com/somewhere/over/the/rainbow'), { credentials: credentials, ttlSec: 300, localtimeOffsetMsec: 1356420407232 - Hawk.utils.now(), ext: 'xandyandz' });            expect(bewit).to.equal('MTIzNDU2XDEzNTY0MjA3MDdca3NjeHdOUjJ0SnBQMVQxekRMTlBiQjVVaUtJVTl0T1NKWFRVZEc3WDloOD1ceGFuZHlhbmR6');            done();        });        it('errors on invalid options', function (done) {            var credentials = {                id: '123456',                key: '2983d45yun89q',                algorithm: 'sha256'            };            var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow', 4);            expect(bewit).to.equal('');            done();        });        it('errors on missing uri', function (done) {            var credentials = {                id: '123456',                key: '2983d45yun89q',                algorithm: 'sha256'            };            var bewit = Hawk.uri.getBewit('', { credentials: credentials, ttlSec: 300, localtimeOffsetMsec: 1356420407232 - Hawk.utils.now(), ext: 'xandyandz' });            expect(bewit).to.equal('');            done();        });        it('errors on invalid uri', function (done) {            var credentials = {                id: '123456',                key: '2983d45yun89q',                algorithm: 'sha256'            };            var bewit = Hawk.uri.getBewit(5, { credentials: credentials, ttlSec: 300, localtimeOffsetMsec: 1356420407232 - Hawk.utils.now(), ext: 'xandyandz' });            expect(bewit).to.equal('');            done();        });        it('errors on invalid credentials (id)', function (done) {            var credentials = {                key: '2983d45yun89q',                algorithm: 'sha256'            };            var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow', { credentials: credentials, ttlSec: 3000, ext: 'xandyandz' });            expect(bewit).to.equal('');            done();        });        it('errors on missing credentials', function (done) {            var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow', { ttlSec: 3000, ext: 'xandyandz' });            expect(bewit).to.equal('');            done();        });        it('errors on invalid credentials (key)', function (done) {            var credentials = {                id: '123456',                algorithm: 'sha256'            };            var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow', { credentials: credentials, ttlSec: 3000, ext: 'xandyandz' });            expect(bewit).to.equal('');            done();        });        it('errors on invalid algorithm', function (done) {            var credentials = {                id: '123456',                key: '2983d45yun89q',                algorithm: 'hmac-sha-0'            };            var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow', { credentials: credentials, ttlSec: 300, ext: 'xandyandz' });            expect(bewit).to.equal('');            done();        });        it('errors on missing options', function (done) {            var credentials = {                id: '123456',                key: '2983d45yun89q',                algorithm: 'hmac-sha-0'            };            var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow');            expect(bewit).to.equal('');            done();        });    });    describe('authenticateMessage()', function () {        it('should generate an authorization then successfully parse it', function (done) {            credentialsFunc('123456', function (err, credentials1) {                var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });                expect(auth).to.exist();                Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, {}, function (err, credentials2) {                    expect(err).to.not.exist();                    expect(credentials2.user).to.equal('steve');                    done();                });            });        });        it('should fail authorization on mismatching host', function (done) {            credentialsFunc('123456', function (err, credentials1) {                var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });                expect(auth).to.exist();                Hawk.server.authenticateMessage('example1.com', 8080, 'some message', auth, credentialsFunc, {}, function (err, credentials2) {                    expect(err).to.exist();                    expect(err.message).to.equal('Bad mac');                    done();                });            });        });        it('should fail authorization on stale timestamp', function (done) {            credentialsFunc('123456', function (err, credentials1) {                var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });                expect(auth).to.exist();                Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, { localtimeOffsetMsec: 100000 }, function (err, credentials2) {                    expect(err).to.exist();                    expect(err.message).to.equal('Stale timestamp');                    done();                });            });        });        it('overrides timestampSkewSec', function (done) {            credentialsFunc('123456', function (err, credentials1) {                var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1, localtimeOffsetMsec: 100000 });                expect(auth).to.exist();                Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, { timestampSkewSec: 500 }, function (err, credentials2) {                    expect(err).to.not.exist();                    done();                });            });        });        it('should fail authorization on invalid authorization', function (done) {            credentialsFunc('123456', function (err, credentials1) {                var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });                expect(auth).to.exist();                delete auth.id;                Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, {}, function (err, credentials2) {                    expect(err).to.exist();                    expect(err.message).to.equal('Invalid authorization');                    done();                });            });        });        it('should fail authorization on bad hash', function (done) {            credentialsFunc('123456', function (err, credentials1) {                var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });                expect(auth).to.exist();                Hawk.server.authenticateMessage('example.com', 8080, 'some message1', auth, credentialsFunc, {}, function (err, credentials2) {                    expect(err).to.exist();                    expect(err.message).to.equal('Bad message hash');                    done();                });            });        });        it('should fail authorization on nonce error', function (done) {            credentialsFunc('123456', function (err, credentials1) {                var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });                expect(auth).to.exist();                Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, {                    nonceFunc: function (key, nonce, ts, callback) {                        callback(new Error('kaboom'));                    }                }, function (err, credentials2) {                    expect(err).to.exist();                    expect(err.message).to.equal('Invalid nonce');                    done();                });            });        });        it('should fail authorization on credentials error', function (done) {            credentialsFunc('123456', function (err, credentials1) {                var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });                expect(auth).to.exist();                var errFunc = function (id, callback) {                    callback(new Error('kablooey'));                };                Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, errFunc, {}, function (err, credentials2) {                    expect(err).to.exist();                    expect(err.message).to.equal('kablooey');                    done();                });            });        });        it('should fail authorization on missing credentials', function (done) {            credentialsFunc('123456', function (err, credentials1) {                var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });                expect(auth).to.exist();                var errFunc = function (id, callback) {                    callback();                };                Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, errFunc, {}, function (err, credentials2) {                    expect(err).to.exist();                    expect(err.message).to.equal('Unknown credentials');                    done();                });            });        });        it('should fail authorization on invalid credentials', function (done) {            credentialsFunc('123456', function (err, credentials1) {                var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });                expect(auth).to.exist();                var errFunc = function (id, callback) {                    callback(null, {});                };                Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, errFunc, {}, function (err, credentials2) {                    expect(err).to.exist();                    expect(err.message).to.equal('Invalid credentials');                    done();                });            });        });        it('should fail authorization on invalid credentials algorithm', function (done) {            credentialsFunc('123456', function (err, credentials1) {                var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });                expect(auth).to.exist();                var errFunc = function (id, callback) {                    callback(null, { key: '123', algorithm: '456' });                };                Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, errFunc, {}, function (err, credentials2) {                    expect(err).to.exist();                    expect(err.message).to.equal('Unknown algorithm');                    done();                });            });        });        it('should fail on missing host', function (done) {            credentialsFunc('123456', function (err, credentials1) {                var auth = Hawk.client.message(null, 8080, 'some message', { credentials: credentials1 });                expect(auth).to.not.exist();                done();            });        });        it('should fail on missing credentials', function (done) {            var auth = Hawk.client.message('example.com', 8080, 'some message', {});            expect(auth).to.not.exist();            done();        });        it('should fail on invalid algorithm', function (done) {            credentialsFunc('123456', function (err, credentials1) {                var creds = Hoek.clone(credentials1);                creds.algorithm = 'blah';                var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: creds });                expect(auth).to.not.exist();                done();            });        });    });});
 |