Back

ruby - 使用 google authenticator (f2a) otp

发布时间: 2018-09-10 01:01:00

注意:  OTP:  One Time Password, 仅仅使用一次的密码.  (例如: 仅仅在登陆的时候使用)

这个不太好搜索。 

直接搜索 google authenticator, 绝大部分都是app的使用方法

直到我搜索到了 wiki pedia. 

给出了三个链接: 

1. https://github.com/heapsource/active_model_otp

特别简单。 上手特别快,还有gem , 还有二维码, 还有教程。   400 star

2. https://github.com/ukazap/twofu   

更加简单。 连README都简单的不得了。  10 star

3. https://github.com/mdp/rotp   800+ star   用这个. 

也兼容 google authenticator. 

考虑之后, 使用了 第三个 . 

用法还是很简单的. 

1. 需要生成一个 临时的密码.  使用 gem 'passgen' 就可以了. 

2. 需要生成一个链接, 例如:  

otpauth://totp/some_information?secret=your_generated_password&issuer=yourwebsite.com

记得上面的 password, 不能是普通的字符串, 需要Base32 的字符串.  可以根据ROTP::Base32.random_base32 来生成.

上面的链接,也可以根据方法来生成:

require 'rubygems'
require 'rotp'
totp = ROTP::TOTP.new('base32之后的密码字符串', issuer: 'yoursite.io')  
 # 这行代码就是生成 otpauth://totp/some_information?secret=your_generated_password&issuer=yourwebsite.com
puts totp.provisioning_uri('username @qq.com')  
# 生成OTP的字符串 , 跟手机App上的一致.
puts "== Current OTP: #{totp.now}"  

这个扫描完之后, 会在 google authenticator中显示:  yourwebsite.com(some_information) 这样的字样.

3. 直接用google 扫描一下就好了.

4. 要是想修改的话,让web端重新生成一下secret, information,  ,然后扫描即可.

Back