123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818 |
- // Load modules
- var Lab = require('lab');
- var Hoek = require('hoek');
- var Hawk = require('../lib');
- var Browser = require('../lib/browser');
- var LocalStorage = require('localStorage');
- // Declare internals
- var internals = {};
- // Test shortcuts
- var expect = Lab.expect;
- var before = Lab.before;
- var after = Lab.after;
- var describe = Lab.experiment;
- var it = Lab.test;
- describe('Browser', 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 header then successfully parse it (configuration)', function (done) {
- var req = {
- method: 'GET',
- url: '/resource/4?filter=a',
- host: 'example.com',
- port: 8080
- };
- credentialsFunc('123456', function (err, credentials) {
- req.authorization = Browser.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, ext: 'some-app-data' }).field;
- expect(req.authorization).to.exist;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
- expect(err).to.not.exist;
- expect(credentials.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- done();
- });
- });
- });
- it('should generate a header then successfully parse it (node request)', function (done) {
- var req = {
- method: 'POST',
- url: '/resource/4?filter=a',
- headers: {
- host: 'example.com:8080',
- 'content-type': 'text/plain;x=y'
- }
- };
- var payload = 'some not so random text';
- credentialsFunc('123456', function (err, credentials) {
- var reqHeader = Browser.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, ext: 'some-app-data', payload: payload, contentType: req.headers['content-type'] });
- req.headers.authorization = reqHeader.field;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
- expect(err).to.not.exist;
- expect(credentials.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- expect(Hawk.server.authenticatePayload(payload, credentials, artifacts, req.headers['content-type'])).to.equal(true);
- var res = {
- headers: {
- 'content-type': 'text/plain'
- },
- getResponseHeader: function (header) {
- return res.headers[header.toLowerCase()];
- }
- };
- res.headers['server-authorization'] = Hawk.server.header(credentials, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });
- expect(res.headers['server-authorization']).to.exist;
- expect(Browser.client.authenticate(res, credentials, artifacts, { payload: 'some reply' })).to.equal(true);
- done();
- });
- });
- });
- it('should generate a header then successfully parse it (no server header options)', function (done) {
- var req = {
- method: 'POST',
- url: '/resource/4?filter=a',
- headers: {
- host: 'example.com:8080',
- 'content-type': 'text/plain;x=y'
- }
- };
- var payload = 'some not so random text';
- credentialsFunc('123456', function (err, credentials) {
- var reqHeader = Browser.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, ext: 'some-app-data', payload: payload, contentType: req.headers['content-type'] });
- req.headers.authorization = reqHeader.field;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
- expect(err).to.not.exist;
- expect(credentials.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- expect(Hawk.server.authenticatePayload(payload, credentials, artifacts, req.headers['content-type'])).to.equal(true);
- var res = {
- headers: {
- 'content-type': 'text/plain'
- },
- getResponseHeader: function (header) {
- return res.headers[header.toLowerCase()];
- }
- };
- res.headers['server-authorization'] = Hawk.server.header(credentials, artifacts);
- expect(res.headers['server-authorization']).to.exist;
- expect(Browser.client.authenticate(res, credentials, artifacts)).to.equal(true);
- done();
- });
- });
- });
- it('should generate a header then successfully parse it (no server header)', function (done) {
- var req = {
- method: 'POST',
- url: '/resource/4?filter=a',
- headers: {
- host: 'example.com:8080',
- 'content-type': 'text/plain;x=y'
- }
- };
- var payload = 'some not so random text';
- credentialsFunc('123456', function (err, credentials) {
- var reqHeader = Browser.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, ext: 'some-app-data', payload: payload, contentType: req.headers['content-type'] });
- req.headers.authorization = reqHeader.field;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
- expect(err).to.not.exist;
- expect(credentials.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- expect(Hawk.server.authenticatePayload(payload, credentials, artifacts, req.headers['content-type'])).to.equal(true);
- var res = {
- headers: {
- 'content-type': 'text/plain'
- },
- getResponseHeader: function (header) {
- return res.headers[header.toLowerCase()];
- }
- };
- expect(Browser.client.authenticate(res, credentials, artifacts)).to.equal(true);
- done();
- });
- });
- });
- it('should generate a header with stale ts and successfully authenticate on second call', function (done) {
- var req = {
- method: 'GET',
- url: '/resource/4?filter=a',
- host: 'example.com',
- port: 8080
- };
- credentialsFunc('123456', function (err, credentials) {
- Browser.utils.setNtpOffset(60 * 60 * 1000);
- var header = Browser.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, ext: 'some-app-data' });
- req.authorization = header.field;
- expect(req.authorization).to.exist;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
- expect(err).to.exist;
- expect(err.message).to.equal('Stale timestamp');
- var res = {
- headers: {
- 'www-authenticate': err.response.headers['WWW-Authenticate']
- },
- getResponseHeader: function (header) {
- return res.headers[header.toLowerCase()];
- }
- };
- expect(Browser.utils.getNtpOffset()).to.equal(60 * 60 * 1000);
- expect(Browser.client.authenticate(res, credentials, header.artifacts)).to.equal(true);
- expect(Browser.utils.getNtpOffset()).to.equal(0);
- req.authorization = Browser.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, ext: 'some-app-data' }).field;
- expect(req.authorization).to.exist;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
- expect(err).to.not.exist;
- expect(credentials.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- done();
- });
- });
- });
- });
- it('should generate a header with stale ts and successfully authenticate on second call (manual localStorage)', function (done) {
- var req = {
- method: 'GET',
- url: '/resource/4?filter=a',
- host: 'example.com',
- port: 8080
- };
- credentialsFunc('123456', function (err, credentials) {
- Browser.utils.setStorage(LocalStorage)
- Browser.utils.setNtpOffset(60 * 60 * 1000);
- var header = Browser.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, ext: 'some-app-data' });
- req.authorization = header.field;
- expect(req.authorization).to.exist;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
- expect(err).to.exist;
- expect(err.message).to.equal('Stale timestamp');
- var res = {
- headers: {
- 'www-authenticate': err.response.headers['WWW-Authenticate']
- },
- getResponseHeader: function (header) {
- return res.headers[header.toLowerCase()];
- }
- };
- expect(parseInt(LocalStorage.getItem('hawk_ntp_offset'))).to.equal(60 * 60 * 1000);
- expect(Browser.utils.getNtpOffset()).to.equal(60 * 60 * 1000);
- expect(Browser.client.authenticate(res, credentials, header.artifacts)).to.equal(true);
- expect(Browser.utils.getNtpOffset()).to.equal(0);
- expect(parseInt(LocalStorage.getItem('hawk_ntp_offset'))).to.equal(0);
- req.authorization = Browser.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, ext: 'some-app-data' }).field;
- expect(req.authorization).to.exist;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
- expect(err).to.not.exist;
- expect(credentials.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- done();
- });
- });
- });
- });
- it('should generate a header then fails to parse it (missing server header hash)', function (done) {
- var req = {
- method: 'POST',
- url: '/resource/4?filter=a',
- headers: {
- host: 'example.com:8080',
- 'content-type': 'text/plain;x=y'
- }
- };
- var payload = 'some not so random text';
- credentialsFunc('123456', function (err, credentials) {
- var reqHeader = Browser.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, ext: 'some-app-data', payload: payload, contentType: req.headers['content-type'] });
- req.headers.authorization = reqHeader.field;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
- expect(err).to.not.exist;
- expect(credentials.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- expect(Hawk.server.authenticatePayload(payload, credentials, artifacts, req.headers['content-type'])).to.equal(true);
- var res = {
- headers: {
- 'content-type': 'text/plain'
- },
- getResponseHeader: function (header) {
- return res.headers[header.toLowerCase()];
- }
- };
- res.headers['server-authorization'] = Hawk.server.header(credentials, artifacts);
- expect(res.headers['server-authorization']).to.exist;
- expect(Browser.client.authenticate(res, credentials, artifacts, { payload: 'some reply' })).to.equal(false);
- done();
- });
- });
- });
- it('should generate a header then successfully parse it (with hash)', function (done) {
- var req = {
- method: 'GET',
- url: '/resource/4?filter=a',
- host: 'example.com',
- port: 8080
- };
- credentialsFunc('123456', function (err, credentials) {
- req.authorization = Browser.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, payload: 'hola!', ext: 'some-app-data' }).field;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
- expect(err).to.not.exist;
- expect(credentials.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- done();
- });
- });
- });
- it('should generate a header then successfully parse it then validate payload', function (done) {
- var req = {
- method: 'GET',
- url: '/resource/4?filter=a',
- host: 'example.com',
- port: 8080
- };
- credentialsFunc('123456', function (err, credentials) {
- req.authorization = Browser.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, payload: 'hola!', ext: 'some-app-data' }).field;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
- expect(err).to.not.exist;
- expect(credentials.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- expect(Hawk.server.authenticatePayload('hola!', credentials, artifacts)).to.be.true;
- expect(Hawk.server.authenticatePayload('hello!', credentials, artifacts)).to.be.false;
- done();
- });
- });
- });
- it('should generate a header then successfully parse it (app)', function (done) {
- var req = {
- method: 'GET',
- url: '/resource/4?filter=a',
- host: 'example.com',
- port: 8080
- };
- credentialsFunc('123456', function (err, credentials) {
- req.authorization = Browser.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, ext: 'some-app-data', app: 'asd23ased' }).field;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
- expect(err).to.not.exist;
- expect(credentials.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- expect(artifacts.app).to.equal('asd23ased');
- done();
- });
- });
- });
- it('should generate a header then successfully parse it (app, dlg)', function (done) {
- var req = {
- method: 'GET',
- url: '/resource/4?filter=a',
- host: 'example.com',
- port: 8080
- };
- credentialsFunc('123456', function (err, credentials) {
- req.authorization = Browser.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, ext: 'some-app-data', app: 'asd23ased', dlg: '23434szr3q4d' }).field;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
- expect(err).to.not.exist;
- expect(credentials.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- expect(artifacts.app).to.equal('asd23ased');
- expect(artifacts.dlg).to.equal('23434szr3q4d');
- done();
- });
- });
- });
- it('should generate a header then fail authentication due to bad hash', function (done) {
- var req = {
- method: 'GET',
- url: '/resource/4?filter=a',
- host: 'example.com',
- port: 8080
- };
- credentialsFunc('123456', function (err, credentials) {
- req.authorization = Browser.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, payload: 'hola!', ext: 'some-app-data' }).field;
- Hawk.server.authenticate(req, credentialsFunc, { payload: 'byebye!' }, function (err, credentials, artifacts) {
- expect(err).to.exist;
- expect(err.response.payload.message).to.equal('Bad payload hash');
- done();
- });
- });
- });
- it('should generate a header for one resource then fail to authenticate another', function (done) {
- var req = {
- method: 'GET',
- url: '/resource/4?filter=a',
- host: 'example.com',
- port: 8080
- };
- credentialsFunc('123456', function (err, credentials) {
- req.authorization = Browser.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, ext: 'some-app-data' }).field;
- req.url = '/something/else';
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
- expect(err).to.exist;
- expect(credentials).to.exist;
- done();
- });
- });
- });
- describe('client', function () {
- describe('#header', function () {
- it('should return a valid authorization header (sha1)', function (done) {
- var credentials = {
- id: '123456',
- key: '2983d45yun89q',
- algorithm: 'sha1'
- };
- var header = Browser.client.header('http://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, ext: 'Bazinga!', timestamp: 1353809207, nonce: 'Ygvqdz', payload: 'something to write about' }).field;
- expect(header).to.equal('Hawk id="123456", ts="1353809207", nonce="Ygvqdz", hash="bsvY3IfUllw6V5rvk4tStEvpBhE=", ext="Bazinga!", mac="qbf1ZPG/r/e06F4ht+T77LXi5vw="');
- done();
- });
- it('should return a valid authorization header (sha256)', function (done) {
- var credentials = {
- id: '123456',
- key: '2983d45yun89q',
- algorithm: 'sha256'
- };
- var header = Browser.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;
- expect(header).to.equal('Hawk id="123456", ts="1353809207", nonce="Ygvqdz", hash="2QfCt3GuY9HQnHWyWD3wX68ZOKbynqlfYmuO2ZBRqtY=", ext="Bazinga!", mac="q1CwFoSHzPZSkbIvl0oYlD+91rBUEvFk763nMjMndj8="');
- done();
- });
- it('should return a valid authorization header (no ext)', function (done) {
- var credentials = {
- id: '123456',
- key: '2983d45yun89q',
- algorithm: 'sha256'
- };
- var header = Browser.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;
- expect(header).to.equal('Hawk id="123456", ts="1353809207", nonce="Ygvqdz", hash="2QfCt3GuY9HQnHWyWD3wX68ZOKbynqlfYmuO2ZBRqtY=", mac="HTgtd0jPI6E4izx8e4OHdO36q00xFCU0FolNq3RiCYs="');
- done();
- });
- it('should return an empty authorization header on missing options', function (done) {
- var header = Browser.client.header('https://example.net/somewhere/over/the/rainbow', 'POST').field;
- expect(header).to.equal('');
- done();
- });
- it('should return an empty authorization header on invalid credentials', function (done) {
- var credentials = {
- key: '2983d45yun89q',
- algorithm: 'sha256'
- };
- var header = Browser.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, ext: 'Bazinga!', timestamp: 1353809207 }).field;
- expect(header).to.equal('');
- done();
- });
- it('should return an empty authorization header on invalid algorithm', function (done) {
- var credentials = {
- id: '123456',
- key: '2983d45yun89q',
- algorithm: 'hmac-sha-0'
- };
- var header = Browser.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, payload: 'something, anything!', ext: 'Bazinga!', timestamp: 1353809207 }).field;
- expect(header).to.equal('');
- done();
- });
- });
- describe('#authenticate', function () {
- it('should return false on invalid header', function (done) {
- var res = {
- headers: {
- 'server-authorization': 'Hawk mac="abc", bad="xyz"'
- },
- getResponseHeader: function (header) {
- return res.headers[header.toLowerCase()];
- }
- };
- expect(Browser.client.authenticate(res, {})).to.equal(false);
- done();
- });
- it('should return false on invalid mac', function (done) {
- var res = {
- headers: {
- 'content-type': 'text/plain',
- 'server-authorization': 'Hawk mac="_IJRsMl/4oL+nn+vKoeVZPdCHXB4yJkNnBbTbHFZUYE=", hash="f9cDF/TDm7TkYRLnGwRMfeDzT6LixQVLvrIKhh0vgmM=", ext="response-specific"'
- },
- getResponseHeader: function (header) {
- return res.headers[header.toLowerCase()];
- }
- };
- var artifacts = {
- method: 'POST',
- host: 'example.com',
- port: '8080',
- resource: '/resource/4?filter=a',
- ts: '1362336900',
- nonce: 'eb5S_L',
- hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
- ext: 'some-app-data',
- app: undefined,
- dlg: undefined,
- mac: 'BlmSe8K+pbKIb6YsZCnt4E1GrYvY1AaYayNR82dGpIk=',
- id: '123456'
- };
- var credentials = {
- id: '123456',
- key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
- algorithm: 'sha256',
- user: 'steve'
- };
- expect(Browser.client.authenticate(res, credentials, artifacts)).to.equal(false);
- done();
- });
- it('should return true on ignoring hash', function (done) {
- var res = {
- headers: {
- 'content-type': 'text/plain',
- 'server-authorization': 'Hawk mac="XIJRsMl/4oL+nn+vKoeVZPdCHXB4yJkNnBbTbHFZUYE=", hash="f9cDF/TDm7TkYRLnGwRMfeDzT6LixQVLvrIKhh0vgmM=", ext="response-specific"'
- },
- getResponseHeader: function (header) {
- return res.headers[header.toLowerCase()];
- }
- };
- var artifacts = {
- method: 'POST',
- host: 'example.com',
- port: '8080',
- resource: '/resource/4?filter=a',
- ts: '1362336900',
- nonce: 'eb5S_L',
- hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
- ext: 'some-app-data',
- app: undefined,
- dlg: undefined,
- mac: 'BlmSe8K+pbKIb6YsZCnt4E1GrYvY1AaYayNR82dGpIk=',
- id: '123456'
- };
- var credentials = {
- id: '123456',
- key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
- algorithm: 'sha256',
- user: 'steve'
- };
- expect(Browser.client.authenticate(res, credentials, artifacts)).to.equal(true);
- done();
- });
- it('should fail on invalid WWW-Authenticate header format', function (done) {
- var res = {
- headers: {
- 'www-authenticate': 'Hawk ts="1362346425875", tsm="PhwayS28vtnn3qbv0mqRBYSXebN/zggEtucfeZ620Zo=", x="Stale timestamp"'
- },
- getResponseHeader: function (header) {
- return res.headers[header.toLowerCase()];
- }
- };
- expect(Browser.client.authenticate(res, {})).to.equal(false);
- done();
- });
- it('should fail on invalid WWW-Authenticate header format', function (done) {
- var credentials = {
- id: '123456',
- key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
- algorithm: 'sha256',
- user: 'steve'
- };
- var res = {
- headers: {
- 'www-authenticate': 'Hawk ts="1362346425875", tsm="hwayS28vtnn3qbv0mqRBYSXebN/zggEtucfeZ620Zo=", error="Stale timestamp"'
- },
- getResponseHeader: function (header) {
- return res.headers[header.toLowerCase()];
- }
- };
- expect(Browser.client.authenticate(res, credentials)).to.equal(false);
- done();
- });
- });
- describe('#message', function () {
- it('should generate an authorization then successfully parse it', function (done) {
- credentialsFunc('123456', function (err, credentials) {
- var auth = Browser.client.message('example.com', 8080, 'some message', { credentials: credentials });
- expect(auth).to.exist;
- Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, {}, function (err, credentials) {
- expect(err).to.not.exist;
- expect(credentials.user).to.equal('steve');
- done();
- });
- });
- });
- it('should fail on missing host', function (done) {
- credentialsFunc('123456', function (err, credentials) {
- var auth = Browser.client.message(null, 8080, 'some message', { credentials: credentials });
- expect(auth).to.not.exist;
- done();
- });
- });
- it('should fail on missing credentials', function (done) {
- var auth = Browser.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, credentials) {
- var creds = Hoek.clone(credentials);
- creds.algorithm = 'blah';
- var auth = Browser.client.message('example.com', 8080, 'some message', { credentials: creds });
- expect(auth).to.not.exist;
- done();
- });
- });
- });
- describe('#authenticateTimestamp', function (done) {
- it('should validate a timestamp', function (done) {
- credentialsFunc('123456', function (err, credentials) {
- var tsm = Hawk.crypto.timestampMessage(credentials);
- expect(Browser.client.authenticateTimestamp(tsm, credentials)).to.equal(true);
- done();
- });
- });
- it('should detect a bad timestamp', function (done) {
- credentialsFunc('123456', function (err, credentials) {
- var tsm = Hawk.crypto.timestampMessage(credentials);
- tsm.ts = 4;
- expect(Browser.client.authenticateTimestamp(tsm, credentials)).to.equal(false);
- done();
- });
- });
- });
- });
- describe('#parseAuthorizationHeader', function (done) {
- it('returns null on missing header', function (done) {
- expect(Browser.utils.parseAuthorizationHeader()).to.equal(null);
- done();
- });
- it('returns null on bad header syntax (structure)', function (done) {
- expect(Browser.utils.parseAuthorizationHeader('Hawk')).to.equal(null);
- done();
- });
- it('returns null on bad header syntax (parts)', function (done) {
- expect(Browser.utils.parseAuthorizationHeader(' ')).to.equal(null);
- done();
- });
- it('returns null on bad scheme name', function (done) {
- expect(Browser.utils.parseAuthorizationHeader('Basic asdasd')).to.equal(null);
- done();
- });
- it('returns null on bad attribute value', function (done) {
- expect(Browser.utils.parseAuthorizationHeader('Hawk test="\t"', ['test'])).to.equal(null);
- done();
- });
- it('returns null on duplicated attribute', function (done) {
- expect(Browser.utils.parseAuthorizationHeader('Hawk test="a", test="b"', ['test'])).to.equal(null);
- done();
- });
- });
- describe('#setNtpOffset', function (done) {
- it('catches localStorage errors', function (done) {
- var orig = Browser.utils.storage.setItem;
- var error = console.error;
- var count = 0;
- console.error = function () { if (count++ === 2) { console.error.error; } };
- Browser.utils.storage.setItem = function () {
- Browser.utils.storage.setItem = orig;
- throw new Error()
- };
- expect(function () {
- Browser.utils.setNtpOffset(100);
- }).not.to.throw();
- done();
- });
- });
- });
|