redis_captcha 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +125 -0
  3. data/Rakefile +38 -0
  4. data/app/controllers/redis_captcha/application_controller.rb +4 -0
  5. data/app/controllers/redis_captcha/captcha_controller.rb +13 -0
  6. data/config/routes.rb +3 -0
  7. data/lib/generators/install_generator.rb +20 -0
  8. data/lib/generators/templates/README +29 -0
  9. data/lib/generators/templates/redis_captcha.rb +73 -0
  10. data/lib/redis_captcha.rb +120 -0
  11. data/lib/redis_captcha/controller.rb +14 -0
  12. data/lib/redis_captcha/engine.rb +5 -0
  13. data/lib/redis_captcha/image.rb +93 -0
  14. data/lib/redis_captcha/key_handler.rb +42 -0
  15. data/lib/redis_captcha/model.rb +45 -0
  16. data/lib/redis_captcha/version.rb +3 -0
  17. data/test/dummy/README.rdoc +261 -0
  18. data/test/dummy/Rakefile +7 -0
  19. data/test/dummy/app/assets/javascripts/application.js +15 -0
  20. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  21. data/test/dummy/app/controllers/application_controller.rb +3 -0
  22. data/test/dummy/app/helpers/application_helper.rb +2 -0
  23. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  24. data/test/dummy/config.ru +4 -0
  25. data/test/dummy/config/application.rb +65 -0
  26. data/test/dummy/config/boot.rb +10 -0
  27. data/test/dummy/config/environment.rb +5 -0
  28. data/test/dummy/config/environments/development.rb +31 -0
  29. data/test/dummy/config/environments/production.rb +64 -0
  30. data/test/dummy/config/environments/test.rb +35 -0
  31. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  32. data/test/dummy/config/initializers/inflections.rb +15 -0
  33. data/test/dummy/config/initializers/mime_types.rb +5 -0
  34. data/test/dummy/config/initializers/secret_token.rb +7 -0
  35. data/test/dummy/config/initializers/session_store.rb +8 -0
  36. data/test/dummy/config/initializers/wrap_parameters.rb +10 -0
  37. data/test/dummy/config/locales/en.yml +5 -0
  38. data/test/dummy/config/routes.rb +4 -0
  39. data/test/dummy/public/404.html +26 -0
  40. data/test/dummy/public/422.html +26 -0
  41. data/test/dummy/public/500.html +25 -0
  42. data/test/dummy/public/favicon.ico +0 -0
  43. data/test/dummy/script/rails +6 -0
  44. data/test/functional/redis_captcha/captcha_controller_test.rb +9 -0
  45. data/test/integration/navigation_test.rb +9 -0
  46. data/test/redis_captcha_test.rb +7 -0
  47. data/test/test_helper.rb +15 -0
  48. data/test/unit/helpers/redis_captcha/captcha_helper_test.rb +6 -0
  49. metadata +173 -0
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
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,125 @@
1
+ # RedisCaptcha
2
+
3
+ RedisCaptcha is a captcha engine base on Redis for Rails 3.2. It provides simple captcha that can be read by human.
4
+
5
+
6
+ ## Features
7
+
8
+ * RedisCaptcha is easy to setup, easy to use.
9
+ * RedisCaptcha provides simple captcha that can be read by human.
10
+ * You don't have to manage image files because they will be removed by Tempfile.
11
+ * You can set option `locked_times` and `locked_time` to avoid someone who want to attack.
12
+ * The Captcha will be Expired automatically by setting `expired_time`.
13
+ * ......
14
+
15
+
16
+ ## Requirements
17
+
18
+ * Ruby >= 1.9
19
+ * Rails > 3.2
20
+ * Redis > 2.4
21
+ * ImageMagick
22
+
23
+ *I haven't tried lower version yet.*
24
+
25
+ ## Getting started
26
+
27
+ **1. Add RedisCaptcha to your gemfile**
28
+
29
+ ```ruby
30
+ gem 'redis_captcha'
31
+ ```
32
+
33
+ **2. Generate initializer**
34
+
35
+ rails g redis_captcha:install
36
+
37
+ It'll generate `config/initializers/redis_captcha.rb`, and you can configure all options here.
38
+
39
+ **3. Mount RedisCaptcha::Engine in your router.rb**
40
+
41
+ ```ruby
42
+ mount RedisCaptcha::Engine => '/captcha', :as => :captcha
43
+ ```
44
+
45
+
46
+ **4. Add before_filter to your controller**
47
+
48
+ ```ruby
49
+ class PostsController < ApplicationController
50
+ before_filter :generate_captcha_key, :only => [:new, :edit]
51
+ ......
52
+ end
53
+ ```
54
+
55
+ **4. Add module to your model**
56
+
57
+ ```ruby
58
+ class Post < ActiveRecord::Base
59
+ include RedisCaptcha::Model
60
+ end
61
+ ```
62
+
63
+ if you set `config.active_record.whitelist_attributes = true` in `config/application.rb`, remember to add attr_accessible to your model:
64
+
65
+ ```ruby
66
+ attr_accessible :captcha, :captcha_key
67
+ ```
68
+
69
+ to your model.
70
+
71
+ **5. Use RedisCaptcha in your view**
72
+
73
+ ```erb
74
+ <%= image_tag(captcha_path(:timestamp => Time.now.to_i), :alt => "captcha") %>
75
+ <%= f.text_field :captcha %>
76
+ <%= f.hidden_field :captcha_key, :value => session[:captcha_key] %>
77
+ ```
78
+
79
+ **6. Use RedisCaptcha in your controller**
80
+
81
+ ```ruby
82
+ # Initialize a post
83
+ @post = Post.new(:title => "test")
84
+
85
+ # valid with captcha
86
+ @post.valid?
87
+
88
+ # save with captcha
89
+ @post.save
90
+
91
+ # valid without captcha
92
+ @post.valid_without_captcha?
93
+
94
+ # save without captcha
95
+ @post.save_without_captcha
96
+ ```
97
+
98
+
99
+ Enjoy it!
100
+
101
+ ## Todo list
102
+
103
+ * To add a simple site
104
+ * To write tests
105
+
106
+
107
+ ## History
108
+
109
+ **0.8.0**
110
+
111
+ First Version
112
+
113
+
114
+ ## Author
115
+
116
+ * I'm hellolucky, I come from Taiwan
117
+ * hellolucky123@gmail.com
118
+ * http://twitter.com/hellolucky123
119
+ * http://blog.hellolucky.info
120
+
121
+
122
+ ## License
123
+
124
+ This project rocks and uses MIT-LICENSE.
125
+
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'RedisCaptcha'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+
38
+ task :default => :test
@@ -0,0 +1,4 @@
1
+ module RedisCaptcha
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ module RedisCaptcha
2
+ class CaptchaController < ApplicationController
3
+ def show
4
+ captcha = RedisCaptcha::Image.new(session[:captcha_key])
5
+ tempfile = captcha.generate
6
+ if tempfile
7
+ send_file(tempfile, :type => captcha.content_type, :disposition => 'inline', :filename => captcha.filename)
8
+ else
9
+ render :nothing => true
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ RedisCaptcha::Engine.routes.draw do
2
+ root :to => 'captcha#show'
3
+ end
@@ -0,0 +1,20 @@
1
+ require 'rails/generators'
2
+
3
+ module RedisCaptcha
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ desc "Copy RedisCaptcha default files"
9
+
10
+ def copy_initializer
11
+ copy_file "redis_captcha.rb", "config/initializers/redis_captcha.rb"
12
+ end
13
+
14
+ def show_readme
15
+ readme "README"
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ ===============================================================================
2
+
3
+ 1. Setup "config/initializers/redis_captcha.rb"
4
+
5
+ 2. Mount RedisCaptcha::Engine in your router.rb
6
+
7
+ mount RedisCaptcha::Engine => '/captcha', :as => :captcha
8
+
9
+ 3. Add to your controller
10
+
11
+ before_filter :generate_captcha_key
12
+
13
+ 4. Add to your model
14
+
15
+ include RedisCaptcha::Model
16
+
17
+ 5. If you set "config.active_record.whitelist_attributes = true" in "config/application.rb", remember to add attr_accessible to your model
18
+
19
+ attr_accessible :captcha, :captcha_key
20
+
21
+ 6. Add to your view
22
+
23
+ <%= image_tag(captcha_path(:timestamp => Time.now.to_i), :alt => "captcha") %>
24
+ <%= f.text_field :captcha %>
25
+ <%= f.hidden_field :captcha_key, :value => session[:captcha_key] %>
26
+
27
+ Enjoy it :)
28
+
29
+ ===============================================================================
@@ -0,0 +1,73 @@
1
+ RedisCaptcha.setup do |config|
2
+
3
+ # ==> Redis configuration
4
+ config.redis_config = {
5
+ :host => "127.0.0.1",
6
+ :port => 6379,
7
+ :db => 0
8
+ }
9
+
10
+ # ==> Redis scope configuration
11
+ config.redis_scope = "RedisCaptcha"
12
+
13
+ # ==> Redis key expired time
14
+ config.expired_time = 1800
15
+
16
+ # ==> Redis locked times and locked time
17
+ config.locked_times = 30
18
+ config.locked_time = 600
19
+
20
+ # ==> ImageMagick path
21
+ config.image_magick_path = ""
22
+
23
+ # ==> Tempfile path
24
+ config.tempfile_path = "/tmp"
25
+
26
+ # ==> Tempfile name
27
+ config.tempfile_name = "redis_captcha"
28
+
29
+ # ==> Tempfile type and content type
30
+ config.tempfile_type = ".png"
31
+ config.content_type = "image/png"
32
+
33
+ # ==> Image width and height
34
+ config.width = 120
35
+ config.height = 30
36
+
37
+ # ==> Char set
38
+ # You can add/remove char you want
39
+ config.chars = %w(2 3 4 5 6 7 9 a c d e f g h j k m n p q r s t w x y z A C D E F G H J K L M N P Q R S T X Y Z)
40
+
41
+ # ==> String length
42
+ # You can set the same value if you don't want it to be random length
43
+ config.string_length = {
44
+ :max => 6,
45
+ :min => 4
46
+ }
47
+
48
+ # ==> Image font color and background
49
+ config.font_color = "gray"
50
+ config.background = "white"
51
+
52
+ # ==> Lines on your captcha
53
+ # You can set 0, 0 if you don't want to draw any line on your captcha
54
+ config.line = {
55
+ :max => 4,
56
+ :min => 2
57
+ }
58
+ config.line_color = "gary"
59
+
60
+ # ==> Swirl
61
+ # http://www.imagemagick.org/Usage/warping/#swirl
62
+ config.swirl_range = {
63
+ :max => 20,
64
+ :min => -20
65
+ }
66
+
67
+ # ==> Case sensitive
68
+ config.case_sensitive = false
69
+
70
+ # ==> Error message
71
+ # if you want to use I18n in your model, you can set it to be nil
72
+ config.error_message = "is invalid"
73
+ end
@@ -0,0 +1,120 @@
1
+ require 'redis'
2
+ require 'redis_captcha/engine'
3
+ require 'redis_captcha/controller'
4
+ require 'generators/install_generator'
5
+
6
+ module RedisCaptcha
7
+
8
+ autoload :Image, "redis_captcha/image"
9
+ autoload :KeyHandler, "redis_captcha/key_handler"
10
+ autoload :Model, "redis_captcha/model"
11
+
12
+ mattr_accessor :redis_config
13
+ @@redis_config = {
14
+ :host => "127.0.0.1",
15
+ :port => 6379,
16
+ :db => 0
17
+ }
18
+
19
+ mattr_accessor :redis_scope
20
+ @@redis_scope = "RedisCaptcha"
21
+
22
+ mattr_accessor :expired_time
23
+ @@expired_time = 1800
24
+
25
+ mattr_accessor :locked_times
26
+ @@locked_times = 30
27
+
28
+ mattr_accessor :locked_time
29
+ @@locked_time = 600
30
+
31
+ mattr_accessor :image_magick_path
32
+ @@image_magick_path = ""
33
+
34
+ mattr_accessor :tempfile_path
35
+ @@tempfile_path = "/tmp"
36
+
37
+ mattr_accessor :tempfile_name
38
+ @@tempfile_name = "redis_captcha"
39
+
40
+ mattr_accessor :tempfile_type
41
+ @@tempfile_type = ".png"
42
+
43
+ mattr_accessor :content_type
44
+ @@content_type = "image/png"
45
+
46
+ mattr_accessor :width
47
+ @@width = 120
48
+
49
+ mattr_accessor :height
50
+ @@height = 30
51
+
52
+ mattr_accessor :chars
53
+ @@chars = %w(2 3 4 5 6 7 9 a b c d e f g h j k m n p q r s t w x y z A C D E F G H J K L M N P Q R S T X Y Z)
54
+
55
+ mattr_accessor :string_length
56
+ @@string_length = {
57
+ :max => 6,
58
+ :min => 4
59
+ }
60
+
61
+ mattr_accessor :font_color
62
+ @@font_color = "gray"
63
+
64
+ mattr_accessor :background
65
+ @@background = "white"
66
+
67
+ mattr_accessor :line
68
+ @@line = {
69
+ :max => 4,
70
+ :min => 2
71
+ }
72
+
73
+ mattr_accessor :line_color
74
+ @@line_color = "gary"
75
+
76
+ mattr_accessor :swirl_range
77
+ @@swirl_range = {
78
+ :max => 20,
79
+ :min => -20
80
+ }
81
+
82
+ mattr_accessor :case_sensitive
83
+ @@case_sensitive = false
84
+
85
+ mattr_accessor :error_message
86
+ @@error_message = "is invalid"
87
+
88
+ def self.setup
89
+ yield self
90
+ end
91
+
92
+ def self.redis
93
+ @@redis ||= Redis.new(redis_config)
94
+ end
95
+
96
+ def self.options
97
+ @@options ||= {
98
+ :redis_scope => redis_scope,
99
+ :expired_time => expired_time,
100
+ :locked_times => locked_times,
101
+ :locked_time => locked_time,
102
+ :image_magick_path => image_magick_path,
103
+ :tempfile_path => tempfile_path,
104
+ :tempfile_name => tempfile_name,
105
+ :tempfile_type => tempfile_type,
106
+ :content_type => content_type,
107
+ :width => width,
108
+ :height => height,
109
+ :chars => chars,
110
+ :string_length => string_length,
111
+ :font_color => font_color,
112
+ :background => background,
113
+ :line => line,
114
+ :line_color => line_color,
115
+ :swirl_range => swirl_range,
116
+ :case_sensitive => case_sensitive,
117
+ :error_message => error_message
118
+ }
119
+ end
120
+ end