two_factor_authentication 1.0 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +20 -0
  3. data/Gemfile +17 -0
  4. data/README.md +27 -18
  5. data/Rakefile +13 -0
  6. data/app/controllers/devise/two_factor_authentication_controller.rb +5 -4
  7. data/app/views/devise/two_factor_authentication/max_login_attempts_reached.html.erb +2 -2
  8. data/config/locales/en.yml +3 -0
  9. data/config/locales/ru.yml +6 -0
  10. data/lib/two_factor_authentication/controllers/helpers.rb +11 -1
  11. data/lib/two_factor_authentication/models/two_factor_authenticatable.rb +12 -4
  12. data/lib/two_factor_authentication/version.rb +1 -1
  13. data/spec/controllers/two_factor_auth_spec.rb +20 -0
  14. data/spec/features/two_factor_authenticatable_spec.rb +86 -0
  15. data/spec/lib/two_factor_authentication/models/two_factor_authenticatable_spec.rb +92 -6
  16. data/spec/rails_app/.gitignore +3 -0
  17. data/spec/rails_app/README.md +3 -0
  18. data/spec/rails_app/Rakefile +7 -0
  19. data/spec/rails_app/app/assets/javascripts/application.js +1 -0
  20. data/spec/rails_app/app/assets/stylesheets/application.css +4 -0
  21. data/spec/rails_app/app/controllers/application_controller.rb +3 -0
  22. data/spec/rails_app/app/controllers/home_controller.rb +10 -0
  23. data/spec/rails_app/app/helpers/application_helper.rb +8 -0
  24. data/spec/rails_app/app/mailers/.gitkeep +0 -0
  25. data/spec/rails_app/app/models/.gitkeep +0 -0
  26. data/spec/rails_app/app/models/guest_user.rb +10 -0
  27. data/spec/rails_app/app/models/user.rb +15 -0
  28. data/spec/rails_app/app/views/home/dashboard.html.erb +7 -0
  29. data/spec/rails_app/app/views/home/index.html.erb +3 -0
  30. data/spec/rails_app/app/views/layouts/application.html.erb +20 -0
  31. data/spec/rails_app/config.ru +4 -0
  32. data/spec/rails_app/config/application.rb +63 -0
  33. data/spec/rails_app/config/boot.rb +10 -0
  34. data/spec/rails_app/config/database.yml +19 -0
  35. data/spec/rails_app/config/environment.rb +5 -0
  36. data/spec/rails_app/config/environments/development.rb +28 -0
  37. data/spec/rails_app/config/environments/production.rb +68 -0
  38. data/spec/rails_app/config/environments/test.rb +32 -0
  39. data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  40. data/spec/rails_app/config/initializers/devise.rb +256 -0
  41. data/spec/rails_app/config/initializers/inflections.rb +15 -0
  42. data/spec/rails_app/config/initializers/mime_types.rb +5 -0
  43. data/spec/rails_app/config/initializers/secret_token.rb +7 -0
  44. data/spec/rails_app/config/initializers/session_store.rb +8 -0
  45. data/spec/rails_app/config/initializers/wrap_parameters.rb +14 -0
  46. data/spec/rails_app/config/locales/devise.en.yml +59 -0
  47. data/spec/rails_app/config/locales/en.yml +5 -0
  48. data/spec/rails_app/config/routes.rb +64 -0
  49. data/spec/rails_app/db/migrate/20140403184646_devise_create_users.rb +42 -0
  50. data/spec/rails_app/db/migrate/20140407172619_two_factor_authentication_add_to_users.rb +15 -0
  51. data/spec/rails_app/db/migrate/20140407215513_add_nickanme_to_users.rb +7 -0
  52. data/spec/rails_app/db/schema.rb +38 -0
  53. data/spec/rails_app/lib/assets/.gitkeep +0 -0
  54. data/spec/rails_app/lib/sms_provider.rb +17 -0
  55. data/spec/rails_app/public/404.html +26 -0
  56. data/spec/rails_app/public/422.html +26 -0
  57. data/spec/rails_app/public/500.html +25 -0
  58. data/spec/rails_app/public/favicon.ico +0 -0
  59. data/spec/rails_app/script/rails +6 -0
  60. data/spec/spec_helper.rb +7 -7
  61. data/spec/support/authenticated_model_helper.rb +18 -19
  62. data/spec/support/capybara.rb +3 -0
  63. data/spec/support/features_spec_helper.rb +19 -0
  64. data/spec/support/sms_provider.rb +5 -0
  65. data/two_factor_authentication.gemspec +3 -1
  66. metadata +141 -13
