devise-two-factor 4.1.1 → 6.1.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 (38) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +6 -0
  3. data/.github/workflows/ci.yml +5 -17
  4. data/.github/workflows/push.yml +28 -0
  5. data/.markdownlint.json +6 -0
  6. data/Appraisals +12 -32
  7. data/CHANGELOG.md +21 -0
  8. data/README.md +109 -63
  9. data/Rakefile +2 -0
  10. data/SECURITY.md +5 -0
  11. data/UPGRADING.md +218 -2
  12. data/devise-two-factor.gemspec +7 -14
  13. data/gemfiles/rails_7.0.gemfile +2 -2
  14. data/gemfiles/{rails_4.1.gemfile → rails_7.1.gemfile} +2 -2
  15. data/gemfiles/{rails_4.2.gemfile → rails_7.2.gemfile} +2 -2
  16. data/gemfiles/{rails_5.0.gemfile → rails_8.0.gemfile} +2 -2
  17. data/lib/devise-two-factor.rb +11 -5
  18. data/lib/devise_two_factor/models/two_factor_authenticatable.rb +32 -17
  19. data/lib/devise_two_factor/models/two_factor_backupable.rb +3 -2
  20. data/lib/devise_two_factor/spec_helpers/two_factor_authenticatable_shared_examples.rb +6 -18
  21. data/lib/devise_two_factor/spec_helpers/two_factor_backupable_shared_examples.rb +2 -2
  22. data/lib/devise_two_factor/strategies/two_factor_authenticatable.rb +1 -1
  23. data/lib/devise_two_factor/strategies/two_factor_backupable.rb +0 -3
  24. data/lib/devise_two_factor/version.rb +1 -1
  25. data/lib/generators/devise_two_factor/devise_two_factor_generator.rb +2 -7
  26. data/spec/devise/models/two_factor_authenticatable_spec.rb +11 -69
  27. data/spec/devise/models/two_factor_backupable_spec.rb +11 -2
  28. data/spec/spec_helper.rb +0 -1
  29. metadata +33 -126
  30. checksums.yaml.gz.sig +0 -0
  31. data/certs/tinfoil-cacert.pem +0 -41
  32. data/certs/tinfoilsecurity-gems-cert.pem +0 -35
  33. data/gemfiles/rails_5.1.gemfile +0 -8
  34. data/gemfiles/rails_5.2.gemfile +0 -8
  35. data/gemfiles/rails_6.0.gemfile +0 -8
  36. data/gemfiles/rails_6.1.gemfile +0 -8
  37. data.tar.gz.sig +0 -0
  38. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 787116f2d8fd1e2ef5a6e663c0a3ed5a65ca6110a491f44d2de5eb1a985da516
4
- data.tar.gz: f379bef9da0239c3697fe14dad33ba6c662ecfd2b6bcc3ab54fb184f75a970d4
3
+ metadata.gz: 84d4fba8bbdcee4f8d8b00c5d2662dc5e8d78cc60e7f0502c7e0e9d5fc93a4d4
4
+ data.tar.gz: 33a77876910f588992917ebb123148f26d82b26fbe713f944e59f3b720c4978c
5
5
  SHA512:
6
- metadata.gz: 23b55432e9df42fa8723db98cd38abe641656ed05ce6f5d9e5d7ee1a1179624ba7212209c460a7ff4b16d4f14347b11c69750d795e7ff9f56513e93af651c619
7
- data.tar.gz: ce784140d65f12c6e46a51d86ed8e97edc6b98e706f276160e67f95bccf615c308a5cf273bd54ad71788b61e5dac92f9676c4e67ebfdd81c83584fa71e0bb509
6
+ metadata.gz: d2f69c9f760278e3a1a40d5913b786468d9d4d12b54e93dd5d5437cfb5edc669f21a56953ea817d66722cbe305190762dcb86cef80009a0fff718e86a39726be
7
+ data.tar.gz: e3086e4034e6208e064f81e5845b1027b5e7c19d38ddd4788c615fe1a837772f1330aba234a30fb3f7f9cc549dbed3864da9f70249aef04b9efa066c8284733c
@@ -0,0 +1,6 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "github-actions"
4
+ directory: "/"
5
+ schedule:
6
+ interval: "weekly"
@@ -12,29 +12,17 @@ jobs:
12
12
  fail-fast: false
