Two factor authentication extension for Devise
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.1 KiB

13 years ago
## Two factor authentication for Devise
## Features
* control sms code pattern
* configure max login attempts
* per user level control if he really need two factor authentication
* your own sms logic
## Configuration
13 years ago
### Initial Setup
In a Rails environment, require the gem in your Gemfile:
13 years ago
gem 'two_factor_authentication'
13 years ago
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:
13 years ago
```ruby
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :two_factor_authenticatable
```
Two default parameters
```ruby
11 years ago
config.devise.login_code_random_pattern = /\w+/
config.devise.max_login_attempts = 3
13 years ago
```
Possible random patterns
13 years ago
13 years ago
```ruby
/\d{5}/
/\w{4,8}/
```
see more https://github.com/benburkert/randexp
13 years ago
### Customisation
By default second factor authentication enabled for each user, you can change it with this method in your User model:
13 years ago
13 years ago
```ruby
def need_two_factor_authentication?(request)
request.ip != '127.0.0.1'
end
```
13 years ago
13 years ago
this will disable two factor authentication for local users
Your send sms logic should be in this method in your User model:
13 years ago
13 years ago
```ruby
def send_two_factor_authentication_code(code)
puts code
end
```
13 years ago
13 years ago
This example just puts the code in the logs.
### External dependencies
Randexp requires words files (Check if it is installed in /usr/share/dict/words or /usr/dict/words),
you might need install it:
```bash
apt-get install wbritish # or whichever you require
```