devise_code_authenticatable 0.0.1
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 +7 -0
- data/LICENSE +20 -0
- data/README.md +28 -0
- data/Rakefile +17 -0
- data/app/controllers/devise_code_authenticatable/sessions_controller.rb +44 -0
- data/config/locales/zh-CN.yml +13 -0
- data/lib/devise_code_authenticatable.rb +23 -0
- data/lib/devise_code_authenticatable/controllers/helpers.rb +4 -0
- data/lib/devise_code_authenticatable/mailer.rb +10 -0
- data/lib/devise_code_authenticatable/mapping.rb +11 -0
- data/lib/devise_code_authenticatable/models/code_authenticatable.rb +56 -0
- data/lib/devise_code_authenticatable/rails.rb +23 -0
- data/lib/devise_code_authenticatable/strategies/code_authenticatable.rb +34 -0
- data/lib/devise_code_authenticatable/version.rb +3 -0
- metadata +78 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 35db566b383d26478ce72d66803c30aa92f96fec77189e1322b65a0d007960f4
|
4
|
+
data.tar.gz: d0f77e75f3cf974803fee99bb2717b7ea808f64b5a3ff2f72713545f2eb56c29
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 34ddfe3e6a7927f9ce401ffd05428ba80cba6ca921ca6779012c94c57f2cece9e0aaa710be2ab45b70314f588c060ad0e9fcf6a2f956233deb104602fecfb724
|
7
|
+
data.tar.gz: a8b92a461a61a505d1ef72522ca9a65b0be21e91bab9d5c8c9248f0e59a32403b6305286d2a964d609a0fb280e5e038c68dbc45746adb52471988266d9793b9b
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2020 vincentying15
|
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,28 @@
|
|
1
|
+
# DeviseCodeAuthenticatable
|
2
|
+
Short description and motivation.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
How to use my plugin.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'devise_code_authenticatable'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
```bash
|
21
|
+
$ gem install devise_code_authenticatable
|
22
|
+
```
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
Contribution directions go here.
|
26
|
+
|
27
|
+
## License
|
28
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
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 = 'DeviseCodeAuthenticatable'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,44 @@
|
|
1
|
+
class DeviseCodeAuthenticatable::SessionsController < Devise::SessionsController
|
2
|
+
before_action :configure_permitted_parameters
|
3
|
+
|
4
|
+
def create
|
5
|
+
byebug
|
6
|
+
if params[:commit] == "发送验证码"
|
7
|
+
resource = resource_class.send_login_code(send_code_params)
|
8
|
+
yield resource if block_given?
|
9
|
+
|
10
|
+
if successfully_sent?(resource)
|
11
|
+
cookies[:token_sent] = resource.email
|
12
|
+
respond_with(resource, location: after_sending_login_code_path_for(resource))
|
13
|
+
end
|
14
|
+
else
|
15
|
+
resource = warden.authenticate!(auth_options)
|
16
|
+
set_flash_message!(:notice, :signed_in)
|
17
|
+
sign_in(resource_name, resource)
|
18
|
+
yield resource if block_given?
|
19
|
+
respond_with resource, location: after_sign_in_path_for(resource)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
def auth_options
|
25
|
+
:code_authenticatable
|
26
|
+
end
|
27
|
+
|
28
|
+
def after_sending_login_code_path_for(resource)
|
29
|
+
new_session_path(resource)
|
30
|
+
end
|
31
|
+
|
32
|
+
def sign_in_params
|
33
|
+
devise_parameter_sanitizer.sanitize(:sign_in)
|
34
|
+
end
|
35
|
+
|
36
|
+
def send_code_params
|
37
|
+
devise_parameter_sanitizer.sanitize(:send_code)
|
38
|
+
end
|
39
|
+
|
40
|
+
def configure_permitted_parameters
|
41
|
+
devise_parameter_sanitizer.permit(:sign_in, keys: [:email, :login_code])
|
42
|
+
devise_parameter_sanitizer.permit(:send_code, keys: [:email])
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module DeviseCodeAuthenticatable
|
2
|
+
autoload :Mailer, 'devise_code_authenticatable/mailer'
|
3
|
+
autoload :Mapping, 'devise_code_authenticatable/mapping'
|
4
|
+
|
5
|
+
module Controllers
|
6
|
+
autoload :Helpers, 'devise_code_authenticatable/controllers/helpers'
|
7
|
+
autoload :Sessions, 'devise_code_authenticatable/controllers/sessions'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
require 'devise'
|
13
|
+
require 'devise_code_authenticatable/strategies/code_authenticatable'
|
14
|
+
require 'devise_code_authenticatable/rails'
|
15
|
+
|
16
|
+
module Devise
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
Devise.add_module :code_authenticatable,
|
22
|
+
strategy: true,
|
23
|
+
model: 'devise_code_authenticatable/models/code_authenticatable'
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Devise
|
2
|
+
module Models
|
3
|
+
module CodeAuthenticatable
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
end
|
8
|
+
|
9
|
+
class_methods do
|
10
|
+
def send_login_code(resource_params)
|
11
|
+
resource = self.find_by(resource_params)
|
12
|
+
if resource
|
13
|
+
resource.resend_login_code
|
14
|
+
resource
|
15
|
+
else
|
16
|
+
self.new(resource_params)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
def resend_login_code
|
23
|
+
code = login_code || regenerate_login_code
|
24
|
+
send_devise_notification(:login_instructions, code, {})
|
25
|
+
end
|
26
|
+
|
27
|
+
def regenerate_login_code
|
28
|
+
self.update(
|
29
|
+
login_code: rand(10000..99999),
|
30
|
+
login_code_retry_time: 0,
|
31
|
+
login_code_created_at: Time.now
|
32
|
+
)
|
33
|
+
login_code
|
34
|
+
end
|
35
|
+
|
36
|
+
def login_code_expired?
|
37
|
+
expire_in = 5.minute
|
38
|
+
retry_time_limit = 5
|
39
|
+
|
40
|
+
!(Time.now < self.login_code_created_at + expire_in && \
|
41
|
+
self.login_code_retry_time.to_i < retry_time_limit)
|
42
|
+
end
|
43
|
+
|
44
|
+
def valid_login_code?(login_code)
|
45
|
+
unless login_code_expired?
|
46
|
+
self.update(login_code_retry_time: (self.login_code_retry_time.to_i + 1))
|
47
|
+
self.login_code == login_code
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def after_code_authentication
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module DeviseCodeAuthenticatable
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
|
4
|
+
ActiveSupport.on_load(:action_controller) do
|
5
|
+
include DeviseCodeAuthenticatable::Controllers::Helpers
|
6
|
+
end
|
7
|
+
|
8
|
+
# We use to_prepare instead of after_initialize here because Devise is a Rails engine; its
|
9
|
+
# mailer is reloaded like the rest of the user's app. Got to make sure that our mailer methods
|
10
|
+
# are included each time Devise.mailer is (re)loaded.
|
11
|
+
config.to_prepare do
|
12
|
+
Devise.mailer.send :include, DeviseCodeAuthenticatable::Mailer
|
13
|
+
unless Devise.mailer.ancestors.include?(Devise::Mailers::Helpers)
|
14
|
+
Devise.mailer.send :include, Devise::Mailers::Helpers
|
15
|
+
end
|
16
|
+
end
|
17
|
+
# # extend mapping with after_initialize because it's not reloaded
|
18
|
+
# config.after_initialize do
|
19
|
+
# Devise::Mapping.send :prepend, DeviseCodeAuthenticatable::Mapping
|
20
|
+
# Devise::ParameterSanitizer.send :prepend, DeviseCodeAuthenticatable::ParameterSanitizer
|
21
|
+
# end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'devise/strategies/base'
|
2
|
+
|
3
|
+
module DeviseCodeAuthenticatable
|
4
|
+
module Strategies
|
5
|
+
|
6
|
+
class CodeAuthenticatable < Devise::Strategies::Authenticatable
|
7
|
+
def authenticate!
|
8
|
+
resource = mapping.to.find_for_authentication(authentication_hash)
|
9
|
+
hashed = false
|
10
|
+
login_code = params[scope].fetch "login_code", ""
|
11
|
+
|
12
|
+
if resource
|
13
|
+
if resource.login_code_expired?
|
14
|
+
fail(:login_code_expired)
|
15
|
+
resource.regenerate_login_code
|
16
|
+
resource.resend_login_code
|
17
|
+
else
|
18
|
+
if validate(resource){ hashed = true; resource.valid_login_code?(login_code) }
|
19
|
+
remember_me(resource)
|
20
|
+
resource.after_code_authentication
|
21
|
+
success!(resource)
|
22
|
+
else
|
23
|
+
fail(:invalid_login_code)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
else
|
27
|
+
fail(:not_found_in_database)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
Warden::Strategies.add :code_authenticatable, DeviseCodeAuthenticatable::Strategies::CodeAuthenticatable
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: devise_code_authenticatable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- vincentying15
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-02-13 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: 6.0.2
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 6.0.2.1
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 6.0.2
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 6.0.2.1
|
33
|
+
description: Devise plugin to allow sign in by email sent code
|
34
|
+
email:
|
35
|
+
- vincent_ying_live@outlook.com
|
36
|
+
executables: []
|
37
|
+
extensions: []
|
38
|
+
extra_rdoc_files: []
|
39
|
+
files:
|
40
|
+
- LICENSE
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- app/controllers/devise_code_authenticatable/sessions_controller.rb
|
44
|
+
- config/locales/zh-CN.yml
|
45
|
+
- lib/devise_code_authenticatable.rb
|
46
|
+
- lib/devise_code_authenticatable/controllers/helpers.rb
|
47
|
+
- lib/devise_code_authenticatable/mailer.rb
|
48
|
+
- lib/devise_code_authenticatable/mapping.rb
|
49
|
+
- lib/devise_code_authenticatable/models/code_authenticatable.rb
|
50
|
+
- lib/devise_code_authenticatable/rails.rb
|
51
|
+
- lib/devise_code_authenticatable/strategies/code_authenticatable.rb
|
52
|
+
- lib/devise_code_authenticatable/version.rb
|
53
|
+
homepage: https://rubygems.org/gems/devise_code_authenticatable
|
54
|
+
licenses:
|
55
|
+
- MIT
|
56
|
+
metadata:
|
57
|
+
allowed_push_host: https://rubygems.org
|
58
|
+
source_code_uri: https://github.com/vincentying15/devise_code_authenticatable
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubygems_version: 3.0.3
|
75
|
+
signing_key:
|
76
|
+
specification_version: 4
|
77
|
+
summary: devise_code_authenticatable
|
78
|
+
test_files: []
|