captcha
captcha copied to clipboard
A Google-style captcha for enterprise Rails apps
captcha
A Google-style captcha for enterprise Rails apps
Goals
- Batch generate captchas
- Use ciphered filenames (no need to store filename/captcha pairs)
- Easy configuration
- Number of captchas
- Period for captcha refresh
- Colors, wave, implode
- Handle lots of users
Compatibility
Tested with Ruby 1.8.6, 1.8.7, and 1.9.1.
Install
script/plugin install git://github.com/winton/captcha.git
Create lib/captcha_config.rb (optional)
Captcha::Config.new(
# Used for filename cipher
:password => 'something-unique',
# Captcha colors
:colors => {
:background => '#FFFFFF',
:font => '#080288'
},
# Number of captcha images to generate
:count => RAILS_ENV == 'production' ? 500 : 10,
# Where to write captchas
:destination => "#{RAILS_ROOT}/public/images/captchas",
# Generate new batch every day
:generate_every => 24 * 60 * 60
)
See lib/captcha/config.rb for more options.
application_controller.rb
class ApplicationControllerYou may now use the
reset_captchamethod in any controller.user.rb
class User "base error when captcha fails", :field => "field error when captcha fails" endWith no parameters, a default error is added to the "captcha" field (
:field => true).Specify
:base => trueto use a default error for base.In your view
<img src="/images/captchas/.jpg" />In your controller
user = User.new user.known_captcha = session[:captcha] user.captcha = params[:captcha] user.save reset_captchacrontab
0 0 * * * cd /path/to/rails/app && /usr/bin/rake RAILS_ENV=production captcha:generateYour config file sets the captcha refresh period. The rake task just checks if its time to repopulate, and does so if necessary.