|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +const { assert } = require('chai'); |
| 6 | +const sinon = require('sinon'); |
| 7 | +const AppError = require('../../../../lib/error'); |
| 8 | +const { strategy } = require('../../../../lib/routes/auth-schemes/mfa'); |
| 9 | +const jwt = require('jsonwebtoken'); |
| 10 | +const uuid = require('uuid'); |
| 11 | + |
| 12 | +function makeJwt(account, sessionToken, config) { |
| 13 | + const now = Math.floor(Date.now() / 1000); |
| 14 | + const claims = { |
| 15 | + sub: account.uid, |
| 16 | + scope: [`mfa:test`], |
| 17 | + iat: now, |
| 18 | + jti: uuid.v4(), |
| 19 | + stid: sessionToken.id, |
| 20 | + }; |
| 21 | + const opts = { |
| 22 | + algorithm: 'HS256', |
| 23 | + expiresIn: config.mfa.jwt.expiresInSec, |
| 24 | + audience: config.mfa.jwt.audience, |
| 25 | + issuer: config.mfa.jwt.issuer, |
| 26 | + }; |
| 27 | + const key = config.mfa.jwt.secretKey; |
| 28 | + return jwt.sign(claims, key, opts); |
| 29 | +} |
| 30 | + |
| 31 | +describe('lib/routes/auth-schemes/mfa', () => { |
| 32 | + const mockSessionToken = { |
| 33 | + uid: 'account-123', |
| 34 | + id: 'session-123', |
| 35 | + get foo() { |
| 36 | + return 'bar'; |
| 37 | + }, |
| 38 | + }; |
| 39 | + const mockAccount = { uid: 'account-123' }; |
| 40 | + const mockConfig = { |
| 41 | + mfa: { |
| 42 | + jwt: { |
| 43 | + expiresInSec: 1, |
| 44 | + audience: 'fxa', |
| 45 | + issuer: 'accounts.firefox.com', |
| 46 | + secretKey: 'foxes'.repeat(13), |
| 47 | + }, |
| 48 | + }, |
| 49 | + }; |
| 50 | + |
| 51 | + it('should authenticate with valid jwt token', async () => { |
| 52 | + const jwt = makeJwt(mockAccount, mockSessionToken, mockConfig); |
| 53 | + const request = { |
| 54 | + headers: { authorization: `Bearer ${jwt}` }, |
| 55 | + auth: { mode: 'required' }, |
| 56 | + }; |
| 57 | + const h = { authenticated: sinon.fake() }; |
| 58 | + const getCredentialsFunc = sinon.fake.resolves(mockSessionToken); |
| 59 | + const authStrategy = strategy(mockConfig, getCredentialsFunc)(); |
| 60 | + |
| 61 | + await authStrategy.authenticate(request, h); |
| 62 | + |
| 63 | + // Important! Session token should be returned as credentials, |
| 64 | + // AND object reference should not change! |
| 65 | + assert.isTrue( |
| 66 | + h.authenticated.calledOnceWithExactly({ |
| 67 | + credentials: sinon.match.same(mockSessionToken), |
| 68 | + }) |
| 69 | + ); |
| 70 | + |
| 71 | + // Session token should be decorated with a scope. |
| 72 | + assert.equal(mockSessionToken.scope[0], 'mfa:test'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should throw an error if no authorization header is provided', async () => { |
| 76 | + const getCredentialsFunc = sinon.fake.resolves(null); |
| 77 | + const authStrategy = strategy(mockConfig, getCredentialsFunc)(); |
| 78 | + |
| 79 | + const request = { headers: {}, auth: { mode: 'required' } }; |
| 80 | + const h = { continue: Symbol('continue') }; |
| 81 | + |
| 82 | + try { |
| 83 | + await authStrategy.authenticate(request, h); |
| 84 | + assert.fail('Should have thrown an error'); |
| 85 | + } catch (err) { |
| 86 | + assert.instanceOf(err, AppError); |
| 87 | + const errorResponse = err.output.payload; |
| 88 | + assert.equal(errorResponse.code, 401); |
| 89 | + assert.equal(errorResponse.errno, 110); |
| 90 | + assert.equal(errorResponse.message, 'Unauthorized for route'); |
| 91 | + assert.equal(errorResponse.detail, 'Token not found'); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + it('should not authenticate if the parent session cannot be found', async () => { |
| 96 | + const getCredentialsFunc = sinon.fake.resolves(null); |
| 97 | + const authStrategy = strategy(mockConfig, getCredentialsFunc)(); |
| 98 | + const jwt = makeJwt(mockAccount, mockSessionToken, mockConfig); |
| 99 | + |
| 100 | + const request = { |
| 101 | + headers: { authorization: `Bearer ${jwt}` }, |
| 102 | + auth: { mode: 'required' }, |
| 103 | + }; |
| 104 | + const h = { continue: Symbol('continue') }; |
| 105 | + |
| 106 | + try { |
| 107 | + await authStrategy.authenticate(request, h); |
| 108 | + assert.fail('Should have thrown an error'); |
| 109 | + } catch (err) { |
| 110 | + assert.instanceOf(err, AppError); |
| 111 | + const errorResponse = err.output.payload; |
| 112 | + assert.equal(errorResponse.code, 401); |
| 113 | + assert.equal(errorResponse.errno, 110); |
| 114 | + assert.equal(errorResponse.message, 'Unauthorized for route'); |
| 115 | + assert.equal(errorResponse.detail, 'Token not found'); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + it('should not authenticate with invalid jwt token due to sub mismatch', async () => { |
| 120 | + const getCredentialsFunc = sinon.fake.resolves({ sub: 'account-234' }); |
| 121 | + const authStrategy = strategy(mockConfig, getCredentialsFunc)(); |
| 122 | + const jwt = makeJwt(mockAccount, mockSessionToken, mockConfig); |
| 123 | + |
| 124 | + const request = { |
| 125 | + headers: { authorization: `Bearer ${jwt}` }, |
| 126 | + auth: { mode: 'required' }, |
| 127 | + }; |
| 128 | + const h = { continue: Symbol('continue') }; |
| 129 | + |
| 130 | + try { |
| 131 | + await authStrategy.authenticate(request, h); |
| 132 | + assert.fail('Should have thrown an error'); |
| 133 | + } catch (err) { |
| 134 | + assert.instanceOf(err, AppError); |
| 135 | + const errorResponse = err.output.payload; |
| 136 | + assert.equal(errorResponse.code, 401); |
| 137 | + assert.equal(errorResponse.errno, 110); |
| 138 | + assert.equal(errorResponse.message, 'Unauthorized for route'); |
| 139 | + assert.equal(errorResponse.detail, 'Token invalid'); |
| 140 | + } |
| 141 | + }); |
| 142 | +}); |
0 commit comments