Skip to content
This repository
Browse code

Tidied up the gemspec plus all the generators seem to work slick

  • Loading branch information...
commit bfb9b2c383c5772d22dd35e72635b3017c1640eb 1 parent 9569079
authored January 22, 2012
1  Rakefile
@@ -21,6 +21,7 @@ Jeweler::Tasks.new do |gem|
21 21
   gem.description = %Q{Devise Google Authenticator Extension, for adding Google's OTP to your Rails apps!}
22 22
   gem.email = "xntrik@gmail.com"
23 23
   gem.authors = ["Christian Frichot"]
  24
+  gem.files = Dir["{app,config,lib}/**/*"] + %w[LICENSE.txt README.rdoc]
24 25
   # dependencies defined in Gemfile
25 26
 end
26 27
 Jeweler::RubygemsDotOrgTasks.new
24  app/controllers/devise/checkga_controller.rb
@@ -16,26 +16,14 @@ def update
16 16
 
17 17
     if not resource.nil?
18 18
 
19  
-      if resource.gauth_tmp_datetime < 10.minutes.ago
20  
-        puts "Too slow"
21  
-        redirect_to :root
  19
+      if resource.validate_token(params[resource_name]['token'].to_i)
  20
+        set_flash_message(:notice, :signed_in) if is_navigational_format?
  21
+        sign_in(resource_name,resource)
  22
+        respond_with resource, :location => redirect_location(resource_name, resource)
22 23
       else
23  
-      
24  
-        valid_vals = []
25  
-        valid_vals << ROTP::TOTP.new(resource.get_qr).at(Time.now)
26  
-        (1..3).each do |cc|
27  
-          valid_vals << ROTP::TOTP.new(resource.get_qr).at(Time.now.ago(30*cc))
28  
-          valid_vals << ROTP::TOTP.new(resource.get_qr).at(Time.now.in(30*cc))
29  
-        end
30  
-        
31  
-        if valid_vals.include?(params[resource_name]['token'].to_i)
32  
-          set_flash_message(:notice, :signed_in) if is_navigational_format?
33  
-          sign_in(resource_name,resource)
34  
-          respond_with resource, :location => redirect_location(resource_name, resource)
35  
-        else
36  
-          redirect_to :root
37  
-        end
  24
+        redirect_to :root
38 25
       end
  26
+
39 27
     else
40 28
       redirect_to :root
41 29
     end
2  app/views/devise/checkga/show.html.erb
@@ -3,5 +3,5 @@
3 3
 <%= form_for(resource, :as => resource_name, :url => [resource_name, :checkga], :html => { :method => :put }) do |f| %>
4 4
 	<%= f.hidden_field :tmpid, {:value => @tmpid} %>
5 5
 	<%= f.text_field :token, :autocomplete => :off%>
6  
-	<p><%= f.submit I18n.t('submit_token', {:scope => 'devise'} %></p>
  6
+	<p><%= f.submit I18n.t('submit_token', {:scope => 'devise'}) %></p>
7 7
 <% end %>
75  devise_google_authenticator.gemspec
@@ -9,85 +9,14 @@ Gem::Specification.new do |s|
9 9
 
10 10
   s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11 11
   s.authors = ["Christian Frichot"]
12  
-  s.date = "2012-01-20"
  12
+  s.date = "2012-01-22"
13 13
   s.description = "Devise Google Authenticator Extension, for adding Google's OTP to your Rails apps!"
14 14
   s.email = "xntrik@gmail.com"
15 15
   s.extra_rdoc_files = [
16 16
     "LICENSE.txt",
17 17
     "README.rdoc"
18 18
   ]
19  
-  s.files = [
20  
-    ".document",
21  
-    "Gemfile",
22  
-    "Gemfile.lock",
23  
-    "LICENSE.txt",
24  
-    "README.rdoc",
25  
-    "Rakefile",
26  
-    "VERSION",
27  
-    "app/controllers/devise/checkga_controller.rb",
28  
-    "app/controllers/devise/displayqr_controller.rb",
29  
-    "app/views/devise/checkga/show.html.erb",
30  
-    "app/views/devise/displayqr/show.html.erb",
31  
-    "config/locales/en.yml",
32  
-    "devise_google_authenticator.gemspec",
33  
-    "lib/devise_google_authenticatable/controllers/helpers.rb",
34  
-    "lib/devise_google_authenticatable/hooks/google_authenticatable.rb",
35  
-    "lib/devise_google_authenticatable/models/google_authenticatable.rb",
36  
-    "lib/devise_google_authenticatable/orm/active_record.rb",
37  
-    "lib/devise_google_authenticatable/patches.rb",
38  
-    "lib/devise_google_authenticatable/patches/check_ga.rb",
39  
-    "lib/devise_google_authenticatable/patches/display_qr.rb",
40  
-    "lib/devise_google_authenticatable/rails.rb",
41  
-    "lib/devise_google_authenticatable/routes.rb",
42  
-    "lib/devise_google_authenticatable/schema.rb",
43  
-    "lib/devise_google_authenticator.rb",
44  
-    "lib/generators/devise_google_authenticator/install_generator.rb",
45  
-    "test/generators_test.rb",
46  
-    "test/integration/invitation_test.rb",
47  
-    "test/integration_tests_helper.rb",
48  
-    "test/mailers/invitation_mail_test.rb",
49  
-    "test/model_tests_helper.rb",
50  
-    "test/models/invitable_test.rb",
51  
-    "test/models_test.rb",
52  
-    "test/orm/active_record.rb",
53  
-    "test/orm/mongoid.rb",
54  
-    "test/rails_app/app/controllers/admins_controller.rb",
55  
-    "test/rails_app/app/controllers/application_controller.rb",
56  
-    "test/rails_app/app/controllers/free_invitations_controller.rb",
57  
-    "test/rails_app/app/controllers/home_controller.rb",
58  
-    "test/rails_app/app/controllers/users_controller.rb",
59  
-    "test/rails_app/app/helpers/application_helper.rb",
60  
-    "test/rails_app/app/models/admin.rb",
61  
-    "test/rails_app/app/models/octopussy.rb",
62  
-    "test/rails_app/app/models/user.rb",
63  
-    "test/rails_app/app/views/admins/new.html.erb",
64  
-    "test/rails_app/app/views/free_invitations/new.html.erb",
65  
-    "test/rails_app/app/views/home/index.html.erb",
66  
-    "test/rails_app/app/views/layouts/application.html.erb",
67  
-    "test/rails_app/app/views/users/invitations/new.html.erb",
68  
-    "test/rails_app/config.ru",
69  
-    "test/rails_app/config/application.rb",
70  
-    "test/rails_app/config/boot.rb",
71  
-    "test/rails_app/config/database.yml",
72  
-    "test/rails_app/config/environment.rb",
73  
-    "test/rails_app/config/environments/development.rb",
74  
-    "test/rails_app/config/environments/production.rb",
75  
-    "test/rails_app/config/environments/test.rb",
76  
-    "test/rails_app/config/initializers/backtrace_silencers.rb",
77  
-    "test/rails_app/config/initializers/devise.rb",
78  
-    "test/rails_app/config/initializers/inflections.rb",
79  
-    "test/rails_app/config/initializers/mime_types.rb",
80  
-    "test/rails_app/config/initializers/secret_token.rb",
81  
-    "test/rails_app/config/initializers/session_store.rb",
82  
-    "test/rails_app/config/locales/en.yml",
83  
-    "test/rails_app/config/mongoid.yml",
84  
-    "test/rails_app/config/routes.rb",
85  
-    "test/rails_app/db/migrate/20100401102949_create_tables.rb",
86  
-    "test/rails_app/script/rails",
87  
-    "test/routes_test.rb",
88  
-    "test/support/locale/en.yml",
89  
-    "test/test_helper.rb"
90  
-  ]
  19
+  s.files = Dir["{app,config,lib}/**/*"] + %w[LICENSE.txt README.rdoc]
91 20
   s.homepage = "http://github.com/AsteriskLabs/devise_google_authenticator"
92 21
   s.licenses = ["MIT"]
93 22
   s.require_paths = ["lib"]
23  lib/devise_google_authenticatable/models/google_authenticatable.rb
@@ -33,18 +33,39 @@ def assign_tmp
33 33
           self.gauth_tmp
34 34
         end
35 35
 
  36
+                def validate_token(token)
  37
+          if self.gauth_tmp_datetime < self.class.ga_timeout.ago
  38
+            return false
  39
+          else
  40
+
  41
+            valid_vals = []
  42
+            valid_vals << ROTP::TOTP.new(self.get_qr).at(Time.now)
  43
+            (1..self.class.ga_timedrift).each do |cc|
  44
+              valid_vals << ROTP::TOTP.new(self.get_qr).at(Time.now.ago(30*cc))
  45
+              valid_vals << ROTP::TOTP.new(self.get_qr).at(Time.now.in(30*cc))
  46
+            end
  47
+            
  48
+            if valid_vals.include?(token.to_i)
  49
+              return true
  50
+            else
  51
+              return false
  52
+            end
  53
+          end
  54
+        end
  55
+
36 56
         private
37 57
 
38 58
         def assign_auth_secret
39 59
           self.gauth_secret = ROTP::Base32.random_base32
40 60
         end
41  
-        
  61
+
42 62
       end
43 63
 
44 64
       module ClassMethods # :nodoc:
45 65
         def find_by_gauth_tmp(gauth_tmp)
46 66
           find(:first, :conditions => {:gauth_tmp => gauth_tmp})
47 67
         end
  68
+        ::Devise::Models.config(self, :ga_timeout, :ga_timedrift)
48 69
       end
49 70
     end
50 71
   end
12  lib/devise_google_authenticator.rb
@@ -5,14 +5,18 @@
5 5
 require 'active_support/concern'
6 6
 require 'devise'
7 7
 
  8
+module Devise # :nodoc:
  9
+	mattr_accessor :ga_timeout
  10
+	@@ga_timeout = 3.minutes
  11
+
  12
+	mattr_accessor :ga_timedrift
  13
+	@@ga_timedrift = 3
  14
+end
  15
+
8 16
 # a security extension for devise
9 17
 module DeviseGoogleAuthenticator
10 18
   autoload :Schema, 'devise_google_authenticatable/schema'
11 19
   autoload :Patches, 'devise_google_authenticatable/patches'
12  
-  
13  
-#  module Controllers # :nodoc:
14  
-#    autoload :Helpers, 'devise_google_authenticatable/controllers/helpers'
15  
-#  end
16 20
 end
17 21
 
18 22
 
13  lib/generators/active_record/devise_google_authenticator_generator.rb
... ...
@@ -0,0 +1,13 @@
  1
+require 'rails/generators/active_record'
  2
+
  3
+module ActiveRecord
  4
+  module Generators
  5
+    class DeviseGoogleAuthenticatorGenerator < ActiveRecord::Generators::Base
  6
+      source_root File.expand_path("../templates", __FILE__)
  7
+
  8
+      def copy_devise_migration
  9
+        migration_template "migration.rb", "db/migrate/devise_google_authenticator_add_to_#{table_name}"
  10
+      end
  11
+    end
  12
+  end
  13
+end
17  lib/generators/active_record/templates/migration.rb
... ...
@@ -0,0 +1,17 @@
  1
+class DeviseGoogleAuthenticatorAddTo<%= table_name.camelize %> < ActiveRecord::Migration
  2
+  def self.up
  3
+    change_table :<%= table_name %> do |t|
  4
+      t.string  :gauth_secret
  5
+      t.string  :gauth_enabled, :default => "f"
  6
+      t.string  :gauth_tmp
  7
+      t.datetime  :gauth_tmp_datetime
  8
+    end
  9
+
  10
+  end
  11
+  
  12
+  def self.down
  13
+    change_table :<%= table_name %> do |t|
  14
+      t.remove :gauth_secret, :gauth_enabled, :gauth_tmp, :gauth_tmp_datetime
  15
+    end
  16
+  end
  17
+end
19  lib/generators/devise_google_authenticator/devise_google_authenticator_generator.rb
... ...
@@ -0,0 +1,19 @@
  1
+module DeviseGoogleAuthenticator
  2
+	module Generators
  3
+		class DeviseGoogleAuthenticatorGenerator < Rails::Generators::NamedBase
  4
+
  5
+			namespace "devise_google_authenticator"
  6
+
  7
+			desc "Add :google_authenticatable directive in the given model, plus accessors. Also generate migration for ActiveRecord"
  8
+
  9
+			def inject_devise_google_authenticator_content
  10
+				path = File.join("app","models","#{file_path}.rb")
  11
+				inject_into_file(path, "google_authenticatable, :", :after => "devise :") if File.exists?(path)
  12
+				inject_into_file(path, "gauth_enabled, :gauth_tmp, :gauth_tmp_datetime, :", :after => "attr_accessible :") if File.exists?(path)
  13
+			end
  14
+
  15
+			hook_for :orm
  16
+
  17
+		end
  18
+	end
  19
+end
9  lib/generators/devise_google_authenticator/install_generator.rb
@@ -6,6 +6,15 @@ class InstallGenerator < Rails::Generators::Base
6 6
 
7 7
       desc "Install the devise google authenticator extension"
8 8
 
  9
+      def add_configs
  10
+        inject_into_file "config/initializers/devise.rb", "\n  # ==> Devise Google Authenticator Extension\n  # Configure extension for devise\n\n" +
  11
+        "  # How long should the user have to enter their token. To change the default, uncomment and change the below:\n" +
  12
+        "  # config.ga_timeout = 3.minutes\n\n" +
  13
+        "  # Change time drift settings for valid token values. To change the default, uncomment and change the below:\n" +
  14
+        "  # config.ga_timedrift = 3\n\n" +
  15
+        "\n", :before => /end[ |\n|]+\Z/
  16
+      end
  17
+
9 18
       def copy_locale
10 19
         copy_file "../../../config/locales/en.yml", "config/locales/devise.google_authenticator.en.yml"
11 20
       end
19  lib/generators/devise_google_authenticator/views_generator.rb
... ...
@@ -0,0 +1,19 @@
  1
+require 'generators/devise/views_generator'
  2
+
  3
+module DeviseGoogleAuthenticator
  4
+  module Generators
  5
+    class ViewsGenerator < Rails::Generators::Base
  6
+      desc 'Copies all Devise Google Authenticator views to your application.'
  7
+
  8
+      argument :scope, :required => false, :default => nil,
  9
+                       :desc => "The scope to copy views to"
  10
+
  11
+      include ::Devise::Generators::ViewPathTemplates
  12
+      source_root File.expand_path("../../../../app/views/devise", __FILE__)
  13
+      def copy_views
  14
+        view_directory :checkga
  15
+        view_directory :displayqr
  16
+      end
  17
+    end
  18
+  end
  19
+end

0 notes on commit bfb9b2c

Please sign in to comment.
Something went wrong with that request. Please try again.