Class: NoPassword::Secret
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- NoPassword::Secret
- Defined in:
- app/models/nopassword/secret.rb
Constant Summary collapse
- DEFAULT_REMAINING_ATTEMPTS =
Maximum number of times that a verification can be attempted.
3
- DEFAULT_TIME_TO_LIVE =
How long can the code live until it expires a new code verification must be created
5.minutes
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#data ⇒ Object
Returns the value of attribute data.
-
#salt ⇒ Object
This is used to derive the ‘data_digest`, which finds the secret.
Class Method Summary collapse
- .digest_code(data_digest:, code:) ⇒ Object
- .digest_data(salt:, data:) ⇒ Object
- .find_by_digest(salt:, data:) ⇒ Object
Instance Method Summary collapse
- #decrement_remaining_attempts ⇒ Object
- #has_authentic_code? ⇒ Boolean
- #has_exceeded_attempts? ⇒ Boolean
- #has_expired? ⇒ Boolean
- #has_tampered_data? ⇒ Boolean
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
32 33 34 |
# File 'app/models/nopassword/secret.rb', line 32 def code @code end |
#data ⇒ Object
Returns the value of attribute data.
40 41 42 |
# File 'app/models/nopassword/secret.rb', line 40 def data @data end |
#salt ⇒ Object
This is used to derive the ‘data_digest`, which finds the secret.
13 14 15 |
# File 'app/models/nopassword/secret.rb', line 13 def salt @salt end |
Class Method Details
.digest_code(data_digest:, code:) ⇒ Object
83 84 85 86 87 88 |
# File 'app/models/nopassword/secret.rb', line 83 def self.digest_code(data_digest:, code:) return if code.nil? return if data_digest.nil? Digest::SHA256.hexdigest(data_digest + code) end |
.digest_data(salt:, data:) ⇒ Object
76 77 78 79 80 81 |
# File 'app/models/nopassword/secret.rb', line 76 def self.digest_data(salt:, data:) return if salt.nil? return if data.nil? Digest::SHA256.hexdigest(salt + data) end |
.find_by_digest(salt:, data:) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/nopassword/secret.rb', line 64 def self.find_by_digest(salt:, data:) return if salt.nil? return if data.nil? find_by(data_digest: digest_data(salt: salt, data: data)).tap do |secret| if secret secret.data = data secret.salt = salt end end end |
Instance Method Details
#decrement_remaining_attempts ⇒ Object
60 61 62 |
# File 'app/models/nopassword/secret.rb', line 60 def decrement_remaining_attempts decrement! :remaining_attempts end |
#has_authentic_code? ⇒ Boolean
56 57 58 |
# File 'app/models/nopassword/secret.rb', line 56 def has_authentic_code? self.code_digest == digest_code end |
#has_exceeded_attempts? ⇒ Boolean
48 49 50 |
# File 'app/models/nopassword/secret.rb', line 48 def has_exceeded_attempts? not remaining_attempts.positive? end |
#has_expired? ⇒ Boolean
44 45 46 |
# File 'app/models/nopassword/secret.rb', line 44 def has_expired? Time.current > expires_at end |
#has_tampered_data? ⇒ Boolean
52 53 54 |
# File 'app/models/nopassword/secret.rb', line 52 def has_tampered_data? self.data_digest != digest_data if persisted? end |