captchachacha 0.0.3 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data.tar.gz.sig CHANGED
Binary file
data/.gitignore CHANGED
@@ -1,5 +1,8 @@
1
1
  *.old
2
+ *.tmproj
3
+ .rvmrc
2
4
  .DS_Store
5
+ *.tmproj
3
6
 
4
7
  *.gem
5
8
 
data/CHANGES CHANGED
@@ -1 +1,11 @@
1
+ v0.4.2 Fixed ultra stupid bug with names of params.
2
+
3
+ v0.4.1 Changes to gemspec to get rid of YAML Psyck errors when installing gem.
4
+
5
+ v0.4.0 Using secure random lib to generate better session id's; using X-... for non standard headers.
6
+
7
+ v0.0.3 Rebuild of v0.0.2 but without development dependencies, as one of them was causing errors via a bug with YAML Psyck::Syck
8
+
9
+ v0.0.2 Working, with tests for rack portion.
10
+
1
11
  v0.0.1 Start of project.
data/README CHANGED
@@ -1,22 +1,20 @@
1
1
  ## Rack::Captchachacha ##
2
2
 
3
- Rack middleware for using the [captchator.com][1] service.
3
+ Rack middleware for using the captchator.com service http://captchator.com/.
4
4
 
5
5
  ### Usage ###
6
6
 
7
7
  #### Sinatra ####
8
8
 
9
9
  ## app.rb
10
+ require 'rack/captchachacha'
10
11
  use Rack::Captchachacha
11
12
  helpers Rack::Recaptcha::Helpers
12
13
 
14
+ There is a longer example on my blog, http://iainbarnett.me.uk/posts/Using-rack-captchachacha/
13
15
 
14
16
  ### Acknowledgments ###
15
17
 
16
- The vast majority of this code comes from [sinatra-captcha][2] and [rack-recaptcha][3]
18
+ The vast majority of this code comes from sinatra-captcha https://github.com/bmizerany/sinatra-captcha and rack-recaptcha https://github.com/achiu/rack-recaptcha
17
19
 
18
20
  Many thanks to Blake Mizerany, the author of sinatra-captcha, and Arthur Chiu, author of rack-recaptcha for making their code public.
19
-
20
- [1]: http://captchator.com/
21
- [2]: https://github.com/bmizerany/sinatra-captcha
22
- [3]: https://github.com/achiu/rack-recaptcha
@@ -11,13 +11,13 @@ Gem::Specification.new do |s|
11
11
  EOF
12
12
  s.version = Rack::Captchachacha::VERSION
13
13
  s.platform = Gem::Platform::RUBY
14
- s.require_path = 'lib'
14
+ s.require_path = "lib"
15
15
  s.required_ruby_version = ">= 1.9.2"
16
- s.author = "Iain Barnett"
16
+ s.authors = ["Iain Barnett"]
17
17
  s.files = `git ls-files`.split("\n")
18
- s.add_dependency('rack', '=1.3.0')
19
- s.add_dependency('curb', '=0.7.15')
20
- s.email = "iainspeed @nospam@ gmail.com"
18
+ s.add_dependency("rack", "~> 1.3.0")
19
+ s.add_dependency("curb", "~> 0.7.15")
20
+ s.email = ["iainspeed @nospam@ gmail.com"]
21
21
  s.homepage = "https://github.com/yb66/Captchachacha"
22
22
  s.test_files = `git ls-files -- {test,spec,features}`.split("\n")
23
23
  s.signing_key = ENV['HOME'] + '/.ssh/gem-private_key.pem'
@@ -8,8 +8,10 @@ module Rack
8
8
 
9
9
  VERIFY_URL = "http://captchator.com/captcha/check_answer"
10
10
  CHALLENGE_FIELD = 'captcha_session'
11
- RESPONSE_FIELD = 'captcha_answer'
12
-
11
+ RESPONSE_FIELD = 'captcha'
12
+ DEFAULT_MESSAGE = "Incorrect response, please try again."
13
+ RESULT_HEADER = 'X-Captcha-Valid'
14
+ RESULT_MESSAGE = 'X-Captcha-Msg'
13
15
 
14
16
  # @param app Rack application
15
17
  # @param [optional,Hash] options Hash of options
@@ -28,9 +30,8 @@ module Rack
28
30
  # @param env Rack environment
29
31
  def _call(env)
30
32
  request = Request.new(env)
31
-
32
33
  if request.params[CHALLENGE_FIELD] && request.params[RESPONSE_FIELD]
33
-
34
+
34
35
  result, msg = verify(
35
36
  request.params[CHALLENGE_FIELD].to_i,
36
37
  request.params[RESPONSE_FIELD] )
@@ -41,20 +42,21 @@ module Rack
41
42
  # If it's a fail then the usual course of action would be to redirect back to the
42
43
  # captcha form, but on success to continue, so the error message will be ignored unless
43
44
  # of failure.
44
- msg ||= "incorrect response, please try again"
45
+ msg ||= DEFAULT_MESSAGE
45
46
 
46
- env.merge!('captcha.valid' => result == true, 'captcha.msg' => msg )
47
+ env.merge!(RESULT_HEADER => result == true, RESULT_MESSAGE => msg )
47
48
  end
49
+
48
50
  @app.call(env)
49
51
  end
50
52
 
51
53
 
52
54
  def verify( session_id, answer )
53
- return false if session_id == 0
55
+ return false if session_id == 0 || session_id.nil?
54
56
  return false if answer.nil?
55
57
 
56
58
  require 'curb'
