devise-two-factor 5.0.0 → 6.3.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.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +6 -0
- data/.github/workflows/ci.yml +10 -3
- data/.github/workflows/push.yml +28 -0
- data/.markdownlint.json +6 -0
- data/Appraisals +15 -30
- data/CHANGELOG.md +33 -0
- data/README.md +79 -37
- data/Rakefile +2 -0
- data/SECURITY.md +5 -0
- data/UPGRADING.md +38 -15
- data/devise-two-factor.gemspec +8 -14
- data/gemfiles/rails_7.0.gemfile +2 -2
- data/gemfiles/{rails_4.2.gemfile → rails_7.1.gemfile} +2 -2
- data/gemfiles/{rails_5.0.gemfile → rails_7.2.gemfile} +2 -2
- data/gemfiles/{rails_5.1.gemfile → rails_8.0.gemfile} +2 -2
- data/gemfiles/{rails_4.1.gemfile → rails_8.1.gemfile} +2 -2
- data/lib/devise-two-factor.rb +7 -4
- data/lib/devise_two_factor/models/two_factor_authenticatable.rb +13 -14
- data/lib/devise_two_factor/models/two_factor_backupable.rb +6 -2
- data/lib/devise_two_factor/spec_helpers/two_factor_authenticatable_shared_examples.rb +5 -5
- data/lib/devise_two_factor/spec_helpers/two_factor_backupable_shared_examples.rb +52 -24
- data/lib/devise_two_factor/strategies/two_factor_authenticatable.rb +8 -2
- data/lib/devise_two_factor/strategies/two_factor_backupable.rb +6 -4
- data/lib/devise_two_factor/version.rb +1 -1
- data/spec/devise/models/two_factor_authenticatable_spec.rb +5 -1
- data/spec/devise/models/two_factor_backupable_spec.rb +4 -0
- data/spec/spec_helper.rb +0 -1
- metadata +45 -109
- checksums.yaml.gz.sig +0 -0
- data/certs/tinfoil-cacert.pem +0 -41
- data/certs/tinfoilsecurity-gems-cert.pem +0 -35
- data/gemfiles/rails_5.2.gemfile +0 -8
- data/gemfiles/rails_6.0.gemfile +0 -8
- data/gemfiles/rails_6.1.gemfile +0 -8
- data.tar.gz.sig +0 -0
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4513e96354e689136149f21dbd447785cc74113a66725bed6153e44758a1709b
|
|
4
|
+
data.tar.gz: 9e83133ebdc4166577129276188688780aeec065a5fead7861073496210215e9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a119f4f3f825409f6cea77f934fe7e35fa741dfb1b242c33a59dd523eb43ef2270b3dda80d1403f4baf7ade03271bbd4b145d21c6c390eeb223e00f0aadb6d29
|
|
7
|
+
data.tar.gz: 0f31866cb9c1523ff94095d2b43cf819d2a06a658a2dad94383d93e4c6c47d9e791092090cf8063f351c7727df6f2146e4bf22bf5a204c0b1f80aaf8a4fec3fb
|
data/.github/workflows/ci.yml
CHANGED
|
@@ -12,14 +12,21 @@ 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: ['
|
|
16
|
-
rails: ['7.0']
|
|
15
|
+
ruby: ['3.1', '3.2', '3.3', '3.4', 'truffleruby-head']
|
|
16
|
+
rails: ['7.0', '7.1', '7.2', '8.0', '8.1']
|
|
17
|
+
exclude:
|
|
18
|
+
- ruby: '3.1'
|
|
19
|
+
rails: '8.0'
|
|
20
|
+
- ruby: '3.1'
|
|
21
|
+
rails: '8.1'
|
|
22
|
+
- ruby: '3.4'
|
|
23
|
+
rails: '7.0'
|
|
17
24
|
|
|
18
25
|
name: Ruby ${{ matrix.ruby }}, Rails ${{ matrix.rails }}
|
|
19
26
|
env: # $BUNDLE_GEMFILE must be set at the job level, so it is set for all steps
|
|
20
27
|
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/rails_${{ matrix.rails }}.gemfile
|
|
21
28
|
steps:
|
|
22
|
-
- uses: actions/checkout@
|
|
29
|
+
- uses: actions/checkout@v6
|
|
23
30
|
- name: Set up Ruby
|
|
24
31
|
uses: ruby/setup-ruby@v1
|
|
25
32
|
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@v6
|
|
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
|
data/.markdownlint.json
ADDED
data/Appraisals
CHANGED
|
@@ -1,39 +1,24 @@
|
|
|
1
|
-
appraise "rails-
|
|
2
|
-
gem 'railties', '~>
|
|
3
|
-
gem 'activesupport', '~>
|
|
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'
|
|
1
|
+
appraise "rails-7.0" do
|
|
2
|
+
gem 'railties', '~> 7.0.0'
|
|
3
|
+
gem 'activesupport', '~> 7.0.0'
|
|
19
4
|
end
|
|
20
5
|
|
|
21
|
-
appraise "rails-
|
|
22
|
-
gem 'railties', '~>
|
|
23
|
-
gem 'activesupport', '~>
|
|
6
|
+
appraise "rails-7.1" do
|
|
7
|
+
gem 'railties', '~> 7.1.0'
|
|
8
|
+
gem 'activesupport', '~> 7.1.0'
|
|
24
9
|
end
|
|
25
10
|
|
|
26
|
-
appraise "rails-
|
|
27
|
-
gem 'railties', '~>
|
|
28
|
-
gem 'activesupport', '~>
|
|
11
|
+
appraise "rails-7.2" do
|
|
12
|
+
gem 'railties', '~> 7.2.0'
|
|
13
|
+
gem 'activesupport', '~> 7.2.0'
|
|
29
14
|
end
|
|
30
15
|
|
|
31
|
-
appraise "rails-
|
|
32
|
-
gem 'railties', '~>
|
|
33
|
-
gem 'activesupport', '~>
|
|
16
|
+
appraise "rails-8.0" do
|
|
17
|
+
gem 'railties', '~> 8.0.0'
|
|
18
|
+
gem 'activesupport', '~> 8.0.0'
|
|
34
19
|
end
|
|
35
20
|
|
|
36
|
-
appraise "rails-
|
|
37
|
-
gem 'railties', '
|
|
38
|
-
gem 'activesupport', '
|
|
21
|
+
appraise "rails-8.1" do
|
|
22
|
+
gem 'railties', '8.1.0'
|
|
23
|
+
gem 'activesupport', '8.1.0'
|
|
39
24
|
end
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,39 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 6.3.0
|
|
6
|
+
|
|
7
|
+
- Fixed timing to be consistent when Devise paranoid mode is active.
|
|
8
|
+
|
|
9
|
+
## 6.2.0
|
|
10
|
+
|
|
11
|
+
- Rails 8.1 support
|
|
12
|
+
|
|
13
|
+
## 6.1.0
|
|
14
|
+
|
|
15
|
+
- Rails 8 support
|
|
16
|
+
|
|
17
|
+
## 6.0.0
|
|
18
|
+
|
|
19
|
+
**Breaking Changes**
|
|
20
|
+
- `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).
|
|
21
|
+
- `consume_otp!` and `invalidate_otp_backup_code!` now call `save!` instead of `save`. See [UPGRADING.md](UPGRADING.md).
|
|
22
|
+
|
|
23
|
+
## 5.1.0
|
|
24
|
+
|
|
25
|
+
- Remove faker dev dependency
|
|
26
|
+
- Insert two_factor_authenticatable at the top of the devise module list
|
|
27
|
+
- README and CI improvements
|
|
28
|
+
|
|
29
|
+
## 5.0.0
|
|
30
|
+
|
|
31
|
+
**Breaking Changes**
|
|
32
|
+
- 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!
|
|
33
|
+
- Rails 7 is now required.
|
|
34
|
+
|
|
35
|
+
## 4.1.0 / 4.1.1
|
|
36
|
+
- Add support for attr_encrypted v4
|
|
37
|
+
|
|
5
38
|
## 4.0.2
|
|
6
39
|
- Add Rails 7.0 support
|
|
7
40
|
- 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
|
-

|
|
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,12 +10,12 @@ 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
|
-
|
|
18
|
+
See [examples](demo/README.md).
|
|
20
19
|
|
|
21
20
|
## Getting Started
|
|
22
21
|
|
|
@@ -27,15 +26,31 @@ Devise-Two-Factor doesn't require much to get started, but there are two prerequ
|
|
|
27
26
|
|
|
28
27
|
First, you'll need a Rails application setup with Devise. Visit the Devise [homepage](https://github.com/plataformatec/devise) for instructions.
|
|
29
28
|
|
|
30
|
-
Devise-Two-Factor uses [ActiveRecord encrypted attributes](https://edgeguides.rubyonrails.org/active_record_encryption.html)
|
|
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.
|
|
31
30
|
|
|
32
31
|
```bash
|
|
33
|
-
#
|
|
34
|
-
|
|
32
|
+
# Generates a random key set and outputs it to stdout
|
|
33
|
+
./bin/rails db:encryption:init
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
You can load the key set using Rails' credentials.
|
|
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
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
To learn more about credentials run `./bin/rails credentials:help`.
|
|
35
45
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
46
|
+
Alternatively, you can configure your application with environment variables rather than Rails' credentials.
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
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']
|
|
39
54
|
```
|
|
40
55
|
|
|
41
56
|
Add Devise-Two-Factor to your Gemfile with:
|
|
@@ -58,14 +73,16 @@ Where `MODEL` is the name of the model you wish to add two-factor functionality
|
|
|
58
73
|
This generator will:
|
|
59
74
|
|
|
60
75
|
1. Create a new migration which adds a few columns to the specified model:
|
|
76
|
+
|
|
61
77
|
```ruby
|
|
62
78
|
add_column :users, :otp_secret, :string
|
|
63
79
|
add_column :users, :consumed_timestep, :integer
|
|
64
80
|
add_column :users, :otp_required_for_login, :boolean
|
|
65
81
|
```
|
|
82
|
+
|
|
66
83
|
1. Edit `app/models/MODEL.rb` (where MODEL is your model name):
|
|
67
84
|
* add the `:two_factor_authenticatable` devise module
|
|
68
|
-
* remove the `:database_authenticatable` if present
|
|
85
|
+
* remove the `:database_authenticatable` devise module, if present; having both modules enabled will lead to issues described below.
|
|
69
86
|
1. Add a Warden config block to your Devise initializer, which enables the strategies required for two-factor authentication.
|
|
70
87
|
|
|
71
88
|
Remember to apply the new migration after you run the generator:
|
|
@@ -90,11 +107,12 @@ Next you need to whitelist `:otp_attempt` as a permitted parameter in Devise `:s
|
|
|
90
107
|
end
|
|
91
108
|
```
|
|
92
109
|
|
|
93
|
-
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.
|
|
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. `:two_factor_authenticatable` includes all of `:database_authenticatable`'s functionality; it will still allow login without two-factor authentication until you enable it on your model's records with `otp_required_for_login`.
|
|
94
111
|
|
|
95
|
-
**Loading both `:database_authenticatable` and `:two_factor_authenticatable` in a model is a security issue
|
|
112
|
+
**Loading both `:database_authenticatable` and `:two_factor_authenticatable` in a model is a security issue.** It will allow users to bypass two-factor authentication regardless of how `otp_required_for_login` is set due to the way Warden handles cascading strategies!
|
|
96
113
|
|
|
97
114
|
## Designing Your Workflow
|
|
115
|
+
|
|
98
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.
|
|
99
117
|
|
|
100
118
|
There are two key workflows you'll have to think about:
|
|
@@ -104,8 +122,8 @@ There are two key workflows you'll have to think about:
|
|
|
104
122
|
|
|
105
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).
|
|
106
124
|
|
|
107
|
-
|
|
108
125
|
### Logging In
|
|
126
|
+
|
|
109
127
|
Logging in with two-factor authentication works extremely similarly to regular database authentication in Devise. The `TwoFactorAuthenticatable` strategy accepts three parameters:
|
|
110
128
|
|
|
111
129
|
1. email
|
|
@@ -115,11 +133,13 @@ Logging in with two-factor authentication works extremely similarly to regular d
|
|
|
115
133
|
These parameters can be submitted to the standard Devise login route, and the strategy will handle the authentication of the user for you.
|
|
116
134
|
|
|
117
135
|
### Disabling Automatic Login After Password Resets
|
|
136
|
+
|
|
118
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.
|
|
119
138
|
|
|
120
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`).
|
|
121
140
|
|
|
122
141
|
### Enabling Two-Factor Authentication
|
|
142
|
+
|
|
123
143
|
Enabling two-factor authentication for a user is easy. For example, if my user model were named User, I could do the following:
|
|
124
144
|
|
|
125
145
|
```ruby
|
|
@@ -135,10 +155,7 @@ At Tinfoil Security, we opted to use the excellent [rqrcode-rails3](https://gith
|
|
|
135
155
|
If you decide to do this you'll need to generate a URI to act as the source for the QR code. This can be done using the `User#otp_provisioning_uri` method.
|
|
136
156
|
|
|
137
157
|
```ruby
|
|
138
|
-
issuer
|
|
139
|
-
label = "#{issuer}:#{current_user.email}"
|
|
140
|
-
|
|
141
|
-
current_user.otp_provisioning_uri(label, issuer: issuer)
|
|
158
|
+
current_user.otp_provisioning_uri(current_user.email, issuer: 'Your App')
|
|
142
159
|
|
|
143
160
|
# > "otpauth://totp/Your%20App:user@example.com?secret=[otp_secret]&issuer=Your+App"
|
|
144
161
|
```
|
|
@@ -150,6 +167,7 @@ current_user.current_otp
|
|
|
150
167
|
```
|
|
151
168
|
|
|
152
169
|
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`.
|
|
170
|
+
|
|
153
171
|
```ruby
|
|
154
172
|
Devise.otp_allowed_drift = 240 # value in seconds
|
|
155
173
|
Devise.setup do |config|
|
|
@@ -166,13 +184,27 @@ However you decide to handle enrollment, there are a few important consideration
|
|
|
166
184
|
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.
|
|
167
185
|
|
|
168
186
|
### Filtering sensitive parameters from the logs
|
|
187
|
+
|
|
169
188
|
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`:
|
|
170
189
|
|
|
171
190
|
```ruby
|
|
172
191
|
Rails.application.config.filter_parameters += [:otp_attempt]
|
|
173
192
|
```
|
|
174
193
|
|
|
194
|
+
### Preventing Brute-Force Attacks
|
|
195
|
+
|
|
196
|
+
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:
|
|
197
|
+
|
|
198
|
+
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.
|
|
199
|
+
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).
|
|
200
|
+
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.
|
|
201
|
+
|
|
202
|
+
#### Acknowledgements
|
|
203
|
+
|
|
204
|
+
Thank you to Christian Reitter (Radically Open Security) and Chris MacNaughton (Centauri Solutions) for reporting the issue.
|
|
205
|
+
|
|
175
206
|
## Backup Codes
|
|
207
|
+
|
|
176
208
|
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.
|
|
177
209
|
|
|
178
210
|
To install it, you need to add the `:two_factor_backupable` directive to your model.
|
|
@@ -187,17 +219,39 @@ You'll also be required to enable the `:two_factor_backupable` strategy, by addi
|
|
|
187
219
|
manager.default_strategies(:scope => :user).unshift :two_factor_backupable
|
|
188
220
|
```
|
|
189
221
|
|
|
190
|
-
|
|
222
|
+
### Migration
|
|
223
|
+
|
|
224
|
+
The final installation step may be dependent on your version of Rails.
|
|
225
|
+
|
|
226
|
+
#### PostgreSQL
|
|
191
227
|
|
|
192
228
|
```ruby
|
|
193
229
|
class AddDeviseTwoFactorBackupableToUsers < ActiveRecord::Migration
|
|
194
230
|
def change
|
|
195
|
-
# Change type from :string to :text if using MySQL database
|
|
196
231
|
add_column :users, :otp_backup_codes, :string, array: true
|
|
197
232
|
end
|
|
198
233
|
end
|
|
199
234
|
```
|
|
200
235
|
|
|
236
|
+
#### MySQL, SQL Server, other databases without an array string type
|
|
237
|
+
|
|
238
|
+
```ruby
|
|
239
|
+
# migration
|
|
240
|
+
class AddDeviseTwoFactorBackupableToUsers < ActiveRecord::Migration
|
|
241
|
+
def change
|
|
242
|
+
add_column :users, :otp_backup_codes, :text
|
|
243
|
+
end
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
# model
|
|
247
|
+
class User < ApplicationRecord
|
|
248
|
+
devise :two_factor_backupable
|
|
249
|
+
serialize :otp_backup_codes, Array
|
|
250
|
+
end
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Generation
|
|
254
|
+
|
|
201
255
|
You can then generate backup codes for a user:
|
|
202
256
|
|
|
203
257
|
```ruby
|
|
@@ -215,23 +269,8 @@ devise :two_factor_backupable, otp_backup_code_length: 32,
|
|
|
215
269
|
otp_number_of_backup_codes: 10
|
|
216
270
|
```
|
|
217
271
|
|
|
218
|
-
### Help! I'm not using Rails 4.0!
|
|
219
|
-
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).
|
|
220
|
-
|
|
221
|
-
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:
|
|
222
|
-
|
|
223
|
-
```ruby
|
|
224
|
-
class AddTwoFactorBackupCodesToUsers < ActiveRecord::Migration
|
|
225
|
-
def change
|
|
226
|
-
# Change type from :string_array to :text_array if using MySQL database
|
|
227
|
-
add_column :users, :otp_backup_codes, :string_array
|
|
228
|
-
end
|
|
229
|
-
end
|
|
230
|
-
```
|
|
231
|
-
|
|
232
|
-
Now just continue with the setup in the previous section, skipping the generator step.
|
|
233
|
-
|
|
234
272
|
## Testing
|
|
273
|
+
|
|
235
274
|
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:
|
|
236
275
|
|
|
237
276
|
```ruby
|
|
@@ -242,6 +281,7 @@ it_behaves_like "two_factor_backupable"
|
|
|
242
281
|
```
|
|
243
282
|
|
|
244
283
|
## Troubleshooting
|
|
284
|
+
|
|
245
285
|
If you are using Rails 4.x and Ruby >= 2.7, you may get an error like
|
|
246
286
|
|
|
247
287
|
```
|
|
@@ -251,9 +291,11 @@ Failure/Error: require 'devise'
|
|
|
251
291
|
NoMethodError:
|
|
252
292
|
undefined method `new' for BigDecimal:Class
|
|
253
293
|
```
|
|
294
|
+
|
|
254
295
|
see https://github.com/ruby/bigdecimal#which-version-should-you-select and https://github.com/ruby/bigdecimal/issues/127
|
|
255
296
|
for more details, but you should be able to solve this
|
|
256
297
|
by explicitly requiring an older version of bigdecimal in your gemfile like
|
|
257
|
-
|
|
298
|
+
|
|
299
|
+
```ruby
|
|
258
300
|
gem "bigdecimal", "~> 1.4"
|
|
259
301
|
```
|
data/Rakefile
CHANGED
data/SECURITY.md
ADDED
data/UPGRADING.md
CHANGED
|
@@ -1,8 +1,25 @@
|
|
|
1
|
-
# Upgrading
|
|
1
|
+
# Upgrading
|
|
2
2
|
|
|
3
|
-
##
|
|
3
|
+
## Upgrading from 5.x to 6.x
|
|
4
4
|
|
|
5
|
-
###
|
|
5
|
+
### save!
|
|
6
|
+
|
|
7
|
+
`consume_otp!` and `invalidate_otp_backup_code!` now call `save!` instead of `save` (or nothing at all in the case of `invalidate_otp_backup_code!`). If you manually called `save`/`save!` after calling `invalidate_otp_backup_code!` you may be able to remove it.
|
|
8
|
+
|
|
9
|
+
### Secret Lengths
|
|
10
|
+
|
|
11
|
+
The `otp_secret_length` and `otp_backup_code_length` options have changed to be the number of random bytes that are generated.
|
|
12
|
+
If you had configured these values you may want to change them if you wish to keep the same output length.
|
|
13
|
+
|
|
14
|
+
`otp_secret_length` now has a default value of 20, generating a 160 bit secret key with an output length length of 32 bytes.
|
|
15
|
+
|
|
16
|
+
`otp_backup_code_length` now has a default value of 16, generating a 32 byte backup code.
|
|
17
|
+
|
|
18
|
+
## Upgrading from 4.x to 5.x
|
|
19
|
+
|
|
20
|
+
### Background
|
|
21
|
+
|
|
22
|
+
#### Database columns in version 4.x and older
|
|
6
23
|
|
|
7
24
|
Versions 4.x and older stored the OTP secret in an attribute called `encrypted_otp_secret` using the [attr_encrypted](https://github.com/attr-encrypted/attr_encrypted) gem. This gem is currently unmaintained which is part of the motivation for moving to Rails encrypted attributes. This attribute was backed by three database columns:
|
|
8
25
|
|
|
@@ -21,7 +38,7 @@ otp_required_for_login
|
|
|
21
38
|
|
|
22
39
|
A fresh install of 4.x would create all five of the database columns above.
|
|
23
40
|
|
|
24
|
-
|
|
41
|
+
#### Database columns in version 5.x and later
|
|
25
42
|
|
|
26
43
|
Versions 5+ of this gem uses a single [Rails 7+ encrypted attribute](https://edgeguides.rubyonrails.org/active_record_encryption.html) named `otp_secret`to store the OTP secret in the database table (usually `users` but will be whatever model you picked).
|
|
27
44
|
|
|
@@ -33,18 +50,15 @@ consumed_timestep
|
|
|
33
50
|
otp_required_for_login
|
|
34
51
|
```
|
|
35
52
|
|
|
36
|
-
### Upgrading from 4.x to 5.x
|
|
37
|
-
|
|
38
|
-
|
|
39
53
|
We have attempted to make the upgrade as painless as possible but unfortunately because of the secret storage change, it cannot be as simple as `bundle update devise-two-factor` :heart:
|
|
40
54
|
|
|
41
|
-
|
|
55
|
+
### Assumptions
|
|
42
56
|
|
|
43
57
|
This guide assumes you are upgrading an existing Rails 6 app (with `devise` and `devise-two-factor`) to Rails 7.
|
|
44
58
|
|
|
45
59
|
This gem must be upgraded **as part of a Rails 7 upgrade**. See [the official Rails upgrading guide](https://guides.rubyonrails.org/upgrading_ruby_on_rails.html) for an overview of upgrading Rails.
|
|
46
60
|
|
|
47
|
-
|
|
61
|
+
### Phase 1: Upgrading devise-two-factor as part of Rails 7 upgrade
|
|
48
62
|
|
|
49
63
|
1. Update the version constraint for Rails in your `Gemfile` to your desired version e.g. `gem "rails", "~> 7.0.3"`
|
|
50
64
|
1. Run `bundle install` and resolve any issues with dependencies.
|
|
@@ -59,7 +73,7 @@ This gem must be upgraded **as part of a Rails 7 upgrade**. See [the official Ra
|
|
|
59
73
|
```
|
|
60
74
|
1. Add a `legacy_otp_secret` method to your user model e.g. `User`.
|
|
61
75
|
* This method is used by the gem to find and decode the OTP secret from the legacy database columns.
|
|
62
|
-
* The implementation shown below works if you set up devise-two-factor with the settings suggested in the [README](
|
|
76
|
+
* The implementation shown below works if you set up devise-two-factor with the settings suggested in the [OLD README](https://github.com/devise-two-factor/devise-two-factor/blob/8d74f5ee45594bf00e60d5d49eb6fcde82c2d2ba/README.md).
|
|
63
77
|
* If you have customised the encryption scheme used to store the OTP secret then you will need to update this method to match.
|
|
64
78
|
* If you are unsure, you should try the method below as is, and if you can still sign in users with OTP enabled then all is well.
|
|
65
79
|
```ruby
|
|
@@ -87,7 +101,7 @@ This gem must be upgraded **as part of a Rails 7 upgrade**. See [the official Ra
|
|
|
87
101
|
cipher_text = raw_cipher_text[0..-17]
|
|
88
102
|
auth_tag = raw_cipher_text[-16..-1]
|
|
89
103
|
|
|
90
|
-
# this
|
|
104
|
+
# this algorithm lifted from
|
|
91
105
|
# https://github.com/attr-encrypted/encryptor/blob/master/lib/encryptor.rb#L54
|
|
92
106
|
|
|
93
107
|
# create an OpenSSL object which will decrypt the AES cipher with 256 bit
|
|
@@ -101,7 +115,7 @@ This gem must be upgraded **as part of a Rails 7 upgrade**. See [the official Ra
|
|
|
101
115
|
cipher.decrypt
|
|
102
116
|
|
|
103
117
|
# Use a Password-Based Key Derivation Function to generate the key actually
|
|
104
|
-
# used for
|
|
118
|
+
# used for encryption from the key we got as input.
|
|
105
119
|
cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(key, salt, hmac_iterations, cipher.key_len)
|
|
106
120
|
|
|
107
121
|
# set the Initialization Vector (IV)
|
|
@@ -149,7 +163,7 @@ You can now deploy your upgraded application and devise-two-factor should work a
|
|
|
149
163
|
|
|
150
164
|
This gem will fall back to **reading** the OTP secret from the legacy columns if it cannot find one in the new `otp_secret` column. When you **write** a new OTP secret it will always be written to the new `otp_secret` column.
|
|
151
165
|
|
|
152
|
-
|
|
166
|
+
### Phase 2: Clean up
|
|
153
167
|
|
|
154
168
|
This "clean up" phase can happen at the same time as your initial deployment but teams managing existing apps will likely want to do clean-up as separate, later deployments.
|
|
155
169
|
|
|
@@ -190,8 +204,17 @@ This "clean up" phase can happen at the same time as your initial deployment but
|
|
|
190
204
|
end
|
|
191
205
|
end
|
|
192
206
|
```
|
|
207
|
+
1. Remove `otp_secret_encryption_key` from the model setup. This also assumes you successfully ran the rake task in step 1.
|
|
208
|
+
```ruby
|
|
209
|
+
# from this:
|
|
210
|
+
devise :two_factor_authenticatable,
|
|
211
|
+
otp_secret_encryption_key: ENV['YOUR_ENCRYPTION_KEY_HERE']
|
|
212
|
+
|
|
213
|
+
# to this:
|
|
214
|
+
devise :two_factor_authenticatable
|
|
215
|
+
```
|
|
193
216
|
|
|
194
|
-
|
|
217
|
+
## Upgrading from 2.x to 3.x
|
|
195
218
|
|
|
196
219
|
Pull request #76 allows for compatibility with `attr_encrypted` 3.0, which should be used due to a security vulnerability discovered in 2.0.
|
|
197
220
|
|
|
@@ -211,7 +234,7 @@ class User < ActiveRecord::Base
|
|
|
211
234
|
:otp_secret_encryption_key => ENV['DEVISE_TWO_FACTOR_ENCRYPTION_KEY']
|
|
212
235
|
```
|
|
213
236
|
|
|
214
|
-
|
|
237
|
+
## Upgrading from 1.x to 2.x
|
|
215
238
|
|
|
216
239
|
Pull request #43 added a new field to protect against "shoulder-surfing" attacks. If upgrading, you'll need to add the `:consumed_timestep` column to your `Users` model.
|
|
217
240
|
|
data/devise-two-factor.gemspec
CHANGED
|
@@ -5,25 +5,19 @@ Gem::Specification.new do |s|
|
|
|
5
5
|
s.name = 'devise-two-factor'
|
|
6
6
|
s.version = DeviseTwoFactor::VERSION.dup
|
|
7
7
|
s.platform = Gem::Platform::RUBY
|
|
8
|
-
s.
|
|
8
|
+
s.license = 'MIT'
|
|
9
9
|
s.summary = 'Barebones two-factor authentication with Devise'
|
|
10
|
-
s.
|
|
11
|
-
s.
|
|
12
|
-
s.
|
|
13
|
-
s.authors = ['Shane Wilton']
|
|
10
|
+
s.homepage = 'https://github.com/devise-two-factor/devise-two-factor'
|
|
11
|
+
s.description = 'Devise-Two-Factor is a minimalist extension to Devise which offers support for two-factor authentication through the TOTP scheme.'
|
|
12
|
+
s.authors = ['Quinn Wilton']
|
|
14
13
|
|
|
15
|
-
s.cert_chain = [
|
|
16
|
-
'certs/tinfoil-cacert.pem',
|
|
17
|
-
'certs/tinfoilsecurity-gems-cert.pem'
|
|
18
|
-
]
|
|
19
|
-
s.signing_key = File.expand_path("~/.ssh/tinfoilsecurity-gems-key.pem") if $0 =~ /gem\z/
|
|
20
14
|
s.files = `git ls-files`.split("\n").delete_if { |x| x.match('demo/*') }
|
|
21
15
|
s.test_files = `git ls-files -- spec/*`.split("\n")
|
|
22
16
|
s.require_paths = ['lib']
|
|
23
17
|
|
|
24
|
-
s.add_runtime_dependency 'railties', '
|
|
25
|
-
s.add_runtime_dependency 'activesupport', '
|
|
26
|
-
s.add_runtime_dependency 'devise', '
|
|
18
|
+
s.add_runtime_dependency 'railties', '>= 7.0', '< 8.2'
|
|
19
|
+
s.add_runtime_dependency 'activesupport', '>= 7.0', '< 8.2'
|
|
20
|
+
s.add_runtime_dependency 'devise', '>= 4.0', '< 5.0'
|
|
27
21
|
s.add_runtime_dependency 'rotp', '~> 6.0'
|
|
28
22
|
|
|
29
23
|
s.add_development_dependency 'activemodel'
|
|
@@ -31,5 +25,5 @@ Gem::Specification.new do |s|
|
|
|
31
25
|
s.add_development_dependency 'bundler', '> 1.0'
|
|
32
26
|
s.add_development_dependency 'rspec', '> 3'
|
|
33
27
|
s.add_development_dependency 'simplecov'
|
|
34
|
-
s.add_development_dependency '
|
|
28
|
+
s.add_development_dependency 'rake', '~> 13'
|
|
35
29
|
end
|
data/gemfiles/rails_7.0.gemfile
CHANGED