devise-multi-factor 3.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +21 -0
  3. data/.github/workflows/gem-push.yml +42 -0
  4. data/.gitignore +23 -0
  5. data/.rubocop.yml +295 -0
  6. data/.travis.yml +28 -0
  7. data/CHANGELOG.md +119 -0
  8. data/Gemfile +32 -0
  9. data/LICENSE +19 -0
  10. data/README.md +322 -0
  11. data/Rakefile +12 -0
  12. data/app/controllers/devise/totp_controller.rb +79 -0
  13. data/app/controllers/devise/two_factor_authentication_controller.rb +84 -0
  14. data/app/views/devise/two_factor_authentication/max_login_attempts_reached.html.erb +3 -0
  15. data/app/views/devise/two_factor_authentication/new.html.erb +14 -0
  16. data/app/views/devise/two_factor_authentication/show.html.erb +19 -0
  17. data/config/locales/de.yml +8 -0
  18. data/config/locales/en.yml +8 -0
  19. data/config/locales/es.yml +8 -0
  20. data/config/locales/fr.yml +8 -0
  21. data/config/locales/ru.yml +8 -0
  22. data/devise-multi-factor.gemspec +40 -0
  23. data/lib/devise-multi-factor.rb +1 -0
  24. data/lib/devise_multi_factor.rb +56 -0
  25. data/lib/devise_multi_factor/controllers/helpers.rb +57 -0
  26. data/lib/devise_multi_factor/hooks/two_factor_authenticatable.rb +17 -0
  27. data/lib/devise_multi_factor/models/totp_enrollable.rb +7 -0
  28. data/lib/devise_multi_factor/models/two_factor_authenticatable.rb +142 -0
  29. data/lib/devise_multi_factor/orm/active_record.rb +14 -0
  30. data/lib/devise_multi_factor/rails.rb +7 -0
  31. data/lib/devise_multi_factor/routes.rb +15 -0
  32. data/lib/devise_multi_factor/schema.rb +23 -0
  33. data/lib/devise_multi_factor/version.rb +3 -0
  34. data/lib/generators/active_record/devise_multi_factor_generator.rb +13 -0
  35. data/lib/generators/active_record/templates/migration.rb +11 -0
  36. data/lib/generators/devise_multi_factor/devise_multi_factor_generator.rb +17 -0
  37. data/spec/controllers/two_factor_authentication_controller_spec.rb +41 -0
  38. data/spec/features/two_factor_authenticatable_spec.rb +237 -0
  39. data/spec/generators/active_record/devise_multi_factor_generator_spec.rb +34 -0
  40. data/spec/lib/devise_multi_factor/models/two_factor_authenticatable_spec.rb +282 -0
  41. data/spec/rails_app/.gitignore +3 -0
  42. data/spec/rails_app/README.md +3 -0
  43. data/spec/rails_app/Rakefile +7 -0
  44. data/spec/rails_app/app/assets/config/manifest.js +2 -0
  45. data/spec/rails_app/app/assets/javascripts/application.js +1 -0
  46. data/spec/rails_app/app/assets/stylesheets/application.css +4 -0
  47. data/spec/rails_app/app/controllers/application_controller.rb +3 -0
  48. data/spec/rails_app/app/controllers/home_controller.rb +10 -0
  49. data/spec/rails_app/app/helpers/application_helper.rb +8 -0
  50. data/spec/rails_app/app/mailers/.gitkeep +0 -0
  51. data/spec/rails_app/app/models/.gitkeep +0 -0
  52. data/spec/rails_app/app/models/admin.rb +6 -0
  53. data/spec/rails_app/app/models/encrypted_user.rb +7 -0
  54. data/spec/rails_app/app/models/guest_user.rb +7 -0
  55. data/spec/rails_app/app/models/test_user.rb +38 -0
  56. data/spec/rails_app/app/models/user.rb +18 -0
  57. data/spec/rails_app/app/views/home/dashboard.html.erb +11 -0
  58. data/spec/rails_app/app/views/home/index.html.erb +3 -0
  59. data/spec/rails_app/app/views/layouts/application.html.erb +20 -0
  60. data/spec/rails_app/config.ru +4 -0
  61. data/spec/rails_app/config/application.rb +61 -0
  62. data/spec/rails_app/config/boot.rb +10 -0
  63. data/spec/rails_app/config/database.yml +19 -0
  64. data/spec/rails_app/config/environment.rb +5 -0
  65. data/spec/rails_app/config/environments/development.rb +28 -0
  66. data/spec/rails_app/config/environments/production.rb +68 -0
  67. data/spec/rails_app/config/environments/test.rb +41 -0
  68. data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  69. data/spec/rails_app/config/initializers/cookies_serializer.rb +3 -0
  70. data/spec/rails_app/config/initializers/devise.rb +258 -0
  71. data/spec/rails_app/config/initializers/inflections.rb +15 -0
  72. data/spec/rails_app/config/initializers/mime_types.rb +5 -0
  73. data/spec/rails_app/config/initializers/secret_token.rb +7 -0
  74. data/spec/rails_app/config/initializers/session_store.rb +8 -0
  75. data/spec/rails_app/config/initializers/wrap_parameters.rb +14 -0
  76. data/spec/rails_app/config/locales/devise.en.yml +59 -0
  77. data/spec/rails_app/config/locales/en.yml +5 -0
  78. data/spec/rails_app/config/routes.rb +65 -0
  79. data/spec/rails_app/db/migrate/20140403184646_devise_create_users.rb +42 -0
  80. data/spec/rails_app/db/migrate/20140407172619_two_factor_authentication_add_to_users.rb +17 -0
  81. data/spec/rails_app/db/migrate/20140407215513_add_nickanme_to_users.rb +7 -0
  82. data/spec/rails_app/db/migrate/20151224171231_add_encrypted_columns_to_user.rb +7 -0
  83. data/spec/rails_app/db/migrate/20151224180310_populate_otp_column.rb +19 -0
  84. data/spec/rails_app/db/migrate/20151228230340_remove_otp_secret_key_from_user.rb +5 -0
  85. data/spec/rails_app/db/migrate/20160209032439_devise_create_admins.rb +42 -0
  86. data/spec/rails_app/db/schema.rb +55 -0
  87. data/spec/rails_app/lib/assets/.gitkeep +0 -0
  88. data/spec/rails_app/lib/sms_provider.rb +17 -0
  89. data/spec/rails_app/public/404.html +26 -0
  90. data/spec/rails_app/public/422.html +26 -0
  91. data/spec/rails_app/public/500.html +25 -0
  92. data/spec/rails_app/public/favicon.ico +0 -0
  93. data/spec/rails_app/script/rails +6 -0
  94. data/spec/spec_helper.rb +26 -0
  95. data/spec/support/authenticated_model_helper.rb +29 -0
  96. data/spec/support/capybara.rb +3 -0
  97. data/spec/support/controller_helper.rb +16 -0
  98. data/spec/support/features_spec_helper.rb +42 -0
  99. data/spec/support/sms_provider.rb +5 -0
  100. data/spec/support/totp_helper.rb +11 -0
  101. metadata +315 -0
