recaptcha-rails3 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,27 @@
1
+ == 0.3.3 / 2011-12-3
2
+
3
+ * Forked and renamed to be released on Rubygems as a different, updated gem
4
+
5
+ == 0.2.2 / 2009-09-14
6
+
7
+ * Add a timeout to the validator
8
+ * Give the documentation some love
9
+
10
+ == 0.2.1 / 2009-09-14
11
+
12
+ * Removed Ambethia namespace, and restructured classes a bit
13
+ * Added an example rails app in the example-rails branch
14
+
15
+ == 0.2.0 / 2009-09-12
16
+
17
+ * RecaptchaOptions AJAX API Fix
18
+ * Added 'cucumber' as a test environment to skip
19
+ * Ruby 1.9 compat fixes
20
+ * Added option :message => 'Custom error message' to verify_recaptcha
21
+ * Removed dependency on ActiveRecord constant
22
+ * Add I18n
23
+
24
+ == 0.1.0 / 2008-2-8
25
+
26
+ * 1 major enhancement
27
+ * Initial Gem Release
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2007 Jason L Perry
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,150 @@
1
+ = reCAPTCHA
2
+
3
+ Author:: Jason L Perry (http://ambethia.com)
4
+ Copyright:: Copyright (c) 2007 Jason L Perry
5
+ License:: {MIT}[http://creativecommons.org/licenses/MIT/]
6
+ Info:: http://ambethia.com/recaptcha
7
+ Git:: http://github.com/ambethia/recaptcha/tree/master
8
+ Bugs:: http://github.com/ambethia/recaptcha/issues
9
+
10
+ This plugin adds helpers for the {reCAPTCHA API}[http://recaptcha.net]. In your
11
+ views you can use the +recaptcha_tags+ method to embed the needed javascript,
12
+ and you can validate in your controllers with +verify_recaptcha+.
13
+
14
+ Beforehand you need to configure Recaptcha with your custom private and public
15
+ key. You may find detailed examples below. Exceptions will be raised if you
16
+ call these methods and the keys can't be found.
17
+
18
+ == About this fork
19
+
20
+ This fork tries to introduces a more convenient way to configure recaptcha's
21
+ settings. The API will be inspired by {Thoughtbot's
22
+ Hoptoad}[http://robots.thoughtbot.com/post/344833329/mygem-configure-block].
23
+
24
+ == Rails Installation
25
+
26
+ reCAPTCHA for Rails, add this to your Gemfile:
27
+
28
+ gem "recaptcha", :require => "recaptcha/rails"
29
+
30
+ Or, it can be installed as a gem:
31
+
32
+ config.gem "recaptcha", :lib => "recaptcha/rails"
33
+
34
+ Or, as a standard rails plugin:
35
+
36
+ script/plugin install git://github.com/ambethia/recaptcha.git
37
+
38
+ == Merb Installation
39
+
40
+ reCAPTCHA can also be used in a Merb application when installed as a gem:
41
+
42
+ dependency "alm-recaptcha", ">=0.2.2.1", :require_as => "recaptcha/merb"
43
+
44
+ Initial Merb compatability funded by ALM Labs.
45
+
46
+ == Setting up your API Keys
47
+
48
+ There are multiple ways to setup your reCAPTCHA API key once you
49
+ {obtain}[http://recaptcha.net/whyrecaptcha.html] a pair.
50
+
51
+ === Recaptcha.configure
52
+
53
+ You may use the block style configuration. The following code could be placed
54
+ into a +config/initializers/recaptcha.rb+ when used in a Rails project.
55
+
56
+ Recaptcha.configure do |config|
57
+ config.public_key = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
58
+ config.private_key = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
59
+ config.proxy = 'http://myrpoxy.com.au:8080'
60
+ end
61
+
62
+ This way, you may also set additional options to fit recaptcha into your
63
+ deployment environment.
64
+
65
+ == Recaptcha#with_configuration
66
+
67
+ If you want to temporarily overwrite the configuration you set with `Recaptcha.configure` (when testing, for example), you can use a `Recaptcha#with_configuration` block:
68
+
69
+ Recaptcha.configure(:public_key => '12345') do
70
+ # Do stuff with the overwritten public_key.
71
+ end
72
+
73
+ === Shell environment
74
+
75
+ Or, you can keep your keys out of your code base by exporting the following
76
+ environment variables. You might do this in the .profile/rc, or equivalent for
77
+ the user running your application:
78
+
79
+ export RECAPTCHA_PUBLIC_KEY = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
80
+ export RECAPTCHA_PRIVATE_KEY = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
81
+
82
+ === Per call
83
+
84
+ You can also pass in your keys as options at runtime, for example:
85
+
86
+ recaptcha_tags :public_key => '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
87
+
88
+ and later,
89
+
90
+ verify_recaptcha :private_key => '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
91
+
92
+ This option might be useful, if the same code base is used for multiple
93
+ reCAPTCHA setups.
94
+
95
+ == To use 'recaptcha'
96
+
97
+ Add +recaptcha_tags+ to each form you want to protect.
98
+
99
+ And, add +verify_recaptcha+ logic to each form action that you've protected.
100
+
101
+ === +recaptcha_tags+
102
+
103
+ Some of the options available:
104
+
105
+ <tt>:ssl</tt>:: Uses secure http for captcha widget (default +false+)
106
+ <tt>:noscript</tt>:: Include <noscript> content (default +true+)
107
+ <tt>:display</tt>:: Takes a hash containing the +theme+ and +tabindex+ options per the API. (default +nil+)
108
+ <tt>:ajax</tt>:: Render the dynamic AJAX captcha per the API. (default +false+)
109
+ <tt>:public_key</tt>:: Your public API key, takes precedence over the ENV variable (default +nil+)
110
+ <tt>:error</tt>:: Override the error code returned from the reCAPTCHA API (default +nil+)
111
+
112
+ You can also override the html attributes for the sizes of the generated +textarea+ and +iframe+
113
+ elements, if CSS isn't your thing. Inspect the source of +recaptcha_tags+ to see these options.
114
+
115
+ === +verify_recaptcha+
116
+
117
+ This method returns +true+ or +false+ after processing the parameters from the reCAPTCHA widget. Why
118
+ isn't this a model validation? Because that violates MVC. You can use it like this, or how ever you
119
+ like. Passing in the ActiveRecord object is optional, if you do--and the captcha fails to verify--an
120
+ error will be added to the object for you to use.
121
+
122
+ Some of the options available:
123
+
124
+ <tt>:model</tt>:: Model to set errors
125
+ <tt>:attribute</tt>:: Model attribute to receive errors (default :base)
126
+ <tt>:message</tt>:: Custom error message
127
+ <tt>:private_key</tt>:: Your private API key, takes precedence over the ENV variable (default +nil+).
128
+ <tt>:timeout</tt>:: The number of seconds to wait for reCAPTCHA servers before give up. (default +3+)
129
+
130
+ respond_to do |format|
131
+ if verify_recaptcha(:model => @post, :message => "Oh! It's error with reCAPTCHA!") && @post.save
132
+ # ...
133
+ else
134
+ # ...
135
+ end
136
+ end
137
+
138
+ == I18n support
139
+ reCAPTCHA passes two types of error explanation to a linked model. It will use the I18n gem
140
+ to translate the default error message if I18n is available. To customize the messages to your locale,
141
+ add these keys to your I18n backend:
142
+
143
+ <tt>recaptcha.errors.verification_failed</tt>:: error message displayed if the captcha words didn't match
144
+ <tt>recaptcha.errors.recaptcha_unavailable</tt>:: displayed if a timout error occured while attempting to verify the captcha
145
+
146
+ == TODO
147
+ * Remove Rails/ActionController dependencies
148
+ * Framework agnostic
149
+ * Add some helpers to use in before_filter and what not
150
+ * Better documentation
data/Rakefile ADDED
@@ -0,0 +1,60 @@
1
+ require 'rake'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gem|
6
+ gem.name = "recaptcha-rails3"
7
+ gem.description = "This plugin adds helpers for the reCAPTCHA API - Forked for Rails 3"
8
+ gem.summary = "Helpers for the reCAPTCHA API"
9
+ gem.homepage = "https://github.com/silviorelli/ry-recaptcha"
10
+ gem.authors = ["Jason L. Perry"]
11
+ gem.email = "jasper@ambethia.com"
12
+ gem.files.reject! { |fn| fn.include? ".gitignore" }
13
+ gem.add_development_dependency "mocha"
14
+ gem.add_development_dependency "activesupport"
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'rake/rdoctask'
22
+ Rake::RDocTask.new do |rd|
23
+ if File.exist?('VERSION.yml')
24
+ config = YAML.load(File.read('VERSION.yml'))
25
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
26
+ else
27
+ version = ""
28
+ end
29
+
30
+ rd.main = "README.rdoc"
31
+ rd.rdoc_files.include "README.rdoc", "LICENSE", "lib/**/*.rb"
32
+ rd.rdoc_dir = 'rdoc'
33
+ rd.options << '-N' # line numbers
34
+ rd.options << '-S' # inline source
35
+ end
36
+
37
+ require 'rake/testtask'
38
+ Rake::TestTask.new(:test) do |test|
39
+ test.libs << 'test'
40
+ test.pattern = 'test/**/*_test.rb'
41
+ # test.verbose = true
42
+ end
43
+
44
+ begin
45
+ require 'rcov/rcovtask'
46
+ Rcov::RcovTask.new do |test|
47
+ test.libs << 'test'
48
+ test.pattern = 'test/**/*_test.rb'
49
+ test.verbose = true
50
+ end
51
+ rescue LoadError
52
+ task :rcov do
53
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
54
+ end
55
+ end
56
+
57
+ task :default => :test
58
+
59
+
60
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.3
data/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ # Rails plugin initialization.
2
+ # You can also install it as a gem:
3
+ # config.gem "ambethia-recaptcha", :lib => "recaptcha/rails", :source => "http://gems.github.com"
4
+
5
+ require 'recaptcha/rails'
@@ -0,0 +1,51 @@
1
+ module Recaptcha
2
+ module ClientHelper
3
+ # Your public API can be specified in the +options+ hash or preferably
4
+ # using the Configuration.
5
+ def recaptcha_tags(options = {})
6
+ # Default options
7
+ key = options[:public_key] ||= Recaptcha.configuration.public_key
8
+ raise RecaptchaError, "No public key specified." unless key
9
+ error = options[:error] ||= (defined? flash ? flash[:recaptcha_error] : "")
10
+ uri = Recaptcha.configuration.api_server_url(options[:ssl])
11
+ html = ""
12
+ if options[:display]
13
+ html << %{<script type="text/javascript">\n}
14
+ html << %{ var RecaptchaOptions = #{options[:display].to_json};\n}
15
+ html << %{</script>\n}
16
+ end
17
+ if options[:ajax]
18
+ html << <<-EOS
19
+ <div id="dynamic_recaptcha"></div>
20
+ <script type="text/javascript">
21
+ var rc_script_tag = document.createElement('script'),
22
+ rc_init_func = function(){Recaptcha.create("#{key}", document.getElementById("dynamic_recaptcha")#{',RecaptchaOptions' if options[:display]});}
23
+ rc_script_tag.src = "#{uri}/js/recaptcha_ajax.js";
24
+ rc_script_tag.type = 'text/javascript';
25
+ rc_script_tag.onload = function(){rc_init_func.call();};
26
+ rc_script_tag.onreadystatechange = function(){
27
+ if (rc_script_tag.readyState == 'loaded' || rc_script_tag.readyState == 'complete') {rc_init_func.call();}
28
+ };
29
+ (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(rc_script_tag);
30
+ </script>
31
+ EOS
32
+ else
33
+ html << %{<script type="text/javascript" src="#{uri}/challenge?k=#{key}}
34
+ html << %{#{error ? "&amp;error=#{CGI::escape(error)}" : ""}"></script>\n}
35
+ unless options[:noscript] == false
36
+ html << %{<noscript>\n }
37
+ html << %{<iframe src="#{uri}/noscript?k=#{key}" }
38
+ html << %{height="#{options[:iframe_height] ||= 300}" }
39
+ html << %{width="#{options[:iframe_width] ||= 500}" }
40
+ html << %{style="border:none;"></iframe><br/>\n }
41
+ html << %{<textarea name="recaptcha_challenge_field" }
42
+ html << %{rows="#{options[:textarea_rows] ||= 3}" }
43
+ html << %{cols="#{options[:textarea_cols] ||= 40}"></textarea>\n }
44
+ html << %{<input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>}
45
+ html << %{</noscript>\n}
46
+ end
47
+ end
48
+ return (html.respond_to?(:html_safe) && html.html_safe) || html
49
+ end # recaptcha_tags
50
+ end # ClientHelper
51
+ end # Recaptcha
@@ -0,0 +1,53 @@
1
+ module Recaptcha
2
+ # This class enables detailed configuration of the recaptcha services.
3
+ #
4
+ # By calling
5
+ #
6
+ # Recaptcha.configuration # => instance of Recaptcha::Configuration
7
+ #
8
+ # or
9
+ # Recaptcha.configure do |config|
10
+ # config # => instance of Recaptcha::Configuration
11
+ # end
12
+ #
13
+ # you are able to perform configuration updates.
14
+ #
15
+ # Your are able to customize all attributes listed below. All values have
16
+ # sensitive default and will very likely not need to be changed.
17
+ #
18
+ # Please note that the public and private key for the reCAPTCHA API Access
19
+ # have no useful default value. The keys may be set via the Shell enviroment
20
+ # or using this configuration. Settings within this configuration always take
21
+ # precedence.
22
+ #
23
+ # Setting the keys with this Configuration
24
+ #
25
+ # Recaptcha.configure do |config|
26
+ # config.public_key = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
27
+ # config.private_key = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
28
+ # end
29
+ #
30
+ class Configuration
31
+ attr_accessor :nonssl_api_server_url,
32
+ :ssl_api_server_url,
33
+ :verify_url,
34
+ :skip_verify_env,
35
+ :private_key,
36
+ :public_key,
37
+ :proxy
38
+
39
+ def initialize #:nodoc:
40
+ @nonssl_api_server_url = RECAPTCHA_API_SERVER_URL
41
+ @ssl_api_server_url = RECAPTCHA_API_SECURE_SERVER_URL
42
+ @verify_url = RECAPTCHA_VERIFY_URL
43
+ @skip_verify_env = SKIP_VERIFY_ENV
44
+
45
+ @private_key = ENV['RECAPTCHA_PRIVATE_KEY']
46
+ @public_key = ENV['RECAPTCHA_PUBLIC_KEY']
47
+ end
48
+
49
+ def api_server_url(ssl = false) #:nodoc:
50
+ ssl ? ssl_api_server_url : nonssl_api_server_url
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,4 @@
1
+ require 'recaptcha'
2
+
3
+ Merb::GlobalHelpers.send(:include, Recaptcha::ClientHelper)
4
+ Merb::Controller.send(:include, Recaptcha::Verify)
@@ -0,0 +1,5 @@
1
+ require 'net/http'
2
+ require 'recaptcha'
3
+
4
+ ActionView::Base.send(:include, Recaptcha::ClientHelper)
5
+ ActionController::Base.send(:include, Recaptcha::Verify)
@@ -0,0 +1,15 @@
1
+ require 'net/http'
2
+ require 'recaptcha'
3
+ module Rails
4
+ module Recaptcha
5
+ class Railtie < Rails::Railtie
6
+ initializer "setup config" do
7
+ begin
8
+ ActionView::Base.send(:include, ::Recaptcha::ClientHelper)
9
+ ActionController::Base.send(:include, ::Recaptcha::Verify)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,61 @@
1
+ require "uri"
2
+ module Recaptcha
3
+ module Verify
4
+ # Your private API can be specified in the +options+ hash or preferably
5
+ # using the Configuration.
6
+ def verify_recaptcha(options = {})
7
+ if !options.is_a? Hash
8
+ options = {:model => options}
9
+ end
10
+
11
+ env = options[:env] || ENV['RAILS_ENV']
12
+ return true if Recaptcha.configuration.skip_verify_env.include? env
13
+ model = options[:model]
14
+ attribute = options[:attribute] || :base
15
+ private_key = options[:private_key] || Recaptcha.configuration.private_key
16
+ raise RecaptchaError, "No private key specified." unless private_key
17
+
18
+ begin
19
+ recaptcha = nil
20
+ if(Recaptcha.configuration.proxy)
21
+ proxy_server = URI.parse(Recaptcha.configuration.proxy)
22
+ http = Net::HTTP::Proxy(proxy_server.host, proxy_server.port, proxy_server.user, proxy_server.password)
23
+ else
24
+ http = Net::HTTP
25
+ end
26
+
27
+ Timeout::timeout(options[:timeout] || 3) do
28
+ recaptcha = http.post_form(URI.parse(Recaptcha.configuration.verify_url), {
29
+ "privatekey" => private_key,
30
+ "remoteip" => request.remote_ip,
31
+ "challenge" => params[:recaptcha_challenge_field],
32
+ "response" => params[:recaptcha_response_field]
33
+ })
34
+ end
35
+ answer, error = recaptcha.body.split.map { |s| s.chomp }
36
+ unless answer == 'true'
37
+ flash[:recaptcha_error] = error
38
+ if model
39
+ message = "Word verification response is incorrect, please try again."
40
+ message = I18n.translate(:'recaptcha.errors.verification_failed', {:default => message}) if defined?(I18n)
41
+ model.errors.add attribute, options[:message] || message
42
+ end
43
+ return false
44
+ else
45
+ flash[:recaptcha_error] = nil
46
+ return true
47
+ end
48
+ rescue Timeout::Error
49
+ flash[:recaptcha_error] = "recaptcha-not-reachable"
50
+ if model
51
+ message = "Oops, we failed to validate your word verification response. Please try again."
52
+ message = I18n.translate(:'recaptcha.errors.recaptcha_unreachable', :default => message) if defined?(I18n)
53
+ model.errors.add attribute, options[:message] || message
54
+ end
55
+ return false
56
+ rescue Exception => e
57
+ raise RecaptchaError, e.message, e.backtrace
58
+ end
59
+ end # verify_recaptcha
60
+ end # Verify
61
+ end # Recaptcha
data/lib/recaptcha.rb ADDED
@@ -0,0 +1,58 @@
1
+ require 'recaptcha/configuration'
2
+ require 'recaptcha/client_helper'
3
+ require 'recaptcha/verify'
4
+
5
+ module Recaptcha
6
+ module VERSION #:nodoc:
7
+ MAJOR = 0
8
+ MINOR = 2
9
+ TINY = 2
10
+ PATCH = 1
11
+
12
+ STRING = [MAJOR, MINOR, TINY, PATCH].join('.')
13
+ end
14
+
15
+
16
+ RECAPTCHA_API_SERVER_URL = 'http://www.google.com/recaptcha/api'
17
+ RECAPTCHA_API_SECURE_SERVER_URL = 'https://www.google.com/recaptcha/api'
18
+ RECAPTCHA_VERIFY_URL = 'http://www.google.com/recaptcha/api/verify'
19
+
20
+ SKIP_VERIFY_ENV = ['test', 'cucumber']
21
+
22
+ # Gives access to the current Configuration.
23
+ def self.configuration
24
+ @configuration ||= Configuration.new
25
+ end
26
+
27
+ # Allows easy setting of multiple configuration options. See Configuration
28
+ # for all available options.
29
+ #--
30
+ # The temp assignment is only used to get a nicer rdoc. Feel free to remove
31
+ # this hack.
32
+ #++
33
+ def self.configure
34
+ config = configuration
35
+ yield(config)
36
+ end
37
+
38
+ def self.with_configuration(config)
39
+ original_config = {}
40
+
41
+ config.each do |key, value|
42
+ original_config[key] = configuration.send(key)
43
+ configuration.send("#{key}=", value)
44
+ end
45
+
46
+ result = yield if block_given?
47
+
48
+ original_config.each { |key, value| configuration.send("#{key}=", value) }
49
+ result
50
+ end
51
+
52
+ class RecaptchaError < StandardError
53
+ end
54
+ end
55
+
56
+ if defined?(Rails)
57
+ require 'recaptcha/rails'
58
+ end
data/recaptcha.gemspec ADDED
@@ -0,0 +1,65 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{recaptcha}
8
+ s.version = "0.3.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jason L. Perry"]
12
+ s.date = %q{2010-12-20}
13
+ s.description = %q{This plugin adds helpers for the reCAPTCHA API }
14
+ s.email = %q{jasper@ambethia.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ "CHANGELOG",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "init.rb",
26
+ "lib/recaptcha.rb",
27
+ "lib/recaptcha/client_helper.rb",
28
+ "lib/recaptcha/configuration.rb",
29
+ "lib/recaptcha/merb.rb",
30
+ "lib/recaptcha/rails.rb",
31
+ "lib/recaptcha/verify.rb",
32
+ "recaptcha.gemspec",
33
+ "tasks/recaptcha_tasks.rake",
34
+ "test/recaptcha_test.rb",
35
+ "test/verify_recaptcha_test.rb"
36
+ ]
37
+ s.homepage = %q{http://ambethia.com/recaptcha}
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.7}
40
+ s.summary = %q{Helpers for the reCAPTCHA API}
41
+ s.test_files = [
42
+ "test/recaptcha_test.rb",
43
+ "test/verify_recaptcha_test.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
+ s.add_development_dependency(%q<mocha>, [">= 0"])
52
+ s.add_development_dependency(%q<activesupport>, [">= 0"])
53
+ s.add_dependency(%q<i18n>, [">= 0"])
54
+ else
55
+ s.add_dependency(%q<mocha>, [">= 0"])
56
+ s.add_dependency(%q<activesupport>, [">= 0"])
57
+ s.add_dependency(%q<i18n>, [">= 0"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<mocha>, [">= 0"])
61
+ s.add_dependency(%q<activesupport>, [">= 0"])
62
+ s.add_dependency(%q<i18n>, [">= 0"])
63
+ end
64
+ end
65
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :recaptcha do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,52 @@
1
+ require 'test/unit'
2
+ require 'cgi'
3
+ require File.dirname(File.expand_path(__FILE__)) + '/../lib/recaptcha'
4
+
5
+ class RecaptchaClientHelperTest < Test::Unit::TestCase
6
+ include Recaptcha
7
+ include Recaptcha::ClientHelper
8
+ include Recaptcha::Verify
9
+
10
+ attr_accessor :session
11
+
12
+ def setup
13
+ @session = {}
14
+ Recaptcha.configure do |config|
15
+ config.public_key = '0000000000000000000000000000000000000000'
16
+ config.private_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
17
+ end
18
+ end
19
+
20
+ def test_recaptcha_tags
21
+ # Might as well match something...
22
+ assert_match /http:\/\/www.google.com\/recaptcha\/api\/challenge/, recaptcha_tags
23
+ end
24
+
25
+ def test_recaptcha_tags_with_ssl
26
+ assert_match /https:\/\/www.google.com\/recaptcha\/api\/challenge/, recaptcha_tags(:ssl => true)
27
+ end
28
+
29
+ def test_recaptcha_tags_without_noscript
30
+ assert_no_match /noscript/, recaptcha_tags(:noscript => false)
31
+ end
32
+
33
+ def test_should_raise_exception_without_public_key
34
+ assert_raise RecaptchaError do
35
+ Recaptcha.configuration.public_key = nil
36
+ recaptcha_tags
37
+ end
38
+ end
39
+
40
+ def test_different_configuration_within_with_configuration_block
41
+ key = Recaptcha.with_configuration(:public_key => '12345') do
42
+ Recaptcha.configuration.public_key
43
+ end
44
+
45
+ assert_equal('12345', key)
46
+ end
47
+
48
+ def test_reset_configuration_after_with_configuration_block
49
+ Recaptcha.with_configuration(:public_key => '12345')
50
+ assert_equal('0000000000000000000000000000000000000000', Recaptcha.configuration.public_key)
51
+ end
52
+ end
@@ -0,0 +1,120 @@
1
+ # coding: utf-8
2
+
3
+ require 'test/unit'
4
+ require 'rubygems'
5
+ require 'active_support/core_ext/string'
6
+ require 'mocha'
7
+ require 'i18n'
8
+ require 'net/http'
9
+ require File.dirname(File.expand_path(__FILE__)) + '/../lib/recaptcha'
10
+
11
+ class RecaptchaVerifyTest < Test::Unit::TestCase
12
+ def setup
13
+ Recaptcha.configuration.private_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
14
+ @controller = TestController.new
15
+ @controller.request = stub(:remote_ip => "1.1.1.1")
16
+ @controller.params = {:recaptcha_challenge_field => "challenge", :recaptcha_response_field => "response"}
17
+
18
+ @expected_post_data = {}
19
+ @expected_post_data["privatekey"] = Recaptcha.configuration.private_key
20
+ @expected_post_data["remoteip"] = @controller.request.remote_ip
21
+ @expected_post_data["challenge"] = "challenge"
22
+ @expected_post_data["response"] = "response"
23
+
24
+ @expected_uri = URI.parse(Recaptcha.configuration.verify_url)
25
+ end
26
+
27
+ def test_should_raise_exception_without_private_key
28
+ assert_raise Recaptcha::RecaptchaError do
29
+ Recaptcha.configuration.private_key = nil
30
+ @controller.verify_recaptcha
31
+ end
32
+ end
33
+
34
+ def test_should_return_false_when_key_is_invalid
35
+ expect_http_post(response_with_body("false\ninvalid-site-private-key"))
36
+
37
+ assert !@controller.verify_recaptcha
38
+ assert_equal "invalid-site-private-key", @controller.flash[:recaptcha_error]
39
+ end
40
+
41
+ def test_returns_true_on_success
42
+ @controller.flash[:recaptcha_error] = "previous error that should be cleared"
43
+ expect_http_post(response_with_body("true\n"))
44
+
45
+ assert @controller.verify_recaptcha
46
+ assert_nil @controller.flash[:recaptcha_error]
47
+ end
48
+
49
+ def test_errors_should_be_added_to_model
50
+ expect_http_post(response_with_body("false\nbad-news"))
51
+
52
+ errors = mock
53
+ errors.expects(:add).with(:base, "Word verification response is incorrect, please try again.")
54
+ model = mock(:errors => errors)
55
+
56
+ assert !@controller.verify_recaptcha(:model => model)
57
+ assert_equal "bad-news", @controller.flash[:recaptcha_error]
58
+ end
59
+
60
+ def test_returns_true_on_success_with_optional_key
61
+ @controller.flash[:recaptcha_error] = "previous error that should be cleared"
62
+ # reset private key
63
+ @expected_post_data["privatekey"] = 'ADIFFERENTPRIVATEKEYXXXXXXXXXXXXXX'
64
+ expect_http_post(response_with_body("true\n"))
65
+
66
+ assert @controller.verify_recaptcha(:private_key => 'ADIFFERENTPRIVATEKEYXXXXXXXXXXXXXX')
67
+ assert_nil @controller.flash[:recaptcha_error]
68
+ end
69
+
70
+ def test_timeout
71
+ expect_http_post(Timeout::Error, :exception => true)
72
+ assert !@controller.verify_recaptcha()
73
+ assert_equal "recaptcha-not-reachable", @controller.flash[:recaptcha_error]
74
+ end
75
+
76
+ def test_message_should_use_i18n
77
+ I18n.locale = :de
78
+ verification_failed_translated = "Sicherheitscode konnte nicht verifiziert werden."
79
+ verification_failed_default = "Word verification response is incorrect, please try again."
80
+ recaptcha_unreachable_translated = "Netzwerkfehler, bitte versuchen Sie es später erneut."
81
+ recaptcha_unreachable_default = "Oops, we failed to validate your word verification response. Please try again."
82
+ I18n.expects(:translate).with(:'recaptcha.errors.verification_failed', :default => verification_failed_default).returns(verification_failed_translated)
83
+ I18n.expects(:translate).with(:'recaptcha.errors.recaptcha_unreachable', :default => recaptcha_unreachable_default).returns(recaptcha_unreachable_translated)
84
+
85
+ errors = mock
86
+ errors.expects(:add).with(:base, verification_failed_translated)
87
+ errors.expects(:add).with(:base, recaptcha_unreachable_translated)
88
+ model = mock; model.stubs(:errors => errors)
89
+
90
+ expect_http_post(response_with_body("false\nbad-news"))
91
+ @controller.verify_recaptcha(:model => model)
92
+
93
+ expect_http_post(Timeout::Error, :exception => true)
94
+ @controller.verify_recaptcha(:model => model)
95
+
96
+ end
97
+
98
+ private
99
+
100
+ class TestController
101
+ include Recaptcha::Verify
102
+ attr_accessor :request, :params, :flash
103
+
104
+ def initialize
105
+ @flash = {}
106
+ end
107
+ end
108
+
109
+ def expect_http_post(response, options = {})
110
+ unless options[:exception]
111
+ Net::HTTP.expects(:post_form).with(@expected_uri, @expected_post_data).returns(response)
112
+ else
113
+ Net::HTTP.expects(:post_form).raises response
114
+ end
115
+ end
116
+
117
+ def response_with_body(body)
118
+ stub(:body => body)
119
+ end
120
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: recaptcha-rails3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason L. Perry
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-03 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: recaptcha
16
+ requirement: &70249229380240 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70249229380240
25
+ - !ruby/object:Gem::Dependency
26
+ name: mocha
27
+ requirement: &70249229379740 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70249229379740
36
+ - !ruby/object:Gem::Dependency
37
+ name: activesupport
38
+ requirement: &70249229379180 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70249229379180
47
+ - !ruby/object:Gem::Dependency
48
+ name: mocha
49
+ requirement: &70249229378680 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70249229378680
58
+ - !ruby/object:Gem::Dependency
59
+ name: activesupport
60
+ requirement: &70249229378100 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70249229378100
69
+ description: This plugin adds helpers for the reCAPTCHA API - Forked for Rails 3
70
+ email: jasper@ambethia.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files:
74
+ - LICENSE
75
+ - README.rdoc
76
+ files:
77
+ - CHANGELOG
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.rdoc
81
+ - Rakefile
82
+ - VERSION
83
+ - init.rb
84
+ - lib/recaptcha.rb
85
+ - lib/recaptcha/client_helper.rb
86
+ - lib/recaptcha/configuration.rb
87
+ - lib/recaptcha/merb.rb
88
+ - lib/recaptcha/rails.rb
89
+ - lib/recaptcha/railtie.rb
90
+ - lib/recaptcha/verify.rb
91
+ - recaptcha.gemspec
92
+ - tasks/recaptcha_tasks.rake
93
+ - test/recaptcha_test.rb
94
+ - test/verify_recaptcha_test.rb
95
+ homepage: https://github.com/silviorelli/ry-recaptcha
96
+ licenses: []
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 1.8.10
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: Helpers for the reCAPTCHA API
119
+ test_files: []