Class: NoPassword::Secret

Inherits:
ApplicationRecord
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#codeObject

Returns the value of attribute code.



32
33
34
# File 'app/models/nopassword/secret.rb', line 32

def code
  @code
end

#dataObject

Returns the value of attribute data.



40
41
42
# File 'app/models/nopassword/secret.rb', line 40

def data
  @data
end

#saltObject

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_attemptsObject



60
61
62
# File 'app/models/nopassword/secret.rb', line 60

def decrement_remaining_attempts
  decrement! :remaining_attempts
end

#has_authentic_code?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


48
49
50
# File 'app/models/nopassword/secret.rb', line 48

def has_exceeded_attempts?
  not remaining_attempts.positive?
end

#has_expired?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/models/nopassword/secret.rb', line 44

def has_expired?
  Time.current > expires_at
end

#has_tampered_data?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'app/models/nopassword/secret.rb', line 52

def has_tampered_data?
  self.data_digest != digest_data if persisted?
end