react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / hawk / test / client.js
81146 views// Load modules12var Url = require('url');3var Code = require('code');4var Hawk = require('../lib');5var Lab = require('lab');678// Declare internals910var internals = {};111213// Test shortcuts1415var lab = exports.lab = Lab.script();16var describe = lab.experiment;17var it = lab.test;18var expect = Code.expect;192021describe('Hawk', function () {2223describe('client', function () {2425describe('#header', function () {2627it('returns a valid authorization header (sha1)', function (done) {2829var credentials = {30id: '123456',31key: '2983d45yun89q',32algorithm: 'sha1'33};3435var 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;36expect(header).to.equal('Hawk id="123456", ts="1353809207", nonce="Ygvqdz", hash="bsvY3IfUllw6V5rvk4tStEvpBhE=", ext="Bazinga!", mac="qbf1ZPG/r/e06F4ht+T77LXi5vw="');37done();38});3940it('returns a valid authorization header (sha256)', function (done) {4142var credentials = {43id: '123456',44key: '2983d45yun89q',45algorithm: 'sha256'46};4748var 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;49expect(header).to.equal('Hawk id="123456", ts="1353809207", nonce="Ygvqdz", hash="2QfCt3GuY9HQnHWyWD3wX68ZOKbynqlfYmuO2ZBRqtY=", ext="Bazinga!", mac="q1CwFoSHzPZSkbIvl0oYlD+91rBUEvFk763nMjMndj8="');50done();51});5253it('returns a valid authorization header (no ext)', function (done) {5455var credentials = {56id: '123456',57key: '2983d45yun89q',58algorithm: 'sha256'59};6061var 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;62expect(header).to.equal('Hawk id="123456", ts="1353809207", nonce="Ygvqdz", hash="2QfCt3GuY9HQnHWyWD3wX68ZOKbynqlfYmuO2ZBRqtY=", mac="HTgtd0jPI6E4izx8e4OHdO36q00xFCU0FolNq3RiCYs="');63done();64});6566it('returns a valid authorization header (null ext)', function (done) {6768var credentials = {69id: '123456',70key: '2983d45yun89q',71algorithm: 'sha256'72};7374var 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;75expect(header).to.equal('Hawk id="123456", ts="1353809207", nonce="Ygvqdz", hash="2QfCt3GuY9HQnHWyWD3wX68ZOKbynqlfYmuO2ZBRqtY=", mac="HTgtd0jPI6E4izx8e4OHdO36q00xFCU0FolNq3RiCYs="');76done();77});7879it('returns a valid authorization header (empty payload)', function (done) {8081var credentials = {82id: '123456',83key: '2983d45yun89q',84algorithm: 'sha256'85};8687var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, timestamp: 1353809207, nonce: 'Ygvqdz', payload: '', contentType: 'text/plain' }).field;88expect(header).to.equal('Hawk id=\"123456\", ts=\"1353809207\", nonce=\"Ygvqdz\", hash=\"q/t+NNAkQZNlq/aAD6PlexImwQTxwgT2MahfTa9XRLA=\", mac=\"U5k16YEzn3UnBHKeBzsDXn067Gu3R4YaY6xOt9PYRZM=\"');89done();90});9192it('returns a valid authorization header (pre hashed payload)', function (done) {9394var credentials = {95id: '123456',96key: '2983d45yun89q',97algorithm: 'sha256'98};99100var options = { credentials: credentials, timestamp: 1353809207, nonce: 'Ygvqdz', payload: 'something to write about', contentType: 'text/plain' };101options.hash = Hawk.crypto.calculatePayloadHash(options.payload, credentials.algorithm, options.contentType);102var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', options).field;103expect(header).to.equal('Hawk id="123456", ts="1353809207", nonce="Ygvqdz", hash="2QfCt3GuY9HQnHWyWD3wX68ZOKbynqlfYmuO2ZBRqtY=", mac="HTgtd0jPI6E4izx8e4OHdO36q00xFCU0FolNq3RiCYs="');104done();105});106107it('errors on missing uri', function (done) {108109var header = Hawk.client.header('', 'POST');110expect(header.field).to.equal('');111expect(header.err).to.equal('Invalid argument type');112done();113});114115it('errors on invalid uri', function (done) {116117var header = Hawk.client.header(4, 'POST');118expect(header.field).to.equal('');119expect(header.err).to.equal('Invalid argument type');120done();121});122123it('errors on missing method', function (done) {124125var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', '');126expect(header.field).to.equal('');127expect(header.err).to.equal('Invalid argument type');128done();129});130131it('errors on invalid method', function (done) {132133var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 5);134expect(header.field).to.equal('');135expect(header.err).to.equal('Invalid argument type');136done();137});138139it('errors on missing options', function (done) {140141var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST');142expect(header.field).to.equal('');143expect(header.err).to.equal('Invalid argument type');144done();145});146147it('errors on invalid credentials (id)', function (done) {148149var credentials = {150key: '2983d45yun89q',151algorithm: 'sha256'152};153154var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, ext: 'Bazinga!', timestamp: 1353809207 });155expect(header.field).to.equal('');156expect(header.err).to.equal('Invalid credential object');157done();158});159160it('errors on missing credentials', function (done) {161162var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { ext: 'Bazinga!', timestamp: 1353809207 });163expect(header.field).to.equal('');164expect(header.err).to.equal('Invalid credential object');165done();166});167168it('errors on invalid credentials', function (done) {169170var credentials = {171id: '123456',172algorithm: 'sha256'173};174175var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, ext: 'Bazinga!', timestamp: 1353809207 });176expect(header.field).to.equal('');177expect(header.err).to.equal('Invalid credential object');178done();179});180181it('errors on invalid algorithm', function (done) {182183var credentials = {184id: '123456',185key: '2983d45yun89q',186algorithm: 'hmac-sha-0'187};188189var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, payload: 'something, anything!', ext: 'Bazinga!', timestamp: 1353809207 });190expect(header.field).to.equal('');191expect(header.err).to.equal('Unknown algorithm');192done();193});194});195196describe('#authenticate', function () {197198it('returns false on invalid header', function (done) {199200var res = {201headers: {202'server-authorization': 'Hawk mac="abc", bad="xyz"'203}204};205206expect(Hawk.client.authenticate(res, {})).to.equal(false);207done();208});209210it('returns false on invalid mac', function (done) {211212var res = {213headers: {214'content-type': 'text/plain',215'server-authorization': 'Hawk mac="_IJRsMl/4oL+nn+vKoeVZPdCHXB4yJkNnBbTbHFZUYE=", hash="f9cDF/TDm7TkYRLnGwRMfeDzT6LixQVLvrIKhh0vgmM=", ext="response-specific"'216}217};218219var artifacts = {220method: 'POST',221host: 'example.com',222port: '8080',223resource: '/resource/4?filter=a',224ts: '1362336900',225nonce: 'eb5S_L',226hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',227ext: 'some-app-data',228app: undefined,229dlg: undefined,230mac: 'BlmSe8K+pbKIb6YsZCnt4E1GrYvY1AaYayNR82dGpIk=',231id: '123456'232};233234var credentials = {235id: '123456',236key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',237algorithm: 'sha256',238user: 'steve'239};240241expect(Hawk.client.authenticate(res, credentials, artifacts)).to.equal(false);242done();243});244245it('returns true on ignoring hash', function (done) {246247var res = {248headers: {249'content-type': 'text/plain',250'server-authorization': 'Hawk mac="XIJRsMl/4oL+nn+vKoeVZPdCHXB4yJkNnBbTbHFZUYE=", hash="f9cDF/TDm7TkYRLnGwRMfeDzT6LixQVLvrIKhh0vgmM=", ext="response-specific"'251}252};253254var artifacts = {255method: 'POST',256host: 'example.com',257port: '8080',258resource: '/resource/4?filter=a',259ts: '1362336900',260nonce: 'eb5S_L',261hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',262ext: 'some-app-data',263app: undefined,264dlg: undefined,265mac: 'BlmSe8K+pbKIb6YsZCnt4E1GrYvY1AaYayNR82dGpIk=',266id: '123456'267};268269var credentials = {270id: '123456',271key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',272algorithm: 'sha256',273user: 'steve'274};275276expect(Hawk.client.authenticate(res, credentials, artifacts)).to.equal(true);277done();278});279280it('fails on invalid WWW-Authenticate header format', function (done) {281282var header = 'Hawk ts="1362346425875", tsm="PhwayS28vtnn3qbv0mqRBYSXebN/zggEtucfeZ620Zo=", x="Stale timestamp"';283expect(Hawk.client.authenticate({ headers: { 'www-authenticate': header } }, {})).to.equal(false);284done();285});286287it('fails on invalid WWW-Authenticate header format', function (done) {288289var credentials = {290id: '123456',291key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',292algorithm: 'sha256',293user: 'steve'294};295296var header = 'Hawk ts="1362346425875", tsm="hwayS28vtnn3qbv0mqRBYSXebN/zggEtucfeZ620Zo=", error="Stale timestamp"';297expect(Hawk.client.authenticate({ headers: { 'www-authenticate': header } }, credentials)).to.equal(false);298done();299});300301it('skips tsm validation when missing ts', function (done) {302303var header = 'Hawk error="Stale timestamp"';304expect(Hawk.client.authenticate({ headers: { 'www-authenticate': header } }, {})).to.equal(true);305done();306});307});308309describe('#message', function () {310311it('generates authorization', function (done) {312313var credentials = {314id: '123456',315key: '2983d45yun89q',316algorithm: 'sha1'317};318319var auth = Hawk.client.message('example.com', 80, 'I am the boodyman', { credentials: credentials, timestamp: 1353809207, nonce: 'abc123' });320expect(auth).to.exist();321expect(auth.ts).to.equal(1353809207);322expect(auth.nonce).to.equal('abc123');323done();324});325326it('errors on invalid host', function (done) {327328var credentials = {329id: '123456',330key: '2983d45yun89q',331algorithm: 'sha1'332};333334var auth = Hawk.client.message(5, 80, 'I am the boodyman', { credentials: credentials, timestamp: 1353809207, nonce: 'abc123' });335expect(auth).to.not.exist();336done();337});338339it('errors on invalid port', function (done) {340341var credentials = {342id: '123456',343key: '2983d45yun89q',344algorithm: 'sha1'345};346347var auth = Hawk.client.message('example.com', '80', 'I am the boodyman', { credentials: credentials, timestamp: 1353809207, nonce: 'abc123' });348expect(auth).to.not.exist();349done();350});351352it('errors on missing host', function (done) {353354var credentials = {355id: '123456',356key: '2983d45yun89q',357algorithm: 'sha1'358};359360var auth = Hawk.client.message('example.com', 0, 'I am the boodyman', { credentials: credentials, timestamp: 1353809207, nonce: 'abc123' });361expect(auth).to.not.exist();362done();363});364365it('errors on null message', function (done) {366367var credentials = {368id: '123456',369key: '2983d45yun89q',370algorithm: 'sha1'371};372373var auth = Hawk.client.message('example.com', 80, null, { credentials: credentials, timestamp: 1353809207, nonce: 'abc123' });374expect(auth).to.not.exist();375done();376});377378it('errors on missing message', function (done) {379380var credentials = {381id: '123456',382key: '2983d45yun89q',383algorithm: 'sha1'384};385386var auth = Hawk.client.message('example.com', 80, undefined, { credentials: credentials, timestamp: 1353809207, nonce: 'abc123' });387expect(auth).to.not.exist();388done();389});390391it('errors on invalid message', function (done) {392393var credentials = {394id: '123456',395key: '2983d45yun89q',396algorithm: 'sha1'397};398399var auth = Hawk.client.message('example.com', 80, 5, { credentials: credentials, timestamp: 1353809207, nonce: 'abc123' });400expect(auth).to.not.exist();401done();402});403404it('errors on missing options', function (done) {405406var credentials = {407id: '123456',408key: '2983d45yun89q',409algorithm: 'sha1'410};411412var auth = Hawk.client.message('example.com', 80, 'I am the boodyman');413expect(auth).to.not.exist();414done();415});416417it('errors on invalid credentials (id)', function (done) {418419var credentials = {420key: '2983d45yun89q',421algorithm: 'sha1'422};423424var auth = Hawk.client.message('example.com', 80, 'I am the boodyman', { credentials: credentials, timestamp: 1353809207, nonce: 'abc123' });425expect(auth).to.not.exist();426done();427});428429it('errors on invalid credentials (key)', function (done) {430431var credentials = {432id: '123456',433algorithm: 'sha1'434};435436var auth = Hawk.client.message('example.com', 80, 'I am the boodyman', { credentials: credentials, timestamp: 1353809207, nonce: 'abc123' });437expect(auth).to.not.exist();438done();439});440});441});442});443444445