|
| 1 | +[[synthetics-mfa]] |
| 2 | += Multi-factor Authentication (MFA) for browser monitors |
| 3 | + |
| 4 | +++++ |
| 5 | +<titleabbrev>Multi-factor Authentication</titleabbrev> |
| 6 | +++++ |
| 7 | + |
| 8 | +Multi-factor Authentication (MFA) adds an essential layer of security to |
| 9 | +applications login processes, protecting against unauthorized access. A very |
| 10 | +common use case in Synthetics is testing user journeys involving websites |
| 11 | +protected by MFA. |
| 12 | + |
| 13 | +Synthetics supports testing websites secured by Time-based One-Time Password |
| 14 | +(TOTP), a common MFA method that provides short-lived one-time tokens to |
| 15 | +enhance security. |
| 16 | + |
| 17 | +[discrete] |
| 18 | +== Configuring TOTP for MFA |
| 19 | + |
| 20 | +To test a browser journey that uses TOTP for MFA, first configure the |
| 21 | +Synthetics authenticator token in the target application. To do this, generate a One-Time |
| 22 | +Password (OTP) using the Synthetics CLI; refer to <<elastic-synthetics-totp-command>>. |
| 23 | + |
| 24 | +```sh |
| 25 | +npx @elastic/synthetics totp <secret> |
| 26 | + |
| 27 | +// prints |
| 28 | +OTP Token: 123456 |
| 29 | +``` |
| 30 | + |
| 31 | +[discrete] |
| 32 | +== Applying the TOTP Token in Browser Journeys |
| 33 | + |
| 34 | +Once the Synthetics TOTP Authentication is configured in your application, you |
| 35 | +can now use the OTP token in the synthetics browser journeys using the `mfa` |
| 36 | +object imported from `@elastic/synthetics`. |
| 37 | + |
| 38 | +```ts |
| 39 | +import { journey, step, mfa} from '@elastic/synthetics'; |
| 40 | + |
| 41 | +journey('MFA Test', ({ page, params }) => { |
| 42 | + step('Login using TOTP token', async () => { |
| 43 | + // login using username and pass and go to 2FA in next page |
| 44 | + const token = mfa.token(params.MFA_GH_SECRET); |
| 45 | + await page.getByPlaceholder("token-input").fill(token) |
| 46 | + }); |
| 47 | +}); |
| 48 | +``` |
| 49 | + |
| 50 | +For monitors created in the Synthetics UI using the Script editor, the `mfa` object can be accessed as shown below: |
| 51 | + |
| 52 | +```ts |
| 53 | +step('Login using 2FA', async () => { |
| 54 | + const token = mfa.token(params.MFA_GH_SECRET); |
| 55 | + await page.getByPlaceholder("token-input").fill(token) |
| 56 | +}); |
| 57 | +``` |
| 58 | + |
| 59 | +[NOTE] |
| 60 | +==== |
| 61 | +`params.MFA_GH_SECRET` would be the encoded secret that was used for registering the Synthetics Authentication in your web application. |
| 62 | +==== |
0 commit comments