rucaptcha 2.5.0 → 2.5.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +147 -83
- data/README.md +6 -2
- data/app/controllers/ru_captcha/captcha_controller.rb +4 -3
- data/config/locales/rucaptcha.en.yml +1 -1
- data/config/locales/rucaptcha.zh-CN.yml +1 -1
- data/config/locales/rucaptcha.zh-TW.yml +1 -1
- data/config/routes.rb +1 -1
- data/ext/rucaptcha/colors.h +265 -18
- data/ext/rucaptcha/extconf.rb +2 -2
- data/ext/rucaptcha/rucaptcha.c +172 -111
- data/lib/rucaptcha.rb +23 -24
- data/lib/rucaptcha/cache.rb +2 -1
- data/lib/rucaptcha/controller_helpers.rb +15 -17
- data/lib/rucaptcha/engine.rb +2 -2
- data/lib/rucaptcha/errors/configuration.rb +1 -1
- data/lib/rucaptcha/version.rb +1 -1
- data/lib/rucaptcha/view_helpers.rb +8 -8
- metadata +7 -7
data/lib/rucaptcha.rb
CHANGED
@@ -1,19 +1,20 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
10
|
-
require
|
11
|
-
require
|
1
|
+
require "rails"
|
2
|
+
require "action_controller"
|
3
|
+
require "active_support/all"
|
4
|
+
require "rucaptcha/rucaptcha"
|
5
|
+
require "rucaptcha/version"
|
6
|
+
require "rucaptcha/configuration"
|
7
|
+
require "rucaptcha/controller_helpers"
|
8
|
+
require "rucaptcha/view_helpers"
|
9
|
+
require "rucaptcha/cache"
|
10
|
+
require "rucaptcha/engine"
|
11
|
+
require "rucaptcha/errors/configuration"
|
12
12
|
|
13
13
|
module RuCaptcha
|
14
14
|
class << self
|
15
15
|
def config
|
16
16
|
return @config if defined?(@config)
|
17
|
+
|
17
18
|
@config = Configuration.new
|
18
19
|
@config.style = :colorful
|
19
20
|
@config.length = 5
|
@@ -22,11 +23,11 @@ module RuCaptcha
|
|
22
23
|
@config.expires_in = 2.minutes
|
23
24
|
@config.skip_cache_store_check = false
|
24
25
|
|
25
|
-
if Rails.application
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
26
|
+
@config.cache_store = if Rails.application
|
27
|
+
Rails.application.config.cache_store
|
28
|
+
else
|
29
|
+
:mem_cache_store
|
30
|
+
end
|
30
31
|
@config.cache_store
|
31
32
|
@config
|
32
33
|
end
|
@@ -35,24 +36,22 @@ module RuCaptcha
|
|
35
36
|
config.instance_exec(&block)
|
36
37
|
end
|
37
38
|
|
38
|
-
def generate
|
39
|
+
def generate
|
39
40
|
style = config.style == :colorful ? 1 : 0
|
40
41
|
length = config.length
|
41
42
|
|
42
|
-
unless length.in?(3..7)
|
43
|
-
raise Rucaptcha::Errors::Configuration, 'length config error, value must in 3..7'
|
44
|
-
end
|
43
|
+
raise RuCaptcha::Errors::Configuration, "length config error, value must in 3..7" unless length.in?(3..7)
|
45
44
|
|
46
45
|
strikethrough = config.strikethrough ? 1 : 0
|
47
46
|
outline = config.outline ? 1 : 0
|
48
|
-
|
47
|
+
create(style, length, strikethrough, outline)
|
49
48
|
end
|
50
49
|
|
51
50
|
def check_cache_store!
|
52
51
|
cache_store = RuCaptcha.config.cache_store
|
53
52
|
store_name = cache_store.is_a?(Array) ? cache_store.first : cache_store
|
54
|
-
if [
|
55
|
-
RuCaptcha.config.cache_store = [:file_store, Rails.root.join(
|
53
|
+
if %i[memory_store null_store file_store].include?(store_name)
|
54
|
+
RuCaptcha.config.cache_store = [:file_store, Rails.root.join("tmp/cache/rucaptcha/session")]
|
56
55
|
|
57
56
|
puts "
|
58
57
|
|
@@ -72,7 +71,7 @@ module RuCaptcha
|
|
72
71
|
end
|
73
72
|
|
74
73
|
ActiveSupport.on_load(:action_controller) do
|
75
|
-
ActionController::Base.
|
74
|
+
ActionController::Base.include RuCaptcha::ControllerHelpers
|
76
75
|
end
|
77
76
|
|
78
77
|
ActiveSupport.on_load(:action_view) do
|
data/lib/rucaptcha/cache.rb
CHANGED
@@ -10,12 +10,16 @@ module RuCaptcha
|
|
10
10
|
def rucaptcha_sesion_key_key
|
11
11
|
session_id = session.respond_to?(:id) ? session.id : session[:session_id]
|
12
12
|
warning_when_session_invalid if session_id.blank?
|
13
|
-
|
13
|
+
|
14
|
+
# With https://github.com/rack/rack/commit/7fecaee81f59926b6e1913511c90650e76673b38
|
15
|
+
# to protected session_id into secret
|
16
|
+
session_id_digest = Digest::SHA256.hexdigest(session_id.inspect)
|
17
|
+
["rucaptcha-session", session_id_digest].join(":")
|
14
18
|
end
|
15
19
|
|
16
20
|
# Generate a new Captcha
|
17
21
|
def generate_rucaptcha
|
18
|
-
res = RuCaptcha.generate
|
22
|
+
res = RuCaptcha.generate
|
19
23
|
session_val = {
|
20
24
|
code: res[0],
|
21
25
|
time: Time.now.to_i
|
@@ -39,7 +43,7 @@ module RuCaptcha
|
|
39
43
|
# verify_rucaptcha?(nil, keep_session: true)
|
40
44
|
# verify_rucaptcha?(nil, captcha: params[:user][:captcha])
|
41
45
|
#
|
42
|
-
def verify_rucaptcha?(
|
46
|
+
def verify_rucaptcha?(_resource = nil, opts = {})
|
43
47
|
opts ||= {}
|
44
48
|
|
45
49
|
store_info = RuCaptcha.cache.read(rucaptcha_sesion_key_key)
|
@@ -47,24 +51,16 @@ module RuCaptcha
|
|
47
51
|
RuCaptcha.cache.delete(rucaptcha_sesion_key_key) unless opts[:keep_session]
|
48
52
|
|
49
53
|
# Make sure session exist
|
50
|
-
if store_info.blank?
|
51
|
-
return add_rucaptcha_validation_error
|
52
|
-
end
|
54
|
+
return add_rucaptcha_validation_error if store_info.blank?
|
53
55
|
|
54
56
|
# Make sure not expire
|
55
|
-
if (Time.now.to_i - store_info[:time]) > RuCaptcha.config.expires_in
|
56
|
-
return add_rucaptcha_validation_error
|
57
|
-
end
|
57
|
+
return add_rucaptcha_validation_error if (Time.now.to_i - store_info[:time]) > RuCaptcha.config.expires_in
|
58
58
|
|
59
59
|
# Make sure parama have captcha
|
60
|
-
captcha = (opts[:captcha] || params[:_rucaptcha] ||
|
61
|
-
if captcha.blank?
|
62
|
-
return add_rucaptcha_validation_error
|
63
|
-
end
|
60
|
+
captcha = (opts[:captcha] || params[:_rucaptcha] || "").downcase.strip
|
61
|
+
return add_rucaptcha_validation_error if captcha.blank?
|
64
62
|
|
65
|
-
if captcha != store_info[:code]
|
66
|
-
return add_rucaptcha_validation_error
|
67
|
-
end
|
63
|
+
return add_rucaptcha_validation_error if captcha != store_info[:code]
|
68
64
|
|
69
65
|
true
|
70
66
|
end
|
@@ -73,12 +69,14 @@ module RuCaptcha
|
|
73
69
|
|
74
70
|
def add_rucaptcha_validation_error
|
75
71
|
if defined?(resource) && resource && resource.respond_to?(:errors)
|
76
|
-
resource.errors.add(:base, t(
|
72
|
+
resource.errors.add(:base, t("rucaptcha.invalid"))
|
77
73
|
end
|
78
74
|
false
|
79
75
|
end
|
80
76
|
|
81
77
|
def warning_when_session_invalid
|
78
|
+
return unless Rails.env.development?
|
79
|
+
|
82
80
|
Rails.logger.warn "
|
83
81
|
WARNING! The session.id is blank, RuCaptcha can't work properly, please keep session available.
|
84
82
|
More details about this: https://github.com/huacnlee/rucaptcha/pull/66
|
data/lib/rucaptcha/engine.rb
CHANGED
@@ -2,11 +2,11 @@ module RuCaptcha
|
|
2
2
|
class Engine < ::Rails::Engine
|
3
3
|
isolate_namespace RuCaptcha
|
4
4
|
|
5
|
-
initializer
|
5
|
+
initializer "rucaptcha.init" do |app|
|
6
6
|
# https://github.com/rails/rails/blob/3-2-stable/actionpack/lib/action_dispatch/routing/route_set.rb#L268
|
7
7
|
# `app.routes.prepend` start from Rails 3.2 - 5.0
|
8
8
|
app.routes.prepend do
|
9
|
-
mount RuCaptcha::Engine =>
|
9
|
+
mount RuCaptcha::Engine => "/rucaptcha"
|
10
10
|
end
|
11
11
|
|
12
12
|
RuCaptcha.check_cache_store! unless RuCaptcha.config.skip_cache_store_check
|
data/lib/rucaptcha/version.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
module RuCaptcha
|
2
2
|
module ViewHelpers
|
3
3
|
def rucaptcha_input_tag(opts = {})
|
4
|
-
opts[:name] =
|
5
|
-
opts[:type] =
|
6
|
-
opts[:autocorrect] =
|
7
|
-
opts[:autocapitalize] =
|
8
|
-
opts[:pattern] =
|
9
|
-
opts[:autocomplete] =
|
10
|
-
opts[:maxlength]
|
4
|
+
opts[:name] = "_rucaptcha"
|
5
|
+
opts[:type] = "text"
|
6
|
+
opts[:autocorrect] = "off"
|
7
|
+
opts[:autocapitalize] = "off"
|
8
|
+
opts[:pattern] = "[a-zA-Z]*"
|
9
|
+
opts[:autocomplete] = "off"
|
10
|
+
opts[:maxlength] = RuCaptcha.config.length
|
11
11
|
tag(:input, opts)
|
12
12
|
end
|
13
13
|
|
14
14
|
def rucaptcha_image_tag(opts = {})
|
15
|
-
opts[:class] = opts[:class] ||
|
15
|
+
opts[:class] = opts[:class] || "rucaptcha-image"
|
16
16
|
opts[:src] = ru_captcha.root_path
|
17
17
|
opts[:onclick] = "this.src = '#{ru_captcha.root_path}?t=' + Date.now();"
|
18
18
|
tag(:img, opts)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rucaptcha
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.5.
|
4
|
+
version: 2.5.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -38,7 +38,7 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1'
|
41
|
-
description:
|
41
|
+
description:
|
42
42
|
email: huacnlee@gmail.com
|
43
43
|
executables: []
|
44
44
|
extensions:
|
@@ -69,7 +69,7 @@ homepage: https://github.com/huacnlee/rucaptcha
|
|
69
69
|
licenses:
|
70
70
|
- MIT
|
71
71
|
metadata: {}
|
72
|
-
post_install_message:
|
72
|
+
post_install_message:
|
73
73
|
rdoc_options: []
|
74
74
|
require_paths:
|
75
75
|
- lib
|
@@ -84,8 +84,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
84
|
- !ruby/object:Gem::Version
|
85
85
|
version: '0'
|
86
86
|
requirements: []
|
87
|
-
rubygems_version: 3.
|
88
|
-
signing_key:
|
87
|
+
rubygems_version: 3.2.3
|
88
|
+
signing_key:
|
89
89
|
specification_version: 4
|
90
90
|
summary: This is a Captcha gem for Rails Applications. It drawing captcha image with
|
91
91
|
C code so it no dependencies.
|