UI for Zipcoin Blue

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. // Load modules
  2. var Url = require('url');
  3. var Code = require('code');
  4. var Hawk = require('../lib');
  5. var Hoek = require('hoek');
  6. var Lab = require('lab');
  7. // Declare internals
  8. var internals = {};
  9. // Test shortcuts
  10. var lab = exports.lab = Lab.script();
  11. var describe = lab.experiment;
  12. var it = lab.test;
  13. var expect = Code.expect;
  14. describe('Server', function () {
  15. var credentialsFunc = function (id, callback) {
  16. var credentials = {
  17. id: id,
  18. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  19. algorithm: (id === '1' ? 'sha1' : 'sha256'),
  20. user: 'steve'
  21. };
  22. return callback(null, credentials);
  23. };
  24. describe('authenticate()', function () {
  25. it('parses a valid authentication header (sha1)', function (done) {
  26. var req = {
  27. method: 'GET',
  28. url: '/resource/4?filter=a',
  29. host: 'example.com',
  30. port: 8080,
  31. authorization: 'Hawk id="1", ts="1353788437", nonce="k3j4h2", mac="zy79QQ5/EYFmQqutVnYb73gAc/U=", ext="hello"'
  32. };
  33. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  34. expect(err).to.not.exist();
  35. expect(credentials.user).to.equal('steve');
  36. done();
  37. });
  38. });
  39. it('parses a valid authentication header (sha256)', function (done) {
  40. var req = {
  41. method: 'GET',
  42. url: '/resource/1?b=1&a=2',
  43. host: 'example.com',
  44. port: 8000,
  45. authorization: 'Hawk id="dh37fgj492je", ts="1353832234", nonce="j4h3g2", mac="m8r1rHbXN6NgO+KIIhjO7sFRyd78RNGVUwehe8Cp2dU=", ext="some-app-data"'
  46. };
  47. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353832234000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  48. expect(err).to.not.exist();
  49. expect(credentials.user).to.equal('steve');
  50. done();
  51. });
  52. });
  53. it('parses a valid authentication header (host override)', function (done) {
  54. var req = {
  55. method: 'GET',
  56. url: '/resource/4?filter=a',
  57. headers: {
  58. host: 'example1.com:8080',
  59. authorization: 'Hawk id="1", ts="1353788437", nonce="k3j4h2", mac="zy79QQ5/EYFmQqutVnYb73gAc/U=", ext="hello"'
  60. }
  61. };
  62. Hawk.server.authenticate(req, credentialsFunc, { host: 'example.com', localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  63. expect(err).to.not.exist();
  64. expect(credentials.user).to.equal('steve');
  65. done();
  66. });
  67. });
  68. it('parses a valid authentication header (host port override)', function (done) {
  69. var req = {
  70. method: 'GET',
  71. url: '/resource/4?filter=a',
  72. headers: {
  73. host: 'example1.com:80',
  74. authorization: 'Hawk id="1", ts="1353788437", nonce="k3j4h2", mac="zy79QQ5/EYFmQqutVnYb73gAc/U=", ext="hello"'
  75. }
  76. };
  77. Hawk.server.authenticate(req, credentialsFunc, { host: 'example.com', port: 8080, localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  78. expect(err).to.not.exist();
  79. expect(credentials.user).to.equal('steve');
  80. done();
  81. });
  82. });
  83. it('parses a valid authentication header (POST with payload)', function (done) {
  84. var req = {
  85. method: 'POST',
  86. url: '/resource/4?filter=a',
  87. host: 'example.com',
  88. port: 8080,
  89. authorization: 'Hawk id="123456", ts="1357926341", nonce="1AwuJD", hash="qAiXIVv+yjDATneWxZP2YCTa9aHRgQdnH9b3Wc+o3dg=", ext="some-app-data", mac="UeYcj5UoTVaAWXNvJfLVia7kU3VabxCqrccXP8sUGC4="'
  90. };
  91. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1357926341000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  92. expect(err).to.not.exist();
  93. expect(credentials.user).to.equal('steve');
  94. done();
  95. });
  96. });
  97. it('errors on missing hash', function (done) {
  98. var req = {
  99. method: 'GET',
  100. url: '/resource/1?b=1&a=2',
  101. host: 'example.com',
  102. port: 8000,
  103. authorization: 'Hawk id="dh37fgj492je", ts="1353832234", nonce="j4h3g2", mac="m8r1rHbXN6NgO+KIIhjO7sFRyd78RNGVUwehe8Cp2dU=", ext="some-app-data"'
  104. };
  105. Hawk.server.authenticate(req, credentialsFunc, { payload: 'body', localtimeOffsetMsec: 1353832234000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  106. expect(err).to.exist();
  107. expect(err.output.payload.message).to.equal('Missing required payload hash');
  108. done();
  109. });
  110. });
  111. it('errors on a stale timestamp', function (done) {
  112. var req = {
  113. method: 'GET',
  114. url: '/resource/4?filter=a',
  115. host: 'example.com',
  116. port: 8080,
  117. authorization: 'Hawk id="123456", ts="1362337299", nonce="UzmxSs", ext="some-app-data", mac="wnNUxchvvryMH2RxckTdZ/gY3ijzvccx4keVvELC61w="'
  118. };
  119. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
  120. expect(err).to.exist();
  121. expect(err.output.payload.message).to.equal('Stale timestamp');
  122. var header = err.output.headers['WWW-Authenticate'];
  123. var ts = header.match(/^Hawk ts\=\"(\d+)\"\, tsm\=\"([^\"]+)\"\, error=\"Stale timestamp\"$/);
  124. var now = Hawk.utils.now();
  125. expect(parseInt(ts[1], 10) * 1000).to.be.within(now - 1000, now + 1000);
  126. var res = {
  127. headers: {
  128. 'www-authenticate': header
  129. }
  130. };
  131. expect(Hawk.client.authenticate(res, credentials, artifacts)).to.equal(true);
  132. done();
  133. });
  134. });
  135. it('errors on a replay', function (done) {
  136. var req = {
  137. method: 'GET',
  138. url: '/resource/4?filter=a',
  139. host: 'example.com',
  140. port: 8080,
  141. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="bXx7a7p1h9QYQNZ8x7QhvDQym8ACgab4m3lVSFn4DBw=", ext="hello"'
  142. };
  143. var memoryCache = {};
  144. var options = {
  145. localtimeOffsetMsec: 1353788437000 - Hawk.utils.now(),
  146. nonceFunc: function (key, nonce, ts, callback) {
  147. if (memoryCache[key + nonce]) {
  148. return callback(new Error());
  149. }
  150. memoryCache[key + nonce] = true;
  151. return callback();
  152. }
  153. };
  154. Hawk.server.authenticate(req, credentialsFunc, options, function (err, credentials1, artifacts1) {
  155. expect(err).to.not.exist();
  156. expect(credentials1.user).to.equal('steve');
  157. Hawk.server.authenticate(req, credentialsFunc, options, function (err, credentials2, artifacts2) {
  158. expect(err).to.exist();
  159. expect(err.output.payload.message).to.equal('Invalid nonce');
  160. done();
  161. });
  162. });
  163. });
  164. it('does not error on nonce collision if keys differ', function (done) {
  165. var reqSteve = {
  166. method: 'GET',
  167. url: '/resource/4?filter=a',
  168. host: 'example.com',
  169. port: 8080,
  170. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="bXx7a7p1h9QYQNZ8x7QhvDQym8ACgab4m3lVSFn4DBw=", ext="hello"'
  171. };
  172. var reqBob = {
  173. method: 'GET',
  174. url: '/resource/4?filter=a',
  175. host: 'example.com',
  176. port: 8080,
  177. authorization: 'Hawk id="456", ts="1353788437", nonce="k3j4h2", mac="LXfmTnRzrLd9TD7yfH+4se46Bx6AHyhpM94hLCiNia4=", ext="hello"'
  178. };
  179. var credentialsFuncion = function (id, callback) {
  180. var credentials = {
  181. '123': {
  182. id: id,
  183. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  184. algorithm: (id === '1' ? 'sha1' : 'sha256'),
  185. user: 'steve'
  186. },
  187. '456': {
  188. id: id,
  189. key: 'xrunpaw3489ruxnpa98w4rxnwerxhqb98rpaxn39848',
  190. algorithm: (id === '1' ? 'sha1' : 'sha256'),
  191. user: 'bob'
  192. }
  193. };
  194. return callback(null, credentials[id]);
  195. };
  196. var memoryCache = {};
  197. var options = {
  198. localtimeOffsetMsec: 1353788437000 - Hawk.utils.now(),
  199. nonceFunc: function (key, nonce, ts, callback) {
  200. if (memoryCache[key + nonce]) {
  201. return callback(new Error());
  202. }
  203. memoryCache[key + nonce] = true;
  204. return callback();
  205. }
  206. };
  207. Hawk.server.authenticate(reqSteve, credentialsFuncion, options, function (err, credentials1, artifacts1) {
  208. expect(err).to.not.exist();
  209. expect(credentials1.user).to.equal('steve');
  210. Hawk.server.authenticate(reqBob, credentialsFuncion, options, function (err, credentials2, artifacts2) {
  211. expect(err).to.not.exist();
  212. expect(credentials2.user).to.equal('bob');
  213. done();
  214. });
  215. });
  216. });
  217. it('errors on an invalid authentication header: wrong scheme', function (done) {
  218. var req = {
  219. method: 'GET',
  220. url: '/resource/4?filter=a',
  221. host: 'example.com',
  222. port: 8080,
  223. authorization: 'Basic asdasdasdasd'
  224. };
  225. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  226. expect(err).to.exist();
  227. expect(err.output.payload.message).to.not.exist();
  228. done();
  229. });
  230. });
  231. it('errors on an invalid authentication header: no scheme', function (done) {
  232. var req = {
  233. method: 'GET',
  234. url: '/resource/4?filter=a',
  235. host: 'example.com',
  236. port: 8080,
  237. authorization: '!@#'
  238. };
  239. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  240. expect(err).to.exist();
  241. expect(err.output.payload.message).to.equal('Invalid header syntax');
  242. done();
  243. });
  244. });
  245. it('errors on an missing authorization header', function (done) {
  246. var req = {
  247. method: 'GET',
  248. url: '/resource/4?filter=a',
  249. host: 'example.com',
  250. port: 8080
  251. };
  252. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
  253. expect(err).to.exist();
  254. expect(err.isMissing).to.equal(true);
  255. done();
  256. });
  257. });
  258. it('errors on an missing host header', function (done) {
  259. var req = {
  260. method: 'GET',
  261. url: '/resource/4?filter=a',
  262. headers: {
  263. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  264. }
  265. };
  266. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  267. expect(err).to.exist();
  268. expect(err.output.payload.message).to.equal('Invalid Host header');
  269. done();
  270. });
  271. });
  272. it('errors on an missing authorization attribute (id)', function (done) {
  273. var req = {
  274. method: 'GET',
  275. url: '/resource/4?filter=a',
  276. host: 'example.com',
  277. port: 8080,
  278. authorization: 'Hawk ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  279. };
  280. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  281. expect(err).to.exist();
  282. expect(err.output.payload.message).to.equal('Missing attributes');
  283. done();
  284. });
  285. });
  286. it('errors on an missing authorization attribute (ts)', function (done) {
  287. var req = {
  288. method: 'GET',
  289. url: '/resource/4?filter=a',
  290. host: 'example.com',
  291. port: 8080,
  292. authorization: 'Hawk id="123", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  293. };
  294. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  295. expect(err).to.exist();
  296. expect(err.output.payload.message).to.equal('Missing attributes');
  297. done();
  298. });
  299. });
  300. it('errors on an missing authorization attribute (nonce)', function (done) {
  301. var req = {
  302. method: 'GET',
  303. url: '/resource/4?filter=a',
  304. host: 'example.com',
  305. port: 8080,
  306. authorization: 'Hawk id="123", ts="1353788437", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  307. };
  308. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  309. expect(err).to.exist();
  310. expect(err.output.payload.message).to.equal('Missing attributes');
  311. done();
  312. });
  313. });
  314. it('errors on an missing authorization attribute (mac)', function (done) {
  315. var req = {
  316. method: 'GET',
  317. url: '/resource/4?filter=a',
  318. host: 'example.com',
  319. port: 8080,
  320. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", ext="hello"'
  321. };
  322. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  323. expect(err).to.exist();
  324. expect(err.output.payload.message).to.equal('Missing attributes');
  325. done();
  326. });
  327. });
  328. it('errors on an unknown authorization attribute', function (done) {
  329. var req = {
  330. method: 'GET',
  331. url: '/resource/4?filter=a',
  332. host: 'example.com',
  333. port: 8080,
  334. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", x="3", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  335. };
  336. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  337. expect(err).to.exist();
  338. expect(err.output.payload.message).to.equal('Unknown attribute: x');
  339. done();
  340. });
  341. });
  342. it('errors on an bad authorization header format', function (done) {
  343. var req = {
  344. method: 'GET',
  345. url: '/resource/4?filter=a',
  346. host: 'example.com',
  347. port: 8080,
  348. authorization: 'Hawk id="123\\", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  349. };
  350. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  351. expect(err).to.exist();
  352. expect(err.output.payload.message).to.equal('Bad header format');
  353. done();
  354. });
  355. });
  356. it('errors on an bad authorization attribute value', function (done) {
  357. var req = {
  358. method: 'GET',
  359. url: '/resource/4?filter=a',
  360. host: 'example.com',
  361. port: 8080,
  362. authorization: 'Hawk id="\t", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  363. };
  364. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  365. expect(err).to.exist();
  366. expect(err.output.payload.message).to.equal('Bad attribute value: id');
  367. done();
  368. });
  369. });
  370. it('errors on an empty authorization attribute value', function (done) {
  371. var req = {
  372. method: 'GET',
  373. url: '/resource/4?filter=a',
  374. host: 'example.com',
  375. port: 8080,
  376. authorization: 'Hawk id="", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  377. };
  378. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  379. expect(err).to.exist();
  380. expect(err.output.payload.message).to.equal('Bad attribute value: id');
  381. done();
  382. });
  383. });
  384. it('errors on duplicated authorization attribute key', function (done) {
  385. var req = {
  386. method: 'GET',
  387. url: '/resource/4?filter=a',
  388. host: 'example.com',
  389. port: 8080,
  390. authorization: 'Hawk id="123", id="456", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  391. };
  392. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  393. expect(err).to.exist();
  394. expect(err.output.payload.message).to.equal('Duplicate attribute: id');
  395. done();
  396. });
  397. });
  398. it('errors on an invalid authorization header format', function (done) {
  399. var req = {
  400. method: 'GET',
  401. url: '/resource/4?filter=a',
  402. host: 'example.com',
  403. port: 8080,
  404. authorization: 'Hawk'
  405. };
  406. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  407. expect(err).to.exist();
  408. expect(err.output.payload.message).to.equal('Invalid header syntax');
  409. done();
  410. });
  411. });
  412. it('errors on an bad host header (missing host)', function (done) {
  413. var req = {
  414. method: 'GET',
  415. url: '/resource/4?filter=a',
  416. headers: {
  417. host: ':8080',
  418. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  419. }
  420. };
  421. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  422. expect(err).to.exist();
  423. expect(err.output.payload.message).to.equal('Invalid Host header');
  424. done();
  425. });
  426. });
  427. it('errors on an bad host header (pad port)', function (done) {
  428. var req = {
  429. method: 'GET',
  430. url: '/resource/4?filter=a',
  431. headers: {
  432. host: 'example.com:something',
  433. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  434. }
  435. };
  436. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  437. expect(err).to.exist();
  438. expect(err.output.payload.message).to.equal('Invalid Host header');
  439. done();
  440. });
  441. });
  442. it('errors on credentialsFunc error', function (done) {
  443. var req = {
  444. method: 'GET',
  445. url: '/resource/4?filter=a',
  446. host: 'example.com',
  447. port: 8080,
  448. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  449. };
  450. var credentialsFuncion = function (id, callback) {
  451. return callback(new Error('Unknown user'));
  452. };
  453. Hawk.server.authenticate(req, credentialsFuncion, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  454. expect(err).to.exist();
  455. expect(err.message).to.equal('Unknown user');
  456. done();
  457. });
  458. });
  459. it('errors on credentialsFunc error (with credentials)', function (done) {
  460. var req = {
  461. method: 'GET',
  462. url: '/resource/4?filter=a',
  463. host: 'example.com',
  464. port: 8080,
  465. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  466. };
  467. var credentialsFuncion = function (id, callback) {
  468. return callback(new Error('Unknown user'), { some: 'value' });
  469. };
  470. Hawk.server.authenticate(req, credentialsFuncion, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  471. expect(err).to.exist();
  472. expect(err.message).to.equal('Unknown user');
  473. expect(credentials.some).to.equal('value');
  474. done();
  475. });
  476. });
  477. it('errors on missing credentials', function (done) {
  478. var req = {
  479. method: 'GET',
  480. url: '/resource/4?filter=a',
  481. host: 'example.com',
  482. port: 8080,
  483. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  484. };
  485. var credentialsFuncion = function (id, callback) {
  486. return callback(null, null);
  487. };
  488. Hawk.server.authenticate(req, credentialsFuncion, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  489. expect(err).to.exist();
  490. expect(err.output.payload.message).to.equal('Unknown credentials');
  491. done();
  492. });
  493. });
  494. it('errors on invalid credentials (id)', function (done) {
  495. var req = {
  496. method: 'GET',
  497. url: '/resource/4?filter=a',
  498. host: 'example.com',
  499. port: 8080,
  500. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  501. };
  502. var credentialsFuncion = function (id, callback) {
  503. var credentials = {
  504. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  505. user: 'steve'
  506. };
  507. return callback(null, credentials);
  508. };
  509. Hawk.server.authenticate(req, credentialsFuncion, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  510. expect(err).to.exist();
  511. expect(err.message).to.equal('Invalid credentials');
  512. expect(err.output.payload.message).to.equal('An internal server error occurred');
  513. done();
  514. });
  515. });
  516. it('errors on invalid credentials (key)', function (done) {
  517. var req = {
  518. method: 'GET',
  519. url: '/resource/4?filter=a',
  520. host: 'example.com',
  521. port: 8080,
  522. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  523. };
  524. var credentialsFuncion = function (id, callback) {
  525. var credentials = {
  526. id: '23434d3q4d5345d',
  527. user: 'steve'
  528. };
  529. return callback(null, credentials);
  530. };
  531. Hawk.server.authenticate(req, credentialsFuncion, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  532. expect(err).to.exist();
  533. expect(err.message).to.equal('Invalid credentials');
  534. expect(err.output.payload.message).to.equal('An internal server error occurred');
  535. done();
  536. });
  537. });
  538. it('errors on unknown credentials algorithm', function (done) {
  539. var req = {
  540. method: 'GET',
  541. url: '/resource/4?filter=a',
  542. host: 'example.com',
  543. port: 8080,
  544. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  545. };
  546. var credentialsFuncion = function (id, callback) {
  547. var credentials = {
  548. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  549. algorithm: 'hmac-sha-0',
  550. user: 'steve'
  551. };
  552. return callback(null, credentials);
  553. };
  554. Hawk.server.authenticate(req, credentialsFuncion, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  555. expect(err).to.exist();
  556. expect(err.message).to.equal('Unknown algorithm');
  557. expect(err.output.payload.message).to.equal('An internal server error occurred');
  558. done();
  559. });
  560. });
  561. it('errors on unknown bad mac', function (done) {
  562. var req = {
  563. method: 'GET',
  564. url: '/resource/4?filter=a',
  565. host: 'example.com',
  566. port: 8080,
  567. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcU4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  568. };
  569. var credentialsFuncion = function (id, callback) {
  570. var credentials = {
  571. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  572. algorithm: 'sha256',
  573. user: 'steve'
  574. };
  575. return callback(null, credentials);
  576. };
  577. Hawk.server.authenticate(req, credentialsFuncion, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  578. expect(err).to.exist();
  579. expect(err.output.payload.message).to.equal('Bad mac');
  580. done();
  581. });
  582. });
  583. });
  584. describe('header()', function () {
  585. it('generates header', function (done) {
  586. var credentials = {
  587. id: '123456',
  588. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  589. algorithm: 'sha256',
  590. user: 'steve'
  591. };
  592. var artifacts = {
  593. method: 'POST',
  594. host: 'example.com',
  595. port: '8080',
  596. resource: '/resource/4?filter=a',
  597. ts: '1398546787',
  598. nonce: 'xUwusx',
  599. hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
  600. ext: 'some-app-data',
  601. mac: 'dvIvMThwi28J61Jc3P0ryAhuKpanU63GXdx6hkmQkJA=',
  602. id: '123456'
  603. };
  604. var header = Hawk.server.header(credentials, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });
  605. expect(header).to.equal('Hawk mac=\"n14wVJK4cOxAytPUMc5bPezQzuJGl5n7MYXhFQgEKsE=\", hash=\"f9cDF/TDm7TkYRLnGwRMfeDzT6LixQVLvrIKhh0vgmM=\", ext=\"response-specific\"');
  606. done();
  607. });
  608. it('generates header (empty payload)', function (done) {
  609. var credentials = {
  610. id: '123456',
  611. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  612. algorithm: 'sha256',
  613. user: 'steve'
  614. };
  615. var artifacts = {
  616. method: 'POST',
  617. host: 'example.com',
  618. port: '8080',
  619. resource: '/resource/4?filter=a',
  620. ts: '1398546787',
  621. nonce: 'xUwusx',
  622. hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
  623. ext: 'some-app-data',
  624. mac: 'dvIvMThwi28J61Jc3P0ryAhuKpanU63GXdx6hkmQkJA=',
  625. id: '123456'
  626. };
  627. var header = Hawk.server.header(credentials, artifacts, { payload: '', contentType: 'text/plain', ext: 'response-specific' });
  628. expect(header).to.equal('Hawk mac=\"i8/kUBDx0QF+PpCtW860kkV/fa9dbwEoe/FpGUXowf0=\", hash=\"q/t+NNAkQZNlq/aAD6PlexImwQTxwgT2MahfTa9XRLA=\", ext=\"response-specific\"');
  629. done();
  630. });
  631. it('generates header (pre calculated hash)', function (done) {
  632. var credentials = {
  633. id: '123456',
  634. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  635. algorithm: 'sha256',
  636. user: 'steve'
  637. };
  638. var artifacts = {
  639. method: 'POST',
  640. host: 'example.com',
  641. port: '8080',
  642. resource: '/resource/4?filter=a',
  643. ts: '1398546787',
  644. nonce: 'xUwusx',
  645. hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
  646. ext: 'some-app-data',
  647. mac: 'dvIvMThwi28J61Jc3P0ryAhuKpanU63GXdx6hkmQkJA=',
  648. id: '123456'
  649. };
  650. var options = { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' };
  651. options.hash = Hawk.crypto.calculatePayloadHash(options.payload, credentials.algorithm, options.contentType);
  652. var header = Hawk.server.header(credentials, artifacts, options);
  653. expect(header).to.equal('Hawk mac=\"n14wVJK4cOxAytPUMc5bPezQzuJGl5n7MYXhFQgEKsE=\", hash=\"f9cDF/TDm7TkYRLnGwRMfeDzT6LixQVLvrIKhh0vgmM=\", ext=\"response-specific\"');
  654. done();
  655. });
  656. it('generates header (null ext)', function (done) {
  657. var credentials = {
  658. id: '123456',
  659. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  660. algorithm: 'sha256',
  661. user: 'steve'
  662. };
  663. var artifacts = {
  664. method: 'POST',
  665. host: 'example.com',
  666. port: '8080',
  667. resource: '/resource/4?filter=a',
  668. ts: '1398546787',
  669. nonce: 'xUwusx',
  670. hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
  671. mac: 'dvIvMThwi28J61Jc3P0ryAhuKpanU63GXdx6hkmQkJA=',
  672. id: '123456'
  673. };
  674. var header = Hawk.server.header(credentials, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: null });
  675. expect(header).to.equal('Hawk mac=\"6PrybJTJs20jsgBw5eilXpcytD8kUbaIKNYXL+6g0ns=\", hash=\"f9cDF/TDm7TkYRLnGwRMfeDzT6LixQVLvrIKhh0vgmM=\"');
  676. done();
  677. });
  678. it('errors on missing artifacts', function (done) {
  679. var credentials = {
  680. id: '123456',
  681. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  682. algorithm: 'sha256',
  683. user: 'steve'
  684. };
  685. var header = Hawk.server.header(credentials, null, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });
  686. expect(header).to.equal('');
  687. done();
  688. });
  689. it('errors on invalid artifacts', function (done) {
  690. var credentials = {
  691. id: '123456',
  692. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  693. algorithm: 'sha256',
  694. user: 'steve'
  695. };
  696. var header = Hawk.server.header(credentials, 5, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });
  697. expect(header).to.equal('');
  698. done();
  699. });
  700. it('errors on missing credentials', function (done) {
  701. var artifacts = {
  702. method: 'POST',
  703. host: 'example.com',
  704. port: '8080',
  705. resource: '/resource/4?filter=a',
  706. ts: '1398546787',
  707. nonce: 'xUwusx',
  708. hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
  709. ext: 'some-app-data',
  710. mac: 'dvIvMThwi28J61Jc3P0ryAhuKpanU63GXdx6hkmQkJA=',
  711. id: '123456'
  712. };
  713. var header = Hawk.server.header(null, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });
  714. expect(header).to.equal('');
  715. done();
  716. });
  717. it('errors on invalid credentials (key)', function (done) {
  718. var credentials = {
  719. id: '123456',
  720. algorithm: 'sha256',
  721. user: 'steve'
  722. };
  723. var artifacts = {
  724. method: 'POST',
  725. host: 'example.com',
  726. port: '8080',
  727. resource: '/resource/4?filter=a',
  728. ts: '1398546787',
  729. nonce: 'xUwusx',
  730. hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
  731. ext: 'some-app-data',
  732. mac: 'dvIvMThwi28J61Jc3P0ryAhuKpanU63GXdx6hkmQkJA=',
  733. id: '123456'
  734. };
  735. var header = Hawk.server.header(credentials, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });
  736. expect(header).to.equal('');
  737. done();
  738. });
  739. it('errors on invalid algorithm', function (done) {
  740. var credentials = {
  741. id: '123456',
  742. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  743. algorithm: 'x',
  744. user: 'steve'
  745. };
  746. var artifacts = {
  747. method: 'POST',
  748. host: 'example.com',
  749. port: '8080',
  750. resource: '/resource/4?filter=a',
  751. ts: '1398546787',
  752. nonce: 'xUwusx',
  753. hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
  754. ext: 'some-app-data',
  755. mac: 'dvIvMThwi28J61Jc3P0ryAhuKpanU63GXdx6hkmQkJA=',
  756. id: '123456'
  757. };
  758. var header = Hawk.server.header(credentials, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });
  759. expect(header).to.equal('');
  760. done();
  761. });
  762. });
  763. describe('authenticateBewit()', function () {
  764. it('errors on uri too long', function (done) {
  765. var long = '/';
  766. for (var i = 0; i < 5000; ++i) {
  767. long += 'x';
  768. }
  769. var req = {
  770. method: 'GET',
  771. url: long,
  772. host: 'example.com',
  773. port: 8080,
  774. authorization: 'Hawk id="1", ts="1353788437", nonce="k3j4h2", mac="zy79QQ5/EYFmQqutVnYb73gAc/U=", ext="hello"'
  775. };
  776. Hawk.server.authenticateBewit(req, credentialsFunc, {}, function (err, credentials, bewit) {
  777. expect(err).to.exist();
  778. expect(err.output.statusCode).to.equal(400);
  779. expect(err.message).to.equal('Resource path exceeds max length');
  780. done();
  781. });
  782. });
  783. });
  784. describe('authenticateMessage()', function () {
  785. it('errors on invalid authorization (ts)', function (done) {
  786. credentialsFunc('123456', function (err, credentials1) {
  787. var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
  788. delete auth.ts;
  789. Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, {}, function (err, credentials2) {
  790. expect(err).to.exist();
  791. expect(err.message).to.equal('Invalid authorization');
  792. done();
  793. });
  794. });
  795. });
  796. it('errors on invalid authorization (nonce)', function (done) {
  797. credentialsFunc('123456', function (err, credentials1) {
  798. var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
  799. delete auth.nonce;
  800. Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, {}, function (err, credentials2) {
  801. expect(err).to.exist();
  802. expect(err.message).to.equal('Invalid authorization');
  803. done();
  804. });
  805. });
  806. });
  807. it('errors on invalid authorization (hash)', function (done) {
  808. credentialsFunc('123456', function (err, credentials1) {
  809. var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
  810. delete auth.hash;
  811. Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, {}, function (err, credentials2) {
  812. expect(err).to.exist();
  813. expect(err.message).to.equal('Invalid authorization');
  814. done();
  815. });
  816. });
  817. });
  818. it('errors with credentials', function (done) {
  819. credentialsFunc('123456', function (err, credentials1) {
  820. var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
  821. Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, function (id, callback) {
  822. callback(new Error('something'), { some: 'value' });
  823. }, {}, function (err, credentials2) {
  824. expect(err).to.exist();
  825. expect(err.message).to.equal('something');
  826. expect(credentials2.some).to.equal('value');
  827. done();
  828. });
  829. });
  830. });
  831. it('errors on nonce collision', function (done) {
  832. credentialsFunc('123456', function (err, credentials1) {
  833. var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
  834. Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, {
  835. nonceFunc: function (key, nonce, ts, nonceCallback) {
  836. nonceCallback(true);
  837. }
  838. }, function (err, credentials2) {
  839. expect(err).to.exist();
  840. expect(err.message).to.equal('Invalid nonce');
  841. done();
  842. });
  843. });
  844. });
  845. it('should generate an authorization then successfully parse it', function (done) {
  846. credentialsFunc('123456', function (err, credentials1) {
  847. var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
  848. expect(auth).to.exist();
  849. Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, {}, function (err, credentials2) {
  850. expect(err).to.not.exist();
  851. expect(credentials2.user).to.equal('steve');
  852. done();
  853. });
  854. });
  855. });
  856. it('should fail authorization on mismatching host', function (done) {
  857. credentialsFunc('123456', function (err, credentials1) {
  858. var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
  859. expect(auth).to.exist();
  860. Hawk.server.authenticateMessage('example1.com', 8080, 'some message', auth, credentialsFunc, {}, function (err, credentials2) {
  861. expect(err).to.exist();
  862. expect(err.message).to.equal('Bad mac');
  863. done();
  864. });
  865. });
  866. });
  867. it('should fail authorization on stale timestamp', function (done) {
  868. credentialsFunc('123456', function (err, credentials1) {
  869. var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
  870. expect(auth).to.exist();
  871. Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, { localtimeOffsetMsec: 100000 }, function (err, credentials2) {
  872. expect(err).to.exist();
  873. expect(err.message).to.equal('Stale timestamp');
  874. done();
  875. });
  876. });
  877. });
  878. it('overrides timestampSkewSec', function (done) {
  879. credentialsFunc('123456', function (err, credentials1) {
  880. var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1, localtimeOffsetMsec: 100000 });
  881. expect(auth).to.exist();
  882. Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, { timestampSkewSec: 500 }, function (err, credentials2) {
  883. expect(err).to.not.exist();
  884. done();
  885. });
  886. });
  887. });
  888. it('should fail authorization on invalid authorization', function (done) {
  889. credentialsFunc('123456', function (err, credentials1) {
  890. var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
  891. expect(auth).to.exist();
  892. delete auth.id;
  893. Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, {}, function (err, credentials2) {
  894. expect(err).to.exist();
  895. expect(err.message).to.equal('Invalid authorization');
  896. done();
  897. });
  898. });
  899. });
  900. it('should fail authorization on bad hash', function (done) {
  901. credentialsFunc('123456', function (err, credentials1) {
  902. var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
  903. expect(auth).to.exist();
  904. Hawk.server.authenticateMessage('example.com', 8080, 'some message1', auth, credentialsFunc, {}, function (err, credentials2) {
  905. expect(err).to.exist();
  906. expect(err.message).to.equal('Bad message hash');
  907. done();
  908. });
  909. });
  910. });
  911. it('should fail authorization on nonce error', function (done) {
  912. credentialsFunc('123456', function (err, credentials1) {
  913. var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
  914. expect(auth).to.exist();
  915. Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, credentialsFunc, {
  916. nonceFunc: function (key, nonce, ts, callback) {
  917. callback(new Error('kaboom'));
  918. }
  919. }, function (err, credentials2) {
  920. expect(err).to.exist();
  921. expect(err.message).to.equal('Invalid nonce');
  922. done();
  923. });
  924. });
  925. });
  926. it('should fail authorization on credentials error', function (done) {
  927. credentialsFunc('123456', function (err, credentials1) {
  928. var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
  929. expect(auth).to.exist();
  930. var errFunc = function (id, callback) {
  931. callback(new Error('kablooey'));
  932. };
  933. Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, errFunc, {}, function (err, credentials2) {
  934. expect(err).to.exist();
  935. expect(err.message).to.equal('kablooey');
  936. done();
  937. });
  938. });
  939. });
  940. it('should fail authorization on missing credentials', function (done) {
  941. credentialsFunc('123456', function (err, credentials1) {
  942. var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
  943. expect(auth).to.exist();
  944. var errFunc = function (id, callback) {
  945. callback();
  946. };
  947. Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, errFunc, {}, function (err, credentials2) {
  948. expect(err).to.exist();
  949. expect(err.message).to.equal('Unknown credentials');
  950. done();
  951. });
  952. });
  953. });
  954. it('should fail authorization on invalid credentials', function (done) {
  955. credentialsFunc('123456', function (err, credentials1) {
  956. var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
  957. expect(auth).to.exist();
  958. var errFunc = function (id, callback) {
  959. callback(null, {});
  960. };
  961. Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, errFunc, {}, function (err, credentials2) {
  962. expect(err).to.exist();
  963. expect(err.message).to.equal('Invalid credentials');
  964. done();
  965. });
  966. });
  967. });
  968. it('should fail authorization on invalid credentials algorithm', function (done) {
  969. credentialsFunc('123456', function (err, credentials1) {
  970. var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: credentials1 });
  971. expect(auth).to.exist();
  972. var errFunc = function (id, callback) {
  973. callback(null, { key: '123', algorithm: '456' });
  974. };
  975. Hawk.server.authenticateMessage('example.com', 8080, 'some message', auth, errFunc, {}, function (err, credentials2) {
  976. expect(err).to.exist();
  977. expect(err.message).to.equal('Unknown algorithm');
  978. done();
  979. });
  980. });
  981. });
  982. it('should fail on missing host', function (done) {
  983. credentialsFunc('123456', function (err, credentials) {
  984. var auth = Hawk.client.message(null, 8080, 'some message', { credentials: credentials });
  985. expect(auth).to.not.exist();
  986. done();
  987. });
  988. });
  989. it('should fail on missing credentials', function (done) {
  990. var auth = Hawk.client.message('example.com', 8080, 'some message', {});
  991. expect(auth).to.not.exist();
  992. done();
  993. });
  994. it('should fail on invalid algorithm', function (done) {
  995. credentialsFunc('123456', function (err, credentials) {
  996. var creds = Hoek.clone(credentials);
  997. creds.algorithm = 'blah';
  998. var auth = Hawk.client.message('example.com', 8080, 'some message', { credentials: creds });
  999. expect(auth).to.not.exist();
  1000. done();
  1001. });
  1002. });
  1003. });
  1004. describe('authenticatePayloadHash()', function () {
  1005. it('checks payload hash', function (done) {
  1006. expect(Hawk.server.authenticatePayloadHash('abcdefg', { hash: 'abcdefg' })).to.equal(true);
  1007. expect(Hawk.server.authenticatePayloadHash('1234567', { hash: 'abcdefg' })).to.equal(false);
  1008. done();
  1009. });
  1010. });
  1011. });