@@ -0,0 +1,15 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new inflection rules using the following format
4
+ # (all these examples are active by default):
5
+ # ActiveSupport::Inflector.inflections do |inflect|
6
+ # inflect.plural /^(ox)$/i, '\1en'
7
+ # inflect.singular /^(ox)en/i, '\1'
8
+ # inflect.irregular 'person', 'people'
9
+ # inflect.uncountable %w( fish sheep )
10
+ # end
11
+ #
12
+ # These inflection rules are supported but not enabled by default:
13
+ # ActiveSupport::Inflector.inflections do |inflect|
14
+ # inflect.acronym 'RESTful'
15
+ # end
@@ -0,0 +1,5 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new mime types for use in respond_to blocks:
4
+ # Mime::Type.register "text/richtext", :rtf
5
+ # Mime::Type.register_alias "text/html", :iphone
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ Dummy::Application.config.secret_token = 'e75d8cdfc7c99757a5d4b427bde4b4b1072eb169c022cdbb038bdbcefb3901ef60ac912b6fb14260db099156520b9cc8838e4bf8e209b7246fad891950825032'
@@ -0,0 +1,8 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Dummy::Application.config.session_store :cookie_store, key: '_rails_app_session'
4
+
5
+ # Use the database for sessions instead of the cookie-based default,
6
+ # which shouldn't be used to store highly confidential information
7
+ # (create the session table with "rails generate session_migration")
8
+ # Dummy::Application.config.session_store :active_record_store
@@ -0,0 +1,14 @@
1
+ # Be sure to restart your server when you modify this file.
2
+ #
3
+ # This file contains settings for ActionController::ParamsWrapper which
4
+ # is enabled by default.
5
+
6
+ # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7
+ ActiveSupport.on_load(:action_controller) do
8
+ wrap_parameters format: [:json]
9
+ end
10
+
11
+ # Disable root element in JSON by default.
12
+ ActiveSupport.on_load(:active_record) do
13
+ self.include_root_in_json = false
14
+ end
@@ -0,0 +1,59 @@
1
+ # Additional translations at https://github.com/plataformatec/devise/wiki/I18n
2
+
3
+ en:
4
+ devise:
5
+ confirmations:
6
+ confirmed: "Your account was successfully confirmed."
7
+ send_instructions: "You will receive an email with instructions about how to confirm your account in a few minutes."
8
+ send_paranoid_instructions: "If your email address exists in our database, you will receive an email with instructions about how to confirm your account in a few minutes."
9
+ failure:
10
+ already_authenticated: "You are already signed in."
11
+ inactive: "Your account is not activated yet."
12
+ invalid: "Invalid email or password."
13
+ locked: "Your account is locked."
14
+ last_attempt: "You have one more attempt before your account will be locked."
15
+ not_found_in_database: "Invalid email or password."
16
+ timeout: "Your session expired. Please sign in again to continue."
17
+ unauthenticated: "You need to sign in or sign up before continuing."
18
+ unconfirmed: "You have to confirm your account before continuing."
19
+ mailer:
20
+ confirmation_instructions:
21
+ subject: "Confirmation instructions"
22
+ reset_password_instructions:
23
+ subject: "Reset password instructions"
24
+ unlock_instructions:
25
+ subject: "Unlock Instructions"
26
+ omniauth_callbacks:
27
+ failure: "Could not authenticate you from %{kind} because \"%{reason}\"."
28
+ success: "Successfully authenticated from %{kind} account."
29
+ passwords:
30
+ no_token: "You can't access this page without coming from a password reset email. If you do come from a password reset email, please make sure you used the full URL provided."
31
+ send_instructions: "You will receive an email with instructions on how to reset your password in a few minutes."
32
+ send_paranoid_instructions: "If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes."
33
+ updated: "Your password was changed successfully. You are now signed in."
34
+ updated_not_active: "Your password was changed successfully."
35
+ registrations:
36
+ destroyed: "Bye! Your account was successfully cancelled. We hope to see you again soon."
37
+ signed_up: "Welcome! You have signed up successfully."
38
+ signed_up_but_inactive: "You have signed up successfully. However, we could not sign you in because your account is not yet activated."
39
+ signed_up_but_locked: "You have signed up successfully. However, we could not sign you in because your account is locked."
40
+ signed_up_but_unconfirmed: "A message with a confirmation link has been sent to your email address. Please open the link to activate your account."
41
+ update_needs_confirmation: "You updated your account successfully, but we need to verify your new email address. Please check your email and click on the confirm link to finalize confirming your new email address."
42
+ updated: "You updated your account successfully."
43
+ sessions:
44
+ signed_in: "Signed in successfully."
45
+ signed_out: "Signed out successfully."
46
+ unlocks:
47
+ send_instructions: "You will receive an email with instructions about how to unlock your account in a few minutes."
48
+ send_paranoid_instructions: "If your account exists, you will receive an email with instructions about how to unlock it in a few minutes."
49
+ unlocked: "Your account has been unlocked successfully. Please sign in to continue."
50
+ errors:
51
+ messages:
52
+ already_confirmed: "was already confirmed, please try signing in"
53
+ confirmation_period_expired: "needs to be confirmed within %{period}, please request a new one"
54
+ expired: "has expired, please request a new one"
55
+ not_found: "not found"
56
+ not_locked: "was not locked"
57
+ not_saved:
58
+ one: "1 error prohibited this %{resource} from being saved:"
59
+ other: "%{count} errors prohibited this %{resource} from being saved:"
@@ -0,0 +1,5 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ hello: "Hello world"
@@ -0,0 +1,64 @@
1
+ Dummy::Application.routes.draw do
2
+ root to: "home#index"
3
+
4
+ match "/dashboard", to: "home#dashboard", as: :dashboard, via: [:get]
5
+
6
+ devise_for :users
7
+
8
+ # The priority is based upon order of creation:
9
+ # first created -> highest priority.
10
+
11
+ # Sample of regular route:
12
+ # match 'products/:id' => 'catalog#view'
13
+ # Keep in mind you can assign values other than :controller and :action
14
+
15
+ # Sample of named route:
16
+ # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
17
+ # This route can be invoked with purchase_url(:id => product.id)
18
+
19
+ # Sample resource route (maps HTTP verbs to controller actions automatically):
20
+ # resources :products
21
+
22
+ # Sample resource route with options:
23
+ # resources :products do
24
+ # member do
25
+ # get 'short'
26
+ # post 'toggle'
27
+ # end
28
+ #
29
+ # collection do
30
+ # get 'sold'
31
+ # end
32
+ # end
33
+
34
+ # Sample resource route with sub-resources:
35
+ # resources :products do
36
+ # resources :comments, :sales
37
+ # resource :seller
38
+ # end
39
+
40
+ # Sample resource route with more complex sub-resources
41
+ # resources :products do
42
+ # resources :comments
43
+ # resources :sales do
44
+ # get 'recent', :on => :collection
45
+ # end
46
+ # end
47
+
48
+ # Sample resource route within a namespace:
49
+ # namespace :admin do
50
+ # # Directs /admin/products/* to Admin::ProductsController
51
+ # # (app/controllers/admin/products_controller.rb)
52
+ # resources :products
53
+ # end
54
+
55
+ # You can have the root of your site routed with "root"
56
+ # just remember to delete public/index.html.
57
+ # root :to => 'welcome#index'
58
+
59
+ # See how all your routes lay out with "rake routes"
60
+
61
+ # This is a legacy wild controller route that's not recommended for RESTful applications.
62
+ # Note: This route will make all actions in every controller accessible via GET requests.
63
+ # match ':controller(/:action(/:id))(.:format)'
64
+ end
@@ -0,0 +1,42 @@
1
+ class DeviseCreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table(:users) 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
35
+ end
36
+
37
+ add_index :users, :email, unique: true
38
+ add_index :users, :reset_password_token, unique: true
39
+ # add_index :users, :confirmation_token, unique: true
40
+ # add_index :users, :unlock_token, unique: true
41
+ end
42
+ end
@@ -0,0 +1,15 @@
1
+ class TwoFactorAuthenticationAddToUsers < ActiveRecord::Migration
2
+ def up
3
+ change_table :users do |t|
4
+ t.string :otp_secret_key
5
+ t.integer :second_factor_attempts_count, :default => 0
6
+ end
7
+
8
+ add_index :users, :otp_secret_key, :unique => true
9
+ end
10
+
11
+ def down
12
+ remove_column :users, :otp_secret_key
13
+ remove_column :users, :second_factor_attempts_count
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ class AddNickanmeToUsers < ActiveRecord::Migration
2
+ def change
3
+ change_table :users do |t|
4
+ t.column :nickname, :string, limit: 64
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended to check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(:version => 20140407215513) do
15
+
16
+ create_table "users", :force => true do |t|
17
+ t.string "email", :default => "", :null => false
18
+ t.string "encrypted_password", :default => "", :null => false
19
+ t.string "reset_password_token"
20
+ t.datetime "reset_password_sent_at"
21
+ t.datetime "remember_created_at"
22
+ t.integer "sign_in_count", :default => 0, :null => false
23
+ t.datetime "current_sign_in_at"
24
+ t.datetime "last_sign_in_at"
25
+ t.string "current_sign_in_ip"
26
+ t.string "last_sign_in_ip"
27
+ t.datetime "created_at", :null => false
28
+ t.datetime "updated_at", :null => false
29
+ t.string "otp_secret_key"
30
+ t.integer "second_factor_attempts_count", :default => 0
31
+ t.string "nickname", :limit => 64
32
+ end
33
+
34
+ add_index "users", ["email"], :name => "index_users_on_email", :unique => true
35
+ add_index "users", ["otp_secret_key"], :name => "index_users_on_otp_secret_key", :unique => true
36
+ add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
37
+
38
+ 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,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
data/spec/spec_helper.rb CHANGED
@@ -1,11 +1,7 @@
1
- require "rubygems"
2
- require "bundler/setup"
3
-
4
- require 'two_factor_authentication'
5
-
6
-
7
- Dir["#{Dir.pwd}/spec/support/**/*.rb"].each {|f| require f}
1
+ ENV["RAILS_ENV"] ||= "test"
2
+ require File.expand_path("../rails_app/config/environment.rb", __FILE__)
8
3
 
4
+ require 'rspec/rails'
9
5
 
10
6
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
11
7
  RSpec.configure do |config|
@@ -13,9 +9,13 @@ RSpec.configure do |config|
13
9
  config.run_all_when_everything_filtered = true
14
10
  config.filter_run :focus
15
11
 
12
+ config.use_transactional_examples = true
13
+
16
14
  # Run specs in random order to surface order dependencies. If you find an
17
15
  # order dependency and want to debug it, you can fix the order by providing
18
16
  # the seed, which is printed after each run.
19
17
  # --seed 1234
20
18
  config.order = 'random'
21
19
  end
20
+
21
+ Dir["#{Dir.pwd}/spec/support/**/*.rb"].each {|f| require f}