idi_captcha 1.1.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.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +36 -0
- data/README.md +116 -0
- data/Rakefile +6 -0
- data/app/controllers/idi_captcha/captcha_controller.rb +11 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/config/routes.rb +3 -0
- data/idi_captcha.gemspec +33 -0
- data/lib/idi_captcha/captcha.rb +38 -0
- data/lib/idi_captcha/engine.rb +11 -0
- data/lib/idi_captcha/helpers/view_helper.rb +9 -0
- data/lib/idi_captcha/version.rb +3 -0
- data/lib/idi_captcha.rb +13 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2378387cc52281bb11dbe6b4544801048b9706b6b4c9ef839a78c655a26feebe
|
4
|
+
data.tar.gz: af8c5e69aa2cf7d1f9b5b2dc0ea03e357c0a0a25b09a3cc0b32712deed1b30ca
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d2a84c7d86c83537f3515a51648e88605c5cecdf7ab9b0cd66c18fca300e2e8b2cb85f0a732e2d5bf4992d2f7e4988ad2adf822b3605a0bd216105217254bb5e
|
7
|
+
data.tar.gz: 0bb88c07c4c2a6f6ee774a9a7823c7e762874cc140d00ba7cdac029b03b3b130d6cb0e060f1dd77610d71a56641a6e3b49599b1f1c49ccd147f1b54e70fe3b81
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
idi_captcha (0.4.0)
|
5
|
+
mini_magick (>= 4.9, < 5.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.6.2)
|
11
|
+
mini_magick (4.13.2)
|
12
|
+
rake (12.3.3)
|
13
|
+
rspec (3.13.1)
|
14
|
+
rspec-core (~> 3.13.0)
|
15
|
+
rspec-expectations (~> 3.13.0)
|
16
|
+
rspec-mocks (~> 3.13.0)
|
17
|
+
rspec-core (3.13.5)
|
18
|
+
rspec-support (~> 3.13.0)
|
19
|
+
rspec-expectations (3.13.5)
|
20
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
21
|
+
rspec-support (~> 3.13.0)
|
22
|
+
rspec-mocks (3.13.5)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.13.0)
|
25
|
+
rspec-support (3.13.4)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
idi_captcha!
|
32
|
+
rake (~> 12.0)
|
33
|
+
rspec (~> 3.0)
|
34
|
+
|
35
|
+
BUNDLED WITH
|
36
|
+
2.1.4
|
data/README.md
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
# idi_captcha
|
2
|
+
|
3
|
+
Offline CAPTCHA for Rails 6+ applications that works without any external service. Fully compatible with Devise and designed for restricted or offline environments.
|
4
|
+
|
5
|
+
## 🔧 Installation
|
6
|
+
|
7
|
+
Add this line to your application's **Gemfile**:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'idi_captcha', path: 'relative/path/to/idi_captcha'
|
11
|
+
```
|
12
|
+
|
13
|
+
Then execute:
|
14
|
+
|
15
|
+
```bash
|
16
|
+
bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
Mount the engine in your `config/routes.rb`:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
mount IdiCaptcha::Engine => "/idi_captcha"
|
23
|
+
```
|
24
|
+
|
25
|
+
## 🚀 Features
|
26
|
+
|
27
|
+
- Offline image CAPTCHA (no Google reCAPTCHA)
|
28
|
+
- Compatible with Rails 6+
|
29
|
+
- Session-based validation
|
30
|
+
- Easy to use with Devise
|
31
|
+
- Image served via internal engine controller
|
32
|
+
|
33
|
+
## 🧩 Usage
|
34
|
+
|
35
|
+
### 1. Show CAPTCHA in your form
|
36
|
+
|
37
|
+
For example, in `devise/sessions/new.html.erb`:
|
38
|
+
|
39
|
+
```erb
|
40
|
+
<%= captcha_tag %>
|
41
|
+
|
42
|
+
<div class="field">
|
43
|
+
<%= label_tag :captcha, "Enter CAPTCHA" %>
|
44
|
+
<%= text_field_tag :captcha, nil, required: true, autocomplete: "off" %>
|
45
|
+
</div>
|
46
|
+
```
|
47
|
+
|
48
|
+
### 2. Validate CAPTCHA in your controller
|
49
|
+
|
50
|
+
Override Devise's sessions controller:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
class Users::SessionsController < Devise::SessionsController
|
54
|
+
def create
|
55
|
+
unless IdiCaptcha::Captcha.valid?(session, params[:captcha])
|
56
|
+
flash[:alert] = "Invalid CAPTCHA"
|
57
|
+
redirect_to new_user_session_path and return
|
58
|
+
end
|
59
|
+
|
60
|
+
super
|
61
|
+
end
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
Update routes:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
devise_for :users, controllers: {
|
69
|
+
sessions: 'users/sessions'
|
70
|
+
}
|
71
|
+
```
|
72
|
+
|
73
|
+
## 📂 Gem Structure
|
74
|
+
|
75
|
+
```text
|
76
|
+
idi_captcha/
|
77
|
+
├── lib/
|
78
|
+
│ ├── idi_captcha.rb
|
79
|
+
│ ├── idi_captcha/
|
80
|
+
│ │ ├── captcha.rb
|
81
|
+
│ │ ├── engine.rb
|
82
|
+
│ │ └── helpers/
|
83
|
+
│ │ └── view_helper.rb
|
84
|
+
├── app/
|
85
|
+
│ └── controllers/
|
86
|
+
│ └── idi_captcha/
|
87
|
+
│ └── captcha_controller.rb
|
88
|
+
├── config/
|
89
|
+
│ └── routes.rb
|
90
|
+
```
|
91
|
+
|
92
|
+
## ✅ Requirements
|
93
|
+
|
94
|
+
- Ruby >= 2.3
|
95
|
+
- Rails >= 6.0
|
96
|
+
- mini_magick >= 4.9, < 5.0
|
97
|
+
|
98
|
+
## 🧪 Testing Locally
|
99
|
+
|
100
|
+
```bash
|
101
|
+
gem build idi_captcha.gemspec
|
102
|
+
bundle install
|
103
|
+
rails server
|
104
|
+
```
|
105
|
+
|
106
|
+
Access `http://localhost:3000/idi_captcha/captcha` to verify the CAPTCHA image loads.
|
107
|
+
|
108
|
+
## 👤 Author
|
109
|
+
|
110
|
+
**Idrees Ibrahim**
|
111
|
+
📧 idrees.ibrahim@pitv.gov.pk
|
112
|
+
🔗 [RubyGems.org](https://rubygems.org/gems/idi_captcha)
|
113
|
+
|
114
|
+
## 📝 License
|
115
|
+
|
116
|
+
This gem is licensed under a Nonstandard license.
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
module IdiCaptcha
|
2
|
+
class CaptchaController < ActionController::Base
|
3
|
+
def show
|
4
|
+
if IdiCaptcha.mode == :math
|
5
|
+
render plain: "Math CAPTCHA enabled. Nothing to show here."
|
6
|
+
else
|
7
|
+
send_data IdiCaptcha::Captcha.generate(session), type: "image/png", disposition: "inline"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "idi_captcha"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/config/routes.rb
ADDED
data/idi_captcha.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative 'lib/idi_captcha/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "idi_captcha"
|
5
|
+
spec.version = IdiCaptcha::VERSION
|
6
|
+
spec.authors = ["idrees ibrahim"]
|
7
|
+
spec.email = ["idrees.ibrahim@pitb.gov.pk"]
|
8
|
+
|
9
|
+
spec.summary = "Offline CAPTCHA for Rails apps"
|
10
|
+
spec.description = "A simple image-based CAPTCHA engine for Rails 6+, works offline and supports Devise integration"
|
11
|
+
spec.homepage = "https://rubygems.org/gems/idi_captcha"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
# Add dependency
|
14
|
+
spec.add_dependency 'mini_magick', '>= 4.9', '< 5.0'
|
15
|
+
|
16
|
+
spec.metadata['base_path'] = File.expand_path('..', __FILE__)
|
17
|
+
|
18
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
19
|
+
|
20
|
+
# spec.metadata["homepage_uri"] = spec.homepage
|
21
|
+
# spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
|
22
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
23
|
+
|
24
|
+
# Specify which files should be added to the gem when it is released.
|
25
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
26
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
27
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
spec.license = "Nonstandard"
|
33
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module IdiCaptcha
|
2
|
+
class Captcha
|
3
|
+
def self.generate(session)
|
4
|
+
if IdiCaptcha.mode == :math
|
5
|
+
a = rand(1..9)
|
6
|
+
b = rand(1..9)
|
7
|
+
session[:captcha_answer] = a + b
|
8
|
+
"#{a} + #{b}"
|
9
|
+
else
|
10
|
+
require "mini_magick"
|
11
|
+
|
12
|
+
text = 5.times.map { ('A'..'Z').to_a.sample }.join
|
13
|
+
session[:captcha_text] = text
|
14
|
+
|
15
|
+
# Use MiniMagick::Tool::Convert to generate the image safely
|
16
|
+
MiniMagick::Tool::Convert.new do |c|
|
17
|
+
c.size "150x50"
|
18
|
+
c.gravity "center"
|
19
|
+
c.xc "white"
|
20
|
+
c.fill "black"
|
21
|
+
c.pointsize "32"
|
22
|
+
# Optional: set font if available
|
23
|
+
# c.font "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
|
24
|
+
c.draw "text 0,0 '#{text}'"
|
25
|
+
c << "png:-" # Output as binary blob
|
26
|
+
end.call
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.valid?(session, input)
|
31
|
+
if IdiCaptcha.mode == :math
|
32
|
+
session[:captcha_answer].to_i == input.to_i
|
33
|
+
else
|
34
|
+
session[:captcha_text].to_s.upcase == input.to_s.upcase
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/idi_captcha.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "idi_captcha/version"
|
2
|
+
require "idi_captcha/engine"
|
3
|
+
require "idi_captcha/captcha"
|
4
|
+
require "idi_captcha/helpers/view_helper"
|
5
|
+
|
6
|
+
module IdiCaptcha
|
7
|
+
mattr_accessor :mode
|
8
|
+
self.mode = :image # or :math
|
9
|
+
|
10
|
+
def self.setup
|
11
|
+
yield self
|
12
|
+
end
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: idi_captcha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- idrees ibrahim
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-07-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mini_magick
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.9'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.9'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5.0'
|
33
|
+
description: A simple image-based CAPTCHA engine for Rails 6+, works offline and supports
|
34
|
+
Devise integration
|
35
|
+
email:
|
36
|
+
- idrees.ibrahim@pitb.gov.pk
|
37
|
+
executables: []
|
38
|
+
extensions: []
|
39
|
+
extra_rdoc_files: []
|
40
|
+
files:
|
41
|
+
- ".gitignore"
|
42
|
+
- ".rspec"
|
43
|
+
- ".travis.yml"
|
44
|
+
- Gemfile
|
45
|
+
- Gemfile.lock
|
46
|
+
- README.md
|
47
|
+
- Rakefile
|
48
|
+
- app/controllers/idi_captcha/captcha_controller.rb
|
49
|
+
- bin/console
|
50
|
+
- bin/setup
|
51
|
+
- config/routes.rb
|
52
|
+
- idi_captcha.gemspec
|
53
|
+
- lib/idi_captcha.rb
|
54
|
+
- lib/idi_captcha/captcha.rb
|
55
|
+
- lib/idi_captcha/engine.rb
|
56
|
+
- lib/idi_captcha/helpers/view_helper.rb
|
57
|
+
- lib/idi_captcha/version.rb
|
58
|
+
homepage: https://rubygems.org/gems/idi_captcha
|
59
|
+
licenses:
|
60
|
+
- Nonstandard
|
61
|
+
metadata:
|
62
|
+
base_path: "/home/idrees/rorapps/idi_captcha"
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: 2.3.0
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubygems_version: 3.1.2
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Offline CAPTCHA for Rails apps
|
82
|
+
test_files: []
|