devise_mobile_confirmable 0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 70b185da869661a090f223f60487c8a5791e22e8
4
+ data.tar.gz: a9203ce3809bbe2cbcccf872fe4fa880cf15c803
5
+ SHA512:
6
+ metadata.gz: 7fece75fb675368adc6dc5e6cd52567047e72f7c1e01bec7b41d865636665fabbd1b1e8750ace80abc6ea0564b41537cf65fb23f7b5b7c8cb5c65bd95c128443
7
+ data.tar.gz: 6cfaf568eb838d54c135fe6ac9c9e7cc567d338f7b63f9724dd3ac44b6adcdf7ab250f5c437a91bf913153511476d528da0f18cb4b441c5e3bb6e0eec6d2555d
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Chen, Yi-Cyuan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,83 @@
1
+ # devise_mobile_confirmable
2
+
3
+ [![Build Status](https://api.travis-ci.org/emn178/devise_mobile_confirmable.png)](https://travis-ci.org/emn178/devise_mobile_confirmable)
4
+ [![Coverage Status](https://coveralls.io/repos/emn178/devise_mobile_confirmable/badge.svg?branch=master)](https://coveralls.io/r/emn178/devise_mobile_confirmable?branch=master)
5
+
6
+ It adds support to devise for confirming users' mobile by SMS.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'devise_mobile_confirmable'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ gem install devise_mobile_confirmable
23
+
24
+ Run the following generator to add DeviseMobileConfirmable's configuration option into the Devise configuration file (config/initializers/devise.rb):
25
+
26
+ rails g devise_mobile_confirmable:install
27
+
28
+ ## Requirements
29
+
30
+ [devise](https://github.com/plataformatec/devise)
31
+ [SMS Carrier](https://github.com/emn178/sms_carrier)
32
+
33
+ ## Usage
34
+ Add DeviseMobileConfirmable to your Devise models using the following generator:
35
+
36
+ rails g devise_mobile_confirmable MODEL
37
+
38
+ For example
39
+
40
+ rails g devise_mobile_confirmable user
41
+
42
+ And you can use following methods:
43
+ ```Ruby
44
+ user.mobile_confirmed? # true if confirmed
45
+ user.change_mobile '+886987654321' # send SMS with token to user
46
+ user.confirm_mobile_token '123456' # user input the token and confirm his mobile phone
47
+ user.mobile # eg. '+886987654321'
48
+
49
+ # return how many seconds to enable next SMS request, equal or less than 0 will unlock
50
+ user.seconds_to_unlock_mobile_confirmation_token
51
+ ```
52
+ You can overwrite token generating method, in model class:
53
+ ```Ruby
54
+ def generate_mobile_confirmation_token
55
+ # return your token
56
+ end
57
+ ```
58
+
59
+ ## Configuration
60
+ You can set options in devise.rb
61
+ ```Ruby
62
+ # ==> Configuration for :mobile_confirmable
63
+ # Specify mobile field in table.
64
+ # Default: :mobile
65
+ # config.mobile_field = :mobile
66
+
67
+ # Prevent too many requests for sending token by SMS.
68
+ # Default: 60.seconds
69
+ # config.throttle_mobile_confirmation_token = 60.seconds
70
+
71
+ # Expire token if too many retries. This is for preventing from brute force attack.
72
+ # Set to 0 to disable this feature.
73
+ # Default: 3
74
+ # config.max_mobile_confirmation_failure = 3
75
+ ```
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
80
+
81
+ ## Contact
82
+ The project's website is located at https://github.com/emn178/devise_mobile_confirmable
83
+ Author: Chen, Yi-Cyuan (emn178@gmail.com)
@@ -0,0 +1,28 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'DeviseMobileConfirmable'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require "rspec/core/rake_task"
25
+
26
+ RSpec::Core::RakeTask.new(:spec)
27
+
28
+ task :default => :spec
@@ -0,0 +1,8 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ indifferent: is indifferent
5
+ retry_later: Please retry later
6
+ devise:
7
+ carrier:
8
+ mobile_confirmation_token: Your token is %{token}
@@ -0,0 +1 @@
1
+ Spring.application_root = './spec/dummy'
@@ -0,0 +1,18 @@
1
+ require 'devise'
2
+ require 'devise_mobile_confirmable/engine'
3
+
4
+ module DeviseMobileConfirmable
5
+ end
6
+
7
+ module Devise
8
+ mattr_accessor :mobile_field
9
+ @@mobile_field = :mobile
10
+
11
+ mattr_accessor :throttle_mobile_confirmation_token
12
+ @@throttle_mobile_confirmation_token = 60.seconds
13
+
14
+ mattr_accessor :max_mobile_confirmation_failure
15
+ @@max_mobile_confirmation_failure = 3
16
+ end
17
+
18
+ Devise.add_module :mobile_confirmable, :model => 'devise_mobile_confirmable/model'
@@ -0,0 +1,4 @@
1
+ module DeviseMobileConfirmable
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,91 @@
1
+ require 'sms_carrier'
2
+
3
+ module Devise
4
+ module Models
5
+ module MobileConfirmable
6
+ def mobile_confirmed?
7
+ mobile_confirmed_at.present?
8
+ end
9
+
10
+ def change_mobile(new_mobile)
11
+ errors.add(self.class.mobile_field, :blank) if new_mobile.blank?
12
+ errors.add(self.class.mobile_field, :indifferent) if mobile == new_mobile
13
+ errors.add(:base, :retry_later) unless can_send_mobile_confirmation_token?
14
+ return false unless errors.empty?
15
+
16
+ self.unconfirmed_mobile = new_mobile
17
+ if mobile_confirmation_token_expired?
18
+ self.mobile_confirmation_token = generate_mobile_confirmation_token
19
+ self.mobile_confirmation_failure = 0
20
+ end
21
+ self.mobile_confirmation_sent_at = Time.now
22
+ return false unless save
23
+ send_mobile_confirmation_token
24
+ errors.empty?
25
+ end
26
+
27
+ def confirm_mobile_token(token)
28
+ errors.add(:mobile_confirmation_token, :blank) if token.blank?
29
+ errors.add(:mobile_confirmation_token, :expired) if mobile_confirmation_token_expired?
30
+ return false unless errors.empty?
31
+ if mobile_confirmation_token != token
32
+ update(:mobile_confirmation_failure => mobile_confirmation_failure + 1)
33
+ errors.add(:mobile_confirmation_token, :invalid)
34
+ return false
35
+ end
36
+ self.mobile = unconfirmed_mobile
37
+ self.unconfirmed_mobile = nil
38
+ self.mobile_confirmation_token = nil
39
+ self.mobile_confirmed_at = Time.now
40
+ self.mobile_confirmation_failure = 0
41
+ save
42
+ end
43
+
44
+ def seconds_to_unlock_mobile_confirmation_token
45
+ return 0 if mobile_confirmation_sent_at.nil?
46
+ self.class.throttle_mobile_confirmation_token - (Time.now - mobile_confirmation_sent_at).to_i
47
+ end
48
+
49
+ def can_send_mobile_confirmation_token?
50
+ seconds_to_unlock_mobile_confirmation_token <= 0
51
+ end
52
+
53
+ def mobile_confirmation_token_expired?
54
+ mobile_confirmation_token.nil? ||
55
+ mobile_confirmation_failure >= self.class.max_mobile_confirmation_failure && self.class.max_mobile_confirmation_failure != 0
56
+ end
57
+
58
+ def mobile
59
+ if self.class.mobile_field == :mobile
60
+ super
61
+ else
62
+ self.send(self.class.mobile_field)
63
+ end
64
+ end
65
+
66
+ def mobile=(value)
67
+ if self.class.mobile_field == :mobile
68
+ super
69
+ else
70
+ self.send(self.class.mobile_field.to_s + '=', value)
71
+ end
72
+ end
73
+
74
+ protected
75
+
76
+ def send_mobile_confirmation_token
77
+ SmsCarrier::Base.sms(:to => unconfirmed_mobile, :body => I18n.t('devise.carrier.mobile_confirmation_token', :token => mobile_confirmation_token)).deliver_now
78
+ end
79
+
80
+ def generate_mobile_confirmation_token
81
+ rand(100000..999999)
82
+ end
83
+
84
+ module ClassMethods
85
+ Devise::Models.config(self, :mobile_field)
86
+ Devise::Models.config(self, :throttle_mobile_confirmation_token)
87
+ Devise::Models.config(self, :max_mobile_confirmation_failure)
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,3 @@
1
+ module DeviseMobileConfirmable
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module ActiveRecord
4
+ module Generators
5
+ class DeviseMobileConfirmableGenerator < ActiveRecord::Generators::Base
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ def copy_devise_migration
9
+ migration_template "migration.rb", "db/migrate/devise_mobile_confirmable_add_to_#{table_name}.rb"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ class DeviseMobileConfirmableAddTo<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def up
3
+ change_table :<%= table_name %> do |t|
4
+ t.string :mobile
5
+ t.string :unconfirmed_mobile
6
+ t.string :mobile_confirmation_token
7
+ t.datetime :mobile_confirmed_at
8
+ t.datetime :mobile_confirmation_sent_at
9
+ t.integer :mobile_confirmation_failure, default: 0, null: false
10
+ end
11
+ # remove unique: true if it's allowed
12
+ add_index :<%= table_name %>, :mobile, unique: true
13
+ end
14
+
15
+ def down
16
+ change_table :<%= table_name %> do |t|
17
+ t.remove :mobile_confirmation_token, :mobile_confirmed_at, :mobile_confirmation_sent_at, :unconfirmed_mobile, :mobile_confirmation_failure
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+ module DeviseMobileConfirmable
2
+ module Generators
3
+ class DeviseMobileConfirmableGenerator < Rails::Generators::NamedBase
4
+ namespace "devise_mobile_confirmable"
5
+
6
+ desc "Add :mobile_confirmable directive in the given model. Also generate migration for ActiveRecord"
7
+
8
+ def inject_devise_invitable_content
9
+ path = File.join(destination_root, "app", "models", "#{file_path}.rb")
10
+ inject_into_file(path, "mobile_confirmable, :", :after => "devise :") if File.exists?(path)
11
+ end
12
+
13
+ hook_for :orm
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,42 @@
1
+ module DeviseMobileConfirmable
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../../../", __FILE__)
5
+ desc "Add DeviseMobileConfirmable config variables to the Devise initializer and copy DeviseMobileConfirmable locale files to your application."
6
+
7
+ def add_config_options_to_initializer
8
+ devise_initializer_path = File.join(destination_root, "config" , "initializers", "devise.rb")
9
+ if File.exist?(devise_initializer_path)
10
+ old_content = File.read(devise_initializer_path)
11
+
12
+ if old_content.include?('# ==> Configuration for :mobile_confirmable')
13
+ false
14
+ else
15
+ inject_into_file(devise_initializer_path, :before => " # ==> Configuration for :confirmable\n") do
16
+ <<-CONTENT
17
+ # ==> Configuration for :mobile_confirmable
18
+ # Specify mobile field in table.
19
+ # Default: :mobile
20
+ # config.mobile_field = :mobile
21
+
22
+ # Prevent too many requests for sending token by SMS.
23
+ # Default: 60.seconds
24
+ # config.throttle_mobile_confirmation_token = 60.seconds
25
+
26
+ # Expire token if too many retries. This is for preventing from brute force attack.
27
+ # Set to 0 to disable this feature.
28
+ # Default: 3
29
+ # config.max_mobile_confirmation_failure = 3
30
+
31
+ CONTENT
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ def copy_locale
38
+ copy_file "config/locales/en.yml", "config/locales/devise_mobile_confirmable.en.yml"
39
+ end
40
+ end
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,197 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devise_mobile_confirmable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chen, Yi-Cyuan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: devise
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sms_carrier
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-its
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: generator_spec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: coveralls
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: database_cleaner
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: It adds support to devise for confirming users' mobile by SMS.
154
+ email:
155
+ - emn178@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - LICENSE.txt
161
+ - README.md
162
+ - Rakefile
163
+ - config/locales/en.yml
164
+ - config/spring.rb
165
+ - lib/devise_mobile_confirmable.rb
166
+ - lib/devise_mobile_confirmable/engine.rb
167
+ - lib/devise_mobile_confirmable/model.rb
168
+ - lib/devise_mobile_confirmable/version.rb
169
+ - lib/generators/active_record/devise_mobile_confirmable_generator.rb
170
+ - lib/generators/active_record/templates/migration.rb
171
+ - lib/generators/devise_mobile_confirmable/devise_mobile_confirmable_generator.rb
172
+ - lib/generators/devise_mobile_confirmable/install_generator.rb
173
+ homepage: https://github.com/emn178/devise_mobile_confirmable
174
+ licenses:
175
+ - MIT
176
+ metadata: {}
177
+ post_install_message:
178
+ rdoc_options: []
179
+ require_paths:
180
+ - lib
181
+ required_ruby_version: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
186
+ required_rubygems_version: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ - - ">="
189
+ - !ruby/object:Gem::Version
190
+ version: '0'
191
+ requirements: []
192
+ rubyforge_project:
193
+ rubygems_version: 2.5.1
194
+ signing_key:
195
+ specification_version: 4
196
+ summary: It adds support to devise for confirming users' mobile by SMS.
197
+ test_files: []