Class: SpamProtect::Guardian

Inherits:
Object
  • Object
show all
Defined in:
lib/spam_protect/guardian.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(honeypot_value, encrypted_timestamp, cookie, min_seconds) ⇒ Guardian



7
8
9
10
11
12
13
# File 'lib/spam_protect/guardian.rb', line 7

def initialize(honeypot_value, encrypted_timestamp, cookie, min_seconds)
  @honeypot_value = honeypot_value
  @encrypted_timestamp = encrypted_timestamp
  @cookie = cookie
  @min_seconds = min_seconds
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/spam_protect/guardian.rb', line 5

def errors
  @errors
end

Instance Method Details

#valid?Boolean



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/spam_protect/guardian.rb', line 15

def valid?
  payload = Encryption::Payload.new(
    Encryption.decrypt(@encrypted_timestamp)
  )

  cookie_payload = if SpamProtect.config.require_js
    Encryption::Payload.new(
      Encryption.decrypt(@cookie)
    )
  end

  payloads = [
    payload,
    cookie_payload
  ].compact

  honeypot_policy = Policies::HoneypotPolicy.new(@honeypot_value)
  if honeypot_policy.invalid?
    @errors.append "Honeypot field is filled in"
    return false
  end

  cookie_policy = Policies::CookiePolicy.new(@cookie)
  if cookie_policy.invalid?
    @errors.append "Cookie is invalid"
    return false
  end

  payloads.each do |p|
    encryption_policy = Policies::EncryptionPolicy.new(p)
    if encryption_policy.invalid?
      @errors.append "Payload encryption is invalid or expired"
      return false
    end

    timestamp_policy = Policies::TimestampPolicy.new(p["timestamp"], @min_seconds)
    if timestamp_policy.invalid?
      @errors.append "Form submitted too quickly"
      return false
    end
  end

  true
rescue
  @errors.append "Encryption failure"
  false
end