Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

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