devise-twilio-verify 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/.github/workflows/build.yml +32 -0
  4. data/.gitignore +45 -0
  5. data/.rspec +2 -0
  6. data/Appraisals +22 -0
  7. data/CHANGELOG.md +15 -0
  8. data/Gemfile +3 -0
  9. data/LICENSE.txt +20 -0
  10. data/README.md +266 -0
  11. data/Rakefile +29 -0
  12. data/app/assets/javascripts/devise_twilio_verify.js +12 -0
  13. data/app/assets/stylesheets/devise_twilio_verify.css +26 -0
  14. data/app/assets/stylesheets/devise_twilio_verify.sass +24 -0
  15. data/app/controllers/devise/devise_twilio_verify_controller.rb +183 -0
  16. data/app/controllers/devise_twilio_verify/passwords_controller.rb +30 -0
  17. data/app/services/twilio_verify_service.rb +66 -0
  18. data/app/views/devise/enable_twilio_verify.html.erb +7 -0
  19. data/app/views/devise/enable_twilio_verify.html.haml +5 -0
  20. data/app/views/devise/verify_twilio_verify.html.erb +16 -0
  21. data/app/views/devise/verify_twilio_verify.html.haml +13 -0
  22. data/app/views/devise/verify_twilio_verify_installation.html.erb +18 -0
  23. data/app/views/devise/verify_twilio_verify_installation.html.haml +16 -0
  24. data/config/locales/en.yml +27 -0
  25. data/config.ru +9 -0
  26. data/devise-twilio-verify.gemspec +49 -0
  27. data/gemfiles/.bundle/config +2 -0
  28. data/gemfiles/rails_5_2.gemfile +14 -0
  29. data/gemfiles/rails_6.gemfile +15 -0
  30. data/lib/devise-twilio-verify/controllers/helpers.rb +87 -0
  31. data/lib/devise-twilio-verify/controllers/view_helpers.rb +50 -0
  32. data/lib/devise-twilio-verify/hooks/twilio_verify_authenticatable.rb +7 -0
  33. data/lib/devise-twilio-verify/mapping.rb +16 -0
  34. data/lib/devise-twilio-verify/models/twilio_verify_authenticatable.rb +21 -0
  35. data/lib/devise-twilio-verify/models/twilio_verify_lockable.rb +43 -0
  36. data/lib/devise-twilio-verify/rails.rb +16 -0
  37. data/lib/devise-twilio-verify/routes.rb +21 -0
  38. data/lib/devise-twilio-verify/version.rb +5 -0
  39. data/lib/devise-twilio-verify.rb +32 -0
  40. data/lib/generators/active_record/devise_twilio_verify_generator.rb +23 -0
  41. data/lib/generators/active_record/templates/migration.rb +18 -0
  42. data/lib/generators/devise_twilio_verify/devise_twilio_verify_generator.rb +30 -0
  43. data/lib/generators/devise_twilio_verify/install_generator.rb +80 -0
  44. metadata +343 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dd3ff424c38dccbccae443b6b21049282ddd5b780d544828da324bdd3806cfb9
