devise_two_factor_authentication 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/tests.yml +42 -0
  3. data/.gitignore +23 -0
  4. data/.rubocop.yml +293 -0
  5. data/CHANGELOG.md +119 -0
  6. data/Gemfile +35 -0
  7. data/LICENSE +19 -0
  8. data/README.md +401 -0
  9. data/Rakefile +16 -0
  10. data/app/controllers/devise/two_factor_authentication_controller.rb +88 -0
  11. data/app/views/devise/two_factor_authentication/max_login_attempts_reached.html.erb +3 -0
  12. data/app/views/devise/two_factor_authentication/show.html.erb +19 -0
  13. data/config/locales/de.yml +8 -0
  14. data/config/locales/en.yml +8 -0
  15. data/config/locales/es.yml +8 -0
  16. data/config/locales/fr.yml +8 -0
  17. data/config/locales/ru.yml +8 -0
  18. data/devise_two_factor_authentication.gemspec +40 -0
  19. data/lib/devise_two_factor_authentication/controllers/helpers.rb +54 -0
  20. data/lib/devise_two_factor_authentication/hooks/two_factor_authenticatable.rb +17 -0
  21. data/lib/devise_two_factor_authentication/models/two_factor_authenticatable.rb +206 -0
  22. data/lib/devise_two_factor_authentication/orm/active_record.rb +14 -0
  23. data/lib/devise_two_factor_authentication/rails.rb +7 -0
  24. data/lib/devise_two_factor_authentication/routes.rb +19 -0
  25. data/lib/devise_two_factor_authentication/schema.rb +31 -0
  26. data/lib/devise_two_factor_authentication/version.rb +3 -0
  27. data/lib/devise_two_factor_authentication.rb +52 -0
  28. data/lib/generators/active_record/templates/migration.rb +15 -0
  29. data/lib/generators/active_record/two_factor_authentication_generator.rb +14 -0
  30. data/lib/generators/two_factor_authentication/two_factor_authentication_generator.rb +17 -0
  31. data/spec/controllers/two_factor_authentication_controller_spec.rb +41 -0
  32. data/spec/features/two_factor_authenticatable_spec.rb +236 -0
  33. data/spec/generators/active_record/two_factor_authentication_generator_spec.rb +36 -0
  34. data/spec/lib/two_factor_authentication/models/two_factor_authenticatable_spec.rb +326 -0
  35. data/spec/rails_app/.gitignore +3 -0
  36. data/spec/rails_app/README.md +3 -0
  37. data/spec/rails_app/Rakefile +9 -0
  38. data/spec/rails_app/app/assets/config/manifest.js +2 -0
  39. data/spec/rails_app/app/assets/javascripts/application.js +1 -0
  40. data/spec/rails_app/app/assets/stylesheets/application.css +4 -0
  41. data/spec/rails_app/app/controllers/application_controller.rb +3 -0
  42. data/spec/rails_app/app/controllers/home_controller.rb +10 -0
  43. data/spec/rails_app/app/helpers/application_helper.rb +8 -0
  44. data/spec/rails_app/app/mailers/.gitkeep +0 -0
  45. data/spec/rails_app/app/models/.gitkeep +0 -0
  46. data/spec/rails_app/app/models/admin.rb +6 -0
  47. data/spec/rails_app/app/models/encrypted_user.rb +15 -0
  48. data/spec/rails_app/app/models/guest_user.rb +17 -0
  49. data/spec/rails_app/app/models/user.rb +14 -0
  50. data/spec/rails_app/app/views/home/dashboard.html.erb +11 -0
  51. data/spec/rails_app/app/views/home/index.html.erb +3 -0
  52. data/spec/rails_app/app/views/layouts/application.html.erb +20 -0
  53. data/spec/rails_app/config/application.rb +64 -0
  54. data/spec/rails_app/config/boot.rb +10 -0
  55. data/spec/rails_app/config/database.yml +19 -0
  56. data/spec/rails_app/config/environment.rb +5 -0
  57. data/spec/rails_app/config/environments/development.rb +28 -0
  58. data/spec/rails_app/config/environments/production.rb +68 -0
  59. data/spec/rails_app/config/environments/test.rb +41 -0
  60. data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  61. data/spec/rails_app/config/initializers/cookies_serializer.rb +3 -0
  62. data/spec/rails_app/config/initializers/devise.rb +258 -0
  63. data/spec/rails_app/config/initializers/inflections.rb +15 -0
  64. data/spec/rails_app/config/initializers/mime_types.rb +5 -0
  65. data/spec/rails_app/config/initializers/secret_token.rb +7 -0
  66. data/spec/rails_app/config/initializers/session_store.rb +8 -0
  67. data/spec/rails_app/config/initializers/wrap_parameters.rb +14 -0
  68. data/spec/rails_app/config/locales/devise.en.yml +59 -0
  69. data/spec/rails_app/config/locales/en.yml +5 -0
  70. data/spec/rails_app/config/routes.rb +65 -0
  71. data/spec/rails_app/config.ru +4 -0
  72. data/spec/rails_app/db/migrate/20140403184646_devise_create_users.rb +42 -0
  73. data/spec/rails_app/db/migrate/20140407172619_two_factor_authentication_add_to_users.rb +15 -0
  74. data/spec/rails_app/db/migrate/20140407215513_add_nickanme_to_users.rb +7 -0
  75. data/spec/rails_app/db/migrate/20151224171231_add_encrypted_columns_to_user.rb +9 -0
  76. data/spec/rails_app/db/migrate/20151224180310_populate_otp_column.rb +19 -0
  77. data/spec/rails_app/db/migrate/20151228230340_remove_otp_secret_key_from_user.rb +5 -0
  78. data/spec/rails_app/db/migrate/20160209032439_devise_create_admins.rb +42 -0
  79. data/spec/rails_app/db/schema.rb +54 -0
  80. data/spec/rails_app/lib/assets/.gitkeep +0 -0
  81. data/spec/rails_app/lib/sms_provider.rb +17 -0
  82. data/spec/rails_app/public/404.html +26 -0
  83. data/spec/rails_app/public/422.html +26 -0
  84. data/spec/rails_app/public/500.html +25 -0
  85. data/spec/rails_app/public/favicon.ico +0 -0
  86. data/spec/rails_app/script/rails +9 -0
  87. data/spec/spec_helper.rb +27 -0
  88. data/spec/support/authenticated_model_helper.rb +59 -0
  89. data/spec/support/capybara.rb +3 -0
  90. data/spec/support/controller_helper.rb +16 -0
  91. data/spec/support/features_spec_helper.rb +42 -0
  92. data/spec/support/sms_provider.rb +5 -0
  93. data/spec/support/totp_helper.rb +11 -0
  94. metadata +294 -0
