> For the complete documentation index, see [llms.txt](https://help.autify.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.autify.com/nocode-web/en/autify-university/advanced/authentication/how-to-test-two-factor-authent.md). Agents can also search and read this documentation through its [MCP server](https://help.autify.com/~gitbook/mcp).

# Testing Two-Factor Authentication (2FA) via Authenticator Apps

In web application testing, ensuring that authentication works correctly is critical. Today, many applications require setting up multi-factor authentication (MFA) in addition to password authentication.

Authenticator apps are a convenient way to generate a TOTP (Time-Based One-Time Password) securely. This article explains how to use Autify to verify that app-based two-factor authentication (2FA) is working correctly💫

## Prerequisites

The following prerequisites must be met to verify that app-based 2FA is working using Autify:

* The web application being tested must support authentication via TOTP authenticator apps.
* An API that acts as an equivalent to the TOTP authenticator app must be available.
  * For more information, please reference the `Prepare the process for two-factor` authentication section.

## What will the Scenario look like?

The following is an example of a Scenario where the user goes through the 2FA process on GitHub.

* **Step 5**: Perform the first step of authentication with a password.
* **Step 6-9**: Execute a JS step in a new window(\*), obtain and return a TOTP in Step 7.
* **Step 10**: Enter the TOTP returned in Step 7 into the 2FA form.
* **Step 11-12**: Proceed with the authentication process and confirm that it is successful

\* Executing in the same window may not work depending on the website being tested.

### Scenario Details Page

![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-2d3f55b6a52b119d36ecd262619dc9333e7cae62%2Fzndsk-Screenshot_2022-03-11_16.04.41.png?alt=media)

### Test Result Page

![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-953729e8817691124deccaf50a988314926ca3c7%2FScreen%20Shot%202023-01-06%20at%2016.39.39.png?alt=media)

In step 10, you can see that the TOTP obtained in step 7 has been entered.

![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-ba70f105c7d958d5001da5e04dbd8c8333db58c9%2FScreen%20Shot%202023-01-06%20at%2016.40.29.png?alt=media)

## How to create the Scenario

Here is how to create the Scenario shown above.

The process can be divided into two stages:

* Preparing the process for two-factor authentication
* Preparing the Scenario

### Preparing the process for two-factor authentication

1. Prepare an API for generating the TOTP. We recommend that you prepare your own environment, but feel free to use the following endpoints hosted by Autify for testing😊

* Sample API code
  * [**https://github.com/autifyhq/autify-jssnipet-util-apis**](https://github.com/autifyhq/autify-jssnipet-util-apis)
* Sample API endpoints (please refer to the repository above on how to use it)&#x20;

```
https://autify-jsstep-util-apis.herokuapp.com/totp
```

2. Prepare the secret key for generating a TOTP. Typically, this is displayed in the application being tested. Secret keys are often displayed as a QR code that users can scan, but for testing, please obtain a character string.

Taking GitHub as an example, the secret key is displayed when you click the section shown below:

![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-539e1f6b70a66d1d2105fbe20da6b8b8fac0c048%2Fzndsk-Screen_Shot_2021-07-02_at_16.42.05\(1\).png?alt=media)

### Preparing the Scenario

1. Create the Scenario by recording operations on the web application as usual until just before performing 2FA.
2. Use the secret key from step 2 (Prepare the process for two-factor authentication) and request the API to generate a TOTP using a [JS Step](https://help.autify.com/nocode-web/en/autify-university/advanced/js-steps/javascript-steps.md) and return the value (TOTP).

The following is a sample code. "YOUR SECRET CODE" needs to be replaced with your actual secret key.

```javascript
var secret = "YOUR SECRET CODE"
var url = "https://autify-jsstep-util-apis.herokuapp.com/totp";
var xhr = new XMLHttpRequest();

var otp = null;

xhr.onerror = function() {
　　throw new Error("Network Error");
};
xhr.onload = function() {
　　otp = xhr.response;
};

xhr.open("POST", url, false);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("key=" + secret);

return otp;
```

3. Set the TOTP returned in the JS step in step 2 as the input value for 2FA.

   ![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-554f6958150820983489cc06dded8462ecfdc33c%2FScreen%20Shot%202022-03-23%20at%2010.48.24\(1\).png?alt=media)

Now, a dynamically generated TOTP will be entered at each test execution🎉

## Notes

* For 2FA specifications on apps/services being tested, please refer to the documentation provided by the app/service.
* You can apply the above method in a Scenario that verifies a sign-up process where a different email address and a different secret key are used each time. In this case, you would use a random email address (available in the [Email Test](https://help.autify.com/nocode-web/en/autify-university/advanced/email-testing-feature/email-test.md) feature).

Should you have any questions, please get in touch with our support team🙌

## Further reading

* [Testing various authentication methods](https://help.autify.com/nocode-web/en/autify-university/advanced/authentication/testing-various-authentication.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.autify.com/nocode-web/en/autify-university/advanced/authentication/how-to-test-two-factor-authent.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