4
+ data.tar.gz: e1cb5007f6acdd22dcd6f315a1dd30fcfd002f654842a014e36a4f6b2f87d4d6
5
+ SHA512:
6
+ metadata.gz: cf276b167404e719c6a5e1a14eb295495a314febaeb1f2df037f1d1ee5c59870a94b2bee6d75661786c6ecaa783af453af6f3c94562d09c0bec89e4b8edacf68
7
+ data.tar.gz: 8d4949acb0d5aad351e8f5efbd3a91a528b700b240853cc1e84e24cf48a68e5f21ae6265ff93bc01288011ae11da72136cf1a513400cb21cbea24d9b80975ab0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
@@ -0,0 +1,32 @@
1
+ name: build
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ test:
7
+ runs-on: ubuntu-latest
8
+ strategy:
9
+ fail-fast: false
10
+ matrix:
11
+ ruby: [2.5, 2.6, 2.7, "3.0", 3.1, head]
12
+ gemfile: [rails_5_2, rails_6]
13
+ exclude:
14
+ - ruby: "3.0"
15
+ gemfile: rails_5_2
16
+ - ruby: 3.1
17
+ gemfile: rails_5_2
18
+ - ruby: head
19
+ gemfile: rails_5_2
20
+ continue-on-error: ${{ endsWith(matrix.ruby, 'head') }}
21
+ env:
22
+ BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
23
+ steps:
24
+ - uses: actions/checkout@v2
25
+ - name: Set up Ruby ${{ matrix.ruby }}
26
+ uses: ruby/setup-ruby@v1
27
+ with:
28
+ ruby-version: ${{ matrix.ruby }}
29
+ - name: Install dependencies
30
+ run: bundle install
31
+ - name: Run tests
32
+ run: bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,45 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ ## Specific to RubyMotion:
14
+ .dat*
15
+ .repl_history
16
+ build/
17
+
18
+ ## Documentation cache and generated files:
19
+ /.yardoc/
20
+ /_yardoc/
21
+ /doc/
22
+ /rdoc/
23
+
24
+ ## Environment normalization:
25
+ /.bundle/
26
+ /vendor/bundle
27
+ /lib/bundler/man/
28
+
29
+ # for a library or gem, you might want to ignore these files since the code is
30
+ # intended to run in multiple environments; otherwise, check them in:
31
+ Gemfile.lock
32
+ .ruby-version
33
+ .ruby-gemset
34
+ gemfiles/*.lock
35
+
36
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
37
+ .rvmrc
38
+
39
+ **/*.sqlite
40
+ **/*.log
41
+
42
+ initializers/twilio_verify.rb
43
+ .byebug_history
44
+
45
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require ./spec/spec_helper
data/Appraisals ADDED
@@ -0,0 +1,22 @@
1
+ appraise "rails-5-2" do
2
+ gem "rails", "~> 5.2.0"
3
+ gem "sqlite3", "~> 1.3.13"
4
+
5
+ group :development, :test do
6
+ gem 'factory_girl_rails', :require => false
7
+ gem 'rspec-rails', "~>4.0.0.beta3", :require => false
8
+ gem 'database_cleaner', :require => false
9
+ end
10
+ end if RUBY_VERSION.to_f < 3.0
11
+
12
+ appraise "rails-6" do
13
+ gem "rails", "~> 6.0.0"
14
+ gem "sqlite3", "~> 1.4"
15
+ gem "net-smtp"
16
+
17
+ group :development, :test do
18
+ gem 'factory_girl_rails', :require => false
19
+ gem 'rspec-rails', "~>4.0.0.beta3", :require => false
20
+ gem 'database_cleaner', :require => false
21
+ end
22
+ end if RUBY_VERSION.to_f >= 2.5
data/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
6
+ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.1.0] - 2023-03-15 Initial release
9
+
10
+ ### Changed
11
+ - Added devise 2FA support via Twilio Verify API
12
+ - Currently only support mobile phones with US country codes
13
+ - Removed Authy support
14
+ - Removed Onetouch support
15
+ - Removed ability to request a phone call
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 Jay Wolff
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,266 @@
1
+ # Migrate Authy to Twilio Verify API (for SMS and TOTP 2FA)
2
+
3
+ ### This gem is meant to be a drop-in replacement for devise-authy in a Rails app (minus the following features)
4
+ - Currently only support mobile phones with US country codes
5
+ - Removed Onetouch support
6
+ - Removed ability to request a phone call
7
+
8
+ ### Just follow the steps below to migrate:
9
+ - Swap out `devise-authy` in your Gemfile with `devise-twilio-verify` (ref this repo/branch for now)
10
+ - `gem 'devise-twilio-verify', git: 'https://github.com/jayywolff/twilio-verify-devise.git', branch: 'authy-to-twilio-verify'`
11
+ - Setup a Twilio Verify account
12
+ - Add env vars and/or Rails credentials for:
13
+ - `TWILIO_AUTH_TOKEN`
14
+ - `TWILIO_ACCOUNT_SID`
15
+ - `TWILIO_VERIFY_SERVICE_SID`
16
+ - Create/run a migration to rename and add the following columns
17
+ ```ruby
18
+ class MigrateAuthyToTwilioVerify < ActiveRecord::Migration[6.1]
19
+ def change
20
+ rename_column :users, :authy_sms, :twilio_verify_sms
21
+ rename_column :users, :authy_enabled, :twilio_verify_enabled
22
+ rename_column :users, :last_sign_in_with_authy, :last_sign_in_with_twilio_verify
23
+ add_column :users, :twilio_totp_factor_sid, :string
24
+ end
25
+ end
26
+
27
+ ```
28
+ - you can also delete the `users.authy_id` column if you choose
29
+ - Twilio Verify service sms will be sent to `users.mobile_phone`, so make sure you store the users 2fa phone number in this column, can make this field name dynamic in the future
30
+ - Do a project code wide search & replace of these terms
31
+ - `devise-authy` -> `devise-twilio-verify`
32
+ - `authy_` -> `twilio_verify_`
33
+ - `_authy` -> `_twilio_verify`
34
+ - `authy-` -> `twilio-verify-`
35
+ - `-authy` -> `-twilio-verify`
36
+ - `Authy` -> `TwilioVerify`
37
+ - Do a project file search & replace of any file with authy in the name (here's a few examples to replace)
38
+ - app/javascript/src/deviseTwilioVerify.js
39
+ - app/assets/stylesheets/devise_twilio_verify.scss
40
+ - config/locales/devise.twilio_verify.en.yml
41
+
42
+ # Twilio Verify Devise [![Build Status](https://github.com/twilio/authy-devise/workflows/build/badge.svg)](https://github.com/twilio/authy-devise/actions)
43
+
44
+ This is a [Devise](https://github.com/heartcombo/devise) extension to add [Two-Factor Authentication with Twilio Verify](https://www.twilio.com/docs/verify) to your Rails application.
45
+
46
+ Please visit the Twilio Docs for more information:
47
+ [Twilio Verify API](https://www.twilio.com/docs/verify)
48
+ * [Verify + Ruby (Rails) quickstart](https://www.twilio.com/docs/verify/quickstarts/ruby-rails)
49
+ * [Twilio Ruby helper library](https://www.twilio.com/docs/libraries/ruby)
50
+ * [Verify API reference](https://www.twilio.com/docs/verify/api)
51
+
52
+
53
+ * [Pre-requisites](#pre-requisites)
54
+ * [Demo](#demo)
55
+ * [Getting started](#getting-started)
56
+ * [Configuring Models](#configuring-models)
57
+ * [With the generator](#with-the-generator)
58
+ * [Manually](#manually)
59
+ * [Final steps](#final-steps)
60
+ * [Custom Views](#custom-views)
61
+ * [Custom Redirect Paths (eg. using modules)](#custom-redirect-paths-eg-using-modules)
62
+ * [I18n](#i18n)
63
+ * [Session variables](#session-variables)
64
+ * [Generic authenticator token support](#generic-authenticator-token-support)
65
+ * [Rails 5 CSRF protection](#rails-5-csrf-protection)
66
+ * [Running Tests](#running-tests)
67
+ * [Copyright](#copyright)
68
+
69
+ ## Pre-requisites
70
+
71
+ To use the Twilio Verify API you will need a Twilio Account, [sign up for a free Twilio account here](https://www.twilio.com/try-twilio).
72
+
73
+ Create an [Twilio Verify Application in the Twilio console](https://www.twilio.com/console/authy/applications) and take note of the API key.
74
+
75
+ ## Getting started
76
+
77
+ First get your Twilio Verify API key from [the Twilio console](https://www.twilio.com/console/authy/applications). We recommend you store your API key as an environment variable.
78
+
79
+ ```bash
80
+ $ export TWILIO_AUTH_TOKEN=YOUR_TWILIO_AUTH_TOKEN
81
+ $ export TWILIO_ACCOUNT_SID=YOUR_TWILIO_ACCOUNT_SID
82
+ $ export TWILIO_VERIFY_SERVICE_SID=YOUR_TWILIO_VERIFY_SERVICE_SID
83
+ ```
84
+
85
+ Next add the gem to your Gemfile:
86
+
87
+ ```ruby
88
+ gem 'devise'
89
+ gem 'devise-twilio-verify'
90
+ ```
91
+
92
+ And then run `bundle install`
93
+
94
+ Add `Devise Twilio Verify` to your App:
95
+
96
+ rails g devise_twilio_verify:install
97
+
98
+ --haml: Generate the views in Haml
99
+ --sass: Generate the stylesheets in Sass
100
+
101
+ ### Configuring Models
102
+
103
+ You can add devise_twilio_verify to your user model in two ways.
104
+
105
+ #### With the generator
106
+
107
+ Run the following command:
108
+
109
+ ```bash
110
+ rails g devise_twilio_verify [MODEL_NAME]
111
+ ```
112
+
113
+ To support account locking (recommended), you must add `:twilio_verify_lockable` to the `devise :twilio_verify_authenticatable, ...` configuration in your model as this is not yet supported by the generator.
114
+
115
+ #### Manually
116
+
117
+ Add `:twilio_verify_authenticatable` and `:twilio_verify_lockable` to the `devise` options in your Devise user model:
118
+
119
+ ```ruby
120
+ devise :twilio_verify_authenticatable, :twilio_verify_lockable, :database_authenticatable, :lockable
121
+ ```
122
+
123
+ (Note, `:twilio_verify_lockable` is optional but recommended. It should be used with Devise's own `:lockable` module).
124
+
125
+ Also add a new migration. For example, if you are adding to the `User` model, use this migration:
126
+
127
+ ```ruby
128
+ class DeviseTwilioVerifyAddToUsers < ActiveRecord::Migration[6.0]
129
+ def self.up
130
+ change_table :users do |t|
131
+ t.string :authy_id
132
+ t.datetime :last_sign_in_with_twilio_verify
133
+ t.boolean :twilio_verify_enabled, :default => false
134
+ end
135
+
136
+ add_index :users, :authy_id
137
+ end
138
+
139
+ def self.down
140
+ change_table :users do |t|
141
+ t.remove :authy_id, :last_sign_in_with_twilio_verify, :twilio_verify_enabled
142
+ end
143
+ end
144
+ end
145
+ ```
146
+
147
+ #### Final steps
148
+
149
+ For either method above, run the migrations:
150
+
151
+ ```bash
152
+ rake db:migrate
153
+ ```
154
+
155
+ **[Optional]** Update the default routes to point to something like:
156
+
157
+ ```ruby
158
+ devise_for :users, :path_names => {
159
+ :verify_twilio_verify => "/verify-token",
160
+ :enable_twilio_verify => "/enable-two-factor",
161
+ :verify_twilio_verify_installation => "/verify-installation"
162
+ }
163
+ ```
164
+
165
+ Now whenever a user wants to enable two-factor authentication they can go to:
166
+
167
+ http://your-app/users/enable-two-factor
168
+
169
+ And when the user logs in they will be redirected to:
170
+
171
+ http://your-app/users/verify-token
172
+
173
+ ## Custom Views
174
+
175
+ If you want to customise your views, you can modify the files that are located at:
176
+
177
+ app/views/devise/devise_twilio_verify/enable_twilio_verify.html.erb
178
+ app/views/devise/devise_twilio_verify/verify_twilio_verify.html.erb
179
+ app/views/devise/devise_twilio_verify/verify_twilio_verify_installation.html.erb
180
+
181
+ ## Custom Redirect Paths (eg. using modules)
182
+
183
+ If you want to customise the redirects you can override them within your own controller like this:
184
+
185
+ ```ruby
186
+ class MyCustomModule::DeviseTwilioVerifyController < Devise::DeviseTwilioVerifyController
187
+
188
+ protected
189
+ def after_twilio_verify_enabled_path_for(resource)
190
+ my_own_path
191
+ end
192
+
193
+ def after_twilio_verify_verified_path_for(resource)
194
+ my_own_path
195
+ end
196
+
197
+ def after_twilio_verify_disabled_path_for(resource)
198
+ my_own_path
199
+ end
200
+
201
+ def invalid_resource_path
202
+ my_own_path
203
+ end
204
+ end
205
+ ```
206
+
207
+ And tell the router to use this controller
208
+
209
+ ```ruby
210
+ devise_for :users, controllers: {devise_twilio_verify: 'my_custom_module/devise_twilio_verify'}
211
+ ```
212
+
213
+ ## I18n
214
+
215
+ The install generator also copies a `Devise Twilio Verify` i18n file which you can find at:
216
+
217
+ config/locales/devise.twilio_verify.en.yml
218
+
219
+ ## Session variables
220
+
221
+ If you want to know if the user is signed in using Two-Factor authentication,
222
+ you can use the following session variable:
223
+
224
+ ```ruby
225
+ session["#{resource_name}_twilio_verify_token_checked"]
226
+
227
+ # Eg.
228
+ session["user_twilio_verify_token_checked"]
229
+ ```
230
+
231
+ ## Generic authenticator token support
232
+
233
+ Twilio Verify supports other authenticator apps by providing a QR code that your users can scan.
234
+
235
+ > **To use this feature, you need to enable it in your [Twilio Console](https://www.twilio.com/console/authy/applications)**
236
+
237
+ Once you have enabled generic authenticator tokens, you can enable this in devise-twilio-verify by modifying the Devise config file `config/initializers/devise.rb` and adding the configuration:
238
+
239
+ ```
240
+ config.twilio_verify_enable_qr_code = true
241
+ ```
242
+
243
+ This will display a QR code on the verification screen (you still need to take a user's phone number and country code). If you have implemented your own views, the QR code URL is available on the verification page as `@twilio_verify_qr_code`.
244
+
245
+ ## Rails 5 CSRF protection
246
+
247
+ In Rails 5 `protect_from_forgery` is no longer prepended to the `before_action` chain. If you call `authenticate_user` before `protect_from_forgery` your request will result in a "Can't verify CSRF token authenticity" error.
248
+
249
+ To remedy this, add `prepend: true` to your `protect_from_forgery` call, like in this example from the [Twilio Verify Devise demo app](https://github.com/twilio/authy-devise-demo):
250
+
251
+ ```ruby
252
+ class ApplicationController < ActionController::Base
253
+ protect_from_forgery with: :exception, prepend: true
254
+ end
255
+ ```
256
+
257
+ ## Running Tests
258
+
259
+ Run the following command:
260
+
261
+ ```bash
262
+ $ bundle exec rspec
263
+ ```
264
+
265
+ ## Copyright
266
+ See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ require 'bundler/gem_tasks'
6
+ begin
7
+ Bundler.setup(:default, :development)
8
+ rescue Bundler::BundlerError => e
9
+ $stderr.puts e.message
10
+ $stderr.puts "Run `bundle install` to install missing gems"
11
+ exit e.status_code
12
+ end
13
+ require 'rake'
14
+
15
+ require 'rspec/core'
16
+ require 'rspec/core/rake_task'
17
+ RSpec::Core::RakeTask.new(:spec) do |spec|
18
+ spec.pattern = FileList['spec/**/*_spec.rb']
19
+ end
20
+
21
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
22
+ spec.pattern = 'spec/**/*_spec.rb'
23
+ spec.rcov = true
24
+ end
25
+
26
+ task :default => :spec
27
+
28
+ require 'yard'
29
+ YARD::Rake::YardocTask.new
@@ -0,0 +1,12 @@
1
+ $(document).ready(function() {
2
+ $('a#twilio-verify-request-sms-link').unbind('ajax:success');
3
+ $('a#twilio-verify-request-sms-link').bind('ajax:success', function(evt, data, status, xhr) {
4
+ alert(data.message);
5
+ });
6
+
7
+ $('a#twilio-verify-request-phone-call-link').unbind('ajax:success');
8
+ $('a#twilio-verify-request-phone-call-link').bind('ajax:success', function(evt, data, status, xhr) {
9
+ alert(data.message);
10
+ });
11
+ });
12
+
@@ -0,0 +1,26 @@
1
+ .devise-twilio-verify {
2
+ margin-left: auto;
3
+ margin-right: auto;
4
+ width: 350px;
5
+ }
6
+
7
+ .twilio-verify-form legend {
8
+ display: block;
9
+ width: 100%;
10
+ padding: 0;
11
+ margin-bottom: 20px;
12
+ font-size: 21px;
13
+ line-height: 40px;
14
+ color: #333;
15
+ border-bottom: 1px solid #E5E5E5;
16
+ }
17
+
18
+ .twilio-verify-form label,
19
+ .twilio-verify-form input,
20
+ .twilio-verify-form button {
21
+ font-size: 14px;
22
+ font-weight: normal;
23
+ line-height: 20px;
24
+ padding: 8px;
25
+ margin: 8px;
26
+ }
@@ -0,0 +1,24 @@
1
+ .devise-twilio-verify
2
+ margin-left: auto
3
+ margin-right: auto
4
+ width: 350px
5
+
6
+ .twilio-verify-form
7
+ legend
8
+ display: block
9
+ width: 100%
10
+ padding: 0
11
+ margin-bottom: 20px
12
+ font-size: 21px
13
+ line-height: 40px
14
+ color: #333
15
+ border-bottom: 1px solid #E5E5E5
16
+
17
+ label,
18
+ input,
19
+ button
20
+ font-size: 14px
21
+ font-weight: normal
22
+ line-height: 20px
23
+ padding: 8px
24
+ margin: 8px