react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / hawk / test / index.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 () {2223var credentialsFunc = function (id, callback) {2425var credentials = {26id: id,27key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',28algorithm: (id === '1' ? 'sha1' : 'sha256'),29user: 'steve'30};3132return callback(null, credentials);33};3435it('generates a header then successfully parse it (configuration)', function (done) {3637var req = {38method: 'GET',39url: '/resource/4?filter=a',40host: 'example.com',41port: 808042};4344credentialsFunc('123456', function (err, credentials) {4546req.authorization = Hawk.client.header(Url.parse('http://example.com:8080/resource/4?filter=a'), req.method, { credentials: credentials, ext: 'some-app-data' }).field;47expect(req.authorization).to.exist();4849Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {5051expect(err).to.not.exist();52expect(credentials.user).to.equal('steve');53expect(artifacts.ext).to.equal('some-app-data');54done();55});56});57});5859it('generates a header then successfully parse it (node request)', function (done) {6061var req = {62method: 'POST',63url: '/resource/4?filter=a',64headers: {65host: 'example.com:8080',66'content-type': 'text/plain;x=y'67}68};6970var payload = 'some not so random text';7172credentialsFunc('123456', function (err, credentials) {7374var reqHeader = Hawk.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'] });75req.headers.authorization = reqHeader.field;7677Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {7879expect(err).to.not.exist();80expect(credentials.user).to.equal('steve');81expect(artifacts.ext).to.equal('some-app-data');82expect(Hawk.server.authenticatePayload(payload, credentials, artifacts, req.headers['content-type'])).to.equal(true);8384var res = {85headers: {86'content-type': 'text/plain'87}88};8990res.headers['server-authorization'] = Hawk.server.header(credentials, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });91expect(res.headers['server-authorization']).to.exist();9293expect(Hawk.client.authenticate(res, credentials, artifacts, { payload: 'some reply' })).to.equal(true);94done();95});96});97});9899it('generates a header then successfully parse it (absolute request uri)', function (done) {100101var req = {102method: 'POST',103url: 'http://example.com:8080/resource/4?filter=a',104headers: {105host: 'example.com:8080',106'content-type': 'text/plain;x=y'107}108};109110var payload = 'some not so random text';111112credentialsFunc('123456', function (err, credentials) {113114var reqHeader = Hawk.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'] });115req.headers.authorization = reqHeader.field;116117Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {118119expect(err).to.not.exist();120expect(credentials.user).to.equal('steve');121expect(artifacts.ext).to.equal('some-app-data');122expect(Hawk.server.authenticatePayload(payload, credentials, artifacts, req.headers['content-type'])).to.equal(true);123124var res = {125headers: {126'content-type': 'text/plain'127}128};129130res.headers['server-authorization'] = Hawk.server.header(credentials, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });131expect(res.headers['server-authorization']).to.exist();132133expect(Hawk.client.authenticate(res, credentials, artifacts, { payload: 'some reply' })).to.equal(true);134done();135});136});137});138139it('generates a header then successfully parse it (no server header options)', function (done) {140141var req = {142method: 'POST',143url: '/resource/4?filter=a',144headers: {145host: 'example.com:8080',146'content-type': 'text/plain;x=y'147}148};149150var payload = 'some not so random text';151152credentialsFunc('123456', function (err, credentials) {153154var reqHeader = Hawk.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'] });155req.headers.authorization = reqHeader.field;156157Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {158159expect(err).to.not.exist();160expect(credentials.user).to.equal('steve');161expect(artifacts.ext).to.equal('some-app-data');162expect(Hawk.server.authenticatePayload(payload, credentials, artifacts, req.headers['content-type'])).to.equal(true);163164var res = {165headers: {166'content-type': 'text/plain'167}168};169170res.headers['server-authorization'] = Hawk.server.header(credentials, artifacts);171expect(res.headers['server-authorization']).to.exist();172173expect(Hawk.client.authenticate(res, credentials, artifacts)).to.equal(true);174done();175});176});177});178179it('generates a header then fails to parse it (missing server header hash)', function (done) {180181var req = {182method: 'POST',183url: '/resource/4?filter=a',184headers: {185host: 'example.com:8080',186'content-type': 'text/plain;x=y'187}188};189190var payload = 'some not so random text';191192credentialsFunc('123456', function (err, credentials) {193194var reqHeader = Hawk.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'] });195req.headers.authorization = reqHeader.field;196197Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {198199expect(err).to.not.exist();200expect(credentials.user).to.equal('steve');201expect(artifacts.ext).to.equal('some-app-data');202expect(Hawk.server.authenticatePayload(payload, credentials, artifacts, req.headers['content-type'])).to.equal(true);203204var res = {205headers: {206'content-type': 'text/plain'207}208};209210res.headers['server-authorization'] = Hawk.server.header(credentials, artifacts);211expect(res.headers['server-authorization']).to.exist();212213expect(Hawk.client.authenticate(res, credentials, artifacts, { payload: 'some reply' })).to.equal(false);214done();215});216});217});218219it('generates a header then successfully parse it (with hash)', function (done) {220221var req = {222method: 'GET',223url: '/resource/4?filter=a',224host: 'example.com',225port: 8080226};227228credentialsFunc('123456', function (err, credentials) {229230req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, payload: 'hola!', ext: 'some-app-data' }).field;231Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {232233expect(err).to.not.exist();234expect(credentials.user).to.equal('steve');235expect(artifacts.ext).to.equal('some-app-data');236done();237});238});239});240241it('generates a header then successfully parse it then validate payload', function (done) {242243var req = {244method: 'GET',245url: '/resource/4?filter=a',246host: 'example.com',247port: 8080248};249250credentialsFunc('123456', function (err, credentials) {251252req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, payload: 'hola!', ext: 'some-app-data' }).field;253Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {254255expect(err).to.not.exist();256expect(credentials.user).to.equal('steve');257expect(artifacts.ext).to.equal('some-app-data');258expect(Hawk.server.authenticatePayload('hola!', credentials, artifacts)).to.be.true();259expect(Hawk.server.authenticatePayload('hello!', credentials, artifacts)).to.be.false();260done();261});262});263});264265it('generates a header then successfully parses and validates payload', function (done) {266267var req = {268method: 'GET',269url: '/resource/4?filter=a',270host: 'example.com',271port: 8080272};273274credentialsFunc('123456', function (err, credentials) {275276req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, payload: 'hola!', ext: 'some-app-data' }).field;277Hawk.server.authenticate(req, credentialsFunc, { payload: 'hola!' }, function (err, credentials, artifacts) {278279expect(err).to.not.exist();280expect(credentials.user).to.equal('steve');281expect(artifacts.ext).to.equal('some-app-data');282done();283});284});285});286287it('generates a header then successfully parse it (app)', function (done) {288289var req = {290method: 'GET',291url: '/resource/4?filter=a',292host: 'example.com',293port: 8080294};295296credentialsFunc('123456', function (err, credentials) {297298req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, ext: 'some-app-data', app: 'asd23ased' }).field;299Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {300301expect(err).to.not.exist();302expect(credentials.user).to.equal('steve');303expect(artifacts.ext).to.equal('some-app-data');304expect(artifacts.app).to.equal('asd23ased');305done();306});307});308});309310it('generates a header then successfully parse it (app, dlg)', function (done) {311312var req = {313method: 'GET',314url: '/resource/4?filter=a',315host: 'example.com',316port: 8080317};318319credentialsFunc('123456', function (err, credentials) {320321req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, ext: 'some-app-data', app: 'asd23ased', dlg: '23434szr3q4d' }).field;322Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {323324expect(err).to.not.exist();325expect(credentials.user).to.equal('steve');326expect(artifacts.ext).to.equal('some-app-data');327expect(artifacts.app).to.equal('asd23ased');328expect(artifacts.dlg).to.equal('23434szr3q4d');329done();330});331});332});333334it('generates a header then fail authentication due to bad hash', function (done) {335336var req = {337method: 'GET',338url: '/resource/4?filter=a',339host: 'example.com',340port: 8080341};342343credentialsFunc('123456', function (err, credentials) {344345req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, payload: 'hola!', ext: 'some-app-data' }).field;346Hawk.server.authenticate(req, credentialsFunc, { payload: 'byebye!' }, function (err, credentials, artifacts) {347348expect(err).to.exist();349expect(err.output.payload.message).to.equal('Bad payload hash');350done();351});352});353});354355it('generates a header for one resource then fail to authenticate another', function (done) {356357var req = {358method: 'GET',359url: '/resource/4?filter=a',360host: 'example.com',361port: 8080362};363364credentialsFunc('123456', function (err, credentials) {365366req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, ext: 'some-app-data' }).field;367req.url = '/something/else';368369Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {370371expect(err).to.exist();372expect(credentials).to.exist();373done();374});375});376});377});378379380