totp - 谷歌两步验证的原理 以及 rotp 的使用
访问量: 2235
参考:https://blog.seetee.me/post/2011/google-two-step-verification/
真正的ruby使用:https://github.com/mdp/rotp
貌似2011年就开始用了.
特点:
1. 相当于多了一个密码
2. 该功能在断网的时候也一样使用.
原理:
1. 手机端下载app
2. 用该app ,扫描网站提供的二维码. 这时,有两件事发生:
2.1 app 与 网站的时间做了基准.
2.2 app与网站,拥有了同样的加密key. (可以认为是从网站传送到app端)
3. 在app与网站之间,使用同样的一套算法(基于时间点的),TOTP, Time based one Time password
每30秒变换一次.
具体的用法:
1. Gemfile
gem 'rotp', '3.3.1'
2. 给用户表,增加列: otp_secret , string
3. 为每个表的otp_secret自动生成内容。
4. 为这个URL生成二维码
@otp_url = "otpauth://totp/#{current_user.email}?secret=#{current_user.otp_secret}&issuer=#{ENV['GOOGLE_OTP_ISSUER']}"
4.1 用户email.
4.2 有secret
4.3 issuer 就是签发网站。 也就是 [email protected] ( your_website.com) 中的 your_website.com
5. 验证:
ROTP::TOTP.new(self.otp_secret, issuer: ENV['GOOGLE_OTP_ISSUER']).now
就可以了。