strategy.test.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 verify address and chain id', function(done) {
  53. chai.passport.use(new Strategy(function(address, chainId, cb) {
  54. expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
  55. expect(chainId).to.equal(1);
  56. return cb(null, { id: '248289761001' });
  57. }))
  58. .request(function(req) {
  59. req.connection = {};
  60. req.headers.host = 'localhost:3000';
  61. req.body = {
  62. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  63. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  64. '\n' +
  65. 'Sign in with Ethereum to the app.\n' +
  66. '\n' +
  67. 'URI: http://localhost:3000\n' +
  68. 'Version: 1\n' +
  69. 'Chain ID: 1\n' +
  70. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  71. 'Issued At: 2022-06-07T16:28:10.957Z',
  72. signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  73. };
  74. req.session = {
  75. messages: [],
  76. 'ethereum:siwe': {
  77. nonce: 'VjglqeaSMDbPSYe0K'
  78. }
  79. };
  80. })
  81. .success(function(user, info) {
  82. expect(user).to.deep.equal({ id: '248289761001' });
  83. expect(info).to.be.undefined;
  84. expect(this.session).to.deep.equal({
  85. messages: []
  86. });
  87. done();
  88. })
  89. .error(done)
  90. .authenticate();
  91. }); // should verify address and chain id
  92. it('should fail when URI is invalid', function(done) {
  93. chai.passport.use(new Strategy(function(address, cb) {
  94. expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
  95. return cb(null, { id: '248289761001' });
  96. }))
  97. .request(function(req) {
  98. req.connection = {};
  99. req.headers.host = 'localhost:3999';
  100. req.body = {
  101. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  102. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  103. '\n' +
  104. 'Sign in with Ethereum to the app.\n' +
  105. '\n' +
  106. 'URI: http://localhost:3000\n' +
  107. 'Version: 1\n' +
  108. 'Chain ID: 1\n' +
  109. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  110. 'Issued At: 2022-06-07T16:28:10.957Z',
  111. signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  112. };
  113. req.session = {
  114. messages: [],
  115. 'ethereum:siwe': {
  116. nonce: 'VjglqeaSMDbPSYe0K'
  117. }
  118. };
  119. })
  120. .fail(function(challenge, status) {
  121. expect(challenge).to.deep.equal({ message: 'URI mismatch.' });
  122. expect(status).to.equal(403);
  123. done();
  124. })
  125. .error(done)
  126. .authenticate();
  127. }); // should fail when URI is invalid
  128. it('should fail when message is expired', function(done) {
  129. chai.passport.use(new Strategy(function(address, cb) {
  130. expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
  131. return cb(null, { id: '248289761001' });
  132. }))
  133. .request(function(req) {
  134. req.connection = {};
  135. req.headers.host = 'localhost:3000';
  136. req.body = {
  137. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  138. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  139. '\n' +
  140. 'Sign in with Ethereum to the app.\n' +
  141. '\n' +
  142. 'URI: http://localhost:3000\n' +
  143. 'Version: 1\n' +
  144. 'Chain ID: 1\n' +
  145. 'Nonce: GFRz6rD1XKFyYyQT\n' +
  146. 'Issued At: 2022-06-07T22:19:22.065Z\n' +
  147. 'Expiration Time: 2022-06-07T22:20:22.065Z',
  148. signature: '0xc5050e9144943695d2ab233e3d5f205687e29735b07f4e99ef6738ff5512f249582c2b8c105c5c8b9cd9c7910e971765532a55071e0dfd2bbd13e931a024e4991c'
  149. };
  150. req.session = {
  151. messages: [],
  152. 'ethereum:siwe': {
  153. nonce: 'GFRz6rD1XKFyYyQT'
  154. }
  155. };
  156. })
  157. .fail(function(challenge, status) {
  158. expect(challenge).to.deep.equal({ message: 'Expired message.' });
  159. expect(status).to.equal(403);
  160. done();
  161. })
  162. .error(done)
  163. .authenticate();
  164. }); // should fail when message is expired
  165. it('should fail when message is not yet valid', function(done) {
  166. clock = sinon.useFakeTimers(1654640839635);
  167. chai.passport.use(new Strategy(function(address, cb) {
  168. expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
  169. return cb(null, { id: '248289761001' });
  170. }))
  171. .request(function(req) {
  172. req.connection = {};
  173. req.headers.host = 'localhost:3000';
  174. req.body = {
  175. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  176. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  177. '\n' +
  178. 'Sign in with Ethereum to the app.\n' +
  179. '\n' +
  180. 'URI: http://localhost:3000\n' +
  181. 'Version: 1\n' +
  182. 'Chain ID: 1\n' +
  183. 'Nonce: uri9Uq8fydQXUDHx\n' +
  184. 'Issued At: 2022-06-07T22:27:19.635Z\n' +
  185. 'Not Before: 2022-06-07T22:28:19.635Z',
  186. signature: '0x045404ec50df21499be5fdecbb334504070b767f75e3692a62806033d5e2e6ae70a2b13011ca34af0284b48b394994da2aeea73fe05f8fc1836e66db3f1b27521b'
  187. };
  188. req.session = {
  189. messages: [],
  190. 'ethereum:siwe': {
  191. nonce: 'uri9Uq8fydQXUDHx'
  192. }
  193. };
  194. })
  195. .fail(function(challenge, status) {
  196. expect(challenge).to.deep.equal({ message: 'Message not yet valid.' });
  197. expect(status).to.equal(403);
  198. done();
  199. })
  200. .error(done)
  201. .authenticate();
  202. }); // should fail when message is not yet valid
  203. it('should fail when nonce is invalid', function(done) {
  204. clock = sinon.useFakeTimers(1654640839635);
  205. chai.passport.use(new Strategy(function(address, cb) {
  206. expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
  207. return cb(null, { id: '248289761001' });
  208. }))
  209. .request(function(req) {
  210. req.connection = {};
  211. req.headers.host = 'localhost:3000';
  212. req.body = {
  213. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  214. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  215. '\n' +
  216. 'Sign in with Ethereum to the app.\n' +
  217. '\n' +
  218. 'URI: http://localhost:3000\n' +
  219. 'Version: 1\n' +
  220. 'Chain ID: 1\n' +
  221. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  222. 'Issued At: 2022-06-07T16:28:10.957Z',
  223. signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  224. };
  225. req.session = {
  226. messages: [],
  227. 'ethereum:siwe': {
  228. nonce: 'Xri9Uq8fydQXUDHx'
  229. }
  230. };
  231. })
  232. .fail(function(challenge, status) {
  233. expect(challenge).to.deep.equal({ message: 'Invalid nonce.' });
  234. expect(status).to.equal(403);
  235. done();
  236. })
  237. .error(done)
  238. .authenticate();
  239. }); // should fail when message is invalid
  240. it('should fail when signature is invalid', function(done) {
  241. chai.passport.use(new Strategy(function(address, cb) {
  242. expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
  243. return cb(null, { id: '248289761001' });
  244. }))
  245. .request(function(req) {
  246. req.connection = {};
  247. req.headers.host = 'localhost:3000';
  248. req.body = {
  249. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  250. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  251. '\n' +
  252. 'Sign in with Ethereum to the app.\n' +
  253. '\n' +
  254. 'URI: http://localhost:3000\n' +
  255. 'Version: 1\n' +
  256. 'Chain ID: 1\n' +
  257. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  258. 'Issued At: 2022-06-07T16:28:10.957Z',
  259. signature: '0xF303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  260. };
  261. req.session = {
  262. messages: [],
  263. 'ethereum:siwe': {
  264. nonce: 'VjglqeaSMDbPSYe0K'
  265. }
  266. };
  267. })
  268. .fail(function(challenge, status) {
  269. expect(challenge).to.deep.equal({ message: 'Invalid signature.: 0x09967aCB4912a3efDb66039b8BC8ABb202a0f3E4 !== 0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758' });
  270. expect(status).to.equal(403);
  271. done();
  272. })
  273. .error(done)
  274. .authenticate();
  275. }); // should fail when signature is invalid
  276. it('should fail when message is malformed (missing address)', function(done) {
  277. chai.passport.use(new Strategy(function(address, cb) {
  278. expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
  279. return cb(null, { id: '248289761001' });
  280. }))
  281. .request(function(req) {
  282. req.connection = {};
  283. req.headers.host = 'localhost:3000';
  284. req.body = {
  285. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  286. '\n' +
  287. 'Sign in with Ethereum to the app.\n' +
  288. '\n' +
  289. 'URI: http://localhost:3000\n' +
  290. 'Version: 1\n' +
  291. 'Chain ID: 1\n' +
  292. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  293. 'Issued At: 2022-06-07T16:28:10.957Z',
  294. signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  295. };
  296. req.session = {
  297. messages: [],
  298. 'ethereum:siwe': {
  299. nonce: 'VjglqeaSMDbPSYe0K'
  300. }
  301. };
  302. })
  303. .fail(function(challenge, status) {
  304. expect(challenge).to.deep.equal({ message: 'Malformed message.' });
  305. expect(status).to.equal(403);
  306. done();
  307. })
  308. .error(done)
  309. .authenticate();
  310. }); // should fail when message is malformed (missing address)
  311. it('should fail when missing message', function(done) {
  312. chai.passport.use(new Strategy(function(address, cb) {
  313. throw new Error('verify function should not be called');
  314. }))
  315. .request(function(req) {
  316. req.connection = {};
  317. req.headers.host = 'localhost:3000';
  318. req.body = {
  319. signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  320. };
  321. req.session = {
  322. messages: []
  323. };
  324. })
  325. .fail(function(challenge, status) {
  326. expect(challenge).to.deep.equal({ message: 'Missing message.' });
  327. expect(status).to.equal(400);
  328. done();
  329. })
  330. .error(done)
  331. .authenticate();
  332. });
  333. it('should fail when missing signature', function(done) {
  334. chai.passport.use(new Strategy(function(address, cb) {
  335. throw new Error('verify function should not be called');
  336. }))
  337. .request(function(req) {
  338. req.connection = {};
  339. req.headers.host = 'localhost:3000';
  340. req.body = {
  341. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  342. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  343. '\n' +
  344. 'Sign in with Ethereum to the app.\n' +
  345. '\n' +
  346. 'URI: http://localhost:3000\n' +
  347. 'Version: 1\n' +
  348. 'Chain ID: 1\n' +
  349. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  350. 'Issued At: 2022-06-07T16:28:10.957Z'
  351. };
  352. req.session = {
  353. messages: []
  354. };
  355. })
  356. .fail(function(challenge, status) {
  357. expect(challenge).to.deep.equal({ message: 'Missing signature.' });
  358. expect(status).to.equal(400);
  359. done();
  360. })
  361. .error(done)
  362. .authenticate();
  363. });
  364. });