index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // Load modules
  2. var Url = require('url');
  3. var Lab = require('lab');
  4. var Hawk = require('../lib');
  5. // Declare internals
  6. var internals = {};
  7. // Test shortcuts
  8. var expect = Lab.expect;
  9. var before = Lab.before;
  10. var after = Lab.after;
  11. var describe = Lab.experiment;
  12. var it = Lab.test;
  13. describe('Hawk', function () {
  14. var credentialsFunc = function (id, callback) {
  15. var credentials = {
  16. id: id,
  17. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  18. algorithm: (id === '1' ? 'sha1' : 'sha256'),
  19. user: 'steve'
  20. };
  21. return callback(null, credentials);
  22. };
  23. it('should generate a header then successfully parse it (configuration)', function (done) {
  24. var req = {
  25. method: 'GET',
  26. url: '/resource/4?filter=a',
  27. host: 'example.com',
  28. port: 8080
  29. };
  30. credentialsFunc('123456', function (err, credentials) {
  31. req.authorization = Hawk.client.header(Url.parse('http://example.com:8080/resource/4?filter=a'), req.method, { credentials: credentials, ext: 'some-app-data' }).field;
  32. expect(req.authorization).to.exist;
  33. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
  34. expect(err).to.not.exist;
  35. expect(credentials.user).to.equal('steve');
  36. expect(artifacts.ext).to.equal('some-app-data');
  37. done();
  38. });
  39. });
  40. });
  41. it('should generate a header then successfully parse it (node request)', function (done) {
  42. var req = {
  43. method: 'POST',
  44. url: '/resource/4?filter=a',
  45. headers: {
  46. host: 'example.com:8080',
  47. 'content-type': 'text/plain;x=y'
  48. }
  49. };
  50. var payload = 'some not so random text';
  51. credentialsFunc('123456', function (err, credentials) {
  52. var 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'] });
  53. req.headers.authorization = reqHeader.field;
  54. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
  55. expect(err).to.not.exist;
  56. expect(credentials.user).to.equal('steve');
  57. expect(artifacts.ext).to.equal('some-app-data');
  58. expect(Hawk.server.authenticatePayload(payload, credentials, artifacts, req.headers['content-type'])).to.equal(true);
  59. var res = {
  60. headers: {
  61. 'content-type': 'text/plain'
  62. }
  63. };
  64. res.headers['server-authorization'] = Hawk.server.header(credentials, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });
  65. expect(res.headers['server-authorization']).to.exist;
  66. expect(Hawk.client.authenticate(res, credentials, artifacts, { payload: 'some reply' })).to.equal(true);
  67. done();
  68. });
  69. });
  70. });
  71. it('should generate a header then successfully parse it (no server header options)', function (done) {
  72. var req = {
  73. method: 'POST',
  74. url: '/resource/4?filter=a',
  75. headers: {
  76. host: 'example.com:8080',
  77. 'content-type': 'text/plain;x=y'
  78. }
  79. };
  80. var payload = 'some not so random text';
  81. credentialsFunc('123456', function (err, credentials) {
  82. var 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'] });
  83. req.headers.authorization = reqHeader.field;
  84. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
  85. expect(err).to.not.exist;
  86. expect(credentials.user).to.equal('steve');
  87. expect(artifacts.ext).to.equal('some-app-data');
  88. expect(Hawk.server.authenticatePayload(payload, credentials, artifacts, req.headers['content-type'])).to.equal(true);
  89. var res = {
  90. headers: {
  91. 'content-type': 'text/plain'
  92. }
  93. };
  94. res.headers['server-authorization'] = Hawk.server.header(credentials, artifacts);
  95. expect(res.headers['server-authorization']).to.exist;
  96. expect(Hawk.client.authenticate(res, credentials, artifacts)).to.equal(true);
  97. done();
  98. });
  99. });
  100. });
  101. it('should generate a header then fails to parse it (missing server header hash)', function (done) {
  102. var req = {
  103. method: 'POST',
  104. url: '/resource/4?filter=a',
  105. headers: {
  106. host: 'example.com:8080',
  107. 'content-type': 'text/plain;x=y'
  108. }
  109. };
  110. var payload = 'some not so random text';
  111. credentialsFunc('123456', function (err, credentials) {
  112. var 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'] });
  113. req.headers.authorization = reqHeader.field;
  114. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
  115. expect(err).to.not.exist;
  116. expect(credentials.user).to.equal('steve');
  117. expect(artifacts.ext).to.equal('some-app-data');
  118. expect(Hawk.server.authenticatePayload(payload, credentials, artifacts, req.headers['content-type'])).to.equal(true);
  119. var res = {
  120. headers: {
  121. 'content-type': 'text/plain'
  122. }
  123. };
  124. res.headers['server-authorization'] = Hawk.server.header(credentials, artifacts);
  125. expect(res.headers['server-authorization']).to.exist;
  126. expect(Hawk.client.authenticate(res, credentials, artifacts, { payload: 'some reply' })).to.equal(false);
  127. done();
  128. });
  129. });
  130. });
  131. it('should generate a header then successfully parse it (with hash)', function (done) {
  132. var req = {
  133. method: 'GET',
  134. url: '/resource/4?filter=a',
  135. host: 'example.com',
  136. port: 8080
  137. };
  138. credentialsFunc('123456', function (err, credentials) {
  139. req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, payload: 'hola!', ext: 'some-app-data' }).field;
  140. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
  141. expect(err).to.not.exist;
  142. expect(credentials.user).to.equal('steve');
  143. expect(artifacts.ext).to.equal('some-app-data');
  144. done();
  145. });
  146. });
  147. });
  148. it('should generate a header then successfully parse it then validate payload', function (done) {
  149. var req = {
  150. method: 'GET',
  151. url: '/resource/4?filter=a',
  152. host: 'example.com',
  153. port: 8080
  154. };
  155. credentialsFunc('123456', function (err, credentials) {
  156. req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, payload: 'hola!', ext: 'some-app-data' }).field;
  157. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
  158. expect(err).to.not.exist;
  159. expect(credentials.user).to.equal('steve');
  160. expect(artifacts.ext).to.equal('some-app-data');
  161. expect(Hawk.server.authenticatePayload('hola!', credentials, artifacts)).to.be.true;
  162. expect(Hawk.server.authenticatePayload('hello!', credentials, artifacts)).to.be.false;
  163. done();
  164. });
  165. });
  166. });
  167. it('should generate a header then successfully parse it (app)', function (done) {
  168. var req = {
  169. method: 'GET',
  170. url: '/resource/4?filter=a',
  171. host: 'example.com',
  172. port: 8080
  173. };
  174. credentialsFunc('123456', function (err, credentials) {
  175. req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, ext: 'some-app-data', app: 'asd23ased' }).field;
  176. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
  177. expect(err).to.not.exist;
  178. expect(credentials.user).to.equal('steve');
  179. expect(artifacts.ext).to.equal('some-app-data');
  180. expect(artifacts.app).to.equal('asd23ased');
  181. done();
  182. });
  183. });
  184. });
  185. it('should generate a header then successfully parse it (app, dlg)', function (done) {
  186. var req = {
  187. method: 'GET',
  188. url: '/resource/4?filter=a',
  189. host: 'example.com',
  190. port: 8080
  191. };
  192. credentialsFunc('123456', function (err, credentials) {
  193. req.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;
  194. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
  195. expect(err).to.not.exist;
  196. expect(credentials.user).to.equal('steve');
  197. expect(artifacts.ext).to.equal('some-app-data');
  198. expect(artifacts.app).to.equal('asd23ased');
  199. expect(artifacts.dlg).to.equal('23434szr3q4d');
  200. done();
  201. });
  202. });
  203. });
  204. it('should generate a header then fail authentication due to bad hash', function (done) {
  205. var req = {
  206. method: 'GET',
  207. url: '/resource/4?filter=a',
  208. host: 'example.com',
  209. port: 8080
  210. };
  211. credentialsFunc('123456', function (err, credentials) {
  212. req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, payload: 'hola!', ext: 'some-app-data' }).field;
  213. Hawk.server.authenticate(req, credentialsFunc, { payload: 'byebye!' }, function (err, credentials, artifacts) {
  214. expect(err).to.exist;
  215. expect(err.response.payload.message).to.equal('Bad payload hash');
  216. done();
  217. });
  218. });
  219. });
  220. it('should generate a header for one resource then fail to authenticate another', function (done) {
  221. var req = {
  222. method: 'GET',
  223. url: '/resource/4?filter=a',
  224. host: 'example.com',
  225. port: 8080
  226. };
  227. credentialsFunc('123456', function (err, credentials) {
  228. req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials, ext: 'some-app-data' }).field;
  229. req.url = '/something/else';
  230. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
  231. expect(err).to.exist;
  232. expect(credentials).to.exist;
  233. done();
  234. });
  235. });
  236. });
  237. });