acts_as_recaptcha 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,55 @@
1
+
2
+ =Acts As Recaptcha
3
+ acts_as_recaptcha is a fork of recaptcha, and a rather shameless copy of the method used in the awesome acts_as_textcaptcha gem. This collab brings recaptcha integration with a rails app almost as easy as the integration of the textcaptcha gem, minus some annoying stuff forced by google's horrible recaptcha API.
4
+
5
+ ==Usage
6
+ install: in your gemfile:
7
+
8
+ gem "acts_as_recaptcha", :git => "git://github.com/unorthodoxgeek/acts_as_recaptcha.git"
9
+
10
+
11
+ first, create a recaptcha.yml file in your config folder, put your private and public keys you get from https://www.google.com/recaptcha/admin/create
12
+
13
+ development:
14
+ private_key: "priv_key"
15
+ public_key: "pub_key"
16
+ locale: "he"
17
+ test:
18
+ private_key: "priv_key"
19
+ public_key: "pub_key"
20
+ production:
21
+ private_key: "priv_key"
22
+ public_key: "pub_key"
23
+
24
+ in the model, simply declare acts_as_recaptcha. This will add the neccesary validations. If you wish to add some logic to showing the captcha, you can override the perform_textcaptcha? method:
25
+
26
+
27
+ def perform textcaptcha?
28
+ return (true or false)
29
+ end
30
+
31
+
32
+ did I mention the recaptcha plugin sucks? well, it does. there's no problem to fill in the answer in the right place, but the encrypted answer to check against has to be inside a form element named "recaptcha_challenge_field", which can't be renamed (I tried...), so when we create the object in the controller, we need to put that in the right place. so in the create / update of your object you need to do something like this:
33
+
34
+ message_params = params[:message].merge({:recaptcha_challenge => params[:recaptcha_challenge_field], :recaptcha_remote_ip => request.remote_ip })
35
+ Message.create(message_params)
36
+
37
+
38
+ ==To show the captcha:
39
+ inside the <%= form_for(whatever) do |f| %> declare a second block:
40
+
41
+ <%= recaptcha_fields(f) do -%>
42
+ <%= recaptcha_image %>
43
+ <%= f.text_field :recaptcha_answer, :id => "recaptcha_response_field", :dir => "ltr" %> #note that the ID must be "recaptcha_response_field"
44
+ <a href="javascript:Recaptcha.reload()>Too hard! give me another!</a>
45
+ <% end %>
46
+
47
+ it's very recommended you add the refresh element, because recaptcha can be quite impossible to solve for normal mortal beings. you can use anything inside that link, of course.
48
+ This gem uses the custom theme, so you can design it however you wish.
49
+
50
+ ==Want to be an awesome person?
51
+ this gem is very simplistic now, and could use more work, If you want to add some awesomeness, you're more than welcome to fork, do your thing and issue a pull request.
52
+ here's a short TODO list:
53
+ * Write tests
54
+ * Add option to use recaptcha's default themes, right now we use the custom theme
55
+ * Make the gem backward compatible with Rails 2.x
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "acts_as_recaptcha/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "acts_as_recaptcha"
7
+ s.version = ActsAsRecaptcha::VERSION
8
+ s.authors = ["Tom Caspy"]
9
+ s.email = ["tom@sir.co.il"]
10
+ s.homepage = ""
11
+ s.summary = %q{Simple recaptcha plugin}
12
+ s.description = %q{Simple recaptcha plugin which allows to add reCAPTCHA validations to your forms}
13
+
14
+ s.rubyforge_project = "acts_as_recaptcha"
15
+
16
+ s.add_dependency "rails", "~> 3.1.0"
17
+ s.add_development_dependency "rspec", "~> 2.6"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ # s.test_files = `git ls-files -- {spec}/*`.split("\n")
21
+ s.require_paths = ["lib"]
22
+ end
@@ -0,0 +1,4 @@
1
+ require "acts_as_recaptcha/recaptcha_helper"
2
+ require "acts_as_recaptcha/validator"
3
+ require "acts_as_recaptcha/acts_as_recaptcha"
4
+ require "acts_as_recaptcha/rails"
@@ -0,0 +1,63 @@
1
+ module ActsAsRecaptcha
2
+ module Recaptcha
3
+
4
+ def acts_as_recaptcha(options = nil)
5
+ cattr_accessor :recaptcha_config
6
+ attr_accessor :recaptcha_answer, :recaptcha_challenge, :skip_recaptcha, :recaptcha_remote_ip
7
+
8
+ if respond_to?(:accessible_attributes)
9
+ if accessible_attributes.nil?
10
+ attr_protected :skip_recaptcha
11
+ else
12
+ attr_accessible :recaptcha_answer, :recaptcha_challenge, :recaptcha_remote_ip
13
+ end
14
+ end
15
+
16
+ validate :validate_recaptcha
17
+
18
+ if options.is_a?(Hash)
19
+ self.recaptcha_config = options.symbolize_keys!
20
+ else
21
+ begin
22
+ self.recaptcha_config = YAML.load(File.read("#{Rails.root ? Rails.root.to_s : '.'}/config/recaptcha.yml"))[Rails.env].symbolize_keys!
23
+ rescue
24
+ raise 'could not find any recaptcha options, in config/recaptcha.yml or model'
25
+ end
26
+ end
27
+
28
+ include InstanceMethods
29
+ end
30
+
31
+ module InstanceMethods
32
+
33
+ # rewrite this method in your model if you want to add logic
34
+ # to whether to show this of not.
35
+
36
+ def perform_recaptcha?
37
+ true
38
+ end
39
+
40
+ protected
41
+
42
+ def validate_recaptcha
43
+ # only spam check on new/unsaved records (ie. no spam check on updates/edits)
44
+ if !respond_to?('new_record?') || new_record?
45
+ if perform_recaptcha? && !validate_recaptcha_answer
46
+ errors.add(:recaptcha_answer, :incorrect_answer, :message => "is incorrect, try another question instead")
47
+ return false
48
+ end
49
+ end
50
+ true
51
+ end
52
+
53
+ def validate_recaptcha_answer
54
+ status, error = Validator.validate_recaptcha(recaptcha_challenge, recaptcha_answer, recaptcha_remote_ip, self.recaptcha_config[:private_key])
55
+ if status == "false"
56
+ return false
57
+ end
58
+ return true
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,4 @@
1
+ ActiveRecord::Base.extend ActsAsRecaptcha::Recaptcha
2
+ ActiveSupport.on_load(:action_view) do
3
+ include ActsAsRecaptcha::RecaptchaHelper
4
+ end
@@ -0,0 +1,44 @@
1
+ module ActsAsRecaptcha
2
+ module RecaptchaHelper
3
+
4
+ def recaptcha_image
5
+ "<div id=\"recaptcha_image\"></div>".html_safe
6
+ end
7
+
8
+ def recaptcha_fields(f, &block)
9
+ model = f.object
10
+ captcha_html = "
11
+ <script type=\"text/javascript\">
12
+ var RecaptchaOptions = {
13
+ #{"lang: '#{model.recaptcha_config[:locale]}'," if model.recaptcha_config[:locale].present?}
14
+ theme : 'custom',
15
+ custom_theme_widget: 'recaptcha_widget'
16
+ };
17
+
18
+ </script>
19
+ <div id=\"recaptcha_widget\" style=\"display:none\">
20
+ "
21
+ captcha_scripts = "<script type=\"text/javascript\"
22
+ src=\"http://www.google.com/recaptcha/api/challenge?k=#{model.recaptcha_config[:public_key]}\">
23
+ </script>
24
+ <noscript>
25
+ <iframe src=\"http://www.google.com/recaptcha/api/noscript?k=#{model.recaptcha_config[:public_key]}\"
26
+ height=\"300\" width=\"500\" frameborder=\"0\"></iframe>
27
+ <input type=\"hidden\" name=\"recaptcha_response_field\" value=\"manual_challenge\">
28
+ </noscript>"
29
+ if model.perform_recaptcha?
30
+ captcha_html += capture(&block)
31
+ captcha_html += "</div>"
32
+ captcha_html += captcha_scripts
33
+ end
34
+
35
+ # Rails 2 compatability
36
+ if Rails::VERSION::MAJOR < 3
37
+ concat captcha_html, &block.binding
38
+ else
39
+ captcha_html.html_safe
40
+ end
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,18 @@
1
+ require "net/http"
2
+ require "uri"
3
+
4
+ module ActsAsRecaptcha
5
+ module Validator
6
+ def self.validate_recaptcha(challenge, response, remoteip, private_key = ENV['RECAPTCHA_PRIVATE_KEY'] )
7
+ uri = URI.parse("http://www.google.com/recaptcha/api/verify")
8
+ params = {
9
+ :privatekey => private_key,
10
+ :challenge => challenge,
11
+ :response => response,
12
+ :remoteip => remoteip
13
+ }
14
+
15
+ Net::HTTP.post_form(uri, params).body.split("\n")
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsRecaptcha
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,3 @@
1
+ $: << File.expand_path("../../lib", __FILE__)
2
+
3
+ require "recaptcha"
@@ -0,0 +1,40 @@
1
+ require "spec_helper"
2
+ require "rspec/mocks/standalone"
3
+
4
+ describe Recaptcha::Validator do
5
+ describe "validate_recaptcha" do
6
+ it "takes 3 args" do
7
+ expect{ Recaptcha::Validator.validate_recaptcha("first", "second", "third") }.to_not raise_error(ArgumentError)
8
+ end
9
+
10
+ context "returns a series of strings seperated by \\n" do
11
+ context "when captcha solution correct" do
12
+ it "returns true" do
13
+ Net::HTTP.stub_chain(:post_form, :body).and_return("true")
14
+ Recaptcha::Validator.validate_recaptcha("1", "2", "3").should == ["true"]
15
+ end
16
+ end
17
+
18
+ context "when incorrect recaptcha key" do
19
+ it "returns false and invalid-site-private-key" do
20
+ Net::HTTP.stub_chain(:post_form, :body).and_return("false\ninvalid-site-private-key")
21
+ Recaptcha::Validator.validate_recaptcha("1", "2", "3").should == ["false", "invalid-site-private-key"]
22
+ end
23
+ end
24
+
25
+ context "when challenge param incorrect" do
26
+ it "returns false and invalid-request-cookie" do
27
+ Net::HTTP.stub_chain(:post_form, :body).and_return("false\ninvalid-request-cookie")
28
+ Recaptcha::Validator.validate_recaptcha("1", "2", "3").should == ["false", "invalid-request-cookie"]
29
+ end
30
+ end
31
+
32
+ context "when captcha solution incorrect" do
33
+ it "returns false and incorrect-captcha-sol" do
34
+ Net::HTTP.stub_chain(:post_form, :body).and_return("false\nincorrect-captcha-sol")
35
+ Recaptcha::Validator.validate_recaptcha("1", "2", "3").should == ["false", "incorrect-captcha-sol"]
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_recaptcha
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tom Caspy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-06 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70247500577860 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70247500577860
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70247500577360 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '2.6'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70247500577360
36
+ description: Simple recaptcha plugin which allows to add reCAPTCHA validations to
37
+ your forms
38
+ email:
39
+ - tom@sir.co.il
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - README.rdoc
47
+ - Rakefile
48
+ - acts_as_recaptcha.gemspec
49
+ - lib/acts_as_recaptcha.rb
50
+ - lib/acts_as_recaptcha/acts_as_recaptcha.rb
51
+ - lib/acts_as_recaptcha/rails.rb
52
+ - lib/acts_as_recaptcha/recaptcha_helper.rb
53
+ - lib/acts_as_recaptcha/validator.rb
54
+ - lib/acts_as_recaptcha/version.rb
55
+ - spec/spec_helper.rb
56
+ - spec/validator_spec.rb
57
+ homepage: ''
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project: acts_as_recaptcha
77
+ rubygems_version: 1.8.10
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Simple recaptcha plugin
81
+ test_files: []