rack-recaptcha 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Arthur Chiu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ = rack-recaptcha
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Arthur. See LICENSE for details.
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rack-recaptcha"
8
+ gem.summary = %Q{Rack middleware for Recaptcha}
9
+ gem.description = %Q{Rack middleware Captcha verification using Recaptcha API.}
10
+ gem.email = "mr.arthur.chiu@gmail.com"
11
+ gem.homepage = "http://github.com/achiu/rack-recaptcha"
12
+ gem.authors = ["Arthur Chiu"]
13
+ gem.add_runtime_dependency "json", ">=0"
14
+ gem.add_development_dependency "riot", ">= 0"
15
+ gem.add_development_dependency "rack-test", ">=0"
16
+ gem.add_development_dependency "rr", ">=0"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ require 'rake/testtask'
25
+ Rake::TestTask.new(:test) do |test|
26
+ test.libs << 'lib' << 'test'
27
+ test.pattern = 'test/**/*_test.rb'
28
+ test.verbose = true
29
+ end
30
+
31
+ begin
32
+ require 'rcov/rcovtask'
33
+ Rcov::RcovTask.new do |test|
34
+ test.libs << 'test'
35
+ test.pattern = 'test/**/*_test.rb'
36
+ test.verbose = true
37
+ end
38
+ rescue LoadError
39
+ task :rcov do
40
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
41
+ end
42
+ end
43
+
44
+ task :test => :check_dependencies
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/rdoctask'
49
+ Rake::RDocTask.new do |rdoc|
50
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "rack-recaptcha #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,9 @@
1
+ require 'json'
2
+
3
+ RECAPTCHA_API_URL = 'http://api.recaptcha.net'
4
+ RECAPTCHA_API_SECURE_URL = 'https://api-secure.recaptcha.net'
5
+ RECAPTCHA_VERIFY_URL = 'http://api-verify.recaptcha.net/verify'
6
+
7
+
8
+ require File.expand_path(File.join(File.dirname(__FILE__),'rack-recaptcha','recaptcha'))
9
+ require File.expand_path(File.join(File.dirname(__FILE__),'rack-recaptcha','helpers'))
@@ -0,0 +1,59 @@
1
+ module Rack
2
+ class Recaptcha
3
+ module Helpers
4
+
5
+ DEFAULT= {
6
+ :height => 300,
7
+ :width => 500,
8
+ :row => 3,
9
+ :cols => 5
10
+ }
11
+
12
+
13
+ def recaptcha_tag(type= :noscript, options={})
14
+ options = DEFAULT.merge(options)
15
+ options[:public_key] ||= Rack::Recaptcha.public_key
16
+ path = options[:ssl] ? RECAPTCHA_API_SECURE_URL : RECAPTCHA_API_URL
17
+ html = case type.to_sym
18
+ when :challenge
19
+ (<<-CHALLENGE).gsub(/^ #{10}/,'')
20
+ <script type="text/javascript" src="#{path}/challenge?k=#{options[:public_key]}">
21
+ </script>
22
+ CHALLENGE
23
+ when :noscript
24
+ (<<-NOSCRIPT).gsub(/^ #{10}/,'')
25
+ <noscript>
26
+ <iframe src="#{path}/noscript?k=#{options[:public_key]}" height="#{options[:height]}" width="#{options[:width]}" frameborder="0"></iframe><br>
27
+ <textarea name="recaptcha_challenge_field" rows="#{options[:row]}" cols="#{options[:cols]}"></textarea>
28
+ <input type="hidden" name="recaptcha_response_field" value="manual_challenge">
29
+ </noscript>
30
+ NOSCRIPT
31
+ when :ajax
32
+ (<<-AJAX).gsub(/^ #{10}/,'')
33
+ <div id="ajax_recaptcha"></div>
34
+ <script type="text/javascript" src="#{path}/js/recaptcha_ajax.js"></script>
35
+ <script type="text/javascript">
36
+ Recaptcha.create('#{options[:public_key]}', document.getElementById('ajax_recaptcha')#{options[:display] ? ',RecaptchaOptions' : ''});
37
+ </script>
38
+ AJAX
39
+ else
40
+ ""
41
+ end
42
+ if options[:display]
43
+ (<<-DISPLAY).gsub(/^ #{10}/,'')
44
+ <script type="text/javascript">
45
+ var RecaptchaOptions = #{options[:display].to_json};
46
+ </script>
47
+ DISPLAY
48
+ else
49
+ ""
50
+ end + html
51
+ end
52
+
53
+ def verified?
54
+ env['recaptcha.value'] == 'true'
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,35 @@
1
+ module Rack
2
+ class Recaptcha
3
+ attr_reader :options
4
+ class << self
5
+ attr_accessor :private_key, :public_key
6
+ end
7
+
8
+ def initialize(app,options = {})
9
+ @app, @options = app,options
10
+ self.class.private_key = options[:private_key]
11
+ self.class.public_key = options[:public_key]
12
+ end
13
+
14
+ def call(env)
15
+ request = Request.new(env)
16
+ if request.post? and request.path == @options[:login_path]
17
+ value, msg = verify(request)
18
+ env.merge!('recaptcha.value' => value, 'recaptcha.msg' => msg)
19
+ end
20
+ @app.call(env)
21
+ end
22
+
23
+ def verify(request)
24
+ params = {
25
+ :privatekey => Rack::Recaptcha.private_key,
26
+ :remoteip => request.ip,
27
+ :challenge => request.params['recaptcha_challenge_field'],
28
+ :response => request.params['recaptcha_response_field']
29
+ }
30
+ response = Net::HTTP.post_form URI.parse(RECAPTCHA_VERIFY_URL), params
31
+ response.body.split("\n")
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,45 @@
1
+ #
2
+ # 'autotest' for riot
3
+ # install watchr
4
+ # $ sudo gem install watchr
5
+ #
6
+ # Run With:
7
+ # $ watchr test.watchr
8
+ #
9
+
10
+ # --------------------------------------------------
11
+ # Helpers
12
+ # --------------------------------------------------
13
+
14
+ def run(cmd)
15
+ puts(cmd)
16
+ system(cmd)
17
+ end
18
+
19
+ def run_all_tests
20
+ system( "rake test" )
21
+ end
22
+
23
+ def sudo(cmd)
24
+ run("sudo #{cmd}")
25
+ end
26
+
27
+ # --------------------------------------------------
28
+ # Watchr Rules
29
+ # --------------------------------------------------
30
+ watch("^lib.*/(.*)\.rb") { |m| run("ruby test/#{m[1]}_test.rb") }
31
+ watch("test.*/teststrap\.rb") { run_all_tests }
32
+ watch("^test/(.*)_test\.rb") { |m| run("ruby test/#{m[1]}_test.rb")}
33
+
34
+
35
+ # --------------------------------------------------
36
+ # Signal Handling
37
+ # --------------------------------------------------
38
+ # Ctrl-\
39
+ Signal.trap('QUIT') do
40
+ puts " --- Running all tests ---\n\n"
41
+ run_all_tests
42
+ end
43
+
44
+ # Ctrl-C
45
+ Signal.trap('INT') { abort("\n") }
@@ -0,0 +1,79 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'teststrap'))
2
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','lib','rack-recaptcha','helpers'))
3
+ require 'riot/rr'
4
+
5
+ class Helper
6
+ attr_accessor :env
7
+ include Rack::Recaptcha::Helpers
8
+ end
9
+
10
+ context "Rack::Recaptcha::Helpers" do
11
+ setup do
12
+ Rack::Recaptcha.public_key = '0'*40
13
+ @helper = Helper.new
14
+ end
15
+
16
+
17
+ context "recaptcha_tag" do
18
+
19
+ context "ajax" do
20
+ context "with display" do
21
+ setup { @helper.recaptcha_tag(:ajax,:display => {:theme => 'red'}) }
22
+ asserts("has js") { topic }.matches %r{recaptcha_ajax.js}
23
+ asserts("has div") { topic }.matches %r{<div id="ajax_recaptcha"></div>}
24
+ asserts("has display") { topic }.matches %r{RecaptchaOptions}
25
+ asserts("has red theme") { topic }.matches %r{"theme":"red"}
26
+ end
27
+ context "without display" do
28
+ setup { @helper.recaptcha_tag(:ajax) }
29
+ asserts("has js") { topic }.matches %r{recaptcha_ajax.js}
30
+ asserts("has div") { topic }.matches %r{<div id="ajax_recaptcha"></div>}
31
+ asserts("has display") { topic =~ %r{RecaptchaOptions} }.not!
32
+ asserts("has red theme") { topic =~ %r{"theme":"red"} }.not!
33
+ end
34
+ end
35
+
36
+ context "noscript" do
37
+ setup { @helper.recaptcha_tag :noscript, :public_key => "hello_world_world" }
38
+ asserts("iframe") { topic }.matches %r{iframe}
39
+ asserts("no script tag") { topic }.matches %r{<noscript>}
40
+ asserts("public key") { topic }.matches %r{hello_world_world}
41
+ asserts("has js") { topic =~ %r{recaptcha_ajax.js} }.not!
42
+ end
43
+
44
+ context "challenge" do
45
+ setup { @helper.recaptcha_tag(:challenge) }
46
+ asserts("has script tag") { topic }.matches %r{script}
47
+ asserts("has challenge js") { topic }.matches %r{challenge}
48
+ asserts("has js") { topic =~ %r{recaptcha_ajax.js} }.not!
49
+ asserts("has display") { topic =~ %r{RecaptchaOptions} }.not!
50
+ asserts("has public_key") { topic }.matches %r{#{'0'*40}}
51
+ end
52
+
53
+ context "server" do
54
+ asserts("using ssl url") { @helper.recaptcha_tag(:challenge, :ssl => true) }.matches %r{https://api-secure.recaptcha.net}
55
+ asserts("using non ssl url") { @helper.recaptcha_tag(:ajax) }.matches %r{http://api.recaptcha.net}
56
+ end
57
+
58
+ end
59
+
60
+ context "verified?" do
61
+
62
+ context "passing" do
63
+ setup do
64
+ mock(@helper.env).[]('recaptcha.value').returns('true')
65
+ @helper.verified?
66
+ end
67
+ asserts_topic
68
+ end
69
+
70
+ context "failing" do
71
+ setup do
72
+ mock(@helper.env).[]('recaptcha.value').returns('false')
73
+ @helper.verified?
74
+ end
75
+ asserts_topic.not!
76
+ end
77
+
78
+ end
79
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'teststrap'))
2
+ require 'fakeweb'
3
+
4
+ FakeWeb.allow_net_connect = false
5
+ context "Rack::Recaptcha" do
6
+
7
+ context "basic request" do
8
+ setup { get("/")}
9
+ asserts("status is 200") { last_response.status }.equals 200
10
+ asserts("body is hello world") { last_response.body }.equals 'Hello world'
11
+ end
12
+
13
+ context "exposes" do
14
+ setup { Rack::Recaptcha }
15
+ asserts("private key") { topic.private_key }.equals PRIVATE_KEY
16
+ asserts("public key") { topic.public_key }.equals PUBLIC_KEY
17
+ end
18
+
19
+ context "login path" do
20
+
21
+ context "get" do
22
+ setup { get('/login') }
23
+ asserts("get login") { last_response.body }.equals 'login'
24
+ end
25
+
26
+ context "post pass" do
27
+ setup do
28
+ FakeWeb.register_uri(:post, RECAPTCHA_VERIFY_URL, :body => "true\nsuccess")
29
+ post("/login")
30
+ end
31
+ asserts("post login") { last_response.body }.equals 'post login'
32
+ end
33
+
34
+ context "post fail" do
35
+ setup do
36
+ FakeWeb.register_uri(:post, RECAPTCHA_VERIFY_URL, :body => "false\nfailed")
37
+ post("/login")
38
+ end
39
+ asserts("post fail") { last_response.body }.equals 'post fail'
40
+ end
41
+ end
42
+
43
+ end
@@ -0,0 +1,42 @@
1
+ require 'rubygems'
2
+ require 'rack/test'
3
+ require 'rack/mock'
4
+ require 'rack/utils'
5
+ require 'rack/session/cookie'
6
+ require 'rack/builder'
7
+ require 'rr'
8
+ require 'riot'
9
+ require 'riot/rr'
10
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','lib','rack-recaptcha'))
11
+
12
+ PUBLIC_KEY = '0'*40
13
+ PRIVATE_KEY = 'X'*40
14
+
15
+ class Riot::Situation
16
+ include Rack::Test::Methods
17
+
18
+ def app
19
+ main_app = lambda { |env|
20
+ request = Rack::Request.new(env)
21
+ return_code, body_text =
22
+ case request.path
23
+ when '/' then [200,'Hello world']
24
+ when '/login'
25
+ if request.post?
26
+ env['recaptcha.value'] == 'true' ? [200, 'post login'] : [200, 'post fail']
27
+ else
28
+ [200,'login']
29
+ end
30
+ else
31
+ [404,'Nothing here']
32
+ end
33
+ [return_code,{'Content-type' => 'text/plain'}, body_text]
34
+ }
35
+
36
+ builder = Rack::Builder.new
37
+ builder.use Rack::Recaptcha, :private_key => PRIVATE_KEY, :public_key => PUBLIC_KEY, :login_path => '/login'
38
+ builder.run main_app
39
+ builder.to_app
40
+ end
41
+
42
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-recaptcha
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Arthur Chiu
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-22 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: json
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: riot
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :development
43
+ version_requirements: *id002
44
+ - !ruby/object:Gem::Dependency
45
+ name: rack-test
46
+ prerelease: false
47
+ requirement: &id003 !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ type: :development
55
+ version_requirements: *id003
56
+ - !ruby/object:Gem::Dependency
57
+ name: rr
58
+ prerelease: false
59
+ requirement: &id004 !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ type: :development
67
+ version_requirements: *id004
68
+ description: Rack middleware Captcha verification using Recaptcha API.
69
+ email: mr.arthur.chiu@gmail.com
70
+ executables: []
71
+
72
+ extensions: []
73
+
74
+ extra_rdoc_files:
75
+ - LICENSE
76
+ - README.rdoc
77
+ files:
78
+ - .document
79
+ - .gitignore
80
+ - LICENSE
81
+ - README.rdoc
82
+ - Rakefile
83
+ - VERSION
84
+ - lib/rack-recaptcha.rb
85
+ - lib/rack-recaptcha/helpers.rb
86
+ - lib/rack-recaptcha/recaptcha.rb
87
+ - test.watchr
88
+ - test/helpers_test.rb
89
+ - test/recaptcha_test.rb
90
+ - test/teststrap.rb
91
+ has_rdoc: true
92
+ homepage: http://github.com/achiu/rack-recaptcha
93
+ licenses: []
94
+
95
+ post_install_message:
96
+ rdoc_options:
97
+ - --charset=UTF-8
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ requirements: []
115
+
116
+ rubyforge_project:
117
+ rubygems_version: 1.3.6
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: Rack middleware for Recaptcha
121
+ test_files:
122
+ - test/helpers_test.rb
123
+ - test/recaptcha_test.rb
124
+ - test/teststrap.rb