13
13
  matrix:
14
14
  # Due to https://github.com/actions/runner/issues/849, we should quote versions
15
- ruby: ['2.3', '2.4', '2.5', '2.6', '2.7', '3.0', 'truffleruby-head']
16
- rails: ['4.1', '4.2', '5.0', '5.1', '5.2', '6.0', '6.1', '7.0']
15
+ ruby: ['3.1', '3.2', '3.3', 'truffleruby-head']
16
+ rails: ['7.0', '7.1', '7.2', '8.0']
17
17
  exclude:
18
- - { ruby: '2.3', rails: '7.0' }
19
- - { ruby: '2.4', rails: '7.0' }
20
- - { ruby: '2.5', rails: '7.0' }
21
- - { ruby: '2.6', rails: '7.0' }
22
- - { ruby: '2.3', rails: '6.0' }
23
- - { ruby: '2.3', rails: '6.1' }
24
- - { ruby: '2.4', rails: '6.0' }
25
- - { ruby: '2.4', rails: '6.1' }
26
- - { ruby: '2.7', rails: '4.1' }
27
- - { ruby: '2.7', rails: '4.2' }
28
- - { ruby: '3.0', rails: '4.1' }
29
- - { ruby: '3.0', rails: '4.2' }
30
- - { ruby: 'truffleruby-head', rails: '4.1' }
31
- - { ruby: 'truffleruby-head', rails: '4.2' }
18
+ - ruby: '3.1'
19
+ rails: '8.0'
32
20
 
33
21
  name: Ruby ${{ matrix.ruby }}, Rails ${{ matrix.rails }}
34
22
  env: # $BUNDLE_GEMFILE must be set at the job level, so it is set for all steps
35
23
  BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/rails_${{ matrix.rails }}.gemfile
36
24
  steps:
37
- - uses: actions/checkout@v2
25
+ - uses: actions/checkout@v4
38
26
  - name: Set up Ruby
39
27
  uses: ruby/setup-ruby@v1
40
28
  with:
@@ -0,0 +1,28 @@
1
+ name: Push Gem
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - v*
7
+
8
+ jobs:
9
+ deployment:
10
+ name: Push gem to RubyGems.org
11
+ environment: RubyGems
12
+ runs-on: ubuntu-latest
13
+
14
+ permissions:
15
+ id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
16
+ contents: write # IMPORTANT: this permission is required for `rake release` to push the release tag
17
+
18
+ steps:
19
+ # Set up
20
+ - uses: actions/checkout@v4
21
+ - name: Set up Ruby
22
+ uses: ruby/setup-ruby@v1
23
+ with:
24
+ bundler-cache: true
25
+ ruby-version: ruby
26
+
27
+ # Release
28
+ - uses: rubygems/release-gem@v1
@@ -0,0 +1,6 @@
1
+ {
2
+ "MD026": false,
3
+ "MD029": false,
4
+ "MD031": false,
5
+ "MD034": false
6
+ }
data/Appraisals CHANGED
@@ -1,39 +1,19 @@
1
- appraise "rails-4.1" do
2
- gem 'railties', '~> 4.1'
3
- gem 'activesupport', '~> 4.1'
4
- end
5
-
6
- appraise "rails-4.2" do
7
- gem 'railties', '~> 4.2'
8
- gem 'activesupport', '~> 4.2'
9
- end
10
-
11
- appraise "rails-5.0" do
12
- gem 'railties', '~> 5.0'
13
- gem 'activesupport', '~> 5.0'
14
- end
15
-
16
- appraise "rails-5.1" do
17
- gem 'railties', '~> 5.1'
18
- gem 'activesupport', '~> 5.1'
19
- end
20
-
21
- appraise "rails-5.2" do
22
- gem 'railties', '~> 5.2'
23
- gem 'activesupport', '~> 5.2'
1
+ appraise "rails-7.0" do
2
+ gem 'railties', '~> 7.0.0'
3
+ gem 'activesupport', '~> 7.0.0'
24
4
  end
