|
| 1 | +/** |
| 2 | + * Copyright (c) HashiCorp, Inc. |
| 3 | + * SPDX-License-Identifier: BUSL-1.1 |
| 4 | + */ |
| 5 | + |
| 6 | +import { module, test } from 'qunit'; |
| 7 | +import { setupApplicationTest } from 'ember-qunit'; |
| 8 | +import { click, visit, fillIn } from '@ember/test-helpers'; |
| 9 | +import { setupMirage } from 'ember-cli-mirage/test-support'; |
| 10 | +import { AUTH_FORM } from 'vault/tests/helpers/auth/auth-form-selectors'; |
| 11 | +import { GENERAL } from 'vault/tests/helpers/general-selectors'; |
| 12 | +import { MFA_SELECTORS } from 'vault/tests/helpers/mfa/mfa-selectors'; |
| 13 | +import { constraintId, setupTotpMfaResponse } from 'vault/tests/helpers/mfa/mfa-helpers'; |
| 14 | +import { fillInLoginFields } from 'vault/tests/helpers/auth/auth-helpers'; |
| 15 | +import { callbackData, WindowStub } from 'vault/tests/helpers/oidc-window-stub'; |
| 16 | +import sinon from 'sinon'; |
| 17 | + |
| 18 | +const ENT_ONLY = ['saml']; |
| 19 | + |
| 20 | +// See AUTH_METHOD_TEST_CASES for how request data maps to method types |
| 21 | +// authRequest is the request made on submit and what returns mfa_validation requirements (if any) |
| 22 | +// additionalRequest are any third party requests the auth method expects |
| 23 | +const REQUEST_DATA = { |
| 24 | + username: { |
| 25 | + loginData: { username: 'matilda', password: 'password' }, |
| 26 | + stubRequests: (server, path) => |
| 27 | + server.post(`/auth/${path}/login/matilda`, () => setupTotpMfaResponse(path)), |
| 28 | + }, |
| 29 | + github: { |
| 30 | + loginData: { token: 'mysupersecuretoken' }, |
| 31 | + stubRequests: (server, path) => server.post(`/auth/${path}/login`, () => setupTotpMfaResponse(path)), |
| 32 | + }, |
| 33 | + oidc: { |
| 34 | + loginData: { role: 'some-dev' }, |
| 35 | + hasPopupWindow: true, |
| 36 | + stubRequests: (server, path) => { |
| 37 | + server.get(`/auth/${path}/oidc/callback`, () => setupTotpMfaResponse(path)); |
| 38 | + server.post(`/auth/${path}/oidc/auth_url`, () => ({ |
| 39 | + data: { auth_url: 'http://dev-foo-bar.com' }, |
| 40 | + })); |
| 41 | + }, |
| 42 | + }, |
| 43 | + saml: { |
| 44 | + loginData: { role: 'some-dev' }, |
| 45 | + hasPopupWindow: true, |
| 46 | + stubRequests: (server, path) => { |
| 47 | + server.put(`/auth/${path}/token`, () => setupTotpMfaResponse(path)); |
| 48 | + server.put(`/auth/${path}/sso_service_url`, () => ({ |
| 49 | + data: { sso_service_url: 'http://sso-url.hashicorp.com/service', token_poll_id: '1234' }, |
| 50 | + })); |
| 51 | + }, |
| 52 | + }, |
| 53 | +}; |
| 54 | + |
| 55 | +// maps auth type to request data (line breaks to help separate and clarify which methods share request paths) |
| 56 | +const AUTH_METHOD_TEST_CASES = [ |
| 57 | + { authType: 'github', options: REQUEST_DATA.github }, |
| 58 | + |
| 59 | + { authType: 'userpass', options: REQUEST_DATA.username }, |
| 60 | + { authType: 'ldap', options: REQUEST_DATA.username }, |
| 61 | + { authType: 'okta', options: REQUEST_DATA.username }, |
| 62 | + { authType: 'radius', options: REQUEST_DATA.username }, |
| 63 | + |
| 64 | + { authType: 'oidc', options: REQUEST_DATA.oidc }, |
| 65 | + { authType: 'jwt', options: REQUEST_DATA.oidc }, |
| 66 | + |
| 67 | + // ENTERPRISE ONLY |
| 68 | + { authType: 'saml', options: REQUEST_DATA.saml }, |
| 69 | +]; |
| 70 | + |
| 71 | +for (const method of AUTH_METHOD_TEST_CASES) { |
| 72 | + const { authType, options } = method; |
| 73 | + const isEntMethod = ENT_ONLY.includes(authType); |
| 74 | + // adding "enterprise" to the module title filters it out of the test runner for the CE repo |
| 75 | + module(`Acceptance | auth | mfa ${authType}${isEntMethod ? ' enterprise' : ''}`, function (hooks) { |
| 76 | + setupApplicationTest(hooks); |
| 77 | + setupMirage(hooks); |
| 78 | + |
| 79 | + hooks.beforeEach(async function () { |
| 80 | + if (options?.hasPopupWindow) { |
| 81 | + this.windowStub = sinon.stub(window, 'open').callsFake(() => new WindowStub()); |
| 82 | + } |
| 83 | + await visit('/vault/auth'); |
| 84 | + }); |
| 85 | + |
| 86 | + hooks.afterEach(function () { |
| 87 | + if (options?.hasPopupWindow) { |
| 88 | + this.windowStub.restore(); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + test(`${authType}: it displays mfa requirement for default paths`, async function (assert) { |
| 93 | + this.mountPath = authType; |
| 94 | + options.stubRequests(this.server, this.mountPath); |
| 95 | + |
| 96 | + const loginKeys = Object.keys(options.loginData); |
| 97 | + assert.expect(3 + loginKeys.length); |
| 98 | + |
| 99 | + // Fill in login form |
| 100 | + await fillIn(AUTH_FORM.method, authType); |
| 101 | + await fillInLoginFields(options.loginData); |
| 102 | + |
| 103 | + if (options?.hasPopupWindow) { |
| 104 | + // fires "message" event which methods that rely on popup windows wait for |
| 105 | + setTimeout(() => { |
| 106 | + // set path which is used to set :mount param in the callback url => /auth/:mount/oidc/callback |
| 107 | + window.postMessage(callbackData({ path: this.mountPath }), window.origin); |
| 108 | + }, 50); |
| 109 | + } |
| 110 | + |
| 111 | + await click(AUTH_FORM.login); |
| 112 | + assert |
| 113 | + .dom(MFA_SELECTORS.mfaForm) |
| 114 | + .hasText( |
| 115 | + 'Multi-factor authentication is enabled for your account. Enter your authentication code to log in. TOTP passcode Verify' |
| 116 | + ); |
| 117 | + await click(GENERAL.backButton); |
| 118 | + assert.dom(AUTH_FORM.form).exists('clicking back returns to auth form'); |
| 119 | + assert.dom(GENERAL.selectByAttr('auth-method')).hasValue(authType, 'preserves method type on back'); |
| 120 | + for (const field of loginKeys) { |
| 121 | + assert.dom(AUTH_FORM.input(field)).hasValue('', `${field} input clears on back`); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + test(`${authType}: it displays mfa requirement for custom paths`, async function (assert) { |
| 126 | + this.mountPath = `${authType}-custom`; |
| 127 | + options.stubRequests(this.server, this.mountPath); |
| 128 | + const loginKeys = Object.keys(options.loginData); |
| 129 | + assert.expect(3 + loginKeys.length); |
| 130 | + |
| 131 | + // Fill in login form |
| 132 | + await fillIn(AUTH_FORM.method, authType); |
| 133 | + // Toggle more options to input a custom mount path |
| 134 | + await fillInLoginFields( |
| 135 | + { ...options.loginData, 'auth-form-mount-path': this.mountPath }, |
| 136 | + { toggleOptions: true } |
| 137 | + ); |
| 138 | + |
| 139 | + if (options?.hasPopupWindow) { |
| 140 | + // fires "message" event which methods that rely on popup windows wait for |
| 141 | + setTimeout(() => { |
| 142 | + // set path which is used to set :mount param in the callback url => /auth/:mount/oidc/callback |
| 143 | + window.postMessage(callbackData({ path: this.mountPath }), window.origin); |
| 144 | + }, 50); |
| 145 | + } |
| 146 | + |
| 147 | + await click(AUTH_FORM.login); |
| 148 | + assert |
| 149 | + .dom(MFA_SELECTORS.mfaForm) |
| 150 | + .hasText( |
| 151 | + 'Multi-factor authentication is enabled for your account. Enter your authentication code to log in. TOTP passcode Verify' |
| 152 | + ); |
| 153 | + await click(GENERAL.backButton); |
| 154 | + assert.dom(AUTH_FORM.form).exists('clicking back returns to auth form'); |
| 155 | + assert.dom(GENERAL.selectByAttr('auth-method')).hasValue(authType, 'preserves method type on back'); |
| 156 | + for (const field of loginKeys) { |
| 157 | + assert.dom(AUTH_FORM.input(field)).hasValue('', `${field} input clears on back`); |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + test(`${authType}: it submits mfa requirement for default paths`, async function (assert) { |
| 162 | + assert.expect(2); |
| 163 | + this.mountPath = authType; |
| 164 | + options.stubRequests(this.server, this.mountPath); |
| 165 | + |
| 166 | + const expectedOtp = '12345'; |
| 167 | + server.post('/sys/mfa/validate', async (_, req) => { |
| 168 | + const [actualOtp] = JSON.parse(req.requestBody).mfa_payload[constraintId]; |
| 169 | + assert.true(true, 'it makes request to mfa validate endpoint'); |
| 170 | + assert.strictEqual(actualOtp, expectedOtp, 'payload contains otp'); |
| 171 | + }); |
| 172 | + |
| 173 | + // Fill in login form |
| 174 | + await fillIn(AUTH_FORM.method, authType); |
| 175 | + await fillInLoginFields(options.loginData); |
| 176 | + |
| 177 | + if (options?.hasPopupWindow) { |
| 178 | + // fires "message" event which methods that rely on popup windows wait for |
| 179 | + setTimeout(() => { |
| 180 | + // set path which is used to set :mount param in the callback url => /auth/:mount/oidc/callback |
| 181 | + window.postMessage(callbackData({ path: this.mountPath }), window.origin); |
| 182 | + }, 50); |
| 183 | + } |
| 184 | + |
| 185 | + await click(AUTH_FORM.login); |
| 186 | + await fillIn(MFA_SELECTORS.passcode(0), expectedOtp); |
| 187 | + await click(MFA_SELECTORS.validate); |
| 188 | + }); |
| 189 | + |
| 190 | + test(`${authType}: it submits mfa requirement for custom paths`, async function (assert) { |
| 191 | + assert.expect(2); |
| 192 | + |
| 193 | + this.mountPath = `${authType}-custom`; |
| 194 | + options.stubRequests(this.server, this.mountPath); |
| 195 | + |
| 196 | + const expectedOtp = '12345'; |
| 197 | + server.post('/sys/mfa/validate', async (_, req) => { |
| 198 | + const [actualOtp] = JSON.parse(req.requestBody).mfa_payload[constraintId]; |
| 199 | + assert.true(true, 'it makes request to mfa validate endpoint'); |
| 200 | + assert.strictEqual(actualOtp, expectedOtp, 'payload contains otp'); |
| 201 | + }); |
| 202 | + |
| 203 | + // Fill in login form |
| 204 | + await fillIn(AUTH_FORM.method, authType); |
| 205 | + // Toggle more options to input a custom mount path |
| 206 | + await fillInLoginFields( |
| 207 | + { ...options.loginData, 'auth-form-mount-path': this.mountPath }, |
| 208 | + { toggleOptions: true } |
| 209 | + ); |
| 210 | + |
| 211 | + if (options?.hasPopupWindow) { |
| 212 | + // fires "message" event which methods that rely on popup windows wait for |
| 213 | + setTimeout(() => { |
| 214 | + // set path which is used to set :mount param in the callback url => /auth/:mount/oidc/callback |
| 215 | + window.postMessage(callbackData({ path: this.mountPath }), window.origin); |
| 216 | + }, 50); |
| 217 | + } |
| 218 | + |
| 219 | + await click(AUTH_FORM.login); |
| 220 | + await fillIn(MFA_SELECTORS.passcode(0), expectedOtp); |
| 221 | + await click(MFA_SELECTORS.validate); |
| 222 | + }); |
| 223 | + }); |
| 224 | +} |
0 commit comments