【问题标题】:How do I integrate google authenticator in my app(nodeJS)?如何在我的应用程序(nodeJS)中集成谷歌身份验证器?
【发布时间】:2019-11-06 23:46:01
【问题描述】:

我看到一些网络应用程序使用 Google Authenticator(即 6 位数字代码生成器)作为二级安全措施(例如:Binance、Kraken 等)。我正在谷歌云平台上制作一个应用程序,并且需要它来使用 Authenticator。

我该怎么做?

不适用

这必须在 nodeJS 服务器上实现

【问题讨论】:

  • 关于 google 身份验证器有几个问题,但它们不是我想要的。请不要标记重复。

标签: google-authenticator


【解决方案1】:

一种可能性是使用 Rapidapi (endpoint):

  1. 您的用户下载 Google Authenticator 应用 https://apps.apple.com/us/app/google-authenticator/id388497605https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2&hl=en&gl=US

  2. 您代表您的用户生成一个“秘密”代码:

    const request = require('request');
    
    const options = {
        method: 'GET',
        url: 'https://google-authenticator.p.rapidapi.com/new/',
        headers: {
            'x-rapidapi-host': 'google-authenticator.p.rapidapi.com',
            'x-rapidapi-key': 'rapidapi-guid',
            u.seQueryString: true
        }    
    };
    
    request(...);
    

    服务器将返回密码(例如“GXPTBCTI4DX4UFJB”),请保留密码,因为您在第 3 步和第 4 步将需要它。

  3. 您通过以下方式为用户生成二维码:

    const options = {
        method: 'GET',
        url: 'https://google-authenticator.p.rapidapi.com/enroll/',
        qs: {secret: 'GXPTBCTI4DX4UFJB', account: 'JohnDoe', issuer: 'AcmeCorp'},
        headers: {
        'x-rapidapi-host': 'google-authenticator.p.rapidapi.com',
        'x-rapidapi-key': 'rapidapi-guid',
        useQueryString: true
        }
    };
    
    request(...);
    

    用户使用 Google Authenticator 应用扫描二维码,现在会生成临时代码。

  4. 现在您可以通过以下方式验证代码:

    const options = {
    method: 'GET',
    url: 'https://google-authenticator.p.rapidapi.com/validate/',
    qs: {code: '266677', secret: 'GXPTBCTI4DX4UFJB'},
    headers: {
        'x-rapidapi-host': 'google-authenticator.p.rapidapi.com',
        'x-rapidapi-key': 'rapidapi-guid',
        useQueryString: true
        }
    };
    
    request(...)
    

查看完整教程here

【讨论】:

    【解决方案2】:

    好例子google-authenticator-node-js-web-app

    > mkdir back-end
    > cd back-end
    > npm init -y
    > npm install --save express body-parser cors qrcode speakeasy
    

    现在,我们创建了一个“后端”目录,并通过安装以下依赖项将其初始化为Node.js 项目:

    express — 这是一个用于创建 API 服务的最小且灵活的 Web 框架。 body-parser — 为了解析HTTP方法的body数据,使用了这个包。

    cors — 此包用于使客户端 Web 应用程序能够与 API 服务通信并避免跨域问题。

    qrcode — 在这个应用程序中,我们将生成 QR 码作为 base64 图像数据,因此我们需要 qrcode 包。

    speakeasy — 这是使我们的应用程序能够提供 Google Authenticator 使用的密钥和 T-OTP 算法的包,对于验证所提供的 Auth 代码也很有用.

    我们现在将创建一些 API 服务,以 app.js 作为执行的主要文件。为了学习过程的简单性,应用程序的脚手架遵循关注点分离。

    【讨论】:

      猜你喜欢
      • 2013-02-11
      • 2016-11-14
      • 1970-01-01
      • 2021-08-30
      • 2021-08-08
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 2016-03-14
      相关资源
      最近更新 更多