strategy.test.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. var chai = require('chai');
  2. var sinon = require('sinon');
  3. var Strategy = require('../lib/strategy');
  4. describe('Strategy', function() {
  5. it('should be named ethereum', function() {
  6. var strategy = new Strategy(function(){});
  7. expect(strategy.name).to.equal('ethereum');
  8. });
  9. it('should verify address', function(done) {
  10. chai.passport.use(new Strategy(function(address, cb) {
  11. expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
  12. return cb(null, { id: '248289761001' });
  13. }))
  14. .request(function(req) {
  15. req.connection = {};
  16. req.headers.host = 'localhost:3000';
  17. req.body = {
  18. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  19. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  20. '\n' +
  21. 'Sign in with Ethereum to the app.\n' +
  22. '\n' +
  23. 'URI: http://localhost:3000\n' +
  24. 'Version: 1\n' +
  25. 'Chain ID: 1\n' +
  26. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  27. 'Issued At: 2022-06-07T16:28:10.957Z',
  28. signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  29. };
  30. req.session = {
  31. messages: [],
  32. 'ethereum:siwe': {
  33. nonce: 'VjglqeaSMDbPSYe0K'
  34. }
  35. };
  36. })
  37. .success(function(user, info) {
  38. expect(user).to.deep.equal({ id: '248289761001' });
  39. expect(info).to.be.undefined;
  40. expect(this.session).to.deep.equal({
  41. messages: []
  42. });
  43. done();
  44. })
  45. .error(done)
  46. .authenticate();
  47. }); // should verify address
  48. it('should fail when URI is invalid', function(done) {
  49. chai.passport.use(new Strategy(function(address, cb) {
  50. expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
  51. return cb(null, { id: '248289761001' });
  52. }))
  53. .request(function(req) {
  54. req.connection = {};
  55. req.headers.host = 'localhost:3999';
  56. req.body = {
  57. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  58. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  59. '\n' +
  60. 'Sign in with Ethereum to the app.\n' +
  61. '\n' +
  62. 'URI: http://localhost:3000\n' +
  63. 'Version: 1\n' +
  64. 'Chain ID: 1\n' +
  65. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  66. 'Issued At: 2022-06-07T16:28:10.957Z',
  67. signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  68. };
  69. req.session = {
  70. messages: [],
  71. 'ethereum:siwe': {
  72. nonce: 'VjglqeaSMDbPSYe0K'
  73. }
  74. };
  75. })
  76. .fail(function(challenge, status) {
  77. expect(challenge).to.deep.equal({ message: 'URI mismatch' });
  78. expect(status).to.equal(403);
  79. done();
  80. })
  81. .error(done)
  82. .authenticate();
  83. }); // should fail when URI is invalid
  84. it('should fail when address is missing from message', function(done) {
  85. chai.passport.use(new Strategy(function(address, cb) {
  86. expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
  87. return cb(null, { id: '248289761001' });
  88. }))
  89. .request(function(req) {
  90. req.connection = {};
  91. req.headers.host = 'localhost:3000';
  92. req.body = {
  93. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  94. '\n' +
  95. 'Sign in with Ethereum to the app.\n' +
  96. '\n' +
  97. 'URI: http://localhost:3000\n' +
  98. 'Version: 1\n' +
  99. 'Chain ID: 1\n' +
  100. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  101. 'Issued At: 2022-06-07T16:28:10.957Z',
  102. signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  103. };
  104. req.session = {
  105. messages: [],
  106. 'ethereum:siwe': {
  107. nonce: 'VjglqeaSMDbPSYe0K'
  108. }
  109. };
  110. })
  111. .fail(function(challenge, status) {
  112. expect(challenge).to.deep.equal({ message: 'Invalid message' });
  113. expect(status).to.equal(403);
  114. done();
  115. })
  116. .error(done)
  117. .authenticate();
  118. }); // should fail when address is missing from message
  119. it('should fail when missing message', function(done) {
  120. chai.passport.use(new Strategy(function(address, cb) {
  121. throw new Error('verify function should not be called');
  122. }))
  123. .request(function(req) {
  124. req.connection = {};
  125. req.headers.host = 'localhost:3000';
  126. req.body = {
  127. signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  128. };
  129. req.session = {
  130. messages: []
  131. };
  132. })
  133. .fail(function(challenge, status) {
  134. expect(challenge).to.deep.equal({ message: 'Missing message' });
  135. expect(status).to.equal(400);
  136. done();
  137. })
  138. .error(done)
  139. .authenticate();
  140. });
  141. it('should fail when missing signature', function(done) {
  142. chai.passport.use(new Strategy(function(address, cb) {
  143. throw new Error('verify function should not be called');
  144. }))
  145. .request(function(req) {
  146. req.connection = {};
  147. req.headers.host = 'localhost:3000';
  148. req.body = {
  149. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  150. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  151. '\n' +
  152. 'Sign in with Ethereum to the app.\n' +
  153. '\n' +
  154. 'URI: http://localhost:3000\n' +
  155. 'Version: 1\n' +
  156. 'Chain ID: 1\n' +
  157. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  158. 'Issued At: 2022-06-07T16:28:10.957Z'
  159. };
  160. req.session = {
  161. messages: []
  162. };
  163. })
  164. .fail(function(challenge, status) {
  165. expect(challenge).to.deep.equal({ message: 'Missing signature' });
  166. expect(status).to.equal(400);
  167. done();
  168. })
  169. .error(done)
  170. .authenticate();
  171. });
  172. });