> 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/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).

# Authenticatorアプリによる二要素認証 (2FA) をテストする

ウェブアプリケーションのテストに、認証は欠かせないものです。昨今ではセキュリティ強化に伴い、シンプルなパスワード認証に加え、多要素の認証が求められるアプリケーションも増えていますよね。

この記事ではそのうちの一つ、TOTP (Time-Based One-Time Password) を発行するAuthenticator appを用いた二要素認証(2FA)をAutifyしていきましょう💫

## はじめに

今回取り上げる二要素認証をAutifyでテストするには、以下が前提となります。

* テスト対象のウェブアプリケーションが、TOTPを生成する任意のAuthenticator appによる認証に対応していること
* TOTPを生成するAuthenticator APIが用意できること
  * 詳しくは「二要素認証を行う仕組みの準備」をご参照ください！

## どんなシナリオができる？

以下は、実際のシナリオでGitHubの2FAを通過している例です。

* **ステップ5**：一段階目のパスワード認証を行う
* **ステップ6-9**：新しいウィンドウ内で（※）JSステップを実行し、ステップ7でTOTPを取得しreturnする
* **ステップ10**：ステップ7でreturnされたTOTPを、二要素認証のフォームへ入力する
* **ステップ11-12**：認証処理を進め、成功したことを確認する

※テスト対象のサイトによっては、同一ウィンドウ内で直接JSステップを実行することができないケースがあるため

### シナリオ詳細画面

![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-7623ffc3172b543b5e68f32410222632c13ce921%2Fzndsk-Screen_Shot_2021-06-21_at_17.46.41.png?alt=media)

### テスト結果詳細画面

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

ステップ10で、ステップ7で取得したTOTPが入力されているのを確認できます。

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

## 作ってみましょう！

上記でご紹介したシナリオを作成する方法を、さっそく見ていきましょう。

手順は大きく

* 二要素認証を行う仕組みの準備
* シナリオの準備

に分かれます。

### 二要素認証を行う仕組みの準備

1\. TOTPを生成するAPIを準備します。ご自身の環境で準備いただくことをおすすめいたしますが、動作検証にあたってはAutifyにてホスティングしている下記エンドポイントをご自由にご利用ください😊

* APIのサンプルコード
  * <https://github.com/autifyhq/autify-jssnipet-util-apis>
* APIのエンドポイントのサンプル（ご利用方法は上記リポジトリをご参照ください）&#x20;

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

2\. TOTPを生成するためのシークレットキーを用意します。通常は、テスト対象のアプリケーションによって表示されます。シークレットキーは撮影向けのQRコードで表示されるケースがよくありますが、文字列を取得してください。

たとえば、GitHubのサイトの場合は、以下で表示されるテキストがシークレットキーに相当します。

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

### シナリオの準備

1\. シナリオを作成します。ウェブアプリケーションに対する操作を、二要素認証を行う直前まで通常通りレコーディングします。

2\. 「二要素認証を行う仕組みの準備」の2. で用意したシークレットキーを使用し、TOTPを生成するAPIに対するリクエストを[JSステップ](https://help.autify.com/nocode-web/autify-university/advanced/js-steps/javascript-steps.md)にて行い、返ってきた値 (TOTP) をreturnします。

サンプルコードは以下の通りです。*"YOUR SECRET CODE"* の部分を、実際のシークレットキーに置き換えてご利用ください。

```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\. 2. のJSステップでreturnされたTOTPを、二要素認証の入力値として設定します。

![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-a0db333d9bed65573ea25382973f67ae5090b822%2Fzndsk-Screen_Shot_2021-06-24_at_18.21.14.png?alt=media)

これで、テスト実行のたびに動的に生成されるTOTPを入力することができるようになりました🎉

## 留意点

* テスト対象のアプリケーションやサービス固有の二要素認証の仕様につきましては、各種サービスのドキュメントをご参照ください。
* 今回のケースを応用し、毎回異なるメールアドレスでサインアップ後、毎回異なるシークレットキーを用いて二要素認証を行うケースも、[メールテスト](https://help.autify.com/nocode-web/autify-university/advanced/email-testing-feature/email-test.md)機能の「ランダムメールアドレス」を併用すれば実現可能な場合があります。是非お試しください！

ご不明点がございましたら、お気軽にサポートまでお問い合わせください🙌

## 参照記事

* [各種認証をテストする](https://help.autify.com/nocode-web/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/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.
