|
| 1 | +/*eslint no-unused-expressions: 0, prefer-arrow-callback: 0 */ |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const supertest = require('supertest'); |
| 6 | +const chai = require('chai'); |
| 7 | +const speakeasy = require('speakeasy'); |
| 8 | + |
| 9 | +const expect = chai.expect; |
| 10 | +chai.config.includeStack = true; |
| 11 | +const config = require('@zone-eu/wild-config'); |
| 12 | + |
| 13 | +const server = supertest.agent(`http://127.0.0.1:${config.api.port}`); |
| 14 | + |
| 15 | +describe('API TOTP', function () { |
| 16 | + this.timeout(10000); // eslint-disable-line no-invalid-this |
| 17 | + |
| 18 | + let user; |
| 19 | + let seed; |
| 20 | + let accessToken; |
| 21 | + let totpNonce; |
| 22 | + |
| 23 | + it('should POST /users expect success / create a user with TOTP', async () => { |
| 24 | + const response = await server |
| 25 | + .post('/users') |
| 26 | + .send({ |
| 27 | + username: 'totpnonceuser', |
| 28 | + name: 'Totp Nonce User', |
| 29 | + address: 'totpnonce@example.com', |
| 30 | + password: 'totpsecretvalue' |
| 31 | + }) |
| 32 | + .expect(200); |
| 33 | + |
| 34 | + expect(response.body.success).to.be.true; |
| 35 | + user = response.body.id; |
| 36 | + }); |
| 37 | + |
| 38 | + it('should POST /users/{user}/2fa/totp/setup expect success', async () => { |
| 39 | + const response = await server |
| 40 | + .post(`/users/${user}/2fa/totp/setup`) |
| 41 | + .send({ |
| 42 | + issuer: 'WildDuck Test' |
| 43 | + }) |
| 44 | + .expect(200); |
| 45 | + |
| 46 | + expect(response.body.success).to.be.true; |
| 47 | + expect(response.body.seed).to.exist; |
| 48 | + |
| 49 | + seed = response.body.seed; |
| 50 | + }); |
| 51 | + |
| 52 | + it('should POST /users/{user}/2fa/totp/enable expect success', async () => { |
| 53 | + const response = await server |
| 54 | + .post(`/users/${user}/2fa/totp/enable`) |
| 55 | + .send({ |
| 56 | + token: speakeasy.totp({ |
| 57 | + secret: seed, |
| 58 | + encoding: 'base32' |
| 59 | + }) |
| 60 | + }) |
| 61 | + .expect(200); |
| 62 | + |
| 63 | + expect(response.body.success).to.be.true; |
| 64 | + }); |
| 65 | + |
| 66 | + it('should POST /authenticate expect success / return auth token and totpNonce', async () => { |
| 67 | + const response = await server |
| 68 | + .post('/authenticate') |
| 69 | + .send({ |
| 70 | + username: 'totpnonceuser', |
| 71 | + password: 'totpsecretvalue', |
| 72 | + token: true |
| 73 | + }) |
| 74 | + .expect(200); |
| 75 | + |
| 76 | + expect(response.body.success).to.be.true; |
| 77 | + expect(response.body.id).to.equal(user); |
| 78 | + expect(response.body.require2fa).to.deep.equal(['totp']); |
| 79 | + expect(response.body.token).to.match(/^[0-9a-f]{40}$/); |
| 80 | + expect(response.body.totpNonce).to.match(/^[0-9a-f]{40}$/); |
| 81 | + |
| 82 | + accessToken = response.body.token; |
| 83 | + totpNonce = response.body.totpNonce; |
| 84 | + }); |
| 85 | + |
| 86 | + it('should POST /users/{user}/2fa/totp/check expect success', async () => { |
| 87 | + const response = await server |
| 88 | + .post(`/users/${user}/2fa/totp/check?accessToken=${accessToken}`) |
| 89 | + .send({ |
| 90 | + token: speakeasy.totp({ |
| 91 | + secret: seed, |
| 92 | + encoding: 'base32' |
| 93 | + }), |
| 94 | + totpNonce |
| 95 | + }) |
| 96 | + .expect(200); |
| 97 | + |
| 98 | + expect(response.body.success).to.be.true; |
| 99 | + }); |
| 100 | + |
| 101 | + it('should POST /users/{user}/2fa/totp/check expect failure / reject reused totpNonce', async () => { |
| 102 | + const response = await server |
| 103 | + .post(`/users/${user}/2fa/totp/check?accessToken=${accessToken}`) |
| 104 | + .send({ |
| 105 | + token: speakeasy.totp({ |
| 106 | + secret: seed, |
| 107 | + encoding: 'base32' |
| 108 | + }), |
| 109 | + totpNonce |
| 110 | + }) |
| 111 | + .expect(403); |
| 112 | + |
| 113 | + expect(response.body.code).to.equal('InvalidTotpNonce'); |
| 114 | + }); |
| 115 | +}); |
0 commit comments