diff --git a/lib/devise_google_authenticatable/controllers/helpers.rb b/lib/devise_google_authenticatable/controllers/helpers.rb index 12c0486..ef7db55 100644 --- a/lib/devise_google_authenticatable/controllers/helpers.rb +++ b/lib/devise_google_authenticatable/controllers/helpers.rb @@ -3,21 +3,21 @@ module Controllers # :nodoc: module Helpers # :nodoc: def google_authenticator_qrcode(user,qualifier=nil) username = username_from_email(user.email) - app = Rails.application.class.parent_name + app = user.class.ga_appname || Rails.application.class.parent_name data = "otpauth://totp/#{otpauth_user(username, app, qualifier)}?secret=#{user.gauth_secret}" data = Rack::Utils.escape(data) url = "https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=#{data}" return image_tag(url, :alt => 'Google Authenticator QRCode') end - + def otpauth_user(username, app, qualifier=nil) "#{username}@#{app}#{qualifier}" - end + end def username_from_email(email) (/^(.*)@/).match(email)[1] end - + end end end diff --git a/lib/devise_google_authenticatable/models/google_authenticatable.rb b/lib/devise_google_authenticatable/models/google_authenticatable.rb index 86fa122..f86e26b 100644 --- a/lib/devise_google_authenticatable/models/google_authenticatable.rb +++ b/lib/devise_google_authenticatable/models/google_authenticatable.rb @@ -88,7 +88,7 @@ module ClassMethods # :nodoc: def find_by_gauth_tmp(gauth_tmp) where(gauth_tmp: gauth_tmp).first end - ::Devise::Models.config(self, :ga_timeout, :ga_timedrift, :ga_remembertime) + ::Devise::Models.config(self, :ga_timeout, :ga_timedrift, :ga_remembertime, :ga_appname) end end end diff --git a/lib/devise_google_authenticator.rb b/lib/devise_google_authenticator.rb index ed5d485..99cdb79 100644 --- a/lib/devise_google_authenticator.rb +++ b/lib/devise_google_authenticator.rb @@ -14,6 +14,9 @@ module Devise # :nodoc: mattr_accessor :ga_remembertime @@ga_remembertime = 1.month + + mattr_accessor :ga_appname + @@ga_appname = Rails.application.class.parent_name end # a security extension for devise diff --git a/lib/generators/devise_google_authenticator/install_generator.rb b/lib/generators/devise_google_authenticator/install_generator.rb index 80ce9d1..7ef7710 100644 --- a/lib/generators/devise_google_authenticator/install_generator.rb +++ b/lib/generators/devise_google_authenticator/install_generator.rb @@ -15,6 +15,9 @@ def add_configs " # Change setting to how long to remember device before requiring another token. Change to nil to turn feature off.\n" + " # To change the default, uncomment and change the below:\n" + " # config.ga_remembertime = 1.month\n\n" + + " # Change setting to assign the application name used by code generator. Defaults to Rails.application.class.parent_name.\n" + + " # To change the default, uncomment and change the below:\n" + + " # config.ga_appname = 'example.com'\n\n" + "\n", :before => /end[ |\n|]+\Z/ end diff --git a/test/models_test.rb b/test/models_test.rb index 6e46b9c..60f4deb 100644 --- a/test/models_test.rb +++ b/test/models_test.rb @@ -28,6 +28,15 @@ def assert_include_modules(klass, *modules) assert_equal 3, User.ga_timedrift end + test 'should have a new value for ga_appname' do + old_ga_appname = User.ga_appname + User.ga_appname = "test.app" + + assert_equal "test.app", User.ga_appname + + User.ga_appname = old_ga_appname + end + test 'should set a new value for ga_timeout' do old_ga_timeout = User.ga_timeout User.ga_timeout = 1.minutes