Front end of the Slack clone application.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // Load modules
  2. var Url = require('url');
  3. var Code = require('code');
  4. var Hawk = require('../lib');
  5. var Lab = require('lab');
  6. // Declare internals
  7. var internals = {};
  8. // Test shortcuts
  9. var lab = exports.lab = Lab.script();
  10. var describe = lab.experiment;
  11. var it = lab.test;
  12. var expect = Code.expect;
  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('generates 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, credentials1) {
  31. req.authorization = Hawk.client.header(Url.parse('http://example.com:8080/resource/4?filter=a'), req.method, { credentials: credentials1, ext: 'some-app-data' }).field;
  32. expect(req.authorization).to.exist();
  33. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
  34. expect(err).to.not.exist();
  35. expect(credentials2.user).to.equal('steve');
  36. expect(artifacts.ext).to.equal('some-app-data');
  37. done();
  38. });
  39. });
  40. });
  41. it('generates 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, credentials1) {
  52. var reqHeader = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data', payload: payload, contentType: req.headers['content-type'] });
  53. req.headers.authorization = reqHeader.field;
  54. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
  55. expect(err).to.not.exist();
  56. expect(credentials2.user).to.equal('steve');
  57. expect(artifacts.ext).to.equal('some-app-data');
  58. expect(Hawk.server.authenticatePayload(payload, credentials2, 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(credentials2, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });
  65. expect(res.headers['server-authorization']).to.exist();
  66. expect(Hawk.client.authenticate(res, credentials2, artifacts, { payload: 'some reply' })).to.equal(true);
  67. done();
  68. });
  69. });
  70. });
  71. it('generates a header then successfully parse it (absolute request uri)', function (done) {
  72. var req = {
  73. method: 'POST',
  74. url: 'http://example.com:8080/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, credentials1) {
  82. var reqHeader = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data', payload: payload, contentType: req.headers['content-type'] });
  83. req.headers.authorization = reqHeader.field;
  84. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
  85. expect(err).to.not.exist();
  86. expect(credentials2.user).to.equal('steve');
  87. expect(artifacts.ext).to.equal('some-app-data');
  88. expect(Hawk.server.authenticatePayload(payload, credentials2, 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(credentials2, artifacts, { payload: 'some reply', contentType: 'text/plain', ext: 'response-specific' });
  95. expect(res.headers['server-authorization']).to.exist();
  96. expect(Hawk.client.authenticate(res, credentials2, artifacts, { payload: 'some reply' })).to.equal(true);
  97. done();
  98. });
  99. });
  100. });
  101. it('generates a header then successfully parse it (no server header options)', 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, credentials1) {
  112. var reqHeader = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data', payload: payload, contentType: req.headers['content-type'] });
  113. req.headers.authorization = reqHeader.field;
  114. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
  115. expect(err).to.not.exist();
  116. expect(credentials2.user).to.equal('steve');
  117. expect(artifacts.ext).to.equal('some-app-data');
  118. expect(Hawk.server.authenticatePayload(payload, credentials2, 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(credentials2, artifacts);
  125. expect(res.headers['server-authorization']).to.exist();
  126. expect(Hawk.client.authenticate(res, credentials2, artifacts)).to.equal(true);
  127. done();
  128. });
  129. });
  130. });
  131. it('generates a header then fails to parse it (missing server header hash)', function (done) {
  132. var req = {
  133. method: 'POST',
  134. url: '/resource/4?filter=a',
  135. headers: {
  136. host: 'example.com:8080',
  137. 'content-type': 'text/plain;x=y'
  138. }
  139. };
  140. var payload = 'some not so random text';
  141. credentialsFunc('123456', function (err, credentials1) {
  142. var reqHeader = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data', payload: payload, contentType: req.headers['content-type'] });
  143. req.headers.authorization = reqHeader.field;
  144. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
  145. expect(err).to.not.exist();
  146. expect(credentials2.user).to.equal('steve');
  147. expect(artifacts.ext).to.equal('some-app-data');
  148. expect(Hawk.server.authenticatePayload(payload, credentials2, artifacts, req.headers['content-type'])).to.equal(true);
  149. var res = {
  150. headers: {
  151. 'content-type': 'text/plain'
  152. }
  153. };
  154. res.headers['server-authorization'] = Hawk.server.header(credentials2, artifacts);
  155. expect(res.headers['server-authorization']).to.exist();
  156. expect(Hawk.client.authenticate(res, credentials2, artifacts, { payload: 'some reply' })).to.equal(false);
  157. done();
  158. });
  159. });
  160. });
  161. it('generates a header then successfully parse it (with hash)', function (done) {
  162. var req = {
  163. method: 'GET',
  164. url: '/resource/4?filter=a',
  165. host: 'example.com',
  166. port: 8080
  167. };
  168. credentialsFunc('123456', function (err, credentials1) {
  169. req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, payload: 'hola!', ext: 'some-app-data' }).field;
  170. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
  171. expect(err).to.not.exist();
  172. expect(credentials2.user).to.equal('steve');
  173. expect(artifacts.ext).to.equal('some-app-data');
  174. done();
  175. });
  176. });
  177. });
  178. it('generates a header then successfully parse it then validate payload', function (done) {
  179. var req = {
  180. method: 'GET',
  181. url: '/resource/4?filter=a',
  182. host: 'example.com',
  183. port: 8080
  184. };
  185. credentialsFunc('123456', function (err, credentials1) {
  186. req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, payload: 'hola!', ext: 'some-app-data' }).field;
  187. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
  188. expect(err).to.not.exist();
  189. expect(credentials2.user).to.equal('steve');
  190. expect(artifacts.ext).to.equal('some-app-data');
  191. expect(Hawk.server.authenticatePayload('hola!', credentials2, artifacts)).to.be.true();
  192. expect(Hawk.server.authenticatePayload('hello!', credentials2, artifacts)).to.be.false();
  193. done();
  194. });
  195. });
  196. });
  197. it('generates a header then successfully parses and validates payload', function (done) {
  198. var req = {
  199. method: 'GET',
  200. url: '/resource/4?filter=a',
  201. host: 'example.com',
  202. port: 8080
  203. };
  204. credentialsFunc('123456', function (err, credentials1) {
  205. req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, payload: 'hola!', ext: 'some-app-data' }).field;
  206. Hawk.server.authenticate(req, credentialsFunc, { payload: 'hola!' }, function (err, credentials2, artifacts) {
  207. expect(err).to.not.exist();
  208. expect(credentials2.user).to.equal('steve');
  209. expect(artifacts.ext).to.equal('some-app-data');
  210. done();
  211. });
  212. });
  213. });
  214. it('generates a header then successfully parse it (app)', function (done) {
  215. var req = {
  216. method: 'GET',
  217. url: '/resource/4?filter=a',
  218. host: 'example.com',
  219. port: 8080
  220. };
  221. credentialsFunc('123456', function (err, credentials1) {
  222. req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data', app: 'asd23ased' }).field;
  223. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
  224. expect(err).to.not.exist();
  225. expect(credentials2.user).to.equal('steve');
  226. expect(artifacts.ext).to.equal('some-app-data');
  227. expect(artifacts.app).to.equal('asd23ased');
  228. done();
  229. });
  230. });
  231. });
  232. it('generates a header then successfully parse it (app, dlg)', function (done) {
  233. var req = {
  234. method: 'GET',
  235. url: '/resource/4?filter=a',
  236. host: 'example.com',
  237. port: 8080
  238. };
  239. credentialsFunc('123456', function (err, credentials1) {
  240. req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data', app: 'asd23ased', dlg: '23434szr3q4d' }).field;
  241. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
  242. expect(err).to.not.exist();
  243. expect(credentials2.user).to.equal('steve');
  244. expect(artifacts.ext).to.equal('some-app-data');
  245. expect(artifacts.app).to.equal('asd23ased');
  246. expect(artifacts.dlg).to.equal('23434szr3q4d');
  247. done();
  248. });
  249. });
  250. });
  251. it('generates a header then fail authentication due to bad hash', function (done) {
  252. var req = {
  253. method: 'GET',
  254. url: '/resource/4?filter=a',
  255. host: 'example.com',
  256. port: 8080
  257. };
  258. credentialsFunc('123456', function (err, credentials1) {
  259. req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, payload: 'hola!', ext: 'some-app-data' }).field;
  260. Hawk.server.authenticate(req, credentialsFunc, { payload: 'byebye!' }, function (err, credentials2, artifacts) {
  261. expect(err).to.exist();
  262. expect(err.output.payload.message).to.equal('Bad payload hash');
  263. done();
  264. });
  265. });
  266. });
  267. it('generates a header for one resource then fail to authenticate another', function (done) {
  268. var req = {
  269. method: 'GET',
  270. url: '/resource/4?filter=a',
  271. host: 'example.com',
  272. port: 8080
  273. };
  274. credentialsFunc('123456', function (err, credentials1) {
  275. req.authorization = Hawk.client.header('http://example.com:8080/resource/4?filter=a', req.method, { credentials: credentials1, ext: 'some-app-data' }).field;
  276. req.url = '/something/else';
  277. Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials2, artifacts) {
  278. expect(err).to.exist();
  279. expect(credentials2).to.exist();
  280. done();
  281. });
  282. });
  283. });
  284. });