@@ -0,0 +1,3 @@
1
+ log/
2
+ tmp/
3
+ *.sqlite3
@@ -0,0 +1,3 @@
1
+ # Dummy
2
+
3
+ You have found the dummy rails app used for integration testing of the `two_factor_authentication` gem.
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,2 @@
1
+ //= link_directory ../javascripts .js
2
+ //= link_directory ../stylesheets .css
@@ -0,0 +1 @@
1
+ //= require_tree .
@@ -0,0 +1,4 @@
1
+ /*
2
+ *= require_self
3
+ *= require_tree .
4
+ */
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,10 @@
1
+ class HomeController < ApplicationController
2
+ before_action :authenticate_user!, only: :dashboard
3
+
4
+ def index
5
+ end
6
+
7
+ def dashboard
8
+ end
9
+
10
+ end
@@ -0,0 +1,8 @@
1
+ module ApplicationHelper
2
+
3
+ def render_flash
4
+ flash.map do |name, message|
5
+ content_tag(:p, message, class: "flash #{name}")
6
+ end.join.html_safe
7
+ end
8
+ end
File without changes
File without changes
@@ -0,0 +1,6 @@
1
+ class Admin < ActiveRecord::Base
2
+ # Include default devise modules. Others available are:
3
+ # :confirmable, :lockable, :timeoutable and :omniauthable
4
+ devise :database_authenticatable, :registerable,
5
+ :recoverable, :rememberable, :trackable, :validatable
6
+ end
@@ -0,0 +1,7 @@
1
+ class EncryptedUser < TestUser
2
+ def self.collection_name
3
+ 'encrypted_users'
4
+ end
5
+
6
+ has_one_time_password
7
+ end
@@ -0,0 +1,7 @@
1
+ class GuestUser < TestUser
2
+ def self.collection_name
3
+ 'guest_users'
4
+ end
5
+
6
+ has_one_time_password
7
+ end
@@ -0,0 +1,38 @@
1
+ class TestUser
2
+ extend ActiveModel::Callbacks
3
+ include ActiveModel::Validations
4
+ include Devise::Models::TwoFactorAuthenticatable
5
+ extend Lockbox::Model
6
+
7
+ define_model_callbacks :create
8
+ attr_accessor :encrypted_otp_secret_key,
9
+ :email,
10
+ :second_factor_attempts_count,
11
+ :totp_timestamp,
12
+ :direct_otp,
13
+ :direct_otp_sent_at,
14
+ :otp_secret_key
15
+
16
+ def self.fields
17
+ {}
18
+ end
19
+
20
+ def self.attribute_names
21
+ []
22
+ end
23
+
24
+ def serializable_hash(_options = nil)
25
+ {}
26
+ end
27
+
28
+ def encrypted_otp_secret_key_changed?
29
+ false
30
+ end
31
+
32
+ def update_columns(values)
33
+ values.each do |key, value|
34
+ send("#{key}=", value)
35
+ end
36
+ true
37
+ end
38
+ end
@@ -0,0 +1,18 @@
1
+ class User < ActiveRecord::Base
2
+ devise :two_factor_authenticatable, :database_authenticatable, :registerable,
3
+ :recoverable, :rememberable, :trackable, :validatable
4
+
5
+ has_one_time_password
6
+
7
+ def need_two_factor_authentication?(_request)
8
+ true
9
+ end
10
+
11
+ def send_two_factor_authentication_code(code)
12
+ SMSProvider.send_message(to: phone_number, body: code)
13
+ end
14
+
15
+ def phone_number
16
+ '14159341234'
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ <h1>Your Personal Dashboard</h1>
2
+
3
+ <p>Hi <%= current_user.nickname %></p>
4
+
5
+ <p>Your registered email address is <%= current_user.email %></p>
6
+
7
+ <p> Param A is <%= params[:A] %></p>
8
+
9
+ <p> Param B is <%= params[:B] %></p>
10
+
11
+ <p>You can only see this page after successfully completing two factor authentication</p>
@@ -0,0 +1,3 @@
1
+ <h1>Welcome Home</h1>
2
+
3
+ <p>Find me in app/views/home/index.html.erb</p>
@@ -0,0 +1,20 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application", :media => "all" %>
6
+ <%= javascript_include_tag "application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+ <nav>
11
+ <% if user_signed_in? %>
12
+ You are signed in as <%= current_user.nickname %>
13
+ <% else %>
14
+ You are signed out
15
+ <% end %>
16
+ </nav>
17
+ <%= render_flash %>
18
+ <%= yield %>
19
+ </body>
20
+ </html>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,61 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require "active_record/railtie"
4
+ require "action_controller/railtie"
5
+ require "action_mailer/railtie"
6
+ require "sprockets/railtie"
7
+
8
+ Bundler.require(*Rails.groups)
9
+ require "devise_multi_factor"
10
+ module Dummy
11
+ class Application < Rails::Application
12
+ # Settings in config/environments/* take precedence over those specified here.
13
+ # Application configuration should go into files in config/initializers
14
+ # -- all .rb files in that directory are automatically loaded.
15
+
16
+ # Custom directories with classes and modules you want to be autoloadable.
17
+ # config.autoload_paths += %W(#{config.root}/extras)
18
+ config.autoload_paths += %W(#{config.root}/lib)
19
+
20
+ # Only load the plugins named here, in the order given (default is alphabetical).
21
+ # :all can be used as a placeholder for all plugins not explicitly named.
22
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
23
+
24
+ # Activate observers that should always be running.
25
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
26
+
27
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
28
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
29
+ # config.time_zone = 'Central Time (US & Canada)'
30
+
31
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
32
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
33
+ # config.i18n.default_locale = :de
34
+
35
+ # Configure the default encoding used in templates for Ruby 1.9.
36
+ config.encoding = "utf-8"
37
+
38
+ # Configure sensitive parameters which will be filtered from the log file.
39
+ config.filter_parameters += [:password]
40
+
41
+ # Enable escaping HTML in JSON.
42
+ config.active_support.escape_html_entities_in_json = true
43
+
44
+ # Use SQL instead of Active Record's schema dumper when creating the database.
45
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
46
+ # like if you have constraints or database-specific column types
47
+ # config.active_record.schema_format = :sql
48
+
49
+ # Enable the asset pipeline
50
+ config.assets.enabled = true
51
+
52
+ # Version of your assets, change this if you want to expire all your assets
53
+ config.assets.version = '1.0'
54
+
55
+ config.action_mailer.default_url_options = { host: 'localhost:3000' }
56
+
57
+ config.i18n.enforce_available_locales = false
58
+
59
+ config.secret_key_base = 'secretvalue'
60
+ end
61
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ if File.exist?(gemfile)
5
+ ENV['BUNDLE_GEMFILE'] = gemfile
6
+ require 'bundler'
7
+ Bundler.setup
8
+ end
9
+
10
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,19 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ #
4
+ # Ensure the SQLite 3 gem is defined in your Gemfile
5
+ # gem 'sqlite3'
6
+ development:
7
+ adapter: sqlite3
8
+ database: db/development.sqlite3
9
+ pool: 5
10
+ timeout: 5000
11
+
12
+ # Warning: The database defined as "test" will be erased and
13
+ # re-generated from your development database when you run "rake".
14
+ # Do not set this db to the same as development or production.
15
+ test:
16
+ adapter: sqlite3
17
+ database: db/test.sqlite3
18
+ pool: 5
19
+ timeout: 5000
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!
@@ -0,0 +1,28 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the web server when you make code changes.
7
+ config.cache_classes = false
8
+ config.eager_load = false
9
+
10
+ # Show full error reports and disable caching
11
+ config.consider_all_requests_local = true
12
+ config.action_controller.perform_caching = false
13
+
14
+ # Don't care if the mailer can't send
15
+ config.action_mailer.raise_delivery_errors = false
16
+
17
+ # Print deprecation notices to the Rails logger
18
+ config.active_support.deprecation = :log
19
+
20
+ # Only use best-standards-support built into browsers
21
+ config.action_dispatch.best_standards_support = :builtin
22
+
23
+ # Do not compress assets
24
+ config.assets.compress = false
25
+
26
+ # Expands the lines which load the assets
27
+ config.assets.debug = true
28
+ end
@@ -0,0 +1,68 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # Code is not reloaded between requests
5
+ config.cache_classes = true
6
+ config.eager_load = false
7
+
8
+ # Full error reports are disabled and caching is turned on
9
+ config.consider_all_requests_local = false
10
+ config.action_controller.perform_caching = true
11
+
12
+ # Disable Rails's static asset server (Apache or nginx will already do this)
13
+ config.serve_static_assets = false
14
+
15
+ # Compress JavaScripts and CSS
16
+ config.assets.compress = true
17
+
18
+ # Don't fallback to assets pipeline if a precompiled asset is missed
19
+ config.assets.compile = false
20
+
21
+ # Generate digests for assets URLs
22
+ config.assets.digest = true
23
+
24
+ # Defaults to nil and saved in location specified by config.assets.prefix
25
+ # config.assets.manifest = YOUR_PATH
26
+
27
+ # Specifies the header that your server uses for sending files
28
+ # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
29
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
30
+
31
+ # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
32
+ # config.force_ssl = true
33
+
34
+ # See everything in the log (default is :info)
35
+ # config.log_level = :debug
36
+
37
+ # Prepend all log lines with the following tags
38
+ # config.log_tags = [ :subdomain, :uuid ]
39
+
40
+ # Use a different logger for distributed setups
41
+ # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
42
+
43
+ # Use a different cache store in production
44
+ # config.cache_store = :mem_cache_store
45
+
46
+ # Enable serving of images, stylesheets, and JavaScripts from an asset server
47
+ # config.action_controller.asset_host = "http://assets.example.com"
48
+
49
+ # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
50
+ # config.assets.precompile += %w( search.js )
51
+
52
+ # Disable delivery errors, bad email addresses will be ignored
53
+ # config.action_mailer.raise_delivery_errors = false
54
+
55
+ # Enable threaded mode
56
+ # config.threadsafe!
57
+
58
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
59
+ # the I18n.default_locale when a translation can not be found)
60
+ config.i18n.fallbacks = true
61
+
62
+ # Send deprecation notices to registered listeners
63
+ config.active_support.deprecation = :notify
64
+
65
+ # Log the query plan for queries taking more than this (works
66
+ # with SQLite, MySQL, and PostgreSQL)
67
+ # config.active_record.auto_explain_threshold_in_seconds = 0.5
68
+ end
@@ -0,0 +1,41 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+ config.eager_load = false
10
+
11
+ # Configure static asset server for tests with Cache-Control for performance
12
+ if Rails::VERSION::MAJOR == 4 && Rails::VERSION::MINOR >= 2 ||
13
+ Rails::VERSION::MAJOR >= 5
14
+ config.serve_static_files = true
15
+ else
16
+ config.serve_static_assets = true
17
+ end
18
+
19
+ config.static_cache_control = "public, max-age=3600"
20
+
21
+ # Show full error reports and disable caching
22
+ config.consider_all_requests_local = true
23
+ config.action_controller.perform_caching = false
24
+
25
+ # Raise exceptions instead of rendering exception templates
26
+ config.action_dispatch.show_exceptions = false
27
+
28
+ # Disable request forgery protection in test environment
29
+ config.action_controller.allow_forgery_protection = false
30
+
31
+ # Tell Action Mailer not to deliver emails to the real world.
32
+ # The :test delivery method accumulates sent emails in the
33
+ # ActionMailer::Base.deliveries array.
34
+ config.action_mailer.delivery_method = :test
35
+
36
+ # Print deprecation notices to the stderr
37
+ config.active_support.deprecation = :stderr
38
+
39
+ # For testing session variables in Capybara specs
40
+ config.middleware.use RackSessionAccess::Middleware
41
+ end
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7
+ # Rails.backtrace_cleaner.remove_silencers!