Class: Tkellem::Recaptcha

Inherits:
Object
  • Object
show all
Defined in:
lib/tkellem/plugins/recaptcha.rb

Instance Method Summary collapse

Constructor Details

#initialize(public_key, private_key) ⇒ Recaptcha



6
7
8
9
10
# File 'lib/tkellem/plugins/recaptcha.rb', line 6

def initialize(public_key, private_key)
  @public_key = public_key
  @private_key = private_key
  @secret = ActiveSupport::SecureRandom.hex(16)
end

Instance Method Details

#challenge_urlObject



12
13
14
# File 'lib/tkellem/plugins/recaptcha.rb', line 12

def challenge_url
  "http://www.google.com/recaptcha/mailhide/d?k=#{@public_key}&c=#{url_encoded_secret}"
end

#encrypted_secretObject



16
17
18
19
20
21
22
23
24
# File 'lib/tkellem/plugins/recaptcha.rb', line 16

def encrypted_secret
  cipher = OpenSSL::Cipher::Cipher.new('aes-128-cbc')
  cipher.encrypt
  cipher.iv = "\x00"*16
  cipher.key = [@private_key].pack("H*") # private key is a hex string
  data = cipher.update(@secret)
  data << cipher.final
  data
end

#url_encoded_secretObject



26
27
28
# File 'lib/tkellem/plugins/recaptcha.rb', line 26

def url_encoded_secret
  [encrypted_secret].pack('m').strip.gsub("\n", '').tr('+/', '-_')
end

#valid_response?(response) ⇒ Boolean



30
31
32
# File 'lib/tkellem/plugins/recaptcha.rb', line 30

def valid_response?(response)
  response.strip == @secret
end