react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / hawk / test / uri.js
81146 views// Load modules12var Http = require('http');3var Url = require('url');4var Code = require('code');5var Hawk = require('../lib');6var Hoek = require('hoek');7var Lab = require('lab');8910// Declare internals1112var internals = {};131415// Test shortcuts1617var lab = exports.lab = Lab.script();18var describe = lab.experiment;19var it = lab.test;20var expect = Code.expect;212223describe('Hawk', function () {2425describe('Uri', function () {2627var credentialsFunc = function (id, callback) {2829var credentials = {30id: id,31key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',32algorithm: 'sha256',33user: 'steve'34};3536return callback(null, credentials);37};3839it('should generate a bewit then successfully authenticate it', function (done) {4041var req = {42method: 'GET',43url: '/resource/4?a=1&b=2',44host: 'example.com',45port: 8046};4748credentialsFunc('123456', function (err, credentials) {4950var bewit = Hawk.uri.getBewit('http://example.com/resource/4?a=1&b=2', { credentials: credentials, ttlSec: 60 * 60 * 24 * 365 * 100, ext: 'some-app-data' });51req.url += '&bewit=' + bewit;5253Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {5455expect(err).to.not.exist();56expect(credentials.user).to.equal('steve');57expect(attributes.ext).to.equal('some-app-data');58done();59});60});61});6263it('should generate a bewit then successfully authenticate it (no ext)', function (done) {6465var req = {66method: 'GET',67url: '/resource/4?a=1&b=2',68host: 'example.com',69port: 8070};7172credentialsFunc('123456', function (err, credentials) {7374var bewit = Hawk.uri.getBewit('http://example.com/resource/4?a=1&b=2', { credentials: credentials, ttlSec: 60 * 60 * 24 * 365 * 100 });75req.url += '&bewit=' + bewit;7677Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {7879expect(err).to.not.exist();80expect(credentials.user).to.equal('steve');81done();82});83});84});8586it('should successfully authenticate a request (last param)', function (done) {8788var req = {89method: 'GET',90url: '/resource/4?a=1&b=2&bewit=MTIzNDU2XDQ1MTE0ODQ2MjFcMzFjMmNkbUJFd1NJRVZDOVkva1NFb2c3d3YrdEVNWjZ3RXNmOGNHU2FXQT1cc29tZS1hcHAtZGF0YQ',91host: 'example.com',92port: 808093};9495Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {9697expect(err).to.not.exist();98expect(credentials.user).to.equal('steve');99expect(attributes.ext).to.equal('some-app-data');100done();101});102});103104it('should successfully authenticate a request (first param)', function (done) {105106var req = {107method: 'GET',108url: '/resource/4?bewit=MTIzNDU2XDQ1MTE0ODQ2MjFcMzFjMmNkbUJFd1NJRVZDOVkva1NFb2c3d3YrdEVNWjZ3RXNmOGNHU2FXQT1cc29tZS1hcHAtZGF0YQ&a=1&b=2',109host: 'example.com',110port: 8080111};112113Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {114115expect(err).to.not.exist();116expect(credentials.user).to.equal('steve');117expect(attributes.ext).to.equal('some-app-data');118done();119});120});121122it('should successfully authenticate a request (only param)', function (done) {123124var req = {125method: 'GET',126url: '/resource/4?bewit=MTIzNDU2XDQ1MTE0ODQ2NDFcZm1CdkNWT3MvcElOTUUxSTIwbWhrejQ3UnBwTmo4Y1VrSHpQd3Q5OXJ1cz1cc29tZS1hcHAtZGF0YQ',127host: 'example.com',128port: 8080129};130131Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {132133expect(err).to.not.exist();134expect(credentials.user).to.equal('steve');135expect(attributes.ext).to.equal('some-app-data');136done();137});138});139140it('should fail on multiple authentication', function (done) {141142var req = {143method: 'GET',144url: '/resource/4?bewit=MTIzNDU2XDQ1MTE0ODQ2NDFcZm1CdkNWT3MvcElOTUUxSTIwbWhrejQ3UnBwTmo4Y1VrSHpQd3Q5OXJ1cz1cc29tZS1hcHAtZGF0YQ',145host: 'example.com',146port: 8080,147authorization: 'Basic asdasdasdasd'148};149150Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {151152expect(err).to.exist();153expect(err.output.payload.message).to.equal('Multiple authentications');154done();155});156});157158it('should fail on method other than GET', function (done) {159160credentialsFunc('123456', function (err, credentials) {161162var req = {163method: 'POST',164url: '/resource/4?filter=a',165host: 'example.com',166port: 8080167};168169var exp = Math.floor(Hawk.utils.now() / 1000) + 60;170var ext = 'some-app-data';171var mac = Hawk.crypto.calculateMac('bewit', credentials, {172timestamp: exp,173nonce: '',174method: req.method,175resource: req.url,176host: req.host,177port: req.port,178ext: ext179});180181var bewit = credentials.id + '\\' + exp + '\\' + mac + '\\' + ext;182183req.url += '&bewit=' + Hoek.base64urlEncode(bewit);184185Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {186187expect(err).to.exist();188expect(err.output.payload.message).to.equal('Invalid method');189done();190});191});192});193194it('should fail on invalid host header', function (done) {195196var req = {197method: 'GET',198url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',199headers: {200host: 'example.com:something'201}202};203204Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {205206expect(err).to.exist();207expect(err.output.payload.message).to.equal('Invalid Host header');208done();209});210});211212it('should fail on empty bewit', function (done) {213214var req = {215method: 'GET',216url: '/resource/4?bewit=',217host: 'example.com',218port: 8080219};220221Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {222223expect(err).to.exist();224expect(err.output.payload.message).to.equal('Empty bewit');225expect(err.isMissing).to.not.exist();226done();227});228});229230it('should fail on invalid bewit', function (done) {231232var req = {233method: 'GET',234url: '/resource/4?bewit=*',235host: 'example.com',236port: 8080237};238239Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {240241expect(err).to.exist();242expect(err.output.payload.message).to.equal('Invalid bewit encoding');243expect(err.isMissing).to.not.exist();244done();245});246});247248it('should fail on missing bewit', function (done) {249250var req = {251method: 'GET',252url: '/resource/4',253host: 'example.com',254port: 8080255};256257Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {258259expect(err).to.exist();260expect(err.output.payload.message).to.not.exist();261expect(err.isMissing).to.equal(true);262done();263});264});265266it('should fail on invalid bewit structure', function (done) {267268var req = {269method: 'GET',270url: '/resource/4?bewit=abc',271host: 'example.com',272port: 8080273};274275Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {276277expect(err).to.exist();278expect(err.output.payload.message).to.equal('Invalid bewit structure');279done();280});281});282283it('should fail on empty bewit attribute', function (done) {284285var req = {286method: 'GET',287url: '/resource/4?bewit=YVxcY1xk',288host: 'example.com',289port: 8080290};291292Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {293294expect(err).to.exist();295expect(err.output.payload.message).to.equal('Missing bewit attributes');296done();297});298});299300it('should fail on missing bewit id attribute', function (done) {301302var req = {303method: 'GET',304url: '/resource/4?bewit=XDQ1NTIxNDc2MjJcK0JFbFhQMXhuWjcvd1Nrbm1ldGhlZm5vUTNHVjZNSlFVRHk4NWpTZVJ4VT1cc29tZS1hcHAtZGF0YQ',305host: 'example.com',306port: 8080307};308309Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {310311expect(err).to.exist();312expect(err.output.payload.message).to.equal('Missing bewit attributes');313done();314});315});316317it('should fail on expired access', function (done) {318319var req = {320method: 'GET',321url: '/resource/4?a=1&b=2&bewit=MTIzNDU2XDEzNTY0MTg1ODNcWk1wZlMwWU5KNHV0WHpOMmRucTRydEk3NXNXTjFjeWVITTcrL0tNZFdVQT1cc29tZS1hcHAtZGF0YQ',322host: 'example.com',323port: 8080324};325326Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {327328expect(err).to.exist();329expect(err.output.payload.message).to.equal('Access expired');330done();331});332});333334it('should fail on credentials function error', function (done) {335336var req = {337method: 'GET',338url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',339host: 'example.com',340port: 8080341};342343Hawk.uri.authenticate(req, function (id, callback) { callback(Hawk.error.badRequest('Boom')); }, {}, function (err, credentials, attributes) {344345expect(err).to.exist();346expect(err.output.payload.message).to.equal('Boom');347done();348});349});350351it('should fail on credentials function error with credentials', function (done) {352353var req = {354method: 'GET',355url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',356host: 'example.com',357port: 8080358};359360Hawk.uri.authenticate(req, function (id, callback) { callback(Hawk.error.badRequest('Boom'), { some: 'value' }); }, {}, function (err, credentials, attributes) {361362expect(err).to.exist();363expect(err.output.payload.message).to.equal('Boom');364expect(credentials.some).to.equal('value');365done();366});367});368369it('should fail on null credentials function response', function (done) {370371var req = {372method: 'GET',373url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',374host: 'example.com',375port: 8080376};377378Hawk.uri.authenticate(req, function (id, callback) { callback(null, null); }, {}, function (err, credentials, attributes) {379380expect(err).to.exist();381expect(err.output.payload.message).to.equal('Unknown credentials');382done();383});384});385386it('should fail on invalid credentials function response', function (done) {387388var req = {389method: 'GET',390url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',391host: 'example.com',392port: 8080393};394395Hawk.uri.authenticate(req, function (id, callback) { callback(null, {}); }, {}, function (err, credentials, attributes) {396397expect(err).to.exist();398expect(err.message).to.equal('Invalid credentials');399done();400});401});402403it('should fail on invalid credentials function response (unknown algorithm)', function (done) {404405var req = {406method: 'GET',407url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',408host: 'example.com',409port: 8080410};411412Hawk.uri.authenticate(req, function (id, callback) { callback(null, { key: 'xxx', algorithm: 'xxx' }); }, {}, function (err, credentials, attributes) {413414expect(err).to.exist();415expect(err.message).to.equal('Unknown algorithm');416done();417});418});419420it('should fail on expired access', function (done) {421422var req = {423method: 'GET',424url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',425host: 'example.com',426port: 8080427};428429Hawk.uri.authenticate(req, function (id, callback) { callback(null, { key: 'xxx', algorithm: 'sha256' }); }, {}, function (err, credentials, attributes) {430431expect(err).to.exist();432expect(err.output.payload.message).to.equal('Bad mac');433done();434});435});436});437438describe('#getBewit', function () {439440it('returns a valid bewit value', function (done) {441442var credentials = {443id: '123456',444key: '2983d45yun89q',445algorithm: 'sha256'446};447448var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow', { credentials: credentials, ttlSec: 300, localtimeOffsetMsec: 1356420407232 - Hawk.utils.now(), ext: 'xandyandz' });449expect(bewit).to.equal('MTIzNDU2XDEzNTY0MjA3MDdca3NjeHdOUjJ0SnBQMVQxekRMTlBiQjVVaUtJVTl0T1NKWFRVZEc3WDloOD1ceGFuZHlhbmR6');450done();451});452453it('returns a valid bewit value (explicit port)', function (done) {454455var credentials = {456id: '123456',457key: '2983d45yun89q',458algorithm: 'sha256'459};460461var bewit = Hawk.uri.getBewit('https://example.com:8080/somewhere/over/the/rainbow', { credentials: credentials, ttlSec: 300, localtimeOffsetMsec: 1356420407232 - Hawk.utils.now(), ext: 'xandyandz' });462expect(bewit).to.equal('MTIzNDU2XDEzNTY0MjA3MDdcaFpiSjNQMmNLRW80a3kwQzhqa1pBa1J5Q1p1ZWc0V1NOYnhWN3ZxM3hIVT1ceGFuZHlhbmR6');463done();464});465466it('returns a valid bewit value (null ext)', function (done) {467468var credentials = {469id: '123456',470key: '2983d45yun89q',471algorithm: 'sha256'472};473474var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow', { credentials: credentials, ttlSec: 300, localtimeOffsetMsec: 1356420407232 - Hawk.utils.now(), ext: null });475expect(bewit).to.equal('MTIzNDU2XDEzNTY0MjA3MDdcSUdZbUxnSXFMckNlOEN4dktQczRKbFdJQStValdKSm91d2dBUmlWaENBZz1c');476done();477});478479it('returns a valid bewit value (parsed uri)', function (done) {480481var credentials = {482id: '123456',483key: '2983d45yun89q',484algorithm: 'sha256'485};486487var bewit = Hawk.uri.getBewit(Url.parse('https://example.com/somewhere/over/the/rainbow'), { credentials: credentials, ttlSec: 300, localtimeOffsetMsec: 1356420407232 - Hawk.utils.now(), ext: 'xandyandz' });488expect(bewit).to.equal('MTIzNDU2XDEzNTY0MjA3MDdca3NjeHdOUjJ0SnBQMVQxekRMTlBiQjVVaUtJVTl0T1NKWFRVZEc3WDloOD1ceGFuZHlhbmR6');489done();490});491492it('errors on invalid options', function (done) {493494var credentials = {495id: '123456',496key: '2983d45yun89q',497algorithm: 'sha256'498};499500var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow', 4);501expect(bewit).to.equal('');502done();503});504505it('errors on missing uri', function (done) {506507var credentials = {508id: '123456',509key: '2983d45yun89q',510algorithm: 'sha256'511};512513var bewit = Hawk.uri.getBewit('', { credentials: credentials, ttlSec: 300, localtimeOffsetMsec: 1356420407232 - Hawk.utils.now(), ext: 'xandyandz' });514expect(bewit).to.equal('');515done();516});517518it('errors on invalid uri', function (done) {519520var credentials = {521id: '123456',522key: '2983d45yun89q',523algorithm: 'sha256'524};525526var bewit = Hawk.uri.getBewit(5, { credentials: credentials, ttlSec: 300, localtimeOffsetMsec: 1356420407232 - Hawk.utils.now(), ext: 'xandyandz' });527expect(bewit).to.equal('');528done();529});530531it('errors on invalid credentials (id)', function (done) {532533var credentials = {534key: '2983d45yun89q',535algorithm: 'sha256'536};537538var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow', { credentials: credentials, ttlSec: 3000, ext: 'xandyandz' });539expect(bewit).to.equal('');540done();541});542543it('errors on missing credentials', function (done) {544545var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow', { ttlSec: 3000, ext: 'xandyandz' });546expect(bewit).to.equal('');547done();548});549550it('errors on invalid credentials (key)', function (done) {551552var credentials = {553id: '123456',554algorithm: 'sha256'555};556557var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow', { credentials: credentials, ttlSec: 3000, ext: 'xandyandz' });558expect(bewit).to.equal('');559done();560});561562it('errors on invalid algorithm', function (done) {563564var credentials = {565id: '123456',566key: '2983d45yun89q',567algorithm: 'hmac-sha-0'568};569570var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow', { credentials: credentials, ttlSec: 300, ext: 'xandyandz' });571expect(bewit).to.equal('');572done();573});574575it('errors on missing options', function (done) {576577var credentials = {578id: '123456',579key: '2983d45yun89q',580algorithm: 'hmac-sha-0'581};582583var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow');584expect(bewit).to.equal('');585done();586});587});588});589590591592