Skip to content

Add generators to make it easier to install and fix deprecation warnings #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 17, 2012
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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 changes: 8 additions & 0 deletions lib/generators/active_record/templates/migration.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 17 deletions lib/two_factor_authentication/controllers/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,26 @@ module Helpers
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 changes: 11 additions & 13 deletions lib/two_factor_authentication/models/two_factor_authenticatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ module ClassMethods
::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
Expand Down
2 changes: 0 additions & 2 deletions lib/two_factor_authentication/orm/active_record.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
module TwoFactorAuthentication
module Orm

module ActiveRecord
module Schema
include TwoFactorAuthentication::Schema

end
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/two_factor_authentication/schema.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down