card-mod-recaptcha 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 54d5a0b7d291ab0be1e56645e7dbcb16580155cd3215b98d4fb29a7f734e1bb3
4
+ data.tar.gz: ef923fc2f02ae4c15614ca1c005b5d5dec7771cfbc5cb395344513231b0a5a0f
5
+ SHA512:
6
+ metadata.gz: aa2c2cacd1f7409f3c22a7051d518017d2226b3ded97abb9ff0db24b66e86c14808bcb620d3944b18838666492d5073bcd748fccfc22a4eb65c10956e9f9f2a7
7
+ data.tar.gz: 77afdbd9ec793079a3e1b80021c06ce2915a97bf7f184c35b5b9f39637817b8b56845dd9823005fd6e439c5951f7ffdda7834686eed400077dd00db80a2f7442
@@ -0,0 +1,55 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "recaptcha"
3
+
4
+ # This initializer module is mostly here to avoid adding methods/vars to the Object
5
+ # namespace
6
+ module RecaptchaCard
7
+ @deprecated = {
8
+ recaptcha_site_key: :recaptcha_public_key,
9
+ recaptcha_secret_key: :recaptcha_private_key
10
+ }
11
+ @defaults = {
12
+ recaptcha_site_key: "6LdoqpgUAAAAAEdhJ4heI1h3XLlpXcDf0YubriCG",
13
+ recaptcha_secret_key: "6LdoqpgUAAAAAP4Sz1L5PY6VKrum_RFxq4-awj4BH"
14
+ }
15
+
16
+ mattr_accessor :using_card_defaults
17
+
18
+ class << self
19
+ def load_recaptcha_config setting
20
+ setting = "recaptcha_#{setting}".to_sym
21
+ Cardio.config.send "#{setting}=", recaptcha_setting_value(setting)
22
+ end
23
+
24
+ def using_defaults?
25
+ Cardio.config.recaptcha_site_key == @defaults[:recaptcha_site_key]
26
+ end
27
+
28
+ # card config overrides application.rb config overrides default
29
+ def recaptcha_setting_value setting
30
+ card_value(setting) || # card content
31
+ config_value(setting) || # application.rb (current setting)
32
+ config_value(@deprecated[setting]) || # application.rb (deprecated setting)
33
+ @defaults[setting]
34
+ end
35
+
36
+ def config_value setting
37
+ Cardio.config.send setting
38
+ end
39
+
40
+ def card_value setting
41
+ return unless Card::Codename.exist? setting # prevents breakage in migrations
42
+ value = Card[setting]&.content
43
+ value if value.present?
44
+ end
45
+ end
46
+ end
47
+
48
+ ActiveSupport.on_load :after_card do
49
+ Recaptcha.configure do |config|
50
+ %i[site_key secret_key].each do |setting|
51
+ config.send "#{setting}=", RecaptchaCard.load_recaptcha_config(setting)
52
+ end
53
+ config.verify_url = "https://www.google.com/recaptcha/api/siteverify"
54
+ end
55
+ end
@@ -0,0 +1,102 @@
1
+ RECAPTCHA_ERROR_CODES = { # LOCALIZE
2
+ "missing-input-secret" => "secret parameter is missing",
3
+ "invalid-input-secret" => "secret parameter is invalid or malformed",
4
+ "missing-input-response" => "response parameter is missing",
5
+ "invalid-input-response" => "response parameter is invalid or malformed",
6
+ "bad-request" => "request is invalid or malformed"
7
+ }
8
+
9
+ def human?
10
+ result = JSON.parse recaptcha_response
11
+ return if recaptcha_success?(result)
12
+
13
+ add_recaptcha_errors result["error-codes"]
14
+ end
15
+
16
+ def recaptcha_on?
17
+ recaptcha_keys? &&
18
+ Env[:controller] &&
19
+ !Auth.signed_in? &&
20
+ !Auth.always_ok? &&
21
+ !Auth.needs_setup? &&
22
+ Card::Rule.toggle(rule(:captcha))
23
+ end
24
+
25
+ def add_recaptcha_errors error_codes
26
+ if error_codes.present?
27
+ error_codes.each do |code|
28
+ errors.add :recaptcha, RECAPTCHA_ERROR_CODES.fetch(code, code)
29
+ end
30
+ else
31
+ errors.add :recaptcha, "Looks like you are not a human" # LOCALIZE
32
+ end
33
+ end
34
+
35
+ def recaptcha_success? result
36
+ result['success'] &&
37
+ (result['score'].to_f >= Cardio.config.recaptcha_minimum_score) &&
38
+ (result['action'].to_sym == action.to_sym)
39
+ end
40
+
41
+ def recaptcha_response
42
+ ::Recaptcha.get({ secret: Card.config.recaptcha_secret_key,
43
+ response: Env.params[:recaptcha_token] }, {})
44
+ end
45
+
46
+ def recaptcha_keys?
47
+ Card.config.recaptcha_site_key && Card.config.recaptcha_secret_key
48
+ end
49
+
50
+ event :recaptcha, :validate, when: :validate_recaptcha? do
51
+ handle_recaptcha_config_errors do
52
+ Env[:recaptcha_used] = true
53
+ human?
54
+ end
55
+ end
56
+
57
+ def handle_recaptcha_config_errors
58
+ if Env.params[:recaptcha_token] == "grecaptcha-undefined"
59
+ errors.add "recaptcha", "needs correct v3 configuration" # LOCALILZE
60
+ elsif Env.params[:recaptcha_token] == "recaptcha-token-field-missing"
61
+ raise Card::Error, "recaptcha token field missing" # LOCALILZE
62
+ else
63
+ yield
64
+ end
65
+ end
66
+
67
+
68
+ def validate_recaptcha?
69
+ !@supercard && !Env[:recaptcha_used] && recaptcha_on?
70
+ end
71
+
72
+ format :html do
73
+ def recaptcha_token action
74
+ output [
75
+ javascript_include_tag(recaptcha_script_url),
76
+ hidden_field_tag("recaptcha_token", "",
77
+ "data-site-key": Card.config.recaptcha_site_key,
78
+ "data-action": action,
79
+ class: "_recaptcha-token")
80
+ ]
81
+ end
82
+
83
+ def recaptcha_script_url
84
+ "https://www.google.com/recaptcha/api.js?render=#{Card.config.recaptcha_site_key}"
85
+ end
86
+
87
+ def hidden_form_tags action, opts
88
+ return super unless recaptcha?(opts)
89
+
90
+ super + recaptcha_token(action)
91
+ end
92
+
93
+ def card_form_html_opts action, opts={}
94
+ super
95
+ opts["data-recaptcha"] ||= "on" if recaptcha?(opts)
96
+ opts
97
+ end
98
+
99
+ def recaptcha? opts
100
+ card.recaptcha_on? && opts[:recaptcha] != :off
101
+ end
102
+ end
@@ -0,0 +1,32 @@
1
+ add_to_basket :warnings, :recaptcha_config_issues
2
+
3
+ def recaptcha_config_issues?
4
+ RecaptchaCard.using_defaults?
5
+ end
6
+
7
+ format :html do
8
+ def recaptcha_config_issues_message
9
+ warning =
10
+ if Card::Env.localhost?
11
+ # %(Your captcha is currently working with temporary settings.
12
+ # This is fine for a local installation, but you will need new
13
+ # recaptcha keys if you want to make this site public.)
14
+ I18n.t(:captcha_temp, scope: "mod.admin.set.self.admin_info",
15
+ recaptcha_link: add_recaptcha_keys_link)
16
+ else
17
+ # %(You are configured to use [[*captcha]], but for that to work
18
+ # you need new recaptcha keys.)
19
+ I18n.t(:captcha_keys, scope: "mod.admin.set.self.admin_info",
20
+ recaptcha_link: add_recaptcha_keys_link,
21
+ captcha_link: link_to_card(:captcha))
22
+ end
23
+ <<-HTML
24
+ <p>#{warning}</p>
25
+ HTML
26
+ end
27
+
28
+ def add_recaptcha_keys_link
29
+ link_text = I18n.t :recaptcha_keys, scope: "mod.admin.set.self.admin_info"
30
+ Card[:recaptcha_settings]&.format&.edit_link link_text: link_text
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ event :set_recaptcha_proxy, :finalize do
2
+ Card.config.recaptcha_proxy = content
3
+ end
@@ -0,0 +1,9 @@
1
+ event :validate_recaptcha_secret_key, :validate do
2
+ return if content.match?(/^[a-zA-Z0-9\-_]*$/)
3
+
4
+ errors.add :content, "invalid key" # LOCALIZE
5
+ end
6
+
7
+ event :set_recaptcha_secret_key, :finalize do
8
+ Card.config.recaptcha_secret_key = content
9
+ end
@@ -0,0 +1,44 @@
1
+ format :html do
2
+ def raw_help_text
3
+ # LOCALIZE
4
+ "Register your domain at Google's [[http://google.com/recaptcha|reCAPTCHA service]] "\
5
+ "and enter your site key and secret key below.<br>"\
6
+ "If you want to turn catchas off then change all [[*captcha|captcha rules]] to 'no'."
7
+ end
8
+
9
+ # def instructions title, steps
10
+ # steps = list_tag steps, ordered: true
11
+ # "#{title}#{steps}"
12
+ # end
13
+ #
14
+ # <h5>#{instructions}</h5>
15
+ # #{howto_add_new_recaptcha_keys}
16
+ # #{howto_turn_captcha_off}
17
+ #
18
+ # def howto_add_new_recaptcha_keys
19
+ # instructions(
20
+ # I18n.t(:howto_add_keys, scope: "mod.admin.set.self.admin_info"),
21
+ # [
22
+ # I18n.t(:howto_register,
23
+ # scope: "mod.admin.set.self.admin_info",
24
+ # recaptcha_link: link_to_resource("http://google.com/recaptcha")),
25
+ # I18n.t(:howto_add,
26
+ # scope: "mod.admin.set.self.admin_info",
27
+ # recaptcha_settings: link_to_card(:recaptcha_settings))
28
+ # ]
29
+ # )
30
+ # end
31
+ #
32
+ # def howto_turn_captcha_off
33
+ # instructions(
34
+ # I18n.t(:howto_turn_off, scope: "mod.admin.set.self.admin_info"),
35
+ # [
36
+ # I18n.t(:howto_go,
37
+ # scope: "mod.admin.set.self.admin_info",
38
+ # captcha_card: link_to_card(:captcha)),
39
+ # I18n.t(:howto_update,
40
+ # scope: "mod.admin.set.self.admin_info")
41
+ # ]
42
+ # )
43
+ # end
44
+ end
@@ -0,0 +1,9 @@
1
+ event :validate_recaptcha_site_key, :validate do
2
+ return if content.match?(/^[a-zA-Z0-9\-_]*$/)
3
+
4
+ errors.add :content, "invalid key" # LOCALIZE
5
+ end
6
+
7
+ event :set_recaptcha_site_key, :finalize do
8
+ Card.config.recaptcha_site_key = content
9
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: card-mod-recaptcha
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Ethan McCutchen
8
+ - Philipp Kühl
9
+ - Gerry Gleason
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2020-09-19 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: ''
16
+ email:
17
+ - info@decko.org
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - config/initializers/recaptcha.rb
23
+ - set/all/recaptcha.rb
24
+ - set/self/admin_info.rb
25
+ - set/self/recaptcha_proxy.rb
26
+ - set/self/recaptcha_secret_key.rb
27
+ - set/self/recaptcha_settings.rb
28
+ - set/self/recaptcha_site_key.rb
29
+ homepage: http://decko.org
30
+ licenses:
31
+ - GPL-2.0
32
+ - GPL-3.0
33
+ metadata:
34
+ card-mod: recaptcha
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.3.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.0.3
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: recaptcha support for decko
54
+ test_files: []