|
| 1 | +[[observability-synthetics-mfa]] |
| 2 | += Multi-factor Authentication (MFA) for browser monitors |
| 3 | + |
| 4 | +++++ |
| 5 | +<titleabbrev>Multifactor Authentication for browser monitors</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 | +[[observability-synthetics-mfa-configuring-totp-for-mfa]] |
| 19 | +== Configuring TOTP for MFA |
| 20 | + |
| 21 | +To test a browser journey that uses TOTP for MFA, first configure the |
| 22 | +Synthetics authenticator token in the target application. To do this, generate a One-Time |
| 23 | +Password (OTP) using the Synthetics CLI; refer to <<observability-synthetics-command-reference,`@elastic/synthetics totp <secret>`>>. |
| 24 | + |
| 25 | +[source,sh] |
| 26 | +---- |
| 27 | +npx @elastic/synthetics totp <secret> |
| 28 | +
|
| 29 | +// prints |
| 30 | +OTP Token: 123456 |
| 31 | +---- |
| 32 | + |
| 33 | +[discrete] |
| 34 | +[[observability-synthetics-mfa-applying-the-totp-token-in-browser-journeys]] |
| 35 | +== Applying the TOTP Token in Browser Journeys |
| 36 | + |
| 37 | +Once the Synthetics TOTP Authentication is configured in your application, you can now use the OTP token in the synthetics browser |
| 38 | +journeys using the `mfa` object imported from `@elastic/synthetics`. |
| 39 | + |
| 40 | +[source,ts] |
| 41 | +---- |
| 42 | +import { journey, step, mfa } from "@elastic/synthetics"; |
| 43 | +
|
| 44 | +journey("MFA Test", ({ page, params }) => { |
| 45 | + step("Login using TOTP token", async () => { |
| 46 | + // login using username and pass and go to 2FA in next page |
| 47 | + const token = mfa.totp(params.MFA_SECRET); |
| 48 | + await page.getByPlaceholder("token-input").fill(token); |
| 49 | + }); |
| 50 | +}); |
| 51 | +---- |
| 52 | + |
| 53 | +For monitors created in the Synthetics UI using the Script editor, the `mfa` object can be accessed as shown below: |
| 54 | + |
| 55 | +[source,ts] |
| 56 | +---- |
| 57 | +step("Login using 2FA", async () => { |
| 58 | + const token = mfa.totp(params.MFA_SECRET); |
| 59 | + await page.getByPlaceholder("token-input").fill(token); |
| 60 | +}); |
| 61 | +---- |
| 62 | + |
| 63 | +[NOTE] |
| 64 | +==== |
| 65 | +`params.MFA_SECRET` would be the encoded secret that was used for registering the Synthetics Authentication in your web application. |
| 66 | +==== |
0 commit comments