wmlele / devise-otp

Two Factors authentication for Devise using Time Based OTP/rfc6238 tokens.
MIT License
206 stars 39 forks source link

Devise::OTP

Devise OTP is a Two-Factor Authentication extension for Devise. The second factor is done using an RFC 6238 Time-Based One-Time Password (TOTP) implemented by the rotp library.

It has the following features:

Some of the compatible token devices are:

Device OTP was recently updated to work with Rails 7 and Turbo.

Sponsor

Devise::OTP development is sponsored by Business Class Rails SaaS starter kit. If you don't want to setup OTP yourself for your new project, consider starting one on Business Class.

Two-Factor Authentication using OTP

Although there's an adjustable drift window, it is important that both the server and the token device (phone) have their clocks set (eg: using NTP).

Installation

If you haven't, set up Devise first.

To add Devise OTP, add this line to your application's Gemfile:

gem "devise-otp"

And then execute:

$ bundle

Or install it yourself as:

$ gem install devise-otp

Run the following generator to add the necessary configuration options to Devise's config file:

rails g devise_otp:install

After you've created your Devise user models (which is usually done with a rails g devise MODEL), set up your Devise OTP additions:

rails g devise_otp MODEL

Don't forget to migrate:

rake db:migrate

Add the gem's JavaScript to you application.js:

//= require devise-otp

Custom views

If you want to customise your views, you can use the following generator to eject the default view files:

rails g devise_otp:views

By default, the files live within the Devise namespace (app/views/devise, but if you want to move them or want to match the Devise configuration, set config.otp_controller_path in your initializers.

I18n

The install generator also installs an english copy of a Devise OTP i18n file. This can be modified (or used to create other language versions) and is located at: config/locales/devise.otp.en.yml

QR codes

By default, Devise OTP assumes that you use Sprockets to render assets and so will use the (qrcode.js) embeded library to render the QR code.

If you need something more, have a look at QR codes documentation file.

Configuration

The install generator adds some options to the end of your Devise config file (config/initializers/devise.rb):

Mandatory OTP

Enforcing mandatory OTP requires adding the ensure_mandatory_{scope}_otp! method to the desired controller(s) to ensure that the user is redirected to the Enable Two-Factor Authentication form before proceeding to other parts of the application. This functions the same way as the authenticate_{scope}! methods, and can be included inline with them in the controllers, e.g.:

before_action :authenticate_user!
before_action :ensure_mandatory_user_otp!

Authors

The project was originally started by Lele Forzani by forking devise_google_authenticator and still contains some devise_google_authenticator code. It's now maintained by Josef Strzibny.

Contributions are welcome!

License

MIT Licensed