123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- // Load modules
- var Url = require('url');
- var Code = require('code');
- var Hawk = require('../lib');
- var Lab = require('lab');
- // Declare internals
- var internals = {};
- // Test shortcuts
- var lab = exports.lab = Lab.script();
- var describe = lab.experiment;
- var it = lab.test;
- var expect = Code.expect;
- describe('Hawk', function () {
- var credentialsFunc = function (id, callback) {
- var credentials = {
- id: id,
- key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
- algorithm: (id === '1' ? 'sha1' : 'sha256'),
- user: 'steve'
- };
- return callback(null, credentials);
- };
- it('generates 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, credentials1) {
- req.authorization = Hawk.client.header(Url.parse('http://example.com:8080/resource/4?filter=a'), req.method, { credentials: credentials1, ext: 'some-app-data' }).field;
- expect(req.authorization).to.exist();
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
- expect(err).to.not.exist();
- expect(credentials2.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- done();
- });
- });
- });
- it('generates 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, credentials1) {
- var reqHeader = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data', payload: payload, contentType: req.headers['content-type'] });
- req.headers.authorization = reqHeader.field;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
- expect(err).to.not.exist();
- expect(credentials2.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- expect(Hawk.server.authenticatePayload(payload, credentials2, artifacts, req.headers['content-type'])).to.equal(true);
- var res = {
- headers: {
- 'content-type': 'text/plain'
- }
- };
- res.headers['server-authorization'] = Hawk.server.header(credentials2, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });
- expect(res.headers['server-authorization']).to.exist();
- expect(Hawk.client.authenticate(res, credentials2, artifacts, { payload: 'some reply' })).to.equal(true);
- done();
- });
- });
- });
- it('generates a header then successfully parse it (absolute request uri)', function (done) {
- var req = {
- method: 'POST',
- url: 'http://example.com:8080/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, credentials1) {
- var reqHeader = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data', payload: payload, contentType: req.headers['content-type'] });
- req.headers.authorization = reqHeader.field;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
- expect(err).to.not.exist();
- expect(credentials2.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- expect(Hawk.server.authenticatePayload(payload, credentials2, artifacts, req.headers['content-type'])).to.equal(true);
- var res = {
- headers: {
- 'content-type': 'text/plain'
- }
- };
- res.headers['server-authorization'] = Hawk.server.header(credentials2, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });
- expect(res.headers['server-authorization']).to.exist();
- expect(Hawk.client.authenticate(res, credentials2, artifacts, { payload: 'some reply' })).to.equal(true);
- done();
- });
- });
- });
- it('generates 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, credentials1) {
- var reqHeader = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data', payload: payload, contentType: req.headers['content-type'] });
- req.headers.authorization = reqHeader.field;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
- expect(err).to.not.exist();
- expect(credentials2.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- expect(Hawk.server.authenticatePayload(payload, credentials2, artifacts, req.headers['content-type'])).to.equal(true);
- var res = {
- headers: {
- 'content-type': 'text/plain'
- }
- };
- res.headers['server-authorization'] = Hawk.server.header(credentials2, artifacts);
- expect(res.headers['server-authorization']).to.exist();
- expect(Hawk.client.authenticate(res, credentials2, artifacts)).to.equal(true);
- done();
- });
- });
- });
- it('generates 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, credentials1) {
- var reqHeader = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data', payload: payload, contentType: req.headers['content-type'] });
- req.headers.authorization = reqHeader.field;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
- expect(err).to.not.exist();
- expect(credentials2.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- expect(Hawk.server.authenticatePayload(payload, credentials2, artifacts, req.headers['content-type'])).to.equal(true);
- var res = {
- headers: {
- 'content-type': 'text/plain'
- }
- };
- res.headers['server-authorization'] = Hawk.server.header(credentials2, artifacts);
- expect(res.headers['server-authorization']).to.exist();
- expect(Hawk.client.authenticate(res, credentials2, artifacts, { payload: 'some reply' })).to.equal(false);
- done();
- });
- });
- });
- it('generates 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, credentials1) {
- req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, payload: 'hola!', ext: 'some-app-data' }).field;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
- expect(err).to.not.exist();
- expect(credentials2.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- done();
- });
- });
- });
- it('generates 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, credentials1) {
- req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, payload: 'hola!', ext: 'some-app-data' }).field;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
- expect(err).to.not.exist();
- expect(credentials2.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- expect(Hawk.server.authenticatePayload('hola!', credentials2, artifacts)).to.be.true();
- expect(Hawk.server.authenticatePayload('hello!', credentials2, artifacts)).to.be.false();
- done();
- });
- });
- });
- it('generates a header then successfully parses and validates payload', function (done) {
- var req = {
- method: 'GET',
- url: '/resource/4?filter=a',
- host: 'example.com',
- port: 8080
- };
- credentialsFunc('123456', function (err, credentials1) {
- req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, payload: 'hola!', ext: 'some-app-data' }).field;
- Hawk.server.authenticate(req, credentialsFunc, { payload: 'hola!' }, function (err, credentials2, artifacts) {
- expect(err).to.not.exist();
- expect(credentials2.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- done();
- });
- });
- });
- it('generates 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, credentials1) {
- req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data', app: 'asd23ased' }).field;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
- expect(err).to.not.exist();
- expect(credentials2.user).to.equal('steve');
- expect(artifacts.ext).to.equal('some-app-data');
- expect(artifacts.app).to.equal('asd23ased');
- done();
- });
- });
- });
- it('generates 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, credentials1) {
- req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data', app: 'asd23ased', dlg: '23434szr3q4d' }).field;
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
- expect(err).to.not.exist();
- expect(credentials2.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('generates 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, credentials1) {
- req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, payload: 'hola!', ext: 'some-app-data' }).field;
- Hawk.server.authenticate(req, credentialsFunc, { payload: 'byebye!' }, function (err, credentials2, artifacts) {
- expect(err).to.exist();
- expect(err.output.payload.message).to.equal('Bad payload hash');
- done();
- });
- });
- });
- it('generates 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, credentials1) {
- req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data' }).field;
- req.url = '/something/else';
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
- expect(err).to.exist();
- expect(credentials2).to.exist();
- done();
- });
- });
- });
- });
|