> For the complete documentation index, see [llms.txt](https://easonwang.gitbook.io/web_advance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://easonwang.gitbook.io/web_advance/oauth/google-authenticator.md).

# Google authenticator

### 定義在 RFC-6238

{% embed url="<https://datatracker.ietf.org/doc/html/rfc6238>" %}

{% embed url="<https://www.twilio.com/docs/glossary/totp>" %}

{% embed url="<https://github.com/google/google-authenticator/wiki/Key-Uri-Format#issuer>" %}

{% embed url="<https://rootprojects.org/authenticator/>" %}

{% embed url="<https://git.coolaj86.com/coolaj86/browser-authenticator.js>" %}

第三方 Node.js 模組：

{% embed url="<https://github.com/speakeasyjs/speakeasy>" %}

{% embed url="<https://github.com/yeojz/otplib>" %}

{% embed url="<https://authenticatorapi.com/>" %}

## 程式範例

以下使用 `speakeasy` 模組，產生 secret 與 qrcode，之後 secret 要記住在後端

```javascript
const speakeasy = require("speakeasy");
const qrcode = require("qrcode");

const secret = speakeasy.generateSecret({ name: "eason@gmail.com" });

console.log(secret)

qrcode.toDataURL(secret.otpauth_url, function (err, url) {
  console.log(url);
});
```

驗證

```javascript
const speakeasy = require("speakeasy");

const verifyResult = speakeasy.totp.verify({
  secret: "<剛才的 secret>",
  encoding: "ascii",
  token: "<手機 google authenticator app 掃碼 qrcode 後產生的六個號碼>"
})

console.log('verifyResult', verifyResult)
```

{% embed url="<https://github.com/speakeasyjs/speakeasy#generateSecret>" %}

{% embed url="<https://www.youtube.com/watch?v=6mxA9Zp8600>" %}

## 演算法：

<https://github.com/bellstrand/totp-generator/blob/master/index.js>


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://easonwang.gitbook.io/web_advance/oauth/google-authenticator.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
