From 62bac6f22031a0166608bdd83cc0e1f98d553649 Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Thu, 9 Dec 2010 18:49:07 -0300 Subject: [PATCH] Adding plugin --- vendor/plugins/rails-math-captcha/MIT-LICENSE | 20 ++++++++++++++++++++ vendor/plugins/rails-math-captcha/README | 45 +++++++++++++++++++++++++++++++++++++++++++++ vendor/plugins/rails-math-captcha/Rakefile | 22 ++++++++++++++++++++++ vendor/plugins/rails-math-captcha/init.rb | 1 + vendor/plugins/rails-math-captcha/install.rb | 1 + vendor/plugins/rails-math-captcha/lib/math_captcha.rb | 2 ++ vendor/plugins/rails-math-captcha/lib/math_captcha/captcha.rb | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ vendor/plugins/rails-math-captcha/lib/math_captcha/has_captcha.rb | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ vendor/plugins/rails-math-captcha/spec/captcha_spec.rb | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ vendor/plugins/rails-math-captcha/spec/spec.opts | 4 ++++ 10 files changed, 266 insertions(+), 0 deletions(-) create mode 100644 vendor/plugins/rails-math-captcha/MIT-LICENSE create mode 100644 vendor/plugins/rails-math-captcha/README create mode 100644 vendor/plugins/rails-math-captcha/Rakefile create mode 100644 vendor/plugins/rails-math-captcha/init.rb create mode 100644 vendor/plugins/rails-math-captcha/install.rb create mode 100644 vendor/plugins/rails-math-captcha/lib/math_captcha.rb create mode 100644 vendor/plugins/rails-math-captcha/lib/math_captcha/captcha.rb create mode 100644 vendor/plugins/rails-math-captcha/lib/math_captcha/has_captcha.rb create mode 100644 vendor/plugins/rails-math-captcha/spec/captcha_spec.rb create mode 100644 vendor/plugins/rails-math-captcha/spec/spec.opts diff --git a/vendor/plugins/rails-math-captcha/MIT-LICENSE b/vendor/plugins/rails-math-captcha/MIT-LICENSE new file mode 100644 index 0000000..570ecf8 --- /dev/null +++ b/vendor/plugins/rails-math-captcha/MIT-LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2007 [name of plugin creator] + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/plugins/rails-math-captcha/README b/vendor/plugins/rails-math-captcha/README new file mode 100644 index 0000000..822e6ac --- /dev/null +++ b/vendor/plugins/rails-math-captcha/README @@ -0,0 +1,45 @@ +MathCaptcha +=========== + +TODO: Create form helpers + +Validates your Model to be saved by a human (or very clever script) by asking +the user to solve a very basic mathematical equation. + +It does not use the session to store the secret, but uses AES from EzCrypto. + + +Requirements +============ + * gem ezcrypto + * users who can calculate + +Usage +===== + +In your Model < ActiveRecord::Base + + has_captcha + + +In your form view: + + <%= @model.captcha.task %> = ? + <%= form.text_field :captcha_solution %> + <%= form.hidden_field :captcha_secret %> + + +If you want to skip the validation (in tests/specs), you can + + Model.skip_captcha! # or + @model.skip_captcha! + +and turn that off by + + Model.dont_skip_captcha! # or + @model.dont_skip_captcha! + + + +Copyright (c) 2007 Pat Nakajima, released under the MIT license +Copyright (c) 2009 Niklas Hofer, released under the MIT license diff --git a/vendor/plugins/rails-math-captcha/Rakefile b/vendor/plugins/rails-math-captcha/Rakefile new file mode 100644 index 0000000..71c2164 --- /dev/null +++ b/vendor/plugins/rails-math-captcha/Rakefile @@ -0,0 +1,22 @@ +require 'rake' +require 'rake/testtask' +require 'rake/rdoctask' + +desc 'Default: run unit tests.' +task :default => :test + +desc 'Test the math_captcha plugin.' +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' + t.pattern = 'test/**/*_test.rb' + t.verbose = true +end + +desc 'Generate documentation for the math_captcha plugin.' +Rake::RDocTask.new(:rdoc) do |rdoc| + rdoc.rdoc_dir = 'rdoc' + rdoc.title = 'MathCaptcha' + rdoc.options << '--line-numbers' << '--inline-source' + rdoc.rdoc_files.include('README') + rdoc.rdoc_files.include('lib/**/*.rb') +end diff --git a/vendor/plugins/rails-math-captcha/init.rb b/vendor/plugins/rails-math-captcha/init.rb new file mode 100644 index 0000000..7745252 --- /dev/null +++ b/vendor/plugins/rails-math-captcha/init.rb @@ -0,0 +1 @@ +require 'math_captcha' \ No newline at end of file diff --git a/vendor/plugins/rails-math-captcha/install.rb b/vendor/plugins/rails-math-captcha/install.rb new file mode 100644 index 0000000..f7732d3 --- /dev/null +++ b/vendor/plugins/rails-math-captcha/install.rb @@ -0,0 +1 @@ +# Install hook code here diff --git a/vendor/plugins/rails-math-captcha/lib/math_captcha.rb b/vendor/plugins/rails-math-captcha/lib/math_captcha.rb new file mode 100644 index 0000000..fcdff2d --- /dev/null +++ b/vendor/plugins/rails-math-captcha/lib/math_captcha.rb @@ -0,0 +1,2 @@ +require 'math_captcha/captcha' +require 'math_captcha/has_captcha' diff --git a/vendor/plugins/rails-math-captcha/lib/math_captcha/captcha.rb b/vendor/plugins/rails-math-captcha/lib/math_captcha/captcha.rb new file mode 100644 index 0000000..472547e --- /dev/null +++ b/vendor/plugins/rails-math-captcha/lib/math_captcha/captcha.rb @@ -0,0 +1,68 @@ +require 'rubygems' +require 'ezcrypto' +class Captcha + NUMBERS = (1..9).to_a + OPERATORS = [:+, :-, :*] + + attr_reader :x, :y, :operator + + def initialize(x=nil, y=nil, operator=nil) + @x = x || NUMBERS.sort_by{rand}.first + @y = y || NUMBERS.sort_by{rand}.first + @operator = operator || OPERATORS.sort_by{rand}.first + end + + # Only the #to_secret is shared with the client. + # It can be reused here to create the Captcha again + def self.from_secret(secret) + yml = cipher.decrypt64 secret + args = YAML.load(yml) + new(args[:x], args[:y], args[:operator]) + end + + def self.cipher + EzCrypto::Key.with_password key, 'bad_fixed_salt' + end + + def self.key + 'ultrasecret' + end + + + def check(answer) + answer == solution + end + + def task + "#{@x} #{@operator.to_s} #{@y}" + end + def task_with_questionmark + "#{@x} #{@operator.to_s} #{@y} = ?" + end + alias_method :to_s, :task + + def solution + @x.send @operator, @y + end + + def to_secret + cipher.encrypt64(to_yaml) + end + + def to_yaml + YAML::dump({ + :x => x, + :y => y, + :operator => operator + }) + end + + private + def cipher + @cipher ||= self.class.cipher + end + def reset_cipher + @cipher = nil + end + +end diff --git a/vendor/plugins/rails-math-captcha/lib/math_captcha/has_captcha.rb b/vendor/plugins/rails-math-captcha/lib/math_captcha/has_captcha.rb new file mode 100644 index 0000000..e71bd94 --- /dev/null +++ b/vendor/plugins/rails-math-captcha/lib/math_captcha/has_captcha.rb @@ -0,0 +1,54 @@ +module MathCaptcha + module HasCaptcha + module InstanceMethods + def must_solve_captcha + self.errors.add(:captcha_solution, "wrong answer.") unless self.captcha.check(self.captcha_solution.to_i) + end + def skip_captcha! + self.class.skip_captcha! + end + def skip_captcha? + self.class.skip_captcha? + end + def captcha + @captcha ||= Captcha.new + end + def captcha_secret=(secret) + @captcha = Captcha.from_secret(secret) + end + def captcha_secret + captcha.to_secret + end + end + + module ClassMethods + def has_captcha + include InstanceMethods + attr_accessor :captcha_solution + dont_skip_captcha! + validates_presence_of :captcha_solution, + :on => :create, :message => "can't be blank", + :unless => Proc.new {|record| record.skip_captcha? } + validate_on_create :must_solve_captcha, + :unless => Proc.new {|record| record.skip_captcha? } + end + def skip_captcha! + @@skip_captcha = true + end + def dont_skip_captcha! + @@skip_captcha = false + end + def skip_captcha? + @@skip_captcha + end + def skipping_captcha(&block) + skipping_before = skip_captcha? + skip_captcha! + yield + dont_skip_captcha! if skipping_before + end + end + end +end + +ActiveRecord::Base.send(:extend, MathCaptcha::HasCaptcha::ClassMethods) diff --git a/vendor/plugins/rails-math-captcha/spec/captcha_spec.rb b/vendor/plugins/rails-math-captcha/spec/captcha_spec.rb new file mode 100644 index 0000000..cc48f35 --- /dev/null +++ b/vendor/plugins/rails-math-captcha/spec/captcha_spec.rb @@ -0,0 +1,49 @@ +require 'lib/math_captcha/captcha' +require 'base64' + +describe Captcha do + describe "with a random task" do + before(:each) do + @captcha = Captcha.new + end + it "should have arguments and an operator" do + @captcha.x.should_not be_nil + @captcha.y.should_not be_nil + @captcha.operator.should_not be_nil + end + it "should use numbers bigger than zero" do + @captcha.x.should > 0 + @captcha.y.should > 0 + end + it "should offer a human readable task" do + @captcha.task.should =~ /^\d+\s*[\+\-\*]\s*\d+$/ + end + it "should have a secret to use in forms" do + @captcha.to_secret.should_not be_nil + @captcha.to_secret.should_not be_empty + end + + it "should re-use its cipher" do + @captcha.send(:cipher).should == @captcha.send(:cipher) + end + + it "should have a base64 encoded secret" do + lambda { Base64.decode64(@captcha.to_secret).should_not be_nil }.should_not raise_error + end + + describe "re-creating another from secret" do + before(:each) do + @secret = @captcha.to_secret + @new_captcha = Captcha.from_secret(@secret) + end + it "should have the same arguments and operator" do + @new_captcha.x.should == @captcha.x + @new_captcha.y.should == @captcha.y + @new_captcha.operator.should == @captcha.operator + end + it "should have the same string" do + @new_captcha.task.should == @captcha.task + end + end + end +end diff --git a/vendor/plugins/rails-math-captcha/spec/spec.opts b/vendor/plugins/rails-math-captcha/spec/spec.opts new file mode 100644 index 0000000..391705b --- /dev/null +++ b/vendor/plugins/rails-math-captcha/spec/spec.opts @@ -0,0 +1,4 @@ +--colour +--format progress +--loadby mtime +--reverse -- libgit2 0.21.2