simple_recaptcha 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +5 -0
- data/LICENSE +20 -0
- data/README.md +32 -0
- data/Rakefile +11 -0
- data/lib/simple_recaptcha.rb +24 -0
- data/lib/simple_recaptcha/client.rb +50 -0
- data/lib/simple_recaptcha/version.rb +3 -0
- data/simple_recaptcha.gemspec +19 -0
- data/test/client_test.rb +109 -0
- data/test/helper.rb +17 -0
- data/test/simple_recaptcha_test.rb +31 -0
- metadata +68 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Wojciech Wnętrzak
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Simple Recaptcha
|
2
|
+
|
3
|
+
Ruby library for [reCAPTCHA](http://www.google.com/recaptcha)
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
require "simple_recaptcha"
|
9
|
+
|
10
|
+
SimpleRecaptcha.verify(:private_key => "your-private-key", :ip => "127.0.0.1", :challenge => "challengestring", :response => "user input")
|
11
|
+
```
|
12
|
+
|
13
|
+
All 4 attributes must be specified: `:private_key`, `:ip`, `:challenge`, `:response`
|
14
|
+
|
15
|
+
### Configuration
|
16
|
+
|
17
|
+
You can configure Simple Recaptcha in two different ways:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
SimpleRecaptcha.private_key = "your-private-key"
|
21
|
+
|
22
|
+
SimpleRecaptcha.configure do |confg|
|
23
|
+
config.private_key = "your-private-key"
|
24
|
+
config.public_key = "your-public-key"
|
25
|
+
end
|
26
|
+
```
|
27
|
+
|
28
|
+
You can set `public_key` for consistency, however it is not used during verification
|
29
|
+
|
30
|
+
## Copyright
|
31
|
+
|
32
|
+
Copyright © 2011 Wojciech Wnętrzak. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "forwardable"
|
2
|
+
|
3
|
+
require "simple_recaptcha/version"
|
4
|
+
require "simple_recaptcha/client"
|
5
|
+
|
6
|
+
module SimpleRecaptcha
|
7
|
+
VERIFY_URL = "http://www.google.com/recaptcha/api/verify"
|
8
|
+
|
9
|
+
class << self
|
10
|
+
extend Forwardable
|
11
|
+
|
12
|
+
attr_accessor :public_key, :private_key
|
13
|
+
|
14
|
+
def_delegators :client, :verify
|
15
|
+
|
16
|
+
def configure
|
17
|
+
yield self
|
18
|
+
end
|
19
|
+
|
20
|
+
def client
|
21
|
+
@client ||= SimpleRecaptcha::Client.new
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "net/http"
|
2
|
+
|
3
|
+
class SimpleRecaptcha::Client
|
4
|
+
attr_accessor :ip, :challenge, :response
|
5
|
+
attr_writer :private_key
|
6
|
+
attr_reader :message
|
7
|
+
|
8
|
+
def private_key
|
9
|
+
@private_key ||= SimpleRecaptcha.private_key
|
10
|
+
end
|
11
|
+
|
12
|
+
def verified
|
13
|
+
!!@verified
|
14
|
+
end
|
15
|
+
alias :verified? :verified
|
16
|
+
|
17
|
+
def verify(attributes = {})
|
18
|
+
self.attributes = attributes
|
19
|
+
parse_response if valid?
|
20
|
+
verified?
|
21
|
+
end
|
22
|
+
|
23
|
+
def attributes=(attributes)
|
24
|
+
attributes.each { |key, value| send("#{key}=", value) }
|
25
|
+
end
|
26
|
+
|
27
|
+
def request
|
28
|
+
Net::HTTP.post_form(URI.parse(SimpleRecaptcha::VERIFY_URL), params)
|
29
|
+
end
|
30
|
+
|
31
|
+
def parse_response
|
32
|
+
@verified, @message = request.body.split("\n")
|
33
|
+
@verified = @verified == "true" ? true : false
|
34
|
+
end
|
35
|
+
|
36
|
+
def valid?
|
37
|
+
["privatekey", "remoteip", "challenge", "response"].none? { |p| params[p].nil? }
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def params
|
43
|
+
{
|
44
|
+
"privatekey" => private_key,
|
45
|
+
"remoteip" => ip,
|
46
|
+
"challenge" => challenge,
|
47
|
+
"response" => response
|
48
|
+
}
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/simple_recaptcha/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Wojciech Wnętrzak"]
|
6
|
+
gem.email = ["w.wnetrzak@gmail.com"]
|
7
|
+
gem.description = %q{Simple Google reCAPTCHA library}
|
8
|
+
gem.summary = %q{Google reCAPTCHA library}
|
9
|
+
gem.homepage = "https://github.com/objectreload/simple_recaptcha"
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "simple_recaptcha"
|
15
|
+
gem.require_paths = ['lib']
|
16
|
+
gem.version = SimpleRecaptcha::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "mocha"
|
19
|
+
end
|
data/test/client_test.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
describe SimpleRecaptcha::Client do
|
4
|
+
before do
|
5
|
+
@attributes = {:ip => "127.0.0.1", :challenge => "challenge", :response => "response", :private_key => "private_key"}
|
6
|
+
@recaptcha = SimpleRecaptcha::Client.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should set attributes on verification" do
|
10
|
+
mock_http
|
11
|
+
@recaptcha.verify(@attributes)
|
12
|
+
@recaptcha.ip.must_equal "127.0.0.1"
|
13
|
+
@recaptcha.challenge.must_equal "challenge"
|
14
|
+
@recaptcha.response.must_equal "response"
|
15
|
+
@recaptcha.private_key.must_equal "private_key"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should be valid with all attributes set" do
|
19
|
+
@recaptcha.attributes = @attributes
|
20
|
+
@recaptcha.valid?.must_equal true
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should be invalid without ip attribute" do
|
24
|
+
@attributes.delete(:ip)
|
25
|
+
@recaptcha.attributes = @attributes
|
26
|
+
@recaptcha.valid?.wont_equal true
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should be invalid without challenge attribute" do
|
30
|
+
@attributes.delete(:challenge)
|
31
|
+
@recaptcha.attributes = @attributes
|
32
|
+
@recaptcha.valid?.wont_equal true
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should be invalid without respnse attribute" do
|
36
|
+
@attributes.delete(:response)
|
37
|
+
@recaptcha.attributes = @attributes
|
38
|
+
@recaptcha.valid?.wont_equal true
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should be invalid without private_key attribute" do
|
42
|
+
@attributes.delete(:private_key)
|
43
|
+
@recaptcha.attributes = @attributes
|
44
|
+
@recaptcha.valid?.wont_equal true
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should use module private key when not set on instance level" do
|
48
|
+
SimpleRecaptcha.private_key = "moduleprivate"
|
49
|
+
recaptcha = SimpleRecaptcha::Client.new
|
50
|
+
recaptcha.private_key.must_equal "moduleprivate"
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should overwrite module private key on verification" do
|
54
|
+
SimpleRecaptcha.private_key = "moduleprivate"
|
55
|
+
mock_http
|
56
|
+
@attributes[:private_key] = "instanceprivate"
|
57
|
+
recaptcha = SimpleRecaptcha::Client.new
|
58
|
+
recaptcha.verify(@attributes)
|
59
|
+
recaptcha.private_key.must_equal "instanceprivate"
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should return true if verified" do
|
63
|
+
@recaptcha.attributes = @attributes
|
64
|
+
mock_http("true\n")
|
65
|
+
@recaptcha.verify.must_equal true
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should be able to check if verified" do
|
69
|
+
@recaptcha.attributes = @attributes
|
70
|
+
mock_http("true\n")
|
71
|
+
@recaptcha.verify
|
72
|
+
@recaptcha.verified.must_equal true
|
73
|
+
@recaptcha.verified?.must_equal true
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should not request when not valid" do
|
77
|
+
@recaptcha.attributes = @attributes
|
78
|
+
@recaptcha.stubs(:valid?).returns(false)
|
79
|
+
@recaptcha.expects(:request).never
|
80
|
+
@recaptcha.verify
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should return false when not valid during verification" do
|
84
|
+
@recaptcha.attributes = @attributes
|
85
|
+
@recaptcha.stubs(:valid?).returns(false)
|
86
|
+
@recaptcha.verify.wont_equal true
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should return false when not verified" do
|
90
|
+
@recaptcha.attributes = @attributes
|
91
|
+
mock_http("false\ninvalid-request-cookie")
|
92
|
+
@recaptcha.verify.wont_equal true
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should be able to check if not verified" do
|
96
|
+
@recaptcha.attributes = @attributes
|
97
|
+
mock_http("false\ninvalid-request-cookie")
|
98
|
+
@recaptcha.verify
|
99
|
+
@recaptcha.verified.wont_equal true
|
100
|
+
@recaptcha.verified?.wont_equal true
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should be able to read message from not verified response" do
|
104
|
+
@recaptcha.attributes = @attributes
|
105
|
+
mock_http("false\ninvalid-request-cookie")
|
106
|
+
@recaptcha.verify
|
107
|
+
@recaptcha.message.must_equal "invalid-request-cookie"
|
108
|
+
end
|
109
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "minitest/autorun"
|
2
|
+
require "mocha"
|
3
|
+
|
4
|
+
require "simple_recaptcha"
|
5
|
+
|
6
|
+
class MiniTest::Unit::TestCase
|
7
|
+
def teardown
|
8
|
+
SimpleRecaptcha.public_key = nil
|
9
|
+
SimpleRecaptcha.private_key = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def mock_http(body = "false\ninvalid-request-cookie")
|
13
|
+
http = mock("Net::HTTPOK")
|
14
|
+
http.stubs(:body => body)
|
15
|
+
Net::HTTP.stubs(:post_form).returns(http)
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
describe SimpleRecaptcha do
|
4
|
+
it "should be possible to set private key on module level" do
|
5
|
+
SimpleRecaptcha.private_key = "private"
|
6
|
+
SimpleRecaptcha.private_key.must_equal "private"
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should be possible to set public key on module level" do
|
10
|
+
SimpleRecaptcha.public_key = "public"
|
11
|
+
SimpleRecaptcha.public_key.must_equal "public"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should be possible to configure via block" do
|
15
|
+
SimpleRecaptcha.configure do |config|
|
16
|
+
config.private_key = "privateviablock"
|
17
|
+
config.public_key = "publicviablock"
|
18
|
+
end
|
19
|
+
SimpleRecaptcha.public_key.must_equal "publicviablock"
|
20
|
+
SimpleRecaptcha.private_key.must_equal "privateviablock"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should be possible to call client on module level" do
|
24
|
+
SimpleRecaptcha.client.must_be_kind_of SimpleRecaptcha::Client
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should be possible to verify on module level" do
|
28
|
+
mock_http("true\n")
|
29
|
+
SimpleRecaptcha.verify(:ip => "127.0.0.1", :challenge => "challenge", :response => "response", :private_key => "privatekey").must_equal true
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_recaptcha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Wojciech Wnętrzak
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-12 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mocha
|
16
|
+
requirement: &16261820 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *16261820
|
25
|
+
description: Simple Google reCAPTCHA library
|
26
|
+
email:
|
27
|
+
- w.wnetrzak@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- lib/simple_recaptcha.rb
|
38
|
+
- lib/simple_recaptcha/client.rb
|
39
|
+
- lib/simple_recaptcha/version.rb
|
40
|
+
- simple_recaptcha.gemspec
|
41
|
+
- test/client_test.rb
|
42
|
+
- test/helper.rb
|
43
|
+
- test/simple_recaptcha_test.rb
|
44
|
+
homepage: https://github.com/objectreload/simple_recaptcha
|
45
|
+
licenses: []
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.8.10
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Google reCAPTCHA library
|
68
|
+
test_files: []
|