Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store :gauth_enabled as a boolean #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def find_by_gauth_tmp(gauth_tmp)
end

def needs_gauth?
gauth_enabled == 1 && gauth_secret?
gauth_enabled && gauth_secret?
end

def get_qr
Expand Down Expand Up @@ -69,11 +69,14 @@ def validate_token(token)
end

def google_authenticator_qrcode_url
return unless gauth_secret?
data = Rack::Utils.escape google_authenticator_registration_url
"https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=#{data}"
end

data = Rack::Utils.escape "otpauth://totp/#{ga_username_from_email(email)}@#{Devise.http_authentication_realm || Rails.application.class.parent_name}?secret=#{gauth_secret}"
def google_authenticator_registration_url
return unless gauth_secret?

"https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=#{data}"
"otpauth://totp/#{ga_username_from_email(email)}@#{Devise.http_authentication_realm || Rails.application.class.parent_name}?secret=#{gauth_secret}"
end

private
Expand All @@ -87,4 +90,4 @@ def ga_username_from_email(email)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/devise_google_authenticatable/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def gauth_secret
end

def gauth_enabled
apply_devise_schema :gauth_enabled, Integer, {:default => 0}
apply_devise_schema :gauth_enabled, Boolean, { default: false }
end

def gauth_tmp
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/active_record/templates/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class DeviseGoogleAuthenticatorAddTo<%= table_name.camelize %> < ActiveRecord::M
def self.up
change_table :<%= table_name %> do |t|
t.string :gauth_secret
t.string :gauth_enabled, :default => "f"
t.boolean :gauth_enabled, default: false
t.string :gauth_tmp
t.datetime :gauth_tmp_datetime
end
Expand Down
12 changes: 6 additions & 6 deletions test/models/google_authenticatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ def setup
end

test 'new users should have gauth_enabled disabled by default' do
assert_equal 0, User.find(1).gauth_enabled.to_i
assert_equal 0, User.find(1).gauth_enabled
end

test 'get_qr method works' do
assert_not_nil User.find(1).get_qr
end

test 'updating gauth_enabled to true' do
User.find(1).set_gauth_enabled(:gauth_enabled => 1)
assert_equal 1, User.find(1).gauth_enabled.to_i
User.find(1).set_gauth_enabled(gauth_enable: true)
assert_equal 1, User.find(1).gauth_enabled
end

test 'updating gauth_enabled back to false' do
User.find(1).set_gauth_enabled(:gauth_enabled => 0)
assert_equal 0, User.find(1).gauth_enabled.to_i
User.find(1).set_gauth_enabled(gauth_enabled: true)
assert_equal 0, User.find(1).gauth_enabled
end

test 'updating the gauth_tmp key' do
Expand Down Expand Up @@ -61,4 +61,4 @@ def setup
assert User.find(1).validate_token(ROTP::TOTP.new(User.find(1).get_qr).at(Time.now))
end

end
end