simple_captcha_reloaded 0.0.1.beta1 → 0.1.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +194 -0
- data/app/models/simple_captcha_reloaded/data.rb +9 -0
- data/app/views/simple_captcha_reloaded/_simple_captcha_reloaded.html.erb +8 -0
- data/lib/simple_captcha_reloaded/adapters/simple_form.rb +10 -18
- data/lib/simple_captcha_reloaded/controller_helper.rb +7 -0
- data/lib/simple_captcha_reloaded/engine.rb +6 -0
- data/lib/simple_captcha_reloaded/model.rb +1 -1
- data/lib/simple_captcha_reloaded/version.rb +1 -1
- data/lib/simple_captcha_reloaded/view_helper.rb +17 -0
- data/lib/simple_captcha_reloaded.rb +11 -0
- metadata +6 -3
- data/app/views/layouts/simple_captcha_reloaded/application.html.erb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f4126818899ea31db41b60c51a036065f0fde3c
|
4
|
+
data.tar.gz: 479910c2d5ad2b20c5e0f9f3eeb0c3d1a693c0bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f69f5ad7cf1e8737d8ccdf91e4b1788d00d3235f80882b20a42a58c5cb598247a8691e316f0771a5342047089d215ab3e11a34d2001f3b86cc43ccdfc822b0f
|
7
|
+
data.tar.gz: 901e27f5873bed57c56ee6d637d49ebb78ece64bed5226ebef34678c4f8b19ca7347f30fb82e4432688459fd99a2c6150b333d14753e562e7cb7277e0de6d553
|
data/README.md
ADDED
@@ -0,0 +1,194 @@
|
|
1
|
+
# Simple Captcha Reloaded
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/zealot128/simple_captcha_reloaded.svg?branch=master)](https://travis-ci.org/zealot128/simple_captcha_reloaded)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/simple_captcha_reloaded.svg)](http://badge.fury.io/rb/simple_captcha_reloaded)
|
5
|
+
|
6
|
+
This is a rewrite of the popular Simple-Captcha Gem. Similarily to Simple-Captcha, it provides an easy way to integrate a Captcha into a Rails appliaction. In comparison to the older Gem(s), I decided to drop support for ancient versions of Rails + Formtastic + Mongoid, but also add specs and support for SimpleForm.
|
7
|
+
|
8
|
+
## Features
|
9
|
+
|
10
|
+
* Works with Rails 4.1+, Ruby 2.0+ (keyword arguments)
|
11
|
+
* Integrated into Model validation flow
|
12
|
+
* Optional controller integration for custom flow
|
13
|
+
* Uses a database table to track random captchas
|
14
|
+
* Uses imagemagick to generate the captcha and stream it to the client, without file access
|
15
|
+
|
16
|
+
## Prerequisites
|
17
|
+
|
18
|
+
This needs Rails (4.1+), ActiveRecord with a database (all should be fine) and imagemagick.
|
19
|
+
|
20
|
+
Mac:
|
21
|
+
|
22
|
+
```
|
23
|
+
brew install imagemagick ghostscript
|
24
|
+
```
|
25
|
+
|
26
|
+
Linux:
|
27
|
+
|
28
|
+
```
|
29
|
+
apt-get install imagemagick ghostscript
|
30
|
+
```
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
Put it into your Gemfile:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
gem 'simple_captcha_reloaded'
|
38
|
+
```
|
39
|
+
|
40
|
+
and run ``bundle install``.
|
41
|
+
|
42
|
+
|
43
|
+
### Integration 1: Model based
|
44
|
+
|
45
|
+
Just integrate the module into on of your ActiveModel::Model compliant Models:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
class Message < ActiveRecord::Base
|
49
|
+
include SimpleCaptchaReloaded::Model
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
this adds 2 new methods: ``valid_with_captcha?`` and ``save_with_captcha`` to the model. Use it like this in controller:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class MessagesController < ApplicationController
|
57
|
+
def submit
|
58
|
+
@message = Message.new(message_params)
|
59
|
+
if @message.save_with_captcha
|
60
|
+
...
|
61
|
+
else
|
62
|
+
render :new
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def message_params
|
67
|
+
params.require(:message).permit(..., :captcha, :captcha_key)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
Make sure to whiteliste the attributes ``captcha`` and ``captcha_key`` with Strong Parameters.
|
73
|
+
|
74
|
+
To show the captcha, you can use the SimpleForm helper:
|
75
|
+
|
76
|
+
### Simple Form
|
77
|
+
|
78
|
+
The Gem provides a custom input for SimpleForm:
|
79
|
+
|
80
|
+
```slim
|
81
|
+
= simple_form_for @model do |f|
|
82
|
+
= f.input :captcha, as: :simple_captcha
|
83
|
+
= f.submit
|
84
|
+
```
|
85
|
+
|
86
|
+
### Formtastic
|
87
|
+
|
88
|
+
PLANNED
|
89
|
+
|
90
|
+
### Manual by using default Rails view helper
|
91
|
+
|
92
|
+
Similar to "Displaying Captcha" below, just change the key and value calls of the ``simple_captcha`` helper method to match your model:
|
93
|
+
|
94
|
+
```slim
|
95
|
+
= simple_captcha(key: 'user[captcha_key]', value: 'user[captcha]', refresh_button: true)
|
96
|
+
```
|
97
|
+
|
98
|
+
### Integration II - Controller
|
99
|
+
|
100
|
+
If you need to use the captcha without a model, you can do the show and validation by hand
|
101
|
+
|
102
|
+
## Displaying Captcha
|
103
|
+
|
104
|
+
Within your form, call the function:
|
105
|
+
|
106
|
+
```slim
|
107
|
+
= form_tag '' do |f|
|
108
|
+
= simple_captcha(key: :captcha_key, value: :captcha, refresh_button: true)
|
109
|
+
= submit_tag 'send'
|
110
|
+
```
|
111
|
+
|
112
|
+
This method is a thin wrapper which will render the view ``simple_captcha_reloaded/simple_captcha_reloaded.html`` in the end. As this is a Rails engine, you can overwrite the view by creating a ``app/views/simple_captcha_reloaded/_simple_captcha_reloaded.html.erb`` in your project tree.
|
113
|
+
|
114
|
+
Adjust it to your needs:
|
115
|
+
|
116
|
+
```erb
|
117
|
+
<div class='simple-captcha-wrapper' id='<%=id %>'>
|
118
|
+
<%= hidden_field_tag key, captcha[:captcha_id] %>
|
119
|
+
<%= image_tag captcha[:captcha_url], class: 'simple-captcha-image' %>
|
120
|
+
<%= text_field_tag value, '', class: 'simple-captcha-input' %>
|
121
|
+
<%- if refresh_button %>
|
122
|
+
<%=link_to t('simple_captcha_reloaded.refresh_button_html'), captcha[:refresh_url], data: { remote: true} %>
|
123
|
+
<% end %>
|
124
|
+
</div>
|
125
|
+
```
|
126
|
+
|
127
|
+
### Manual Validation
|
128
|
+
|
129
|
+
Now you can call a method in your controller to check if the captcha is valid:
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
def submit
|
133
|
+
if captcha_valid?(params[:captcha_key], params[:captcha])
|
134
|
+
..
|
135
|
+
else
|
136
|
+
... error
|
137
|
+
end
|
138
|
+
end
|
139
|
+
```
|
140
|
+
|
141
|
+
## Customizing
|
142
|
+
|
143
|
+
### Captcha Options
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
# Default Options
|
147
|
+
SimpleCaptchaReloaded::Config.tap do |config|
|
148
|
+
config.captcha_path = '/simple_captcha'
|
149
|
+
config.image = SimpleCaptchaReloaded::Image.new
|
150
|
+
config.characters = %w[a b c d e f g h j k m n p q r s t u v w x y z 0 2 3 4 5 6 8 9]
|
151
|
+
config.length = 6
|
152
|
+
end
|
153
|
+
```
|
154
|
+
|
155
|
+
To change the appearance of the Captcha, initialize a SimpleCaptchaReloaded::Image.new with different parameters (default parameters shown):
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
config.image = SimpleCaptchaReloaded::Image.new(implode: :medium,
|
159
|
+
distortion: :random,
|
160
|
+
image_styles: IMAGE_STYLES.slice('simply_red', 'simply_green', 'simply_blue'),
|
161
|
+
noise: 0,
|
162
|
+
size: '100x28',
|
163
|
+
image_magick_path: '',
|
164
|
+
tmp_path: nil
|
165
|
+
)
|
166
|
+
|
167
|
+
# whereas IMAGE_STYLES are:
|
168
|
+
IMAGE_STYLES = {
|
169
|
+
'embosed_silver' => ['-fill darkblue', '-shade 20x60', '-background white'],
|
170
|
+
'simply_red' => ['-fill darkred', '-background white'],
|
171
|
+
'simply_green' => ['-fill darkgreen', '-background white'],
|
172
|
+
'simply_blue' => ['-fill darkblue', '-background white'],
|
173
|
+
'distorted_black' => ['-fill darkblue', '-edge 10', '-background white'],
|
174
|
+
'all_black' => ['-fill darkblue', '-edge 2', '-background white'],
|
175
|
+
'charcoal_grey' => ['-fill darkblue', '-charcoal 5', '-background white'],
|
176
|
+
'almost_invisible' => ['-fill red', '-solarize 50', '-background white']
|
177
|
+
}
|
178
|
+
```
|
179
|
+
|
180
|
+
|
181
|
+
## Contributing
|
182
|
+
|
183
|
+
Report bugs/feature requests to the Github issues.
|
184
|
+
If you'd like to contribute, here a little setup guide.
|
185
|
+
The Gem uses **Appraisal**, to create various configuration Gem sets. For each Gem set, a new dummy app will be automatically created.
|
186
|
+
|
187
|
+
1. Fork, and clone
|
188
|
+
2. ``bundle``
|
189
|
+
3. ``appraisal install``
|
190
|
+
4. ``appraisal rails-stable make regenerate`` To build the dummy app with the Rails-Stable configuration
|
191
|
+
4. ``appraisal rails-stable rspec`` To run tests
|
192
|
+
|
193
|
+
If you need to add new controller methods to the dummy app, have a look at ``spec/template.rb``
|
194
|
+
|
@@ -11,6 +11,15 @@ module SimpleCaptchaReloaded
|
|
11
11
|
key
|
12
12
|
end
|
13
13
|
|
14
|
+
def self.valid_captcha?(key,value)
|
15
|
+
element = find_by_key(key)
|
16
|
+
value && element && element.valid_captcha?(value)
|
17
|
+
end
|
18
|
+
|
19
|
+
def valid_captcha?(other)
|
20
|
+
other.present? and value == other.strip.downcase
|
21
|
+
end
|
22
|
+
|
14
23
|
def self.clear
|
15
24
|
SimpleCaptchaReloaded::Data.where('created_at < ?', 1.hour.ago).delete_all
|
16
25
|
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
<div class='simple-captcha-wrapper' id='<%=id %>'>
|
2
|
+
<%= hidden_field_tag key, captcha[:captcha_id] %>
|
3
|
+
<%= image_tag captcha[:captcha_url], class: 'simple-captcha-image' %>
|
4
|
+
<%= text_field_tag value, '', class: 'simple-captcha-input' %>
|
5
|
+
<%- if refresh_button %>
|
6
|
+
<%=link_to t('simple_captcha_reloaded.refresh_button_html'), captcha[:refresh_url], data: { remote: true} %>
|
7
|
+
<% end %>
|
8
|
+
</div>
|
@@ -2,7 +2,7 @@ require 'simple_form/version'
|
|
2
2
|
class SimpleCaptchaInput < SimpleForm::Inputs::StringInput
|
3
3
|
def input(wrapper_options=nil)
|
4
4
|
set_options
|
5
|
-
|
5
|
+
@captcha = SimpleCaptchaReloaded.generate_captcha(id: options[:captcha][:id], request: template.request)
|
6
6
|
|
7
7
|
if SimpleForm::VERSION[/^3\.0/]
|
8
8
|
input = super()
|
@@ -10,13 +10,13 @@ class SimpleCaptchaInput < SimpleForm::Inputs::StringInput
|
|
10
10
|
input = super
|
11
11
|
end
|
12
12
|
refresh = if options[:captcha][:refresh_button]
|
13
|
-
refresh_button(
|
13
|
+
refresh_button(@captcha)
|
14
14
|
else
|
15
15
|
""
|
16
16
|
end
|
17
17
|
[
|
18
|
-
image_tag(
|
19
|
-
captcha_key(
|
18
|
+
image_tag(@captcha),
|
19
|
+
captcha_key(@captcha),
|
20
20
|
input,
|
21
21
|
refresh
|
22
22
|
].join.html_safe
|
@@ -24,10 +24,9 @@ class SimpleCaptchaInput < SimpleForm::Inputs::StringInput
|
|
24
24
|
|
25
25
|
protected
|
26
26
|
|
27
|
-
def refresh_button(
|
27
|
+
def refresh_button(captcha)
|
28
28
|
template.content_tag :div, class: 'simple-captcha-reload' do
|
29
|
-
|
30
|
-
template.link_to url, class: options[:captcha][:refresh_button_class], data: {remote: true} do
|
29
|
+
template.link_to captcha[:refresh_url], class: options[:captcha][:refresh_button_class], data: {remote: true} do
|
31
30
|
I18n.t('simple_captcha_reloaded.refresh_button_html')
|
32
31
|
end
|
33
32
|
end
|
@@ -40,19 +39,12 @@ class SimpleCaptchaInput < SimpleForm::Inputs::StringInput
|
|
40
39
|
options[:wrapper_html][:id] ||= options[:captcha][:id]
|
41
40
|
end
|
42
41
|
|
43
|
-
def
|
44
|
-
|
45
|
-
SimpleCaptchaReloaded::Data.generate_captcha_id(old_key: old_key)
|
42
|
+
def image_tag(captcha)
|
43
|
+
template.content_tag(:img, nil, src: captcha[:captcha_url], alt: 'Captcha', class: 'simple-captcha-image')
|
46
44
|
end
|
47
45
|
|
48
|
-
|
49
|
-
|
50
|
-
url = SimpleCaptchaReloaded::Config.image_url(code, template.request)
|
51
|
-
template.content_tag(:img, nil, src: url, alt: 'Captcha', class: 'simple-captcha-image')
|
52
|
-
end
|
53
|
-
|
54
|
-
def captcha_key(code)
|
55
|
-
@builder.hidden_field :captcha_key, value: code
|
46
|
+
def captcha_key(captcha)
|
47
|
+
@builder.hidden_field :captcha_key, value: captcha[:captcha_id]
|
56
48
|
end
|
57
49
|
|
58
50
|
def default_options
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'simple_captcha_reloaded/view_helper'
|
2
|
+
require 'simple_captcha_reloaded/controller_helper'
|
1
3
|
module SimpleCaptchaReloaded
|
2
4
|
class Engine < ::Rails::Engine
|
3
5
|
isolate_namespace SimpleCaptchaReloaded
|
@@ -5,6 +7,10 @@ module SimpleCaptchaReloaded
|
|
5
7
|
if defined?(SimpleForm)
|
6
8
|
require 'simple_captcha_reloaded/adapters/simple_form'
|
7
9
|
end
|
10
|
+
ActiveSupport.on_load :action_controller do
|
11
|
+
helper SimpleCaptchaReloaded::ViewHelper
|
12
|
+
ActionController::Base.send(:include, SimpleCaptchaReloaded::ControllerHelper)
|
13
|
+
end
|
8
14
|
app.middleware.use SimpleCaptchaReloaded::Middleware
|
9
15
|
end
|
10
16
|
|
@@ -19,7 +19,7 @@ module SimpleCaptchaReloaded::Model
|
|
19
19
|
elsif !captcha.present?
|
20
20
|
errors.add :captcha, I18n.t('simple_captcha_reloaded.errors.blank')
|
21
21
|
false
|
22
|
-
elsif data.
|
22
|
+
elsif not data.valid_captcha?(captcha)
|
23
23
|
errors.add :captcha, I18n.t('simple_captcha_reloaded.errors.wrong')
|
24
24
|
false
|
25
25
|
else
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module SimpleCaptchaReloaded
|
2
|
+
module ViewHelper
|
3
|
+
# key: params[key] for the captcha id
|
4
|
+
# value: params[value] for the user-answer
|
5
|
+
# refresh_button: show refresh button
|
6
|
+
# id: wrapper id for the element, to refresh the captcha
|
7
|
+
def simple_captcha(key: :captcha_key, value: :captcha, refresh_button: true, id: 'simple_captcha_wrapper')
|
8
|
+
captcha = SimpleCaptchaReloaded.generate_captcha(id: id, request: request)
|
9
|
+
render 'simple_captcha_reloaded/simple_captcha_reloaded',
|
10
|
+
key: key,
|
11
|
+
value: value,
|
12
|
+
captcha: captcha,
|
13
|
+
refresh_button: refresh_button,
|
14
|
+
id: id
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -5,4 +5,15 @@ require 'simple_captcha_reloaded/model'
|
|
5
5
|
require 'simple_captcha_reloaded/middleware'
|
6
6
|
|
7
7
|
module SimpleCaptchaReloaded
|
8
|
+
def self.generate_captcha(id:, request:, old_key: request.session[:captcha])
|
9
|
+
captcha_id = SimpleCaptchaReloaded::Data.generate_captcha_id(old_key: old_key)
|
10
|
+
captcha_url = SimpleCaptchaReloaded::Config.image_url(captcha_id, request)
|
11
|
+
refresh_url = SimpleCaptchaReloaded::Config.refresh_url(request, id)
|
12
|
+
{
|
13
|
+
captcha_id: captcha_id,
|
14
|
+
captcha_url: captcha_url,
|
15
|
+
refresh_url: refresh_url
|
16
|
+
}
|
17
|
+
|
18
|
+
end
|
8
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_captcha_reloaded
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.1.0.beta1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Wienert
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -186,13 +186,14 @@ extensions: []
|
|
186
186
|
extra_rdoc_files: []
|
187
187
|
files:
|
188
188
|
- MIT-LICENSE
|
189
|
+
- README.md
|
189
190
|
- Rakefile
|
190
191
|
- app/assets/javascripts/simple_captcha_reloaded/application.js
|
191
192
|
- app/assets/stylesheets/simple_captcha_reloaded/application.css
|
192
193
|
- app/controllers/simple_captcha_reloaded/application_controller.rb
|
193
194
|
- app/helpers/simple_captcha_reloaded/application_helper.rb
|
194
195
|
- app/models/simple_captcha_reloaded/data.rb
|
195
|
-
- app/views/
|
196
|
+
- app/views/simple_captcha_reloaded/_simple_captcha_reloaded.html.erb
|
196
197
|
- config/environment.rb
|
197
198
|
- config/locales/simple_captcha.en.yml
|
198
199
|
- config/routes.rb
|
@@ -200,11 +201,13 @@ files:
|
|
200
201
|
- lib/simple_captcha_reloaded.rb
|
201
202
|
- lib/simple_captcha_reloaded/adapters/simple_form.rb
|
202
203
|
- lib/simple_captcha_reloaded/config.rb
|
204
|
+
- lib/simple_captcha_reloaded/controller_helper.rb
|
203
205
|
- lib/simple_captcha_reloaded/engine.rb
|
204
206
|
- lib/simple_captcha_reloaded/image.rb
|
205
207
|
- lib/simple_captcha_reloaded/middleware.rb
|
206
208
|
- lib/simple_captcha_reloaded/model.rb
|
207
209
|
- lib/simple_captcha_reloaded/version.rb
|
210
|
+
- lib/simple_captcha_reloaded/view_helper.rb
|
208
211
|
- lib/tasks/simple_captcha_reloaded_tasks.rake
|
209
212
|
homepage: https://github.com/zealot128/simple_captcha_reloaded
|
210
213
|
licenses:
|
@@ -1,14 +0,0 @@
|
|
1
|
-
<!DOCTYPE html>
|
2
|
-
<html>
|
3
|
-
<head>
|
4
|
-
<title>SimpleCaptchaReloaded</title>
|
5
|
-
<%= stylesheet_link_tag "simple_captcha_reloaded/application", media: "all" %>
|
6
|
-
<%= javascript_include_tag "simple_captcha_reloaded/application" %>
|
7
|
-
<%= csrf_meta_tags %>
|
8
|
-
</head>
|
9
|
-
<body>
|
10
|
-
|
11
|
-
<%= yield %>
|
12
|
-
|
13
|
-
</body>
|
14
|
-
</html>
|