Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
Allows remember device when enabling 2FA for first time.
Browse files Browse the repository at this point in the history
Fixes #90.
  • Loading branch information
philnash committed Apr 28, 2020
1 parent 84e02da commit af1d193
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/controllers/devise/devise_authy_controller.rb
Expand Up @@ -109,6 +109,7 @@ def POST_verify_authy_installation
self.resource.authy_enabled = token.ok?

if token.ok? && self.resource.save
remember_device(@resource.id) if params[:remember_device].to_i == 1
record_authy_authentication
set_flash_message(:notice, :enabled)
redirect_to after_authy_verified_path_for(resource)
Expand Down
4 changes: 4 additions & 0 deletions app/views/devise/verify_authy_installation.html.erb
Expand Up @@ -4,6 +4,10 @@
<legend><%= I18n.t('submit_token_title', {:scope => 'devise'}) %></legend>
<%= label_tag :token %>
<%= text_field_tag :token, "", :autocomplete => "one-time-code", :inputmode => "numeric", :pattern => "[0-9]*", :id => 'authy-token' %>
<label>
<%= check_box_tag :remember_device %>
<span><%= I18n.t('remember_device', {:scope => 'devise'}) %></span>
</label>
<%= authy_request_sms_link %>
<%= submit_tag I18n.t('enable_my_account', {:scope => 'devise'}), :class => 'btn' %>
<% end %>
Expand Down
3 changes: 3 additions & 0 deletions app/views/devise/verify_authy_installation.html.haml
Expand Up @@ -3,6 +3,9 @@
%legend= I18n.t('submit_token_title', {:scope => 'devise'})
= label_tag :token
= text_field_tag :token, "", :autocomplete => "one-time-code", :inputmode => "numeric", :pattern => "[0-9]*", :id => 'authy-token'
%label
= check_box_tag :remember_device
%span= I18n.t('remember_device', {:scope => 'devise'})
= authy_request_sms_link
= submit_tag I18n.t('enable_my_account', {:scope => 'devise'}), :class => 'btn'

37 changes: 36 additions & 1 deletion spec/controllers/devise_authy_controller_spec.rb
Expand Up @@ -557,7 +557,7 @@
:token => token,
:force => true
).and_return(double("Authy::Response", :ok? => true))
post :POST_verify_authy_installation, :params => { :token => token }
post :POST_verify_authy_installation, :params => { :token => token, :remember_device => '0' }
end

it "should enable authy for user" do
Expand All @@ -573,6 +573,41 @@
expect(response).to redirect_to(root_path)
expect(flash[:notice]).to eq('Two factor authentication was enabled')
end

it "should not set a remember_device cookie" do
expect(cookies["remember_device"]).to be_nil
end
end

describe "successful verification with remember device" do
before(:each) do
expect(Authy::API).to receive(:verify).with(
:id => user.authy_id,
:token => token,
:force => true
).and_return(double("Authy::Response", :ok? => true))
post :POST_verify_authy_installation, :params => { :token => token, :remember_device => '1' }
end

it "should enable authy for user" do
user.reload
expect(user.authy_enabled).to be true
end
it "should set {resource}_authy_token_checked in the session" do
expect(session["user_authy_token_checked"]).to be true
end
it "should set a flash notice and redirect" do
expect(response).to redirect_to(root_path)
expect(flash[:notice]).to eq('Two factor authentication was enabled')
end

it "should set a signed remember_device cookie" do
jar = ActionDispatch::Cookies::CookieJar.build(request, cookies.to_hash)
cookie = jar.signed["remember_device"]
expect(cookie).not_to be_nil
parsed_cookie = JSON.parse(cookie)
expect(parsed_cookie["id"]).to eq(user.id)
end
end

describe "unsuccessful verification" do
Expand Down

0 comments on commit af1d193

Please sign in to comment.