Giter Site home page Giter Site logo

kylelkh / rucaptcha Goto Github PK

View Code? Open in Web Editor NEW

This project forked from huacnlee/rucaptcha

1.0 1.0 0.0 102 KB

This is a Captcha gem for Rails Application. It run ImageMagick command to draw Captcha image.

Home Page: http://huacnlee.com/rucaptcha

License: MIT License

Ruby 100.00%

rucaptcha's Introduction

RuCaptcha

Gem Version Build Status Code Climate

This is a Captcha gem for Rails Applications. It runs an ImageMagick command to draw Captcha image - so it has NO performance issues or memory leak issues. There is NO: RMagick

Example

1 2 3 4 5 6 7

Idea by: https://ruby-china.org/topics/20558#reply4

中文介绍和使用说明

Feature

  • Only need ImageMagick, No RMagick, No mini_magick;
  • For Rails Application;
  • Simple, Easy to use;
  • File Caching for performance.

Requirements

  • ImageMagick 6.9+

Ubuntu

sudo apt-get install imagemagick ghostscript

Mac OS X

brew install imagemagick ghostscript

Usage

Security Notice!

You need change your application Session store from CookieStore (Rails default) to backend store location for avoid Session Replay Attacks security issue.

config/initializers/session_store.rb

Rails.application.config.session_store :redis_session_store, { ... }

Put rucaptcha in your Gemfile:

gem 'rucaptcha'

Create config/initializers/rucaptcha.rb

RuCaptcha.configure do
  # Number of chars, default: 4
  self.len = 4
  # Image font size, default: 45
  self.font_size = 45
  # Cache generated images in file store, this is config files limit, default: 100
  # set 0 to disable file cache.
  self.cache_limit = 100
  # Custom captcha code expire time if you need, default: 2 minutes
  # self.expires_in = 120
  # Color style, default: :colorful, allows: [:colorful, :black_white]
  # self.style = :colorful
end

Edit config/routes.rb, add the following code:

Rails.application.routes.draw do
  ...
  mount RuCaptcha::Engine => "/rucaptcha"
  ...
end

Controller app/controller/account_controller.rb

class AccountController < ApplicationController
  def create
    @user = User.new(params[:user])
    if verify_rucaptcha?(@user) && @user.save
      redirect_to root_path, notice: 'Sign up successed.'
    else
      render 'account/new'
    end
  end
end

View app/views/account/new.html.erb

<form>
  ...
  <div class="form-group">
    <%= rucaptcha_input_tag(class: 'form-control', placeholder: 'Input Captcha') %>
    <%= rucaptcha_image_tag(alt: 'Captcha') %>
  </div>
  ...
</form>

And if you are use Devise, you can read this to add validation: RuCaptcha with Devise.

Write your test skip captcha validation

for RSpec

describe 'sign up and login', type: :feature do
  before do
    allow_any_instance_of(ActionController::Base).to receive(:verify_rucaptcha?).and_return(true)
  end

  it { ... }
end

for MiniTest

class ActionDispatch::IntegrationTest
  def sign_in(user)
    ActionController::Base.any_instance.stubs(:verify_rucaptcha?).returns(true)
    post user_session_path \
         'user[email]'    => user.email,
         'user[password]' => user.password
  end
end

rucaptcha's People

Contributors

huacnlee avatar abookyun avatar eric-guo avatar anvyzhang avatar haibinyu avatar hiveer avatar sonic182 avatar iamphill avatar ramirovarandas avatar flypiggy avatar kamionayuki avatar chechaoyang avatar

Stargazers

Kyle Liu avatar

Watchers

Kyle Liu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.