-
Notifications
You must be signed in to change notification settings - Fork 229
bug(auth): Fix mfa strategy return type #19470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
packages/fxa-auth-server/test/local/routes/auth-schemes/mfa.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| const { assert } = require('chai'); | ||
| const sinon = require('sinon'); | ||
| const AppError = require('../../../../lib/error'); | ||
| const { strategy } = require('../../../../lib/routes/auth-schemes/mfa'); | ||
| const jwt = require('jsonwebtoken'); | ||
| const uuid = require('uuid'); | ||
|
|
||
| function makeJwt(account, sessionToken, config) { | ||
| const now = Math.floor(Date.now() / 1000); | ||
| const claims = { | ||
| sub: account.uid, | ||
| scope: [`mfa:test`], | ||
| iat: now, | ||
| jti: uuid.v4(), | ||
| stid: sessionToken.id, | ||
| }; | ||
| const opts = { | ||
| algorithm: 'HS256', | ||
| expiresIn: config.mfa.jwt.expiresInSec, | ||
| audience: config.mfa.jwt.audience, | ||
| issuer: config.mfa.jwt.issuer, | ||
| }; | ||
| const key = config.mfa.jwt.secretKey; | ||
| return jwt.sign(claims, key, opts); | ||
| } | ||
|
|
||
| describe('lib/routes/auth-schemes/mfa', () => { | ||
| const mockSessionToken = { | ||
| uid: 'account-123', | ||
| id: 'session-123', | ||
| get foo() { | ||
| return 'bar'; | ||
| }, | ||
| }; | ||
| const mockAccount = { uid: 'account-123' }; | ||
| const mockConfig = { | ||
| mfa: { | ||
| jwt: { | ||
| expiresInSec: 1, | ||
|
dschom marked this conversation as resolved.
|
||
| audience: 'fxa', | ||
| issuer: 'accounts.firefox.com', | ||
| secretKey: 'foxes'.repeat(13), | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| it('should authenticate with valid jwt token', async () => { | ||
| const jwt = makeJwt(mockAccount, mockSessionToken, mockConfig); | ||
| const request = { | ||
| headers: { authorization: `Bearer ${jwt}` }, | ||
| auth: { mode: 'required' }, | ||
| }; | ||
| const h = { authenticated: sinon.fake() }; | ||
| const getCredentialsFunc = sinon.fake.resolves(mockSessionToken); | ||
| const authStrategy = strategy(mockConfig, getCredentialsFunc)(); | ||
|
|
||
| await authStrategy.authenticate(request, h); | ||
|
|
||
| // Important! Session token should be returned as credentials, | ||
| // AND object reference should not change! | ||
| assert.isTrue( | ||
| h.authenticated.calledOnceWithExactly({ | ||
| credentials: sinon.match.same(mockSessionToken), | ||
| }) | ||
| ); | ||
|
|
||
| // Session token should be decorated with a scope. | ||
| assert.equal(mockSessionToken.scope[0], 'mfa:test'); | ||
| }); | ||
|
|
||
| it('should throw an error if no authorization header is provided', async () => { | ||
| const getCredentialsFunc = sinon.fake.resolves(null); | ||
| const authStrategy = strategy(mockConfig, getCredentialsFunc)(); | ||
|
|
||
| const request = { headers: {}, auth: { mode: 'required' } }; | ||
| const h = { continue: Symbol('continue') }; | ||
|
|
||
| try { | ||
| await authStrategy.authenticate(request, h); | ||
| assert.fail('Should have thrown an error'); | ||
| } catch (err) { | ||
| assert.instanceOf(err, AppError); | ||
| const errorResponse = err.output.payload; | ||
| assert.equal(errorResponse.code, 401); | ||
| assert.equal(errorResponse.errno, 110); | ||
| assert.equal(errorResponse.message, 'Unauthorized for route'); | ||
| assert.equal(errorResponse.detail, 'Token not found'); | ||
| } | ||
| }); | ||
|
|
||
| it('should not authenticate if the parent session cannot be found', async () => { | ||
| const getCredentialsFunc = sinon.fake.resolves(null); | ||
| const authStrategy = strategy(mockConfig, getCredentialsFunc)(); | ||
| const jwt = makeJwt(mockAccount, mockSessionToken, mockConfig); | ||
|
|
||
| const request = { | ||
| headers: { authorization: `Bearer ${jwt}` }, | ||
| auth: { mode: 'required' }, | ||
| }; | ||
| const h = { continue: Symbol('continue') }; | ||
|
|
||
| try { | ||
| await authStrategy.authenticate(request, h); | ||
| assert.fail('Should have thrown an error'); | ||
| } catch (err) { | ||
| assert.instanceOf(err, AppError); | ||
| const errorResponse = err.output.payload; | ||
| assert.equal(errorResponse.code, 401); | ||
| assert.equal(errorResponse.errno, 110); | ||
| assert.equal(errorResponse.message, 'Unauthorized for route'); | ||
| assert.equal(errorResponse.detail, 'Token not found'); | ||
| } | ||
| }); | ||
|
|
||
| it('should not authenticate with invalid jwt token due to sub mismatch', async () => { | ||
| const getCredentialsFunc = sinon.fake.resolves({ sub: 'account-234' }); | ||
| const authStrategy = strategy(mockConfig, getCredentialsFunc)(); | ||
| const jwt = makeJwt(mockAccount, mockSessionToken, mockConfig); | ||
|
|
||
| const request = { | ||
| headers: { authorization: `Bearer ${jwt}` }, | ||
| auth: { mode: 'required' }, | ||
| }; | ||
| const h = { continue: Symbol('continue') }; | ||
|
|
||
| try { | ||
| await authStrategy.authenticate(request, h); | ||
| assert.fail('Should have thrown an error'); | ||
| } catch (err) { | ||
| assert.instanceOf(err, AppError); | ||
| const errorResponse = err.output.payload; | ||
| assert.equal(errorResponse.code, 401); | ||
| assert.equal(errorResponse.errno, 110); | ||
| assert.equal(errorResponse.message, 'Unauthorized for route'); | ||
| assert.equal(errorResponse.detail, 'Token invalid'); | ||
| } | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.