25
5
 
26
- appraise "rails-6.0" do
27
- gem 'railties', '~> 6.0'
28
- gem 'activesupport', '~> 6.0'
6
+ appraise "rails-7.1" do
7
+ gem 'railties', '~> 7.1.0'
8
+ gem 'activesupport', '~> 7.1.0'
29
9
  end
30
10
 
31
- appraise "rails-6.1" do
32
- gem 'railties', '~> 6.1'
33
- gem 'activesupport', '~> 6.1'
11
+ appraise "rails-7.2" do
12
+ gem 'railties', '~> 7.2.0'
13
+ gem 'activesupport', '~> 7.2.0'
34
14
  end
35
15
 
36
- appraise "rails-7.0" do
37
- gem 'railties', '~> 7.0'
38
- gem 'activesupport', '~> 7.0'
16
+ appraise "rails-8.0" do
17
+ gem 'railties', '~> 8.0.0'
18
+ gem 'activesupport', '~> 8.0.0'
39
19
  end
data/CHANGELOG.md CHANGED
@@ -2,6 +2,27 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 6.0.0
6
+
7
+ **Breaking Changes**
8
+ - `otp_secret_length` and `otp_backup_code_length` options have changed to be the number of random bytes that are generated. See [UPGRADING.md](UPGRADING.md).
9
+ - `consume_otp!` and `invalidate_otp_backup_code!` now call `save!` instead of `save`. See [UPGRADING.md](UPGRADING.md).
10
+
11
+ ## 5.1.0
12
+
13
+ - Remove faker dev dependency
14
+ - Insert two_factor_authenticatable at the top of the devise module list
15
+ - README and CI improvements
16
+
17
+ ## 5.0.0
18
+
19
+ **Breaking Changes**
20
+ - attr_encrypted has been deprecated in favor of native Rails attribute encryption. See [UPGRADING.md](UPGRADING.md) for details on how to migrate your records. You **must** use or build a migration strategy (see examples in [UPGRADING.md](UPGRADING.md)) to use existing data!
21
+ - Rails 7 is now required.
22
+
23
+ ## 4.1.0 / 4.1.1
24
+ - Add support for attr_encrypted v4
25
+
5
26
  ## 4.0.2
6
27
  - Add Rails 7.0 support
7
28
  - Renew signing certificate
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
1
  # Devise-Two-Factor Authentication
