client.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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('client', function () {
  15. describe('#header', function () {
  16. it('should return a valid authorization header (sha1)', function (done) {
  17. var credentials = {
  18. id: '123456',
  19. key: '2983d45yun89q',
  20. algorithm: 'sha1'
  21. };
  22. var header = Hawk.client.header('http://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, ext: 'Bazinga!', timestamp: 1353809207, nonce: 'Ygvqdz', payload: 'something to write about' }).field;
  23. expect(header).to.equal('Hawk id="123456", ts="1353809207", nonce="Ygvqdz", hash="bsvY3IfUllw6V5rvk4tStEvpBhE=", ext="Bazinga!", mac="qbf1ZPG/r/e06F4ht+T77LXi5vw="');
  24. done();
  25. });
  26. it('should return a valid authorization header (sha256)', function (done) {
  27. var credentials = {
  28. id: '123456',
  29. key: '2983d45yun89q',
  30. algorithm: 'sha256'
  31. };
  32. var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, ext: 'Bazinga!', timestamp: 1353809207, nonce: 'Ygvqdz', payload: 'something to write about', contentType: 'text/plain' }).field;
  33. expect(header).to.equal('Hawk id="123456", ts="1353809207", nonce="Ygvqdz", hash="2QfCt3GuY9HQnHWyWD3wX68ZOKbynqlfYmuO2ZBRqtY=", ext="Bazinga!", mac="q1CwFoSHzPZSkbIvl0oYlD+91rBUEvFk763nMjMndj8="');
  34. done();
  35. });
  36. it('should return a valid authorization header (no ext)', function (done) {
  37. var credentials = {
  38. id: '123456',
  39. key: '2983d45yun89q',
  40. algorithm: 'sha256'
  41. };
  42. var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, timestamp: 1353809207, nonce: 'Ygvqdz', payload: 'something to write about', contentType: 'text/plain' }).field;
  43. expect(header).to.equal('Hawk id="123456", ts="1353809207", nonce="Ygvqdz", hash="2QfCt3GuY9HQnHWyWD3wX68ZOKbynqlfYmuO2ZBRqtY=", mac="HTgtd0jPI6E4izx8e4OHdO36q00xFCU0FolNq3RiCYs="');
  44. done();
  45. });
  46. it('should return an empty authorization header on missing options', function (done) {
  47. var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST').field;
  48. expect(header).to.equal('');
  49. done();
  50. });
  51. it('should return an empty authorization header on invalid credentials', function (done) {
  52. var credentials = {
  53. key: '2983d45yun89q',
  54. algorithm: 'sha256'
  55. };
  56. var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, ext: 'Bazinga!', timestamp: 1353809207 }).field;
  57. expect(header).to.equal('');
  58. done();
  59. });
  60. it('should return an empty authorization header on invalid algorithm', function (done) {
  61. var credentials = {
  62. id: '123456',
  63. key: '2983d45yun89q',
  64. algorithm: 'hmac-sha-0'
  65. };
  66. var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, payload: 'something, anything!', ext: 'Bazinga!', timestamp: 1353809207 }).field;
  67. expect(header).to.equal('');
  68. done();
  69. });
  70. });
  71. describe('#authenticate', function () {
  72. it('should return false on invalid header', function (done) {
  73. var res = {
  74. headers: {
  75. 'server-authorization': 'Hawk mac="abc", bad="xyz"'
  76. }
  77. };
  78. expect(Hawk.client.authenticate(res, {})).to.equal(false);
  79. done();
  80. });
  81. it('should return false on invalid mac', function (done) {
  82. var res = {
  83. headers: {
  84. 'content-type': 'text/plain',
  85. 'server-authorization': 'Hawk mac="_IJRsMl/4oL+nn+vKoeVZPdCHXB4yJkNnBbTbHFZUYE=", hash="f9cDF/TDm7TkYRLnGwRMfeDzT6LixQVLvrIKhh0vgmM=", ext="response-specific"'
  86. }
  87. };
  88. var artifacts = {
  89. method: 'POST',
  90. host: 'example.com',
  91. port: '8080',
  92. resource: '/resource/4?filter=a',
  93. ts: '1362336900',
  94. nonce: 'eb5S_L',
  95. hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
  96. ext: 'some-app-data',
  97. app: undefined,
  98. dlg: undefined,
  99. mac: 'BlmSe8K+pbKIb6YsZCnt4E1GrYvY1AaYayNR82dGpIk=',
  100. id: '123456'
  101. };
  102. var credentials = {
  103. id: '123456',
  104. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  105. algorithm: 'sha256',
  106. user: 'steve'
  107. };
  108. expect(Hawk.client.authenticate(res, credentials, artifacts)).to.equal(false);
  109. done();
  110. });
  111. it('should return true on ignoring hash', function (done) {
  112. var res = {
  113. headers: {
  114. 'content-type': 'text/plain',
  115. 'server-authorization': 'Hawk mac="XIJRsMl/4oL+nn+vKoeVZPdCHXB4yJkNnBbTbHFZUYE=", hash="f9cDF/TDm7TkYRLnGwRMfeDzT6LixQVLvrIKhh0vgmM=", ext="response-specific"'
  116. }
  117. };
  118. var artifacts = {
  119. method: 'POST',
  120. host: 'example.com',
  121. port: '8080',
  122. resource: '/resource/4?filter=a',
  123. ts: '1362336900',
  124. nonce: 'eb5S_L',
  125. hash: 'nJjkVtBE5Y/Bk38Aiokwn0jiJxt/0S2WRSUwWLCf5xk=',
  126. ext: 'some-app-data',
  127. app: undefined,
  128. dlg: undefined,
  129. mac: 'BlmSe8K+pbKIb6YsZCnt4E1GrYvY1AaYayNR82dGpIk=',
  130. id: '123456'
  131. };
  132. var credentials = {
  133. id: '123456',
  134. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  135. algorithm: 'sha256',
  136. user: 'steve'
  137. };
  138. expect(Hawk.client.authenticate(res, credentials, artifacts)).to.equal(true);
  139. done();
  140. });
  141. it('should fail on invalid WWW-Authenticate header format', function (done) {
  142. var header = 'Hawk ts="1362346425875", tsm="PhwayS28vtnn3qbv0mqRBYSXebN/zggEtucfeZ620Zo=", x="Stale timestamp"';
  143. expect(Hawk.client.authenticate({ headers: { 'www-authenticate': header } }, {})).to.equal(false);
  144. done();
  145. });
  146. it('should fail on invalid WWW-Authenticate header format', function (done) {
  147. var credentials = {
  148. id: '123456',
  149. key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
  150. algorithm: 'sha256',
  151. user: 'steve'
  152. };
  153. var header = 'Hawk ts="1362346425875", tsm="hwayS28vtnn3qbv0mqRBYSXebN/zggEtucfeZ620Zo=", error="Stale timestamp"';
  154. expect(Hawk.client.authenticate({ headers: { 'www-authenticate': header } }, credentials)).to.equal(false);
  155. done();
  156. });
  157. });
  158. });
  159. });