From 725606cc8c4a972cdfa5838d39acd92f58fd65e3 Mon Sep 17 00:00:00 2001 From: Ross Dakin Date: Tue, 5 Mar 2013 21:49:04 -0800 Subject: [PATCH 1/3] Store :gauth_enabled as a boolean Signed-off-by: ToonRQ --- lib/devise_google_authenticatable/schema.rb | 2 +- lib/generators/active_record/templates/migration.rb | 2 +- test/models/google_authenticatable_test.rb | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/devise_google_authenticatable/schema.rb b/lib/devise_google_authenticatable/schema.rb index 4b9b8ba..7da6629 100644 --- a/lib/devise_google_authenticatable/schema.rb +++ b/lib/devise_google_authenticatable/schema.rb @@ -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 diff --git a/lib/generators/active_record/templates/migration.rb b/lib/generators/active_record/templates/migration.rb index e51e420..6b5281b 100644 --- a/lib/generators/active_record/templates/migration.rb +++ b/lib/generators/active_record/templates/migration.rb @@ -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 diff --git a/test/models/google_authenticatable_test.rb b/test/models/google_authenticatable_test.rb index 589814f..701f762 100644 --- a/test/models/google_authenticatable_test.rb +++ b/test/models/google_authenticatable_test.rb @@ -12,7 +12,7 @@ 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 @@ -20,13 +20,13 @@ def setup 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 @@ -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 \ No newline at end of file +end From 30ce855958650273c1769c2928c7df5d957fcb5c Mon Sep 17 00:00:00 2001 From: AmIMeYet Date: Wed, 8 May 2013 12:44:35 +0300 Subject: [PATCH 2/3] Fix needs_gauth? to check for boolean instead of 1 --- .../models/google_authenticatable.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/devise_google_authenticatable/models/google_authenticatable.rb b/lib/devise_google_authenticatable/models/google_authenticatable.rb index 8f470ad..99213ff 100644 --- a/lib/devise_google_authenticatable/models/google_authenticatable.rb +++ b/lib/devise_google_authenticatable/models/google_authenticatable.rb @@ -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 @@ -87,4 +87,4 @@ def ga_username_from_email(email) end end end -end \ No newline at end of file +end From 7b6a9fdd74a96df862f2d48a01aa05ecb1914b10 Mon Sep 17 00:00:00 2001 From: Brian Beyer Date: Wed, 15 May 2013 08:51:22 -0600 Subject: [PATCH 3/3] extracted out the registration url so it can be used by qrcode generators other than the deprecated google chart api --- .../models/google_authenticatable.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/devise_google_authenticatable/models/google_authenticatable.rb b/lib/devise_google_authenticatable/models/google_authenticatable.rb index 99213ff..6116d28 100644 --- a/lib/devise_google_authenticatable/models/google_authenticatable.rb +++ b/lib/devise_google_authenticatable/models/google_authenticatable.rb @@ -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