Browse Source

Merge pull request #2 from carvil/master

Add generators to make it easier to install and fix deprecation warnings
for_activeadmin
Dmitrii Golub 13 years ago
parent
commit
e9351da36b
  1. 34
      README.md
  2. 8
      lib/generators/active_record/templates/migration.rb
  3. 14
      lib/generators/active_record/two_factor_authentication_generator.rb
  4. 17
      lib/generators/two_factor_authentication/two_factor_authentication_generator.rb
  5. 32
      lib/two_factor_authentication/controllers/helpers.rb
  6. 24
      lib/two_factor_authentication/models/two_factor_authenticatable.rb
  7. 2
      lib/two_factor_authentication/orm/active_record.rb
  8. 5
      lib/two_factor_authentication/schema.rb

34
README.md

@ -9,7 +9,33 @@
## Configuration
To enable two factor authentication for User model, you should add two_factor_authentication to your devise line, like:
### Initial Setup
In a Rails environment, require the gem in your Gemfile:
gem 'two_factor_authentication', git: "http://github.com/Houdini/two_factor_authentication.git"
Once that's done, run:
bundle install
### Automatic installation
In order to add two factor authorisation to a model, run the command:
bundle exec rails g two_factor_authentication MODEL
Where MODEL is your model name (e.g. User or Admin). This generator will add `:two_factor_authenticatable` to your model
and create a migration in `db/migrate/`, which will add `::second_factor_pass_code` and `:second_factor_attempts_count` to your table.
Finally, run the migration with:
bundle exec rake db:migrate
### Manual installation
To manually enable two factor authentication for the User model, you should add two_factor_authentication to your devise line, like:
```ruby
devise :database_authenticatable, :registerable,
@ -32,7 +58,9 @@ Possible random patterns
see more https://github.com/benburkert/randexp
By default second factor authentication enabled for each user, you can change it with this method in your User mdoel:
### Customisation
By default second factor authentication enabled for each user, you can change it with this method in your User model:
```ruby
def need_two_factor_authentication?(request)
@ -50,4 +78,4 @@ Your send sms logic should be in this method in your User model:
end
```
This example just puts code in logs
This example just puts the code in the logs.

8
lib/generators/active_record/templates/migration.rb

@ -0,0 +1,8 @@
class TwoFactorAuthenticationAddTo<%= table_name.camelize %> < ActiveRecord::Migration
def change
change_table :<%= table_name %> do |t|
t.string :second_factor_pass_code , :limit => 32
t.integer :second_factor_attempts_count, :default => 0
end
end
end

14
lib/generators/active_record/two_factor_authentication_generator.rb

@ -0,0 +1,14 @@
require 'rails/generators/active_record'
module ActiveRecord
module Generators
class TwoFactorAuthenticationGenerator < ActiveRecord::Generators::Base
source_root File.expand_path("../templates", __FILE__)
def copy_two_factor_authentication_migration
migration_template "migration.rb", "db/migrate/two_factor_authentication_add_to_#{table_name}"
end
end
end
end

17
lib/generators/two_factor_authentication/two_factor_authentication_generator.rb

@ -0,0 +1,17 @@
module TwoFactorAuthenticatable
module Generators
class TwoFactorAuthenticationGenerator < Rails::Generators::NamedBase
namespace "two_factor_authentication"
desc "Adds :two_factor_authenticable directive in the given model. It also generates an active record migration."
def inject_two_factor_authentication_content
path = File.join("app", "models", "#{file_path}.rb")
inject_into_file(path, "two_factor_authenticatable, :", :after => "devise :") if File.exists?(path)
end
hook_for :orm
end
end
end

32
lib/two_factor_authentication/controllers/helpers.rb

@ -7,28 +7,26 @@ module TwoFactorAuthentication
before_filter :handle_two_factor_authentication
end
module InstanceMethods
private
private
def handle_two_factor_authentication
if not request.format.nil? and request.format.html? and not devise_controller?
Devise.mappings.keys.flatten.any? do |scope|
if signed_in?(scope) and warden.session(scope)[:need_two_factor_authentication]
session["#{scope}_return_tor"] = request.path if request.get?
redirect_to two_factor_authentication_path_for(scope)
return
end
end
def handle_two_factor_authentication
if not request.format.nil? and request.format.html? and not devise_controller?
Devise.mappings.keys.flatten.any? do |scope|
if signed_in?(scope) and warden.session(scope)[:need_two_factor_authentication]
session["#{scope}_return_tor"] = request.path if request.get?
redirect_to two_factor_authentication_path_for(scope)
return
end
end
end
end
def two_factor_authentication_path_for(resource_or_scope = nil)
scope = Devise::Mapping.find_scope!(resource_or_scope)
change_path = "#{scope}_two_factor_authentication_path"
send(change_path)
end
def two_factor_authentication_path_for(resource_or_scope = nil)
scope = Devise::Mapping.find_scope!(resource_or_scope)
change_path = "#{scope}_two_factor_authentication_path"
send(change_path)
end
end
end
end

24
lib/two_factor_authentication/models/two_factor_authenticatable.rb

@ -8,22 +8,20 @@ module Devise
::Devise::Models.config(self, :login_code_random_pattern, :max_login_attempts)
end
module InstanceMethods
def need_two_factor_authentication?
true
end
def need_two_factor_authentication?
true
end
def generate_two_factor_code
self.class.login_code_random_pattern.gen
end
def generate_two_factor_code
self.class.login_code_random_pattern.gen
end
def send_two_factor_authentication_code(code)
p "Code is #{code}"
end
def send_two_factor_authentication_code(code)
p "Code is #{code}"
end
def max_login_attempts?
second_factor_attempts_count >= self.class.max_login_attempts
end
def max_login_attempts?
second_factor_attempts_count >= self.class.max_login_attempts
end
end
end

2
lib/two_factor_authentication/orm/active_record.rb

@ -1,10 +1,8 @@
module TwoFactorAuthentication
module Orm
module ActiveRecord
module Schema
include TwoFactorAuthentication::Schema
end
end
end

5
lib/two_factor_authentication/schema.rb

@ -1,7 +1,10 @@
module TwoFactorAuthentication
module Schema
def two_factor_authenticatable
def second_factor_pass_code
apply_devise_schema :second_factor_pass_code, String, :limit => 32
end
def second_factor_attempts_count
apply_devise_schema :second_factor_attempts_count, Integer, :default => 0
end
end

Loading…
Cancel
Save