one_time_password 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -19
- data/lib/generators/one_time_password/templates/config/initializers/one_time_password.rb +0 -4
- data/lib/generators/one_time_password/templates/db/migrate/create_one_time_authentication.rb.erb +1 -1
- data/lib/one_time_password/errors.rb +5 -0
- data/lib/one_time_password/one_time_authentication_model.rb +19 -12
- data/lib/one_time_password/version.rb +1 -1
- data/lib/one_time_password.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc46ac36457860d5fef057d9f61f4248fda3a1bf86a14926ebc713f8349cc216
|
4
|
+
data.tar.gz: 030f37f6b244c8715d1899c0fbac377097e4e3f341e020bf0f235ad93d42dc52
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc716b7f866b17e2ed3c8aa4c484a771af590e62144fe768122f25d7c841404da4ba1182007fc3a6e118e09b3a76e1cc67c0634443043604056911d7543f69c7
|
7
|
+
data.tar.gz: 81edb4614b3bb1154797e9f84c8b45224d18f76239dcb14029fe9b9149f592f838e6ffc102f58ae0c33061978b46d2f66853e04f9c833f1aba0abcf37f829d9b
|
data/README.md
CHANGED
@@ -2,9 +2,8 @@
|
|
2
2
|
|
3
3
|
This Gem can be used to create 2FA (Two-Factor Authentication) function, email address verification function for member registration and etc in Ruby on Rails.
|
4
4
|
|
5
|
-
|
6
|
-
|
7
5
|
## Installation
|
6
|
+
|
8
7
|
Add this line to your application's Gemfile:
|
9
8
|
|
10
9
|
```ruby
|
@@ -12,17 +11,17 @@ gem "one_time_password"
|
|
12
11
|
```
|
13
12
|
|
14
13
|
And then execute:
|
14
|
+
|
15
15
|
```bash
|
16
16
|
$ bundle
|
17
17
|
```
|
18
18
|
|
19
19
|
Or install it yourself as:
|
20
|
+
|
20
21
|
```bash
|
21
22
|
$ gem install one_time_password
|
22
23
|
```
|
23
24
|
|
24
|
-
|
25
|
-
|
26
25
|
## Usage
|
27
26
|
|
28
27
|
### Run command for an installation.
|
@@ -32,6 +31,7 @@ bundle exec rails g one_time_password:install
|
|
32
31
|
```
|
33
32
|
|
34
33
|
The following events will take place when using the install generator:
|
34
|
+
|
35
35
|
- An initializer file will be created at `config/initializers/one_time_password.rb`
|
36
36
|
- A migration file will be created at `db/migrate/xxxxxxxxxxxxxx_create_one_time_authentication.rb`
|
37
37
|
- A model file will be created at `app/models/one_time_authentication.rb`
|
@@ -46,29 +46,26 @@ bundle exec rails db:migrate
|
|
46
46
|
|
47
47
|
Configuration in `config/initializers/one_time_password.rb`.
|
48
48
|
|
49
|
-
`FUNCTION_NAMES`: Using function_name in OneTimeAuthentication Model enum.
|
50
|
-
|
49
|
+
`FUNCTION_NAMES`: Using function_name in OneTimeAuthentication Model enum.
|
51
50
|
|
52
51
|
Hash, one of `CONTEXTS`:
|
53
|
-
|
|
52
|
+
| | |
|
54
53
|
| --- | --- |
|
55
|
-
| function_name (Symbol)
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
| password_length (Integer) | Password length. At 6, for example, the password would be 123456. |
|
54
|
+
| function_name (Symbol) | Name each function. |
|
55
|
+
| expires_in (ActiveSupport::Duration) | Password validity time. |
|
56
|
+
| max_authenticate_password_count (Integer) | Number of times user can enter password each generated password. |
|
57
|
+
| password_length (Integer) | Password length. At 6, for example, the password would be 123456. |
|
60
58
|
| password_failed_limit (Integer)<br>password_failed_period (ActiveSupport::Duration) | If you try to authenticate with the wrong password a password_failed_limit times within the time set by password_failed_period, you will not be able to generate a new password. |
|
61
|
-
|
|
59
|
+
| | |
|
62
60
|
|
63
61
|
### See example and its sequence diagram
|
62
|
+
|
64
63
|
[here](#example-and-its-sequence-diagram)
|
65
64
|
|
66
65
|
### `OneTimePassword::OneTimeAuthentication`'s methods.
|
67
66
|
|
68
67
|
For more information, see the [implementation of OneTimePassword :: OneTimeAuthenticationModel](https://github.com/yosipy/one_time_password/blob/main/lib/one_time_password/one_time_authentication_model.rb).
|
69
68
|
|
70
|
-
|
71
|
-
|
72
69
|
## Example and its sequence diagram
|
73
70
|
|
74
71
|
See [sign up exsample](https://github.com/yosipy/one_time_password/blob/main/spec/dummy/app/controllers/test_users_controller.rb).
|
@@ -77,12 +74,9 @@ Sequence diagram.
|
|
77
74
|
|
78
75
|
![sequence diagram image](document/sequence_diagram/sequencediagram.png)
|
79
76
|
|
80
|
-
|
81
|
-
|
82
77
|
<!-- ## Contributing
|
83
78
|
Contribution directions go here. -->
|
84
79
|
|
85
|
-
|
86
|
-
|
87
80
|
## License
|
81
|
+
|
88
82
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -17,7 +17,6 @@ module OneTimePassword
|
|
17
17
|
|
18
18
|
# {
|
19
19
|
# function_name (Symbol): Name each function.
|
20
|
-
# version (Integer): Version each function_name.
|
21
20
|
# expires_in (ActiveSupport::Duration): Password validity time.
|
22
21
|
# max_authenticate_password_count (Integer): Number of times user can enter password each generated password.
|
23
22
|
# password_length (Integer): Password length. At 6, for example, the password would be 123456.
|
@@ -28,7 +27,6 @@ module OneTimePassword
|
|
28
27
|
CONTEXTS = [
|
29
28
|
{
|
30
29
|
function_name: :sign_up,
|
31
|
-
version: 0,
|
32
30
|
expires_in: 30.minutes,
|
33
31
|
max_authenticate_password_count: 5,
|
34
32
|
password_length: 6,
|
@@ -37,7 +35,6 @@ module OneTimePassword
|
|
37
35
|
},
|
38
36
|
{
|
39
37
|
function_name: :sign_in,
|
40
|
-
version: 0,
|
41
38
|
expires_in: 30.minutes,
|
42
39
|
max_authenticate_password_count: 5,
|
43
40
|
password_length: 10,
|
@@ -46,7 +43,6 @@ module OneTimePassword
|
|
46
43
|
},
|
47
44
|
# {
|
48
45
|
# function_name: :change_email,
|
49
|
-
# version: 0,
|
50
46
|
# expires_in: 30.minutes,
|
51
47
|
# max_authenticate_password_count: 5,
|
52
48
|
# password_length: 6
|
data/lib/generators/one_time_password/templates/db/migrate/create_one_time_authentication.rb.erb
CHANGED
@@ -2,7 +2,7 @@ class CreateOneTimeAuthentication < ActiveRecord::Migration<%= migration_version
|
|
2
2
|
def change
|
3
3
|
create_table :one_time_authentications do |t|
|
4
4
|
t.integer :function_name, null: false
|
5
|
-
t.integer :version, null: false, index: true
|
5
|
+
t.integer :version, null: false, index: true, default: 0
|
6
6
|
t.string :user_key, null: false, index: true
|
7
7
|
t.string :client_token
|
8
8
|
t.integer :password_length, null: false
|
@@ -17,32 +17,36 @@ module OneTimePassword
|
|
17
17
|
end
|
18
18
|
|
19
19
|
module ClassMethods
|
20
|
-
def find_context(function_name
|
20
|
+
def find_context(function_name)
|
21
21
|
context = OneTimePassword::CONTEXTS
|
22
22
|
.select{ |context|
|
23
|
-
context[:function_name] == function_name
|
24
|
-
context[:version] == version
|
23
|
+
context[:function_name] == function_name
|
25
24
|
}
|
26
25
|
.first
|
27
|
-
|
26
|
+
|
28
27
|
if context.nil?
|
29
|
-
raise ArgumentError
|
28
|
+
raise ArgumentError, 'Not found context.'
|
30
29
|
elsif context[:expires_in].class != ActiveSupport::Duration
|
31
|
-
raise RuntimeError
|
30
|
+
raise RuntimeError, 'Mistake OneTimePassword::CONTEXTS[:expires_in].'
|
32
31
|
elsif context[:max_authenticate_password_count].class != Integer
|
33
|
-
raise RuntimeError
|
32
|
+
raise RuntimeError, 'Mistake OneTimePassword::CONTEXTS[:max_authenticate_password_count].'
|
34
33
|
elsif context[:password_length].class != Integer
|
35
|
-
raise RuntimeError
|
34
|
+
raise RuntimeError, 'Mistake OneTimePassword::CONTEXTS[:password_length].'
|
36
35
|
elsif context[:password_failed_limit].class != Integer
|
37
|
-
raise RuntimeError
|
36
|
+
raise RuntimeError, 'Mistake OneTimePassword::CONTEXTS[:password_failed_limit].'
|
38
37
|
elsif context[:password_failed_period].class != ActiveSupport::Duration
|
39
|
-
raise RuntimeError
|
38
|
+
raise RuntimeError, 'Mistake OneTimePassword::CONTEXTS[:password_failed_period].'
|
40
39
|
end
|
41
40
|
|
42
41
|
context
|
43
42
|
end
|
44
43
|
|
45
44
|
def create_one_time_authentication(context, user_key, user_key_downcase: true)
|
45
|
+
if user_key.blank?
|
46
|
+
raise OneTimePassword::Errors::NoUserKeyArgmentError,
|
47
|
+
'Not present user_key.'
|
48
|
+
end
|
49
|
+
|
46
50
|
user_key = user_key.downcase if user_key_downcase
|
47
51
|
|
48
52
|
recent_failed_authenticate_password_count =
|
@@ -55,7 +59,6 @@ module OneTimePassword
|
|
55
59
|
if recent_failed_authenticate_password_count <= context[:password_failed_limit]
|
56
60
|
one_time_authentication = OneTimeAuthentication.new(
|
57
61
|
function_name: context[:function_name],
|
58
|
-
version: context[:version],
|
59
62
|
user_key: user_key,
|
60
63
|
password_length: context[:password_length],
|
61
64
|
expires_seconds: context[:expires_in].to_i,
|
@@ -71,11 +74,15 @@ module OneTimePassword
|
|
71
74
|
end
|
72
75
|
|
73
76
|
def find_one_time_authentication(context, user_key, user_key_downcase: true)
|
77
|
+
if user_key.blank?
|
78
|
+
raise OneTimePassword::Errors::NoUserKeyArgmentError,
|
79
|
+
'Not present user_key.'
|
80
|
+
end
|
81
|
+
|
74
82
|
user_key = user_key.downcase if user_key_downcase
|
75
83
|
|
76
84
|
OneTimeAuthentication
|
77
85
|
.where(function_name: context[:function_name])
|
78
|
-
.where(version: context[:version])
|
79
86
|
.where(user_key: user_key)
|
80
87
|
.last
|
81
88
|
end
|
data/lib/one_time_password.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: one_time_password
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yosipy
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-05-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -95,6 +95,7 @@ files:
|
|
95
95
|
- lib/generators/one_time_password/templates/config/initializers/one_time_password.rb
|
96
96
|
- lib/generators/one_time_password/templates/db/migrate/create_one_time_authentication.rb.erb
|
97
97
|
- lib/one_time_password.rb
|
98
|
+
- lib/one_time_password/errors.rb
|
98
99
|
- lib/one_time_password/one_time_authentication_model.rb
|
99
100
|
- lib/one_time_password/railtie.rb
|
100
101
|
- lib/one_time_password/version.rb
|