Class: Gringotts::AttemptValidator

Inherits:
Object
  • Object
show all
Defined in:
app/models/gringotts/attempt_validator.rb

Constant Summary collapse

CODE_FRESHNESS_LIMIT =
30.minutes
LOCKOUT_PERIOD =
30.minutes
MAX_UNSUCCESSFUL_ATTEMPTS =
3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attempt, code) ⇒ AttemptValidator

Returns a new instance of AttemptValidator.


12
13
14
15
# File 'app/models/gringotts/attempt_validator.rb', line 12

def initialize(attempt, code)
  @attempt = attempt
  @code    = code
end

Instance Attribute Details

#attemptObject (readonly)

Returns the value of attribute attempt


17
18
19
# File 'app/models/gringotts/attempt_validator.rb', line 17

def attempt
  @attempt
end

#codeObject (readonly)

Returns the value of attribute code


17
18
19
# File 'app/models/gringotts/attempt_validator.rb', line 17

def code
  @code
end

Class Method Details

.valid?(attempt) ⇒ Boolean

Returns:

  • (Boolean)

8
9
10
# File 'app/models/gringotts/attempt_validator.rb', line 8

def self.valid?(attempt)
  return AttemptValidator.new(attempt, attempt.vault.recent_code).successful?
end

Instance Method Details

#mark_successfulObject


41
42
43
# File 'app/models/gringotts/attempt_validator.rb', line 41

def mark_successful
  @attempt.successful = 1
end

#mark_unsuccessful(msg) ⇒ Object


36
37
38
39
# File 'app/models/gringotts/attempt_validator.rb', line 36

def mark_unsuccessful(msg)
  @attempt.successful = 0
  @attempt.errors[:validator] << msg
end

#matches?Boolean

Returns:

  • (Boolean)

53
54
55
# File 'app/models/gringotts/attempt_validator.rb', line 53

def matches?
  return @attempt.code_received == @code.value
end

#stale?Boolean

Returns:

  • (Boolean)

45
46
47
# File 'app/models/gringotts/attempt_validator.rb', line 45

def stale?
  return @code.expires_at < (Time.now - CODE_FRESHNESS_LIMIT)
end

#successful?Boolean

Returns:

  • (Boolean)

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/gringotts/attempt_validator.rb', line 19

def successful?
  
  if self.matches?
    if self.stale?
      self.mark_unsuccessful("Code expired")
    elsif self.used?
      self.mark_unsuccessful("Code already used")
    else
      self.mark_successful
    end
  else
    self.mark_unsuccessful("Invalid code")
  end
  
  return @attempt.successful?
end

#used?Boolean

Returns:

  • (Boolean)

49
50
51
# File 'app/models/gringotts/attempt_validator.rb', line 49

def used?
  return Gringotts::Attempt.where(vault_id: @attempt.vault_id, code_received: @attempt.code_received, successful: true).count > 0
end