rucaptcha 2.5.0 → 2.5.5

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.
@@ -1,19 +1,20 @@
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'
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
- @config.cache_store = Rails.application.config.cache_store
27
- else
28
- @config.cache_store = :mem_cache_store
29
- end
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
- self.create(style, length, strikethrough, outline)
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 [:memory_store, :null_store, :file_store].include?(store_name)
55
- RuCaptcha.config.cache_store = [:file_store, Rails.root.join('tmp/cache/rucaptcha/session')]
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.send :include, RuCaptcha::ControllerHelpers
74
+ ActionController::Base.include RuCaptcha::ControllerHelpers
76
75
  end
77
76
 
78
77
  ActiveSupport.on_load(:action_view) do
@@ -1,9 +1,10 @@
1
- require 'fileutils'
1
+ require "fileutils"
2
2
 
3
3
  module RuCaptcha
4
4
  class << self
5
5
  def cache
6
6
  return @cache if defined? @cache
7
+
7
8
  @cache = ActiveSupport::Cache.lookup_store(RuCaptcha.config.cache_store)
8
9
  @cache
9
10
  end
@@ -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
- ['rucaptcha-session', session_id].join(':')
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?(resource = nil, opts = {})
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] || '').downcase.strip
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('rucaptcha.invalid'))
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
@@ -2,11 +2,11 @@ module RuCaptcha
2
2
  class Engine < ::Rails::Engine
3
3
  isolate_namespace RuCaptcha
4
4
 
5
- initializer 'rucaptcha.init' do |app|
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 => '/rucaptcha'
9
+ mount RuCaptcha::Engine => "/rucaptcha"
10
10
  end
11
11
 
12
12
  RuCaptcha.check_cache_store! unless RuCaptcha.config.skip_cache_store_check
@@ -1,4 +1,4 @@
1
- module Rucaptcha
1
+ module RuCaptcha
2
2
  module Errors
3
3
  class Configuration < StandardError; end
4
4
  end
@@ -1,3 +1,3 @@
1
1
  module RuCaptcha
2
- VERSION = '2.5.0'
2
+ VERSION = "2.5.5"
3
3
  end
@@ -1,18 +1,18 @@
1
1
  module RuCaptcha
2
2
  module ViewHelpers
3
3
  def rucaptcha_input_tag(opts = {})
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] ||= 5
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] || 'rucaptcha-image'
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.0
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: 2019-03-07 00:00:00.000000000 Z
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.0.1
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.