Module: ZeroCaptcha::SpamProtection

Defined in:
lib/zero-captcha.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



28
29
30
31
32
33
34
35
36
# File 'lib/zero-captcha.rb', line 28

def self.included(base) # :nodoc:
  base.send :helper_method, :zero_captcha_fields

  if base.respond_to? :before_action
    base.send :prepend_before_action, :protect_from_spam_with_zero_captcha, :only => [:create, :update]
  elsif base.respond_to? :before_filter
    base.send :prepend_before_filter, :protect_from_spam_with_zero_captcha, :only => [:create, :update]
  end
end

Instance Method Details

#protect_from_spam_with_zero_captchaObject

change this in controller by overriding ‘zero_captcha_fields` to force forms to feed through specific values



17
18
19
20
21
22
# File 'lib/zero-captcha.rb', line 17

def protect_from_spam_with_zero_captcha
  if request.post?
    head :ok if params[:_zc_timestamp].present? && Time.at(params[:_zc_timestamp].to_i) < 20.minutes.ago 
    head :ok if params[:_zc_field].present? && params[:_zc_field] != OpenSSL::HMAC.hexdigest('sha256', Rails.application.secret_key_base, "#{params[:_zc_timestamp]}")
  end
end

#require_zero_captchaObject



24
25
26
# File 'lib/zero-captcha.rb', line 24

def require_zero_captcha
  head :ok if zero_captcha_fields.any? { |name, value| !params.has_key?(name) }
end

#zero_captcha_fieldsObject

change this in controller by overriding ‘zero_captcha_fields` to force forms to feed through specific values



7
8
9
10
11
12
13
# File 'lib/zero-captcha.rb', line 7

def zero_captcha_fields
  timestamp = Time.current.to_i
  return { 
    :_zc_field => OpenSSL::HMAC.hexdigest('sha256', Rails.application.secret_key_base, "#{timestamp}"),
    :_zc_timestamp => timestamp
  } 
end