@@ -0,0 +1,19 @@
1
+ class PopulateOtpColumn < ActiveRecord::Migration[4.2]
2
+ def up
3
+ User.reset_column_information
4
+
5
+ User.find_each do |user|
6
+ user.otp_secret_key = user.read_attribute('otp_secret_key')
7
+ user.save!
8
+ end
9
+ end
10
+
11
+ def down
12
+ User.reset_column_information
13
+
14
+ User.find_each do |user|
15
+ user.otp_secret_key = ROTP::Base32.random_base32
16
+ user.save!
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ class RemoveOtpSecretKeyFromUser < ActiveRecord::Migration[4.2]
2
+ def change
3
+ remove_column :users, :otp_secret_key, :string
4
+ end
5
+ end
@@ -0,0 +1,42 @@
1
+ class DeviseCreateAdmins < ActiveRecord::Migration[4.2]
2
+ def change
3
+ create_table(:admins) do |t|
4
+ ## Database authenticatable
5
+ t.string :email, null: false, default: ""
6
+ t.string :encrypted_password, null: false, default: ""
7
+
8
+ ## Recoverable
9
+ t.string :reset_password_token
10
+ t.datetime :reset_password_sent_at
11
+
12
+ ## Rememberable
13
+ t.datetime :remember_created_at
14
+
15
+ ## Trackable
16
+ t.integer :sign_in_count, default: 0, null: false
17
+ t.datetime :current_sign_in_at
18
+ t.datetime :last_sign_in_at
19
+ t.string :current_sign_in_ip
20
+ t.string :last_sign_in_ip
21
+
22
+ ## Confirmable
23
+ # t.string :confirmation_token
24
+ # t.datetime :confirmed_at
25
+ # t.datetime :confirmation_sent_at
26
+ # t.string :unconfirmed_email # Only if using reconfirmable
27
+
28
+ ## Lockable
29
+ # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
30
+ # t.string :unlock_token # Only if unlock strategy is :email or :both
31
+ # t.datetime :locked_at
32
+
33
+
34
+ t.timestamps null: false
35
+ end
36
+
37
+ add_index :admins, :email, unique: true
38
+ add_index :admins, :reset_password_token, unique: true
39
+ # add_index :admins, :confirmation_token, unique: true
40
+ # add_index :admins, :unlock_token, unique: true
41
+ end
42
+ end
@@ -0,0 +1,54 @@
1
+ # This file is auto-generated from the current state of the database. Instead
2
+ # of editing this file, please use the migrations feature of Active Record to
3
+ # incrementally modify your database, and then regenerate this schema definition.
4
+ #
5
+ # This file is the source Rails uses to define your schema when running `bin/rails
6
+ # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
7
+ # be faster and is potentially less error prone than running all of your
8
+ # migrations from scratch. Old migrations may fail to apply correctly if those
9
+ # migrations use external dependencies or application code.
10
+ #
11
+ # It's strongly recommended that you check this file into your version control system.
12
+
13
+ ActiveRecord::Schema[7.0].define(version: 2016_02_09_032439) do
14
+ create_table "admins", force: :cascade do |t|
15
+ t.string "email", default: "", null: false
16
+ t.string "encrypted_password", default: "", null: false
17
+ t.string "reset_password_token"
18
+ t.datetime "reset_password_sent_at", precision: nil
19
+ t.datetime "remember_created_at", precision: nil
20
+ t.integer "sign_in_count", default: 0, null: false
21
+ t.datetime "current_sign_in_at", precision: nil
22
+ t.datetime "last_sign_in_at", precision: nil
23
+ t.string "current_sign_in_ip"
24
+ t.string "last_sign_in_ip"
25
+ t.datetime "created_at", precision: nil, null: false
26
+ t.datetime "updated_at", precision: nil, null: false
27
+ t.index ["email"], name: "index_admins_on_email", unique: true
28
+ t.index ["reset_password_token"], name: "index_admins_on_reset_password_token", unique: true
29
+ end
30
+
31
+ create_table "users", force: :cascade do |t|
32
+ t.string "email", default: "", null: false
33
+ t.string "encrypted_password", default: "", null: false
34
+ t.string "reset_password_token"
35
+ t.datetime "reset_password_sent_at", precision: nil
36
+ t.datetime "remember_created_at", precision: nil
37
+ t.integer "sign_in_count", default: 0, null: false
38
+ t.datetime "current_sign_in_at", precision: nil
39
+ t.datetime "last_sign_in_at", precision: nil
40
+ t.string "current_sign_in_ip"
41
+ t.string "last_sign_in_ip"
42
+ t.datetime "created_at", precision: nil, null: false
43
+ t.datetime "updated_at", precision: nil, null: false
44
+ t.integer "second_factor_attempts_count", default: 0
45
+ t.string "nickname", limit: 64
46
+ t.string "encrypted_otp_secret_key"
47
+ t.string "encrypted_otp_secret_key_iv"
48
+ t.string "encrypted_otp_secret_key_salt"
49
+ t.index ["email"], name: "index_users_on_email", unique: true
50
+ t.index ["encrypted_otp_secret_key"], name: "index_users_on_encrypted_otp_secret_key", unique: true
51
+ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
52
+ end
53
+
54
+ end
File without changes
@@ -0,0 +1,17 @@
1
+ require 'ostruct'
2
+
3
+ class SmsProvider
4
+ Message = Class.new(OpenStruct)
5
+
6
+ class_attribute :messages
7
+ self.messages = []
8
+
9
+ def self.send_message(opts = {})
10
+ self.messages << Message.new(opts)
11
+ end
12
+
13
+ def self.last_message
14
+ self.messages.last
15
+ end
16
+
17
+ end
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ </div>
24
+ </body>
25
+ </html>
File without changes
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # This command will automatically be run when you run "rails" with Rails 3
5
+ # gems installed from the root of your application.
6
+
7
+ APP_PATH = File.expand_path('../config/application', __dir__)
8
+ require File.expand_path('../config/boot', __dir__)
9
+ require 'rails/commands'
@@ -0,0 +1,27 @@
1
+ ENV["RAILS_ENV"] ||= "test"
2
+ require File.expand_path("../rails_app/config/environment.rb", __FILE__)
3
+
4
+ require 'rspec/rails'
5
+ require 'timecop'
6
+ require 'rack_session_access/capybara'
7
+
8
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
+ RSpec.configure do |config|
10
+ config.run_all_when_everything_filtered = true
11
+ config.filter_run :focus
12
+
13
+ config.use_transactional_examples = true
14
+
15
+ config.include Capybara::DSL
16
+
17
+ # Run specs in random order to surface order dependencies. If you find an
18
+ # order dependency and want to debug it, you can fix the order by providing
19
+ # the seed, which is printed after each run.
20
+ # --seed 1234
21
+ config.order = 'random'
22
+
23
+ config.after(:each) { Timecop.return }
24
+ end
25
+
26
+ Dir["#{Dir.pwd}/spec/support/**/*.rb"].each {|f| require f}
27
+ Dir["#{Dir.pwd}/spec/rails_app/lib/*.rb"].each {|f| require f}
@@ -0,0 +1,59 @@
1
+ module AuthenticatedModelHelper
2
+ def build_guest_user
3
+ GuestUser.new
4
+ end
5
+
6
+ def create_user(type = 'encrypted', attributes = {})
7
+ create_table_for_nonencrypted_user if type == 'not_encrypted'
8
+
9
+ User.create!(valid_attributes(attributes))
10
+ end
11
+
12
+ def create_admin
13
+ Admin.create!(valid_attributes.except(:nickname))
14
+ end
15
+
16
+ def valid_attributes(attributes={})
17
+ {
18
+ nickname: 'Marissa',
19
+ email: generate_unique_email,
20
+ password: 'password',
21
+ password_confirmation: 'password'
22
+ }.merge(attributes)
23
+ end
24
+
25
+ def generate_unique_email
26
+ @@email_count ||= 0
27
+ @@email_count += 1
28
+ "user#{@@email_count}@example.com"
29
+ end
30
+
31
+ def create_table_for_nonencrypted_user
32
+ ActiveRecord::Migration.suppress_messages do
33
+ ActiveRecord::Schema.define(version: 1) do
34
+ create_table 'users', force: :cascade do |t|
35
+ t.string 'email', default: '', null: false
36
+ t.string 'encrypted_password', default: '', null: false
37
+ t.string 'reset_password_token'
38
+ t.datetime 'reset_password_sent_at'
39
+ t.datetime 'remember_created_at'
40
+ t.integer 'sign_in_count', default: 0, null: false
41
+ t.datetime 'current_sign_in_at'
42
+ t.datetime 'last_sign_in_at'
43
+ t.string 'current_sign_in_ip'
44
+ t.string 'last_sign_in_ip'
45
+ t.datetime 'created_at', null: false
46
+ t.datetime 'updated_at', null: false
47
+ t.integer 'second_factor_attempts_count', default: 0
48
+ t.string 'nickname', limit: 64
49
+ t.string 'otp_secret_key'
50
+ t.string 'direct_otp'
51
+ t.datetime 'direct_otp_sent_at'
52
+ t.timestamp 'totp_timestamp'
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ RSpec.configuration.send(:include, AuthenticatedModelHelper)
@@ -0,0 +1,3 @@
1
+ require 'capybara/rspec'
2
+
3
+ Capybara.app = Dummy::Application
@@ -0,0 +1,16 @@
1
+ module ControllerHelper
2
+ def sign_in(user = create_user('not_encrypted'))
3
+ allow(warden).to receive(:authenticated?).with(:user).and_return(true)
4
+ allow(controller).to receive(:current_user).and_return(user)
5
+ warden.session(:user)[DeviseTwoFactorAuthentication::NEED_AUTHENTICATION] = true
6
+ end
7
+ end
8
+
9
+ RSpec.configure do |config|
10
+ config.include Devise::Test::ControllerHelpers, type: :controller
11
+ config.include ControllerHelper, type: :controller
12
+
13
+ config.before(:example, type: :controller) do
14
+ @request.env['devise.mapping'] = Devise.mappings[:user]
15
+ end
16
+ end
@@ -0,0 +1,42 @@
1
+ require 'warden'
2
+
3
+ module FeaturesSpecHelper
4
+ def warden
5
+ request.env['warden']
6
+ end
7
+
8
+ def complete_sign_in_form_for(user)
9
+ fill_in "Email", with: user.email
10
+ fill_in "Password", with: 'password'
11
+ find('.actions input').click # 'Sign in' or 'Log in'
12
+ end
13
+
14
+ def set_cookie key, value
15
+ page.driver.browser.set_cookie [key, value].join('=')
16
+ end
17
+
18
+ def get_cookie key
19
+ Capybara.current_session.driver.request.cookies[key]
20
+ end
21
+
22
+ def set_tfa_cookie value
23
+ set_cookie DeviseTwoFactorAuthentication::REMEMBER_TFA_COOKIE_NAME, value
24
+ end
25
+
26
+ def get_tfa_cookie
27
+ get_cookie DeviseTwoFactorAuthentication::REMEMBER_TFA_COOKIE_NAME
28
+ end
29
+ end
30
+
31
+ RSpec.configure do |config|
32
+ config.include Warden::Test::Helpers, type: :feature
33
+ config.include FeaturesSpecHelper, type: :feature
34
+
35
+ config.before(:each) do
36
+ Warden.test_mode!
37
+ end
38
+
39
+ config.after(:each) do
40
+ Warden.test_reset!
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ RSpec.configure do |c|
2
+ c.before(:each) do
3
+ SmsProvider.messages.clear
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ # Helper class to simulate a user generating TOTP codes from a secret key
2
+ class TotpHelper
3
+ def initialize(secret_key, otp_length)
4
+ @secret_key = secret_key
5
+ @otp_length = otp_length
6
+ end
7
+
8
+ def totp_code(time = Time.now)
9
+ ROTP::TOTP.new(@secret_key, digits: @otp_length).at(time)
10
+ end
11
+ end