区块链18爱莉莎学区块链投资系列Ruby

【爱莉莎RoR实验室】只要六步,轻松添加Google两步认证

2017-09-15  本文已影响47人  竹三七
公众号_爱莉莎的雪月花.jpg

明天就要在西子湖畔,参加RubyChina大会了。
猛然发现,自己还从来没在论坛里发过帖,没共享过任何知识。好心虚!
一拍大腿,前一阵不是试过 Google 两步认证吗?
赶紧写一篇使用心得!
奉上github示例代码: https://github.com/elizachen/google-authenticator-example
再开个网站演示: https://elizarorlab.herokuapp.com/
以表达爱莉莎的心意!

什么是Google两步认证?

Google两步认证,是Google公司提供的,一种增强账户安全性的方式。
它使用TOTP算法(基于时间的一次性密码算法)。
借助两步验证,可以为你的网站用户增加一重安全防护。

它包括两个部分

使用前的准备

使用步骤
一般,在关键步骤(比如需要付费),网站要求你输入授权码
第一步,打开手机应用,查看当前的授权码

公众号_爱莉莎的雪月花.jpg

第二步,回到网站,输入授权码,进行验证。

超级简单,有没有!

为啥要用Google两步认证?

第一,安全性能大大提高!
如果用户帐户只有密码这道安全防线,密码被盗后,就完了!作恶的人可以尽情狂欢,转钱、高买低卖,好恐怖!
启用两步验证后,即使有坏人破解了您的密码,对于那些要花钱的项目坏人仍需要借助您的手机,才能继续操作。你的手机也被偷了?纳尼?如果你是苹果用户,赶快通过【查找我的iPhone】挂失,让iphone立马变砖头呀!(不要问我怎么知道的,我的小六plus就是被偷的)。
第二,google不作恶。google出品,安全可靠
第三,只要两步,超级简单!
第四,不用花钱。短信验证码,条数超过配额,是要花银子的啊!
第五,更专业!多一道屏障,用户也觉得你靠谱。

如何添加Google 两步认证?

作者的技术栈是RoR, 以Rails 举例。

  1. 安装google-authenticator-rails gem
    在Gemfile里添加:
    gem 'google-authenticator-rails'

  2. 创建用户数据模型, 启用acts_as_google_authenticated
    $ rails g model user name:string email:string salt:string google_secret:string
    $ rake db:migrate
    #app / models / user.rb

class User < ActiveRecord::Base
 acts_as_google_authenticated lookup_token: :salt, drift: 30, issuer: 'eliza''s ROR Lab'
 before_save {|record| record.salt = SecureRandom.hex unless record.salt }
 after_create {|record| record.set_google_secret }
end

acts_as_google_authenticated:添加必要的验证功能。
drift: 是授权码的有效时限。比如设定为30,30秒内的验证码有效。
issuer: 显示发行人,如下图:


1.jpg

3 .创建UserMfaSession和UserMfaSessionController
UserMfaSession是一个处理MFA session的class。继承自:
GoogleAuthenticatorRails :: Session :: Base
# app / models / user_mfa_session.rb

class UserMfaSession < GoogleAuthenticatorRails::Session::Base
 # no real code needed here
end

$ rails g controller user_mfa_sessions
UserMfaSessionController 是处理UserMfaSession的Controller类。
# app/controllers / user_mfa_sessions_controller.rb

class UserMfaSessionsController < ApplicationController
 skip_before_filter :check_mfa

 def new
   @user = current_user
 end

 def create
   @user = current_user
   if @user.google_authentic?(params[:auth][:mfa_code])
     UserMfaSession.create(@user)
     redirect_to root_url
   else
     flash[:error] = "Wrong code"
     render :new
   end
 end
end
  1. 更改View代码:验证授权码是否正确。
    #app / views / user_mfa_sessions / new.html.erb
<% if flash[:error] %>
  <%= flash[:error] %>
  <br />
<% end %>
<img src="<%= @user.google_qr_uri %>">
<br />
<%= form_tag user_mfa_session_path, method: :post do %>
  <div class="actions">
    <%= text_field :auth, :mfa_code %>
    <%= submit_tag 'authenticate' %>
  </div>
<% end %>
  1. 在 ApplicationController中添加 check_mfa。
class ApplicationController < ActionController::Base
  before_filter :check_mfa

  def current_user
    @current_user = User.find_or_create_by(name: 'elizachen', email: 'elizachen34@gmail.com')
  end

  private
  def check_mfa
     if !(user_mfa_session = UserMfaSession.find) && (user_mfa_session ? user_mfa_session.record == current_user : !user_mfa_session)
      redirect_to new_user_mfa_session_url
    end
  end
end

使用checkmfa检查MFA身份验证是否完成,如果未通过身份验证,则将其重定向到MFA身份验证。

6.路由设定
#config / routes.rb

Rails.application.routes.draw do
  root 'user_mfa_session#new'
  resource :user_mfa_session, only: %i(new create)
end
Screen Shot 2017-09-15 at 13.27.35.png

快来「爱莉莎的RoR实验室」试一试

网址:https://elizarorlab.herokuapp.com/
1.在手机上下载 Google Authenticator认证程序。

2.通过手机扫描网页端的二维码,绑定你的认证账户。

公众号_爱莉莎的雪月花.jpg 公众号_爱莉莎的雪月花.jpg
  1. 打开手机应用,查看当前的授权码
  2. 回到「爱莉莎的RoR实验室」https://elizarorlab.herokuapp.com/网站,输入授权码,进行验证。
    超级简单,有没有!

是不是很容易呢?

简单、安全、不花钱!
Google两步认证,你的网站,值得拥有!!

示例代码也准备好了,在爱莉莎的github: https://github.com/elizachen/google-authenticator-example
你不试一试吗?

上一篇下一篇

猜你喜欢

热点阅读