server.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. 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('should parse 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('should parse 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('should parse 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('should parse 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('should parse 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('should fail 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.response.payload.message).to.equal('Missing required payload hash');
  108. done();
  109. });
  110. });
  111. it('should fail 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.response.payload.message).to.equal('Stale timestamp');
  122. var header = err.response.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('should fail 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 (nonce, ts, callback) {
  147. if (memoryCache[nonce]) {
  148. return callback(new Error());
  149. }
  150. memoryCache[nonce] = true;
  151. return callback();
  152. }
  153. };
  154. Hawk.server.authenticate(req, credentialsFunc, options, function (err, credentials, artifacts) {
  155. expect(err).to.not.exist;
  156. expect(credentials.user).to.equal('steve');
  157. Hawk.server.authenticate(req, credentialsFunc, options, function (err, credentials, artifacts) {
  158. expect(err).to.exist;
  159. expect(err.response.payload.message).to.equal('Invalid nonce');
  160. done();
  161. });
  162. });
  163. });
  164. it('should fail on an invalid authentication header: wrong scheme', function (done) {
  165. var req = {
  166. method: 'GET',
  167. url: '/resource/4?filter=a',
  168. host: 'example.com',
  169. port: 8080,
  170. authorization: 'Basic asdasdasdasd'
  171. };
  172. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  173. expect(err).to.exist;
  174. expect(err.response.payload.message).to.not.exist;
  175. done();
  176. });
  177. });
  178. it('should fail on an invalid authentication header: no scheme', function (done) {
  179. var req = {
  180. method: 'GET',
  181. url: '/resource/4?filter=a',
  182. host: 'example.com',
  183. port: 8080,
  184. authorization: '!@#'
  185. };
  186. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  187. expect(err).to.exist;
  188. expect(err.response.payload.message).to.equal('Invalid header syntax');
  189. done();
  190. });
  191. });
  192. it('should fail on an missing authorization header', function (done) {
  193. var req = {
  194. method: 'GET',
  195. url: '/resource/4?filter=a',
  196. host: 'example.com',
  197. port: 8080
  198. };
  199. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
  200. expect(err).to.exist;
  201. expect(err.isMissing).to.equal(true);
  202. done();
  203. });
  204. });
  205. it('should fail on an missing host header', function (done) {
  206. var req = {
  207. method: 'GET',
  208. url: '/resource/4?filter=a',
  209. headers: {
  210. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  211. }
  212. };
  213. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  214. expect(err).to.exist;
  215. expect(err.response.payload.message).to.equal('Invalid Host header');
  216. done();
  217. });
  218. });
  219. it('should fail on an missing authorization attribute (id)', function (done) {
  220. var req = {
  221. method: 'GET',
  222. url: '/resource/4?filter=a',
  223. host: 'example.com',
  224. port: 8080,
  225. authorization: 'Hawk ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  226. };
  227. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  228. expect(err).to.exist;
  229. expect(err.response.payload.message).to.equal('Missing attributes');
  230. done();
  231. });
  232. });
  233. it('should fail on an missing authorization attribute (ts)', function (done) {
  234. var req = {
  235. method: 'GET',
  236. url: '/resource/4?filter=a',
  237. host: 'example.com',
  238. port: 8080,
  239. authorization: 'Hawk id="123", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  240. };
  241. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  242. expect(err).to.exist;
  243. expect(err.response.payload.message).to.equal('Missing attributes');
  244. done();
  245. });
  246. });
  247. it('should fail on an missing authorization attribute (nonce)', function (done) {
  248. var req = {
  249. method: 'GET',
  250. url: '/resource/4?filter=a',
  251. host: 'example.com',
  252. port: 8080,
  253. authorization: 'Hawk id="123", ts="1353788437", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  254. };
  255. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  256. expect(err).to.exist;
  257. expect(err.response.payload.message).to.equal('Missing attributes');
  258. done();
  259. });
  260. });
  261. it('should fail on an missing authorization attribute (mac)', function (done) {
  262. var req = {
  263. method: 'GET',
  264. url: '/resource/4?filter=a',
  265. host: 'example.com',
  266. port: 8080,
  267. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", ext="hello"'
  268. };
  269. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  270. expect(err).to.exist;
  271. expect(err.response.payload.message).to.equal('Missing attributes');
  272. done();
  273. });
  274. });
  275. it('should fail on an unknown authorization attribute', function (done) {
  276. var req = {
  277. method: 'GET',
  278. url: '/resource/4?filter=a',
  279. host: 'example.com',
  280. port: 8080,
  281. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", x="3", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  282. };
  283. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  284. expect(err).to.exist;
  285. expect(err.response.payload.message).to.equal('Unknown attribute: x');
  286. done();
  287. });
  288. });
  289. it('should fail on an bad authorization header format', function (done) {
  290. var req = {
  291. method: 'GET',
  292. url: '/resource/4?filter=a',
  293. host: 'example.com',
  294. port: 8080,
  295. authorization: 'Hawk id="123\\", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  296. };
  297. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  298. expect(err).to.exist;
  299. expect(err.response.payload.message).to.equal('Bad header format');
  300. done();
  301. });
  302. });
  303. it('should fail on an bad authorization attribute value', function (done) {
  304. var req = {
  305. method: 'GET',
  306. url: '/resource/4?filter=a',
  307. host: 'example.com',
  308. port: 8080,
  309. authorization: 'Hawk id="\t", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  310. };
  311. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  312. expect(err).to.exist;
  313. expect(err.response.payload.message).to.equal('Bad attribute value: id');
  314. done();
  315. });
  316. });
  317. it('should fail on an empty authorization attribute value', function (done) {
  318. var req = {
  319. method: 'GET',
  320. url: '/resource/4?filter=a',
  321. host: 'example.com',
  322. port: 8080,
  323. authorization: 'Hawk id="", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  324. };
  325. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  326. expect(err).to.exist;
  327. expect(err.response.payload.message).to.equal('Bad attribute value: id');
  328. done();
  329. });
  330. });
  331. it('should fail on duplicated authorization attribute key', function (done) {
  332. var req = {
  333. method: 'GET',
  334. url: '/resource/4?filter=a',
  335. host: 'example.com',
  336. port: 8080,
  337. authorization: 'Hawk id="123", id="456", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  338. };
  339. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  340. expect(err).to.exist;
  341. expect(err.response.payload.message).to.equal('Duplicate attribute: id');
  342. done();
  343. });
  344. });
  345. it('should fail on an invalid authorization header format', function (done) {
  346. var req = {
  347. method: 'GET',
  348. url: '/resource/4?filter=a',
  349. host: 'example.com',
  350. port: 8080,
  351. authorization: 'Hawk'
  352. };
  353. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  354. expect(err).to.exist;
  355. expect(err.response.payload.message).to.equal('Invalid header syntax');
  356. done();
  357. });
  358. });
  359. it('should fail on an bad host header (missing host)', function (done) {
  360. var req = {
  361. method: 'GET',
  362. url: '/resource/4?filter=a',
  363. headers: {
  364. host: ':8080',
  365. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  366. }
  367. };
  368. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  369. expect(err).to.exist;
  370. expect(err.response.payload.message).to.equal('Invalid Host header');
  371. done();
  372. });
  373. });
  374. it('should fail on an bad host header (pad port)', function (done) {
  375. var req = {
  376. method: 'GET',
  377. url: '/resource/4?filter=a',
  378. headers: {
  379. host: 'example.com:something',
  380. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  381. }
  382. };
  383. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  384. expect(err).to.exist;
  385. expect(err.response.payload.message).to.equal('Invalid Host header');
  386. done();
  387. });
  388. });
  389. it('should fail on credentialsFunc error', function (done) {
  390. var req = {
  391. method: 'GET',
  392. url: '/resource/4?filter=a',
  393. host: 'example.com',
  394. port: 8080,
  395. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  396. };
  397. var credentialsFunc = function (id, callback) {
  398. return callback(new Error('Unknown user'));
  399. };
  400. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  401. expect(err).to.exist;
  402. expect(err.message).to.equal('Unknown user');
  403. done();
  404. });
  405. });
  406. it('should fail on missing credentials', function (done) {
  407. var req = {
  408. method: 'GET',
  409. url: '/resource/4?filter=a',
  410. host: 'example.com',
  411. port: 8080,
  412. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  413. };
  414. var credentialsFunc = function (id, callback) {
  415. return callback(null, null);
  416. };
  417. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  418. expect(err).to.exist;
  419. expect(err.response.payload.message).to.equal('Unknown credentials');
  420. done();
  421. });
  422. });
  423. it('should fail on invalid credentials', function (done) {
  424. var req = {
  425. method: 'GET',
  426. url: '/resource/4?filter=a',
  427. host: 'example.com',
  428. port: 8080,
  429. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  430. };
  431. var credentialsFunc = function (id, callback) {
  432. var credentials = {
  433. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  434. user: 'steve'
  435. };
  436. return callback(null, credentials);
  437. };
  438. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  439. expect(err).to.exist;
  440. expect(err.message).to.equal('Invalid credentials');
  441. expect(err.response.payload.message).to.equal('An internal server error occurred');
  442. done();
  443. });
  444. });
  445. it('should fail on unknown credentials algorithm', function (done) {
  446. var req = {
  447. method: 'GET',
  448. url: '/resource/4?filter=a',
  449. host: 'example.com',
  450. port: 8080,
  451. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcUyW6EEgUH4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  452. };
  453. var credentialsFunc = function (id, callback) {
  454. var credentials = {
  455. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  456. algorithm: 'hmac-sha-0',
  457. user: 'steve'
  458. };
  459. return callback(null, credentials);
  460. };
  461. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  462. expect(err).to.exist;
  463. expect(err.message).to.equal('Unknown algorithm');
  464. expect(err.response.payload.message).to.equal('An internal server error occurred');
  465. done();
  466. });
  467. });
  468. it('should fail on unknown bad mac', function (done) {
  469. var req = {
  470. method: 'GET',
  471. url: '/resource/4?filter=a',
  472. host: 'example.com',
  473. port: 8080,
  474. authorization: 'Hawk id="123", ts="1353788437", nonce="k3j4h2", mac="/qwS4UjfVWMcU4jlr7T/wuKe3dKijvTvSos=", ext="hello"'
  475. };
  476. var credentialsFunc = function (id, callback) {
  477. var credentials = {
  478. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  479. algorithm: 'sha256',
  480. user: 'steve'
  481. };
  482. return callback(null, credentials);
  483. };
  484. Hawk.server.authenticate(req, credentialsFunc, { localtimeOffsetMsec: 1353788437000 - Hawk.utils.now() }, function (err, credentials, artifacts) {
  485. expect(err).to.exist;
  486. expect(err.response.payload.message).to.equal('Bad mac');
  487. done();
  488. });
  489. });
  490. });
  491. describe('#header', function () {
  492. it('should return an empty authorization header on missing options', function (done) {
  493. var header = Hawk.server.header();
  494. expect(header).to.equal('');
  495. done();
  496. });
  497. it('should return an empty authorization header on missing credentials', function (done) {
  498. var header = Hawk.server.header(null, {});
  499. expect(header).to.equal('');
  500. done();
  501. });
  502. it('should return an empty authorization header on invalid credentials', function (done) {
  503. var credentials = {
  504. key: '2983d45yun89q'
  505. };
  506. var header = Hawk.server.header(credentials);
  507. expect(header).to.equal('');
  508. done();
  509. });
  510. it('should return an empty authorization header on invalid algorithm', function (done) {
  511. var artifacts = {
  512. id: '123456'
  513. };
  514. var credentials = {
  515. key: '2983d45yun89q',
  516. algorithm: 'hmac-sha-0'
  517. };
  518. var header = Hawk.server.header(credentials, artifacts);
  519. expect(header).to.equal('');
  520. done();
  521. });
  522. });
  523. });
  524. });