2
- By [Tinfoil Security](https://www.tinfoilsecurity.com/) (acq. [Synopsys](https://www.synopsys.com/) 2020). Interested in [working with us](https://www.synopsys.com/careers.html)? We're hiring!
3
2
 
4
- ![Build Status](https://github.com/tinfoil/devise-two-factor/actions/workflows/ci.yml/badge.svg)
3
+ ![Build Status](https://github.com/devise-two-factor/devise-two-factor/actions/workflows/ci.yml/badge.svg)
5
4
 
6
5
  Devise-Two-Factor is a minimalist extension to Devise which offers support for two-factor authentication, through the [TOTP](https://en.wikipedia.org/wiki/Time-based_One-Time_Password) scheme. It:
7
6
 
@@ -11,89 +10,109 @@ Devise-Two-Factor is a minimalist extension to Devise which offers support for t
11
10
  * Is extensible, and includes two-factor backup codes as an example of how plugins can be structured
12
11
 
13
12
  ## Contributing
13
+
14
14
  We welcome pull requests, bug reports, and other contributions. We're especially looking for help getting this gem fully compatible with Rails 5+ and squashing any deprecation messages.
15
15
 
16
16
  ## Example App
17
- An example Rails 4 application is provided in the `demo` directory. It showcases a minimal example of Devise-Two-Factor in action, and can act as a reference for integrating the gem into your own application.
18
17
 
19
- For the demo app to work, create an encryption key and store it as an environment variable. One way to do this is to create a file named `local_env.yml` in the application root. Set the value of `ENCRYPTION_KEY` in the YML file. That value will be loaded into the application environment by `application.rb`.
18
+ See [examples](demo/README.md).
20
19
 
21
20
  ## Getting Started
22
- Devise-Two-Factor doesn't require much to get started, but there are a few prerequisites before you can start using it in your application.
21
+
22
+ Devise-Two-Factor doesn't require much to get started, but there are two prerequisites before you can start using it in your application:
23
+
24
+ 1. A Rails application with [devise](https://github.com/heartcombo/devise) installed
25
+ 1. Secrets configured for ActiveRecord encrypted attributes
23
26
 
24
27
  First, you'll need a Rails application setup with Devise. Visit the Devise [homepage](https://github.com/plataformatec/devise) for instructions.
25
28
 
26
- You can add Devise-Two-Factor to your Gemfile with:
29
+ Devise-Two-Factor uses [ActiveRecord encrypted attributes](https://edgeguides.rubyonrails.org/active_record_encryption.html). If you haven't already set up ActiveRecord encryption you must generate a key set and configure your application to use them either with Rails' encrypted credentials or from another source such as environment variables.
27
30
 
28
- ```ruby
29
- gem 'devise-two-factor'
31
+ ```bash
32
+ # Generates a random key set and outputs it to stdout
33
+ ./bin/rails db:encryption:init
30
34
  ```
31
35
 
32
- Next, since Devise-Two-Factor encrypts its secrets before storing them in the database, you'll need to generate an encryption key, and store it in an environment variable of your choice. Set the encryption key in the model that uses Devise:
33
-
34
- ```ruby
35
- devise :two_factor_authenticatable,
36
- :otp_secret_encryption_key => ENV['YOUR_ENCRYPTION_KEY_HERE']
36
+ You can load the key set using Rails' credentials.
37
37
 
38
+ ```bash
39
+ # Copy the generated key set into your encrypted credentials file
40
+ # Setting the EDITOR environment variable is optional, but without it your default editor will open
41
+ EDITOR="code --wait" ./bin/rails credentials:edit
38
42
  ```
39
43
 
40
- Finally, you can automate all of the required setup by simply running:
44
+ To learn more about credentials run `./bin/rails credentials:help`.
45
+
46
+ Alternatively, you can configure your application with environment variables rather than Rails' credentials.
41
47
 
42
48
  ```ruby
43
- rails generate devise_two_factor MODEL ENVIRONMENT_VARIABLE
49
+ # Copy the generate key set and set them as environment variables
50
+
51
+ config.active_record.encryption.primary_key = ENV['ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY']
52
+ config.active_record.encryption.deterministic_key = ENV['ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY']
53
+ config.active_record.encryption.key_derivation_salt = ENV['ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT']
44
54
  ```
45
55
 
46
- Where `MODEL` is the name of the model you wish to add two-factor functionality to (for example `user`), and `ENVIRONMENT_VARIABLE` is the name of the variable you're storing your encryption key in.
56
+ Add Devise-Two-Factor to your Gemfile with:
47
57
 
48
- This generator will add a few columns to the specified model:
58
+ ```ruby
59
+ # Gemfile
49
60
 
50
- * encrypted_otp_secret
51
- * encrypted_otp_secret_iv
52
- * encrypted_otp_secret_salt
53
- * consumed_timestep
54
- * otp_required_for_login
61
+ gem 'devise-two-factor'
62
+ ```
55
63
 
56
- Remember to apply the new migration.
64
+ There is a generator which automates most of the setup:
57
65
 
58
- ```ruby
59
- bundle exec rake db:migrate
66
+ ```bash
67
+ # MODEL is the name of the model you wish to configure devise_two_factor e.g. User or Admin
68
+ ./bin/rails generate devise_two_factor MODEL
60
69
  ```
61
70
 
62
- It also adds the `:two_factor_authenticatable` directive to your model, and sets up your encryption key. If present, it will remove `:database_authenticatable` from the model, as the two strategies are incompatible. Lastly, the generator will add a Warden config block to your Devise initializer, which enables the strategies required for two-factor authentication.
71
+ Where `MODEL` is the name of the model you wish to add two-factor functionality to (for example `user`)
63
72
 
64
- If you're running Rails 3, or do not have strong parameters enabled, the generator will also setup the required mass-assignment security options in your model.
73
+ This generator will:
65
74
 
66
- If you're running Rails 4, you'll also need to whitelist `:otp_attempt` as a permitted parameter in Devise `:sign_in` controller. You can do this by adding the following to your `application_controller.rb`:
75
+ 1. Create a new migration which adds a few columns to the specified model:
67
76
 
68
- ```ruby
69
- before_action :configure_permitted_parameters, if: :devise_controller?
77
+ ```ruby
78
+ add_column :users, :otp_secret, :string
79
+ add_column :users, :consumed_timestep, :integer
80
+ add_column :users, :otp_required_for_login, :boolean
81
+ ```
70
82
 
71
- ...
83
+ 1. Edit `app/models/MODEL.rb` (where MODEL is your model name):
84
+ * add the `:two_factor_authenticatable` devise module
85
+ * remove the `:database_authenticatable` if present because it is incompatible with `:two_factor_authenticatable`
86
+ 1. Add a Warden config block to your Devise initializer, which enables the strategies required for two-factor authentication.
72
87
 
73
- protected
88
+ Remember to apply the new migration after you run the generator:
74
89
 
75
- def configure_permitted_parameters
76
- devise_parameter_sanitizer.for(:sign_in) << :otp_attempt
77
- end
90
+ ```bash
91
+ ./bin/rails db:migrate
78
92
  ```
79
93
 
80
- If you're running Devise 4.0.0 or above, you'll want to use `.permit` instead:
94
+ Next you need to whitelist `:otp_attempt` as a permitted parameter in Devise `:sign_in` controller. You can do this by adding the following to your `application_controller.rb`:
81
95
 
82
96
  ```ruby
83
- before_action :configure_permitted_parameters, if: :devise_controller?
97
+ # app/controllers/application_controller.rb
84
98
 
85
- ...
99
+ before_action :configure_permitted_parameters, if: :devise_controller?
86
100
 
87
- protected
101
+ # ...
88
102
 
89
- def configure_permitted_parameters
90
- devise_parameter_sanitizer.permit(:sign_in, keys: [:otp_attempt])
91
- end
103
+ protected
104
+
105
+ def configure_permitted_parameters
106
+ devise_parameter_sanitizer.permit(:sign_in, keys: [:otp_attempt])
107
+ end
92
108
  ```
93
109
 
94
- **After running the generator, verify that `:database_authenticatable` is not being loaded by your model. The generator will try to remove it, but if you have a non-standard Devise setup, this step may fail. Loading both `:database_authenticatable` and `:two_factor_authenticatable` in a model will allow users to bypass two-factor authenticatable due to the way Warden handles cascading strategies.**
110
+ Finally you should verify that `:database_authenticatable` is **not** being loaded by your model. The generator will try to remove it, but if you have a non-standard Devise setup, this step may fail.
111
+
112
+ **Loading both `:database_authenticatable` and `:two_factor_authenticatable` in a model is a security issue** It will allow users to bypass two-factor authenticatable due to the way Warden handles cascading strategies!
95
113
 
96
114
  ## Designing Your Workflow
115
+
97
116
  Devise-Two-Factor only worries about the backend, leaving the details of the integration up to you. This means that you're responsible for building the UI that drives the gem. While there is an example Rails application included in the gem, it is important to remember that this gem is intentionally very open-ended, and you should build a user experience which fits your individual application.
98
117
 
99
118
  There are two key workflows you'll have to think about:
@@ -103,8 +122,8 @@ There are two key workflows you'll have to think about:
103
122
 
104
123
  We chose to keep things as simple as possible, and our implementation can be found by registering at [Tinfoil Security](https://www.tinfoilsecurity.com/), and enabling two-factor authentication from the [security settings page](https://www.tinfoilsecurity.com/account/security).
105
124
 
106
-
107
125
  ### Logging In
126
+
108
127
  Logging in with two-factor authentication works extremely similarly to regular database authentication in Devise. The `TwoFactorAuthenticatable` strategy accepts three parameters:
109
128
 
110
129
  1. email
@@ -114,11 +133,13 @@ Logging in with two-factor authentication works extremely similarly to regular d
114
133
  These parameters can be submitted to the standard Devise login route, and the strategy will handle the authentication of the user for you.
115
134
 
116
135
  ### Disabling Automatic Login After Password Resets
136
+
117
137
  If you use the Devise `recoverable` strategy, the default behavior after a password reset is to automatically authenticate the user and log them in. This is obviously a problem if a user has two-factor authentication enabled, as resetting the password would get around the two-factor requirement.
118
138
 
119
139
  Because of this, you need to set `sign_in_after_reset_password` to `false` (either globally in your Devise initializer or via `devise_for`).
120
140
 
121
141
  ### Enabling Two-Factor Authentication
142
+
122
143
  Enabling two-factor authentication for a user is easy. For example, if my user model were named User, I could do the following:
123
144
 
124
145
  ```ruby
@@ -149,6 +170,7 @@ current_user.current_otp
149
170
  ```
150
171
 
151
172
  The generated code will be valid for the duration specified by `otp_allowed_drift`. This value can be modified by adding a config in `config/initializers/devise.rb`.
173
+
152
174
  ```ruby
153
175
  Devise.otp_allowed_drift = 240 # value in seconds
154
176
  Devise.setup do |config|
@@ -165,13 +187,27 @@ However you decide to handle enrollment, there are a few important consideration
165
187
  It sounds like a lot of work, but most of these problems have been very elegantly solved by other people. We recommend taking a look at the excellent workflows used by Heroku and Google for inspiration.
166
188
 
167
189
  ### Filtering sensitive parameters from the logs
190
+
168
191
  To prevent two-factor authentication codes from leaking if your application logs get breached, you'll want to filter sensitive parameters from the Rails logs. Add the following to `config/initializers/filter_parameter_logging.rb`:
169
192
 
170
193
  ```ruby
171
194
  Rails.application.config.filter_parameters += [:otp_attempt]
172
195
  ```
173
196
 
197
+ ### Preventing Brute-Force Attacks
198
+
199
+ With any authentication solution it is also important to protect your users from brute-force attacks. For Devise-Two-Factor specifically if a user's username and password have already been compromised an attacker would be able to try possible TOTP codes and see if they can hit a lucky collision to log in. While Devise-Two-Factor is open-ended by design and cannot solve this for all applications natively there are some possible mitigations to consider. A non-exhaustive list follows:
200
+
201
+ 1. Use the `lockable` strategy from Devise to lock a user after a certain number of failed login attempts. See https://www.rubydoc.info/github/heartcombo/devise/main/Devise/Models/Lockable for more information.
202
+ 2. Configure a rate limit for your application, especially on the endpoints used to log in. One such library to accomplish this is [rack-attack](https://rubygems.org/gems/rack-attack).
203
+ 3. When displaying authentication errors hide whether validating a username/password combination failed or a two-factor code failed behind a more generic error message.
204
+
205
+ #### Acknowledgements
206
+
207
+ Thank you to Christian Reitter (Radically Open Security) and Chris MacNaughton (Centauri Solutions) for reporting the issue.
208
+
174
209
  ## Backup Codes
210
+
175
211
  Devise-Two-Factor is designed with extensibility in mind. One such extension, `TwoFactorBackupable`, is included and serves as a good example of how to extend this gem. This plugin allows you to add the ability to generate single-use backup codes for a user, which they may use to bypass two-factor authentication, in the event that they lose access to their device.
176
212
 
177
213
  To install it, you need to add the `:two_factor_backupable` directive to your model.
@@ -186,17 +222,39 @@ You'll also be required to enable the `:two_factor_backupable` strategy, by addi
186
222
  manager.default_strategies(:scope => :user).unshift :two_factor_backupable
187
223
  ```
188
224
 
189
- The final installation step is dependent on your version of Rails. If you're not running Rails 4, skip to the next section. Otherwise, create the following migration:
225
+ ### Migration
226
+
227
+ The final installation step may be dependent on your version of Rails.
228
+
229
+ #### PostgreSQL
190
230
 
191
231
  ```ruby
192
232
  class AddDeviseTwoFactorBackupableToUsers < ActiveRecord::Migration
193
233
  def change
194
- # Change type from :string to :text if using MySQL database
195
234
  add_column :users, :otp_backup_codes, :string, array: true
196
235
  end
197
236
  end
198
237
  ```
199
238
 
239
+ #### MySQL
240
+
241
+ ```ruby
242
+ # migration
243
+ class AddDeviseTwoFactorBackupableToUsers < ActiveRecord::Migration
244
+ def change
245
+ add_column :users, :otp_backup_codes, :text
246
+ end
247
+ end
248
+
249
+ # model
250
+ class User < ApplicationRecord
251
+ devise :two_factor_backupable
252
+ serialize :otp_backup_codes, Array
253
+ end
254
+ ```
255
+
256
+ ### Generation
257
+
200
258
  You can then generate backup codes for a user:
201
259
 
202
260
  ```ruby
@@ -214,23 +272,8 @@ devise :two_factor_backupable, otp_backup_code_length: 32,
214
272
  otp_number_of_backup_codes: 10
215
273
  ```
216
274
 
217
- ### Help! I'm not using Rails 4.0!
218
- Don't worry! `TwoFactorBackupable` stores the backup codes as an array of strings in the database. In Rails 4.0 this is supported natively, but in earlier versions you can use a gem to emulate this behavior: we recommend [activerecord-postgres-array](https://github.com/tlconnor/activerecord-postgres-array).
219
-
220
- You'll then simply have to create a migration to add an array named `otp_backup_codes` to your model. If you use the above gem, this migration might look like:
221
-
222
- ```ruby
223
- class AddTwoFactorBackupCodesToUsers < ActiveRecord::Migration
224
- def change
225
- # Change type from :string_array to :text_array if using MySQL database
226
- add_column :users, :otp_backup_codes, :string_array
227
- end
228
- end
229
- ```
230
-
231
- Now just continue with the setup in the previous section, skipping the generator step.
232
-
233
275
  ## Testing
276
+
234
277
  Devise-Two-Factor includes shared-examples for both `TwoFactorAuthenticatable` and `TwoFactorBackupable`. Adding the following two lines to the specs for your two-factor enabled models will allow you to test your models for two-factor functionality:
235
278
 
236
279
  ```ruby
@@ -241,6 +284,7 @@ it_behaves_like "two_factor_backupable"
241
284
  ```
242
285
 
243
286
  ## Troubleshooting
287
+
244
288
  If you are using Rails 4.x and Ruby >= 2.7, you may get an error like
245
289
 
246
290
  ```
@@ -250,9 +294,11 @@ Failure/Error: require 'devise'
250
294
  NoMethodError:
251
295
  undefined method `new' for BigDecimal:Class
252
296
  ```
297
+
253
298
  see https://github.com/ruby/bigdecimal#which-version-should-you-select and https://github.com/ruby/bigdecimal/issues/127
254
299
  for more details, but you should be able to solve this
255
300
  by explicitly requiring an older version of bigdecimal in your gemfile like
256
- ```
301
+
302
+ ```ruby
257
303
  gem "bigdecimal", "~> 1.4"
258
304
  ```
data/Rakefile CHANGED
@@ -11,6 +11,8 @@ rescue Bundler::BundlerError => e
11
11
  end
12
12
  require 'rake'
13
13
 
14
+ require 'bundler/gem_tasks'
15
+
14
16
  require 'rspec/core'
15
17
  require 'rspec/core/rake_task'
16
18
  RSpec::Core::RakeTask.new(:spec) do |spec|
data/SECURITY.md ADDED
@@ -0,0 +1,5 @@
1
+ # Security Policy
2
+
3
+ ## Reporting a Vulnerability
4
+
5
+ Please report any vulnerabilities to the [Black Duck PSIRT](psirt@blackduck.com).