strategy.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. var chai = require('chai');
  2. var sinon = require('sinon');
  3. var Strategy = require('../lib/strategy');
  4. describe('Strategy', function() {
  5. var clock;
  6. afterEach(function() {
  7. clock && clock.restore();
  8. });
  9. it('should be named ethereum', function() {
  10. var strategy = new Strategy(function(){});
  11. expect(strategy.name).to.equal('ethereum');
  12. });
  13. it('should verify address', function(done) {
  14. chai.passport.use(new Strategy(function(address, cb) {
  15. expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
  16. return cb(null, { id: '248289761001' });
  17. }))
  18. .request(function(req) {
  19. req.connection = {};
  20. req.headers.host = 'localhost:3000';
  21. req.body = {
  22. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  23. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  24. '\n' +
  25. 'Sign in with Ethereum to the app.\n' +
  26. '\n' +
  27. 'URI: http://localhost:3000\n' +
  28. 'Version: 1\n' +
  29. 'Chain ID: 1\n' +
  30. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  31. 'Issued At: 2022-06-07T16:28:10.957Z',
  32. signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  33. };
  34. req.session = {
  35. messages: [],
  36. 'ethereum:siwe': {
  37. nonce: 'VjglqeaSMDbPSYe0K'
  38. }
  39. };
  40. })
  41. .success(function(user, info) {
  42. expect(user).to.deep.equal({ id: '248289761001' });
  43. expect(info).to.be.undefined;
  44. expect(this.session).to.deep.equal({
  45. messages: []
  46. });
  47. done();
  48. })
  49. .error(done)
  50. .authenticate();
  51. }); // should verify address
  52. it('should fail when URI is invalid', function(done) {
  53. chai.passport.use(new Strategy(function(address, cb) {
  54. expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
  55. return cb(null, { id: '248289761001' });
  56. }))
  57. .request(function(req) {
  58. req.connection = {};
  59. req.headers.host = 'localhost:3999';
  60. req.body = {
  61. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  62. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  63. '\n' +
  64. 'Sign in with Ethereum to the app.\n' +
  65. '\n' +
  66. 'URI: http://localhost:3000\n' +
  67. 'Version: 1\n' +
  68. 'Chain ID: 1\n' +
  69. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  70. 'Issued At: 2022-06-07T16:28:10.957Z',
  71. signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  72. };
  73. req.session = {
  74. messages: [],
  75. 'ethereum:siwe': {
  76. nonce: 'VjglqeaSMDbPSYe0K'
  77. }
  78. };
  79. })
  80. .fail(function(challenge, status) {
  81. expect(challenge).to.deep.equal({ message: 'URI mismatch.' });
  82. expect(status).to.equal(403);
  83. done();
  84. })
  85. .error(done)
  86. .authenticate();
  87. }); // should fail when URI is invalid
  88. it('should fail when message is expired', function(done) {
  89. chai.passport.use(new Strategy(function(address, cb) {
  90. expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
  91. return cb(null, { id: '248289761001' });
  92. }))
  93. .request(function(req) {
  94. req.connection = {};
  95. req.headers.host = 'localhost:3000';
  96. req.body = {
  97. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  98. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  99. '\n' +
  100. 'Sign in with Ethereum to the app.\n' +
  101. '\n' +
  102. 'URI: http://localhost:3000\n' +
  103. 'Version: 1\n' +
  104. 'Chain ID: 1\n' +
  105. 'Nonce: GFRz6rD1XKFyYyQT\n' +
  106. 'Issued At: 2022-06-07T22:19:22.065Z\n' +
  107. 'Expiration Time: 2022-06-07T22:20:22.065Z',
  108. signature: '0xc5050e9144943695d2ab233e3d5f205687e29735b07f4e99ef6738ff5512f249582c2b8c105c5c8b9cd9c7910e971765532a55071e0dfd2bbd13e931a024e4991c'
  109. };
  110. req.session = {
  111. messages: [],
  112. 'ethereum:siwe': {
  113. nonce: 'GFRz6rD1XKFyYyQT'
  114. }
  115. };
  116. })
  117. .fail(function(challenge, status) {
  118. expect(challenge).to.deep.equal({ message: 'Expired message.' });
  119. expect(status).to.equal(403);
  120. done();
  121. })
  122. .error(done)
  123. .authenticate();
  124. }); // should fail when message is expired
  125. it('should fail when message is not yet valid', function(done) {
  126. clock = sinon.useFakeTimers(1654640839635);
  127. chai.passport.use(new Strategy(function(address, cb) {
  128. expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
  129. return cb(null, { id: '248289761001' });
  130. }))
  131. .request(function(req) {
  132. req.connection = {};
  133. req.headers.host = 'localhost:3000';
  134. req.body = {
  135. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  136. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  137. '\n' +
  138. 'Sign in with Ethereum to the app.\n' +
  139. '\n' +
  140. 'URI: http://localhost:3000\n' +
  141. 'Version: 1\n' +
  142. 'Chain ID: 1\n' +
  143. 'Nonce: uri9Uq8fydQXUDHx\n' +
  144. 'Issued At: 2022-06-07T22:27:19.635Z\n' +
  145. 'Not Before: 2022-06-07T22:28:19.635Z',
  146. signature: '0x045404ec50df21499be5fdecbb334504070b767f75e3692a62806033d5e2e6ae70a2b13011ca34af0284b48b394994da2aeea73fe05f8fc1836e66db3f1b27521b'
  147. };
  148. req.session = {
  149. messages: [],
  150. 'ethereum:siwe': {
  151. nonce: 'uri9Uq8fydQXUDHx'
  152. }
  153. };
  154. })
  155. .fail(function(challenge, status) {
  156. expect(challenge).to.deep.equal({ message: 'Message not yet valid.' });
  157. expect(status).to.equal(403);
  158. done();
  159. })
  160. .error(done)
  161. .authenticate();
  162. }); // should fail when message is not yet valid
  163. it('should fail when signature is invalid', function(done) {
  164. chai.passport.use(new Strategy(function(address, cb) {
  165. expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
  166. return cb(null, { id: '248289761001' });
  167. }))
  168. .request(function(req) {
  169. req.connection = {};
  170. req.headers.host = 'localhost:3000';
  171. req.body = {
  172. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  173. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  174. '\n' +
  175. 'Sign in with Ethereum to the app.\n' +
  176. '\n' +
  177. 'URI: http://localhost:3000\n' +
  178. 'Version: 1\n' +
  179. 'Chain ID: 1\n' +
  180. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  181. 'Issued At: 2022-06-07T16:28:10.957Z',
  182. signature: '0xF303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  183. };
  184. req.session = {
  185. messages: [],
  186. 'ethereum:siwe': {
  187. nonce: 'VjglqeaSMDbPSYe0K'
  188. }
  189. };
  190. })
  191. .fail(function(challenge, status) {
  192. expect(challenge).to.deep.equal({ message: 'Invalid signature.: 0x09967aCB4912a3efDb66039b8BC8ABb202a0f3E4 !== 0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758' });
  193. expect(status).to.equal(403);
  194. done();
  195. })
  196. .error(done)
  197. .authenticate();
  198. }); // should fail when signature is invalid
  199. it('should fail when message is malformed (missing message)', function(done) {
  200. chai.passport.use(new Strategy(function(address, cb) {
  201. expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
  202. return cb(null, { id: '248289761001' });
  203. }))
  204. .request(function(req) {
  205. req.connection = {};
  206. req.headers.host = 'localhost:3000';
  207. req.body = {
  208. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  209. '\n' +
  210. 'Sign in with Ethereum to the app.\n' +
  211. '\n' +
  212. 'URI: http://localhost:3000\n' +
  213. 'Version: 1\n' +
  214. 'Chain ID: 1\n' +
  215. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  216. 'Issued At: 2022-06-07T16:28:10.957Z',
  217. signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  218. };
  219. req.session = {
  220. messages: [],
  221. 'ethereum:siwe': {
  222. nonce: 'VjglqeaSMDbPSYe0K'
  223. }
  224. };
  225. })
  226. .fail(function(challenge, status) {
  227. expect(challenge).to.deep.equal({ message: 'Invalid message.' });
  228. expect(status).to.equal(403);
  229. done();
  230. })
  231. .error(done)
  232. .authenticate();
  233. }); // should fail when message is malformed (missing message)
  234. it('should fail when missing message', function(done) {
  235. chai.passport.use(new Strategy(function(address, cb) {
  236. throw new Error('verify function should not be called');
  237. }))
  238. .request(function(req) {
  239. req.connection = {};
  240. req.headers.host = 'localhost:3000';
  241. req.body = {
  242. signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  243. };
  244. req.session = {
  245. messages: []
  246. };
  247. })
  248. .fail(function(challenge, status) {
  249. expect(challenge).to.deep.equal({ message: 'Missing message.' });
  250. expect(status).to.equal(400);
  251. done();
  252. })
  253. .error(done)
  254. .authenticate();
  255. });
  256. it('should fail when missing signature', function(done) {
  257. chai.passport.use(new Strategy(function(address, cb) {
  258. throw new Error('verify function should not be called');
  259. }))
  260. .request(function(req) {
  261. req.connection = {};
  262. req.headers.host = 'localhost:3000';
  263. req.body = {
  264. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  265. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  266. '\n' +
  267. 'Sign in with Ethereum to the app.\n' +
  268. '\n' +
  269. 'URI: http://localhost:3000\n' +
  270. 'Version: 1\n' +
  271. 'Chain ID: 1\n' +
  272. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  273. 'Issued At: 2022-06-07T16:28:10.957Z'
  274. };
  275. req.session = {
  276. messages: []
  277. };
  278. })
  279. .fail(function(challenge, status) {
  280. expect(challenge).to.deep.equal({ message: 'Missing signature.' });
  281. expect(status).to.equal(400);
  282. done();
  283. })
  284. .error(done)
  285. .authenticate();
  286. });
  287. });