one_time_password 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 341d54de2197c31d888d896395ae79c514d3d7899a27637c66d8ec9899e8cf97
4
- data.tar.gz: 1f6346bd6e38f32a52394c0ccb37e3058fcd956ef3878ef69f214bcc67ac617c
3
+ metadata.gz: 4d2150c35f0f1278b3d5d57455fdecdfe820b8bc90a3dfe8eebd755f38201423
4
+ data.tar.gz: ef926487c185fd3dbf2ed3f27748c8c97cf52042d3945f7912a20c29bb08acdb
5
5
  SHA512:
6
- metadata.gz: 2c76a03d196b10156ca63236f4fe835f3b63223bcadf63233e4b68bb2c7aebb196e4cb34ab99235a4418dbe7418c90aa3e31a3760e23df766c54971b9b24dc2b
7
- data.tar.gz: 5750577c5bc3fb5c3bd13b1d39302da51e60867bf154168e69bc360c5907468d6c81fa2ac55faaceb4d4eea00c36f2f7a90dd3c99ffdca1c9de1a11efe999d2e
6
+ metadata.gz: bdaea968efa8a0d2f1907c5b5e30513aeb9e1d5389df785c8f9523a1d03e74d6d6d2ba66ea12f7f24c1ac3117800f00c079113b4e484828f0242b8f4286abef8
7
+ data.tar.gz: 2b02bb4f76bb1987866c8e2f660f6bf3fc31b73b0ed61e50064f63d1fa4ff5d559414e32700617178ba129f0a98792a671b746393fa460e78bba6653ab0b329d
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) | Name each function. |
56
- | version (Integer) | Version each function_name. |
57
- | expires_in (ActiveSupport::Duration) | Password validity time. |
58
- | max_authenticate_password_count (Integer) | Number of times user can enter password each generated password. |
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
@@ -2,7 +2,6 @@ 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
6
5
  t.string :user_key, null: false, index: true
7
6
  t.string :client_token
8
7
  t.integer :password_length, null: false
@@ -0,0 +1,5 @@
1
+ module OneTimePassword
2
+ module Errors
3
+ class NoUserKeyArgmentError < StandardError; end
4
+ end
5
+ end
@@ -17,32 +17,36 @@ module OneTimePassword
17
17
  end
18
18
 
19
19
  module ClassMethods
20
- def find_context(function_name, version: 0)
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.new('Not found context.')
28
+ raise ArgumentError, 'Not found context.'
30
29
  elsif context[:expires_in].class != ActiveSupport::Duration
31
- raise RuntimeError.new('Mistake OneTimePassword::CONTEXTS[:expires_in]')
30
+ raise RuntimeError, 'Mistake OneTimePassword::CONTEXTS[:expires_in].'
32
31
  elsif context[:max_authenticate_password_count].class != Integer
33
- raise RuntimeError.new('Mistake OneTimePassword::CONTEXTS[:max_authenticate_password_count]')
32
+ raise RuntimeError, 'Mistake OneTimePassword::CONTEXTS[:max_authenticate_password_count].'
34
33
  elsif context[:password_length].class != Integer
35
- raise RuntimeError.new('Mistake OneTimePassword::CONTEXTS[:password_length]')
34
+ raise RuntimeError, 'Mistake OneTimePassword::CONTEXTS[:password_length].'
36
35
  elsif context[:password_failed_limit].class != Integer
37
- raise RuntimeError.new('Mistake OneTimePassword::CONTEXTS[:password_failed_limit]')
36
+ raise RuntimeError, 'Mistake OneTimePassword::CONTEXTS[:password_failed_limit].'
38
37
  elsif context[:password_failed_period].class != ActiveSupport::Duration
39
- raise RuntimeError.new('Mistake OneTimePassword::CONTEXTS[:password_failed_period]')
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
@@ -1,3 +1,3 @@
1
1
  module OneTimePassword
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -1,3 +1,4 @@
1
1
  require "one_time_password/version"
2
2
  require "one_time_password/railtie"
3
+ require "one_time_password/errors"
3
4
  require "one_time_password/one_time_authentication_model"
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.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - yosipy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-02 00:00:00.000000000 Z
11
+ date: 2022-05-09 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