Skip to content

Commit

Permalink
Adds ga_appname config setting
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecsl committed Sep 6, 2014
1 parent 74e7f33 commit f51d7d1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
8 changes: 4 additions & 4 deletions lib/devise_google_authenticatable/controllers/helpers.rb
Expand Up @@ -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
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/devise_google_authenticator.rb
Expand Up @@ -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
Expand Down
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions test/models_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit f51d7d1

Please sign in to comment.