57
- Curl::Easy.perform("#{VERIFY_URL}/#{session_id}/#{answer}").body_str.to_i == 1
59
+ Curl::Easy.perform("#{VERIFY_URL}/#{session_id}/#{answer}").body_str == "1"
58
60
  end # def
59
61
 
60
62
  end # class
@@ -7,25 +7,25 @@
7
7
  module Rack
8
8
  class Captchachacha
9
9
  module Helpers
10
-
10
+ require 'securerandom'
11
11
 
12
12
  def captcha_valid?
13
- request.env['captcha.valid']
13
+ request.env['X-Captcha-Valid']
14
14
  end # def
15
15
 
16
16
 
17
17
  def captcha_session
18
- @captcha_session ||= rand(9000) + 1000
18
+ @captcha_session ||= SecureRandom.random_number.to_s[2..-1]
19
19
  end
20
20
 
21
21
 
22
22
  def captcha_answer_tag
23
- %Q!<input id="captcha-answer" name="captcha_answer" type="text" size="10"/>!
23
+ %Q!<input id="captcha_answer" name="captcha_answer" type="text" size="6"/>!
24
24
  end
25
25
 
26
26
 
27
27
  def captcha_image_tag
28
- %Q!<input name="captcha_session" type="hidden" value="#{captcha_session}"/>\n<img id="captcha-image" src="http://captchator.com/captcha/image/#{captcha_session}"/>!
28
+ %Q!<input id="captcha_session" name="captcha_session" type="hidden" value="#{captcha_session}"/><img id="captcha_image" src="http://captchator.com/captcha/image/#{captcha_session}"/>!
29
29
  end
30
30
 
31
31
  end # Helpers
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class Captchachacha
3
- VERSION = "0.0.3"
3
+ VERSION = "0.4.2"
4
4
  end
5
- end
5
+ end
@@ -3,6 +3,7 @@ require 'rack/test'
3
3
  require 'rack/mock'
4
4
  require 'curb'
5
5
  require 'webmock/rspec'
6
+ require 'securerandom'
6
7
 
7
8
  require_relative '../lib/rack/captchachacha.rb'
8
9
 
@@ -23,7 +24,7 @@ include Rack::Test::Methods
23
24
  when '/' then [200,'Hello world']
24
25
  when '/login'
25
26
  if request.post?
26
- env['captcha.valid'] ? [200, 'post login'] : [200, 'post fail']
27
+ env['X-Captcha-Valid'] ? [200, 'post login'] : [200, 'post fail']
27
28
  else
28
29
  [200,'login']
29
30
  end
@@ -51,12 +52,12 @@ include Rack::Test::Methods
51
52
  end # context
52
53
 
53
54
  context "a page that requires a captcha" do
54
- let(:session_id){ rand(1000) }
55
+ let(:session_id){ SecureRandom.random_number.to_s[2..-1] }
55
56
  let(:url_to_request) { "#{Rack::Captchachacha::VERIFY_URL}/#{session_id}/response" }
56
57
 
57
58
  it "should pass the captcha" do
58
59
  stub_request(:get, url_to_request).to_return({:body => "1"})
59
- post "/login", {'captcha_session' => session_id, 'captcha_answer' => 'response'}
60
+ post "/login", {'captcha_session' => session_id, 'captcha' => 'response'}
60
61
 
61
62
  WebMock.should have_requested(:get, url_to_request)
62
63
 
@@ -67,7 +68,7 @@ include Rack::Test::Methods
67
68
 
68
69
  it "should fail the captcha" do
69
70
  stub_request(:get, url_to_request).to_return({:body => "0"})
70
- post "/login", {'captcha_session' => session_id, 'captcha_answer' => 'response'}
71
+ post "/login", {'captcha_session' => session_id, 'captcha' => 'response'}
71
72
 
72
73
  WebMock.should have_requested(:get, url_to_request)
73
74
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: captchachacha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.4.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -50,34 +50,35 @@ cert_chain:
50
50
  -----END CERTIFICATE-----
51
51
 
52
52
  '
53
- date: 2011-06-14 00:00:00.000000000Z
53
+ date: 2011-06-28 00:00:00.000000000Z
54
54
  dependencies:
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rack
57
- requirement: &2161671660 !ruby/object:Gem::Requirement
57
+ requirement: &2156651920 !ruby/object:Gem::Requirement
58
58
  none: false
59
59
  requirements:
60
- - - =
60
+ - - ~>
61
61
  - !ruby/object:Gem::Version
62
62
  version: 1.3.0
63
63
  type: :runtime
64
64
  prerelease: false
65
- version_requirements: *2161671660
65
+ version_requirements: *2156651920
66
66
  - !ruby/object:Gem::Dependency
67
67
  name: curb
68
- requirement: &2161671200 !ruby/object:Gem::Requirement
68
+ requirement: &2156651460 !ruby/object:Gem::Requirement
69
69
  none: false
70
70
  requirements:
71
- - - =
71
+ - - ~>
72
72
  - !ruby/object:Gem::Version
73
73
  version: 0.7.15
74
74
  type: :runtime
75
75
  prerelease: false
76
- version_requirements: *2161671200
76
+ version_requirements: *2156651460
77
77
  description: ! ' Captchator as Rack middleware, and helpers for sinatra.
78
78
 
79
79
  '
80
- email: iainspeed @nospam@ gmail.com
80
+ email:
81
+ - iainspeed @nospam@ gmail.com
81
82
  executables: []
82
83
  extensions: []
83
84
  extra_rdoc_files: []
metadata.gz.sig CHANGED
Binary file