uri.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // Load modules
  2. var Http = require('http');
  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('Uri', function () {
  15. var credentialsFunc = function (id, callback) {
  16. var credentials = {
  17. id: id,
  18. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  19. algorithm: 'sha256',
  20. user: 'steve'
  21. };
  22. return callback(null, credentials);
  23. };
  24. it('should generate a bewit then successfully authenticate it', function (done) {
  25. var req = {
  26. method: 'GET',
  27. url: '/resource/4?a=1&b=2',
  28. host: 'example.com',
  29. port: 80
  30. };
  31. credentialsFunc('123456', function (err, credentials) {
  32. var bewit = Hawk.uri.getBewit('http://example.com/resource/4?a=1&b=2', { credentials: credentials, ttlSec: 60 * 60 * 24 * 365 * 100, ext: 'some-app-data' });
  33. req.url += '&bewit=' + bewit;
  34. Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {
  35. expect(err).to.not.exist;
  36. expect(credentials.user).to.equal('steve');
  37. expect(attributes.ext).to.equal('some-app-data');
  38. done();
  39. });
  40. });
  41. });
  42. it('should generate a bewit then successfully authenticate it (no ext)', function (done) {
  43. var req = {
  44. method: 'GET',
  45. url: '/resource/4?a=1&b=2',
  46. host: 'example.com',
  47. port: 80
  48. };
  49. credentialsFunc('123456', function (err, credentials) {
  50. var bewit = Hawk.uri.getBewit('http://example.com/resource/4?a=1&b=2', { credentials: credentials, ttlSec: 60 * 60 * 24 * 365 * 100 });
  51. req.url += '&bewit=' + bewit;
  52. Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {
  53. expect(err).to.not.exist;
  54. expect(credentials.user).to.equal('steve');
  55. done();
  56. });
  57. });
  58. });
  59. it('should successfully authenticate a request (last param)', function (done) {
  60. var req = {
  61. method: 'GET',
  62. url: '/resource/4?a=1&b=2&bewit=MTIzNDU2XDQ1MTE0ODQ2MjFcMzFjMmNkbUJFd1NJRVZDOVkva1NFb2c3d3YrdEVNWjZ3RXNmOGNHU2FXQT1cc29tZS1hcHAtZGF0YQ',
  63. host: 'example.com',
  64. port: 8080
  65. };
  66. Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {
  67. expect(err).to.not.exist;
  68. expect(credentials.user).to.equal('steve');
  69. expect(attributes.ext).to.equal('some-app-data');
  70. done();
  71. });
  72. });
  73. it('should successfully authenticate a request (first param)', function (done) {
  74. var req = {
  75. method: 'GET',
  76. url: '/resource/4?bewit=MTIzNDU2XDQ1MTE0ODQ2MjFcMzFjMmNkbUJFd1NJRVZDOVkva1NFb2c3d3YrdEVNWjZ3RXNmOGNHU2FXQT1cc29tZS1hcHAtZGF0YQ&a=1&b=2',
  77. host: 'example.com',
  78. port: 8080
  79. };
  80. Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {
  81. expect(err).to.not.exist;
  82. expect(credentials.user).to.equal('steve');
  83. expect(attributes.ext).to.equal('some-app-data');
  84. done();
  85. });
  86. });
  87. it('should successfully authenticate a request (only param)', function (done) {
  88. var req = {
  89. method: 'GET',
  90. url: '/resource/4?bewit=MTIzNDU2XDQ1MTE0ODQ2NDFcZm1CdkNWT3MvcElOTUUxSTIwbWhrejQ3UnBwTmo4Y1VrSHpQd3Q5OXJ1cz1cc29tZS1hcHAtZGF0YQ',
  91. host: 'example.com',
  92. port: 8080
  93. };
  94. Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {
  95. expect(err).to.not.exist;
  96. expect(credentials.user).to.equal('steve');
  97. expect(attributes.ext).to.equal('some-app-data');
  98. done();
  99. });
  100. });
  101. it('should fail on multiple authentication', function (done) {
  102. var req = {
  103. method: 'GET',
  104. url: '/resource/4?bewit=MTIzNDU2XDQ1MTE0ODQ2NDFcZm1CdkNWT3MvcElOTUUxSTIwbWhrejQ3UnBwTmo4Y1VrSHpQd3Q5OXJ1cz1cc29tZS1hcHAtZGF0YQ',
  105. host: 'example.com',
  106. port: 8080,
  107. authorization: 'Basic asdasdasdasd'
  108. };
  109. Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {
  110. expect(err).to.exist;
  111. expect(err.response.payload.message).to.equal('Multiple authentications');
  112. done();
  113. });
  114. });
  115. it('should fail on method other than GET', function (done) {
  116. credentialsFunc('123456', function (err, credentials) {
  117. var req = {
  118. method: 'POST',
  119. url: '/resource/4?filter=a',
  120. host: 'example.com',
  121. port: 8080
  122. };
  123. var exp = Math.floor(Hawk.utils.now() / 1000) + 60;
  124. var ext = 'some-app-data';
  125. var mac = Hawk.crypto.calculateMac('bewit', credentials, {
  126. timestamp: exp,
  127. nonce: '',
  128. method: req.method,
  129. resource: req.url,
  130. host: req.host,
  131. port: req.port,
  132. ext: ext
  133. });
  134. var bewit = credentials.id + '\\' + exp + '\\' + mac + '\\' + ext;
  135. req.url += '&bewit=' + Hawk.utils.base64urlEncode(bewit);
  136. Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {
  137. expect(err).to.exist;
  138. expect(err.response.payload.message).to.equal('Invalid method');
  139. done();
  140. });
  141. });
  142. });
  143. it('should fail on invalid host header', function (done) {
  144. var req = {
  145. method: 'GET',
  146. url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',
  147. headers: {
  148. host: 'example.com:something'
  149. }
  150. };
  151. Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {
  152. expect(err).to.exist;
  153. expect(err.response.payload.message).to.equal('Invalid Host header');
  154. done();
  155. });
  156. });
  157. it('should fail on empty bewit', function (done) {
  158. var req = {
  159. method: 'GET',
  160. url: '/resource/4?bewit=',
  161. host: 'example.com',
  162. port: 8080
  163. };
  164. Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {
  165. expect(err).to.exist;
  166. expect(err.response.payload.message).to.equal('Empty bewit');
  167. expect(err.isMissing).to.not.exist;
  168. done();
  169. });
  170. });
  171. it('should fail on invalid bewit', function (done) {
  172. var req = {
  173. method: 'GET',
  174. url: '/resource/4?bewit=*',
  175. host: 'example.com',
  176. port: 8080
  177. };
  178. Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {
  179. expect(err).to.exist;
  180. expect(err.response.payload.message).to.equal('Invalid bewit encoding');
  181. expect(err.isMissing).to.not.exist;
  182. done();
  183. });
  184. });
  185. it('should fail on missing bewit', function (done) {
  186. var req = {
  187. method: 'GET',
  188. url: '/resource/4',
  189. host: 'example.com',
  190. port: 8080
  191. };
  192. Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {
  193. expect(err).to.exist;
  194. expect(err.response.payload.message).to.not.exist;
  195. expect(err.isMissing).to.equal(true);
  196. done();
  197. });
  198. });
  199. it('should fail on invalid bewit structure', function (done) {
  200. var req = {
  201. method: 'GET',
  202. url: '/resource/4?bewit=abc',
  203. host: 'example.com',
  204. port: 8080
  205. };
  206. Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {
  207. expect(err).to.exist;
  208. expect(err.response.payload.message).to.equal('Invalid bewit structure');
  209. done();
  210. });
  211. });
  212. it('should fail on empty bewit attribute', function (done) {
  213. var req = {
  214. method: 'GET',
  215. url: '/resource/4?bewit=YVxcY1xk',
  216. host: 'example.com',
  217. port: 8080
  218. };
  219. Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {
  220. expect(err).to.exist;
  221. expect(err.response.payload.message).to.equal('Missing bewit attributes');
  222. done();
  223. });
  224. });
  225. it('should fail on expired access', function (done) {
  226. var req = {
  227. method: 'GET',
  228. url: '/resource/4?a=1&b=2&bewit=MTIzNDU2XDEzNTY0MTg1ODNcWk1wZlMwWU5KNHV0WHpOMmRucTRydEk3NXNXTjFjeWVITTcrL0tNZFdVQT1cc29tZS1hcHAtZGF0YQ',
  229. host: 'example.com',
  230. port: 8080
  231. };
  232. Hawk.uri.authenticate(req, credentialsFunc, {}, function (err, credentials, attributes) {
  233. expect(err).to.exist;
  234. expect(err.response.payload.message).to.equal('Access expired');
  235. done();
  236. });
  237. });
  238. it('should fail on credentials function error', function (done) {
  239. var req = {
  240. method: 'GET',
  241. url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',
  242. host: 'example.com',
  243. port: 8080
  244. };
  245. Hawk.uri.authenticate(req, function (id, callback) { callback(Hawk.error.badRequest('Boom')); }, {}, function (err, credentials, attributes) {
  246. expect(err).to.exist;
  247. expect(err.response.payload.message).to.equal('Boom');
  248. done();
  249. });
  250. });
  251. it('should fail on null credentials function response', function (done) {
  252. var req = {
  253. method: 'GET',
  254. url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',
  255. host: 'example.com',
  256. port: 8080
  257. };
  258. Hawk.uri.authenticate(req, function (id, callback) { callback(null, null); }, {}, function (err, credentials, attributes) {
  259. expect(err).to.exist;
  260. expect(err.response.payload.message).to.equal('Unknown credentials');
  261. done();
  262. });
  263. });
  264. it('should fail on invalid credentials function response', function (done) {
  265. var req = {
  266. method: 'GET',
  267. url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',
  268. host: 'example.com',
  269. port: 8080
  270. };
  271. Hawk.uri.authenticate(req, function (id, callback) { callback(null, {}); }, {}, function (err, credentials, attributes) {
  272. expect(err).to.exist;
  273. expect(err.message).to.equal('Invalid credentials');
  274. done();
  275. });
  276. });
  277. it('should fail on invalid credentials function response (unknown algorithm)', function (done) {
  278. var req = {
  279. method: 'GET',
  280. url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',
  281. host: 'example.com',
  282. port: 8080
  283. };
  284. Hawk.uri.authenticate(req, function (id, callback) { callback(null, { key: 'xxx', algorithm: 'xxx' }); }, {}, function (err, credentials, attributes) {
  285. expect(err).to.exist;
  286. expect(err.message).to.equal('Unknown algorithm');
  287. done();
  288. });
  289. });
  290. it('should fail on expired access', function (done) {
  291. var req = {
  292. method: 'GET',
  293. url: '/resource/4?bewit=MTIzNDU2XDQ1MDk5OTE3MTlcTUE2eWkwRWRwR0pEcWRwb0JkYVdvVDJrL0hDSzA1T0Y3MkhuZlVmVy96Zz1cc29tZS1hcHAtZGF0YQ',
  294. host: 'example.com',
  295. port: 8080
  296. };
  297. Hawk.uri.authenticate(req, function (id, callback) { callback(null, { key: 'xxx', algorithm: 'sha256' }); }, {}, function (err, credentials, attributes) {
  298. expect(err).to.exist;
  299. expect(err.response.payload.message).to.equal('Bad mac');
  300. done();
  301. });
  302. });
  303. });
  304. describe('#getBewit', function () {
  305. it('should return a valid bewit value', function (done) {
  306. var credentials = {
  307. id: '123456',
  308. key: '2983d45yun89q',
  309. algorithm: 'sha256'
  310. };
  311. var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow', { credentials: credentials, ttlSec: 300, localtimeOffsetMsec: 1356420407232 - Hawk.utils.now(), ext: 'xandyandz' });
  312. expect(bewit).to.equal('MTIzNDU2XDEzNTY0MjA3MDdca3NjeHdOUjJ0SnBQMVQxekRMTlBiQjVVaUtJVTl0T1NKWFRVZEc3WDloOD1ceGFuZHlhbmR6');
  313. done();
  314. });
  315. it('should return an empty bewit on invalid credentials', function (done) {
  316. var credentials = {
  317. key: '2983d45yun89q',
  318. algorithm: 'sha256'
  319. };
  320. var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow', { credentials: credentials, ttlSec: 3000, ext: 'xandyandz' });
  321. expect(bewit).to.equal('');
  322. done();
  323. });
  324. it('should return an empty bewit on invalid algorithm', function (done) {
  325. var credentials = {
  326. id: '123456',
  327. key: '2983d45yun89q',
  328. algorithm: 'hmac-sha-0'
  329. };
  330. var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow', { credentials: credentials, ttlSec: 300, ext: 'xandyandz' });
  331. expect(bewit).to.equal('');
  332. done();
  333. });
  334. it('should return an empty bewit on missing options', function (done) {
  335. var credentials = {
  336. id: '123456',
  337. key: '2983d45yun89q',
  338. algorithm: 'hmac-sha-0'
  339. };
  340. var bewit = Hawk.uri.getBewit('https://example.com/somewhere/over/the/rainbow');
  341. expect(bewit).to.equal('');
  342. done();
  343. });
  344. });
  345. });