Giter Site home page Giter Site logo

benjohnson77 / rack-recaptcha Goto Github PK

View Code? Open in Web Editor NEW

This project forked from achiurizo/rack-recaptcha

0.0 1.0 0.0 796 KB

Rack Middleware for CAPTCHA verification via Recaptcha API

Home Page: twitter.com/arthur_chiu

License: MIT License

Ruby 100.00%

rack-recaptcha's Introduction

rack-recaptcha

Build Status

Drop this Rack middleware in your web application to enable CAPTCHA verification via Recaptcha API.

How to Use

Configuration

First, install the library with: [sudo] gem install rack-recaptcha

You have to require 'rack-recaptcha' in your gemfile.

## Gemfile
gem 'rack-recaptcha', :require => 'rack/recaptcha'
Available options for `Rack::Recaptcha` middleware are:

* :public_key -- your ReCaptcha API public key *(required)*
* :private_key -- your ReCaptcha API private key *(required)*
* :proxy_host -- the HTTP Proxy hostname *(optional)*
* :proxy_port -- the HTTP Proxy port *(optional)*
* :proxy_user -- the HTTP Proxy user *(optional, omit unless the proxy requires it)*
* :proxy_password -- the HTTP Proxy password *(optional, omit unless the proxy requires it)*

Now configure your app to use the middleware. This might be different across each web framework.

Sinatra

## app.rb
use Rack::Recaptcha, :public_key => 'KEY', :private_key => 'SECRET'
helpers Rack::Recaptcha::Helpers

Padrino

## app/app.rb
use Rack::Recaptcha, :public_key => 'KEY', :private_key => 'SECRET'
helpers Rack::Recaptcha::Helpers

Rails

## application.rb:
module YourRailsAppName
  class Application < Rails::Application
    ...
    config.gem 'rack-recaptcha', :lib => 'rack/recaptcha'
    config.middleware.use Rack::Recaptcha, :public_key => 'KEY', :private_key => 'SECRET'
  end
end

## application_helper.rb or whatever helper you want it in.
module ApplicationHelper
  include Rack::Recaptcha::Helpers
end

## application_controller.rb or whatever controller you want it in.
class ApplicationController < ActionController::Base
  ...
  include Rack::Recaptcha::Helpers
  ...
end

Helpers

The Rack::Recaptcha::Helpers module (for Sinatra, Rails, Padrino) adds these methods to your app:

Return a javascript recaptcha form

  recaptcha_tag :challenge

Return a non-javascript recaptcha form

  recaptcha_tag :noscript

Return a ajax recaptcha form

  recaptcha_tag :ajax

For ajax recaptcha's, you can pass additional options like:

  recaptcha_tag :ajax, :display => { :theme => 'red'}

For non-ajax recaptcha's, you can pass additional options like:

  # Overrides the key set in Middleware
  recaptcha_tag :challenge, :public_key => KEY

  # Adjust Height and/or Width
  recaptcha_tag :noscript, :height => 300, :width => 500

  # Adjust the rows and/or columns
  recaptcha_tag :challenge, :row => 3, :cols => 5

  # Set the language
  recaptcha_tag :noscript, :language => :en

To test whether or not the verification passed, you can use:

  recaptcha_valid?

The recaptcha_valid? helper can also be overloaded during tests. You can set its response to either true or false by doing the following:

 # Have recaptcha_valid? return true
 Rack::Recaptcha.test_mode!

 # Or have it return false
 Rack::Recaptcha.test_mode! :return => false

For additional options and resources checkout the customization page

Example

In Padrino, here's how you would use the helpers.

## new.haml
- form_tag '/login', :class => 'some_form', :method => 'post' do
  = text_field_tag :email
  = password_field_tag :password
  = recaptcha_tag(:challenge)
  = submit_tag "Submit"

## sessions.rb
post :create, :map => '/login' do
  if recaptcha_valid?
    "passed!"
    else
    "failed!"
  end
end

In rails, you'll need to use also use the raw method:

## new.html.haml
- form_tag '/login' do
  = raw recaptcha_tag(:challenge)
  = submit_tag "Submit"

Contributors

Daniel Mendler - minad

  • support for multiple paths and helpers clean up

Eric Anderson - eric1234

  • Make verify independently usable.

Chad Johnston - iamthechad

  • Adding Error Message handling in recaptcha widget

Eric Hu - eric-hu

  • Patching error message issue when no request is present

Tobias Begalke - elcamino

  • Added HTTP Proxy support

Julik Tarkhanov - julik

  • Adding rack-recaptcha to travis-ci

Rob Worley - robworley

  • Adding language setting for recaptcha form

sam71 - sam71

  • Fix recaptcha hanging when used in proxy environment.

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright (c) 2010, 2011, 2012 Arthur Chiu. See LICENSE for details.

rack-recaptcha's People

Contributors

achiurizo avatar iamthechad avatar eric1234 avatar julik avatar robworley avatar eric-hu avatar sam71 avatar

Watchers

James Cloos 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.