hcaptcha 7.0.1 → 7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a8d13c5892c269bd846a2be83ff471553e73cabe9e029bd0665835f38ab0750
4
- data.tar.gz: 61eb765060b5f609a35e058f6841144c829b52ebf3019d55929f659b3adf8e7d
3
+ metadata.gz: b4103929669b3b8601acea5112dcb3ce2008975be3c8609298ea387ef03d8195
4
+ data.tar.gz: 63402b16c6e3881ff5099c3c29f3c726801f7af6371abae7cf5d358fb2f9b5ba
5
5
  SHA512:
6
- metadata.gz: 4dd587e34892cdf438cc18039659760682985c69d50fee1eaadc8becdf660a79b1d4297ad46699e0a6e81bd7bf2e7dc80cb7d4bd45263250801e5585453848f5
7
- data.tar.gz: 1902d04442608418ddfac1e3bb1ad4acef2763691482c1b9a0a632adcdb254d5392b03f5e3deb389f74ddd797e967edfb5b0c83597585279e04c004592c86beb
6
+ metadata.gz: 7b12e0b36c8374214c5320d35883ebdd34046b4ccb2ec31fbdf206c9d24e59f1ec90ff06770ea6358c5aca83982fbb5ca923cc639a94ae3b3ffedf978d459c5c
7
+ data.tar.gz: 0264d37d64670c010827e0fd74eb5897e6f3741dde3d09dc0ee3d341de5831135b96787e7ca7e25803b0834799604f36a37582fcf1140fc2ede0b35f19357dd7
data/README.md CHANGED
@@ -1,12 +1,16 @@
1
1
  # hCaptcha
2
2
  [![Gem Version](https://badge.fury.io/rb/hcaptcha.svg)](https://badge.fury.io/rb/hcaptcha)
3
3
 
4
- Disclaimer: This gem is forked from the [recaptcha gem](https://github.com/ambethia/recaptcha). All ideas, including the documentation and demo Rails and Sinatra integrations come from [recaptcha gem](https://github.com/ambethia/recaptcha) but are adoped for hCaptcha.
4
+ ## Credits
5
5
 
6
- Author: Tyler VanNurden & Jason L Perry (http://ambethia.com)<br/>
7
- License: [MIT](http://creativecommons.org/licenses/MIT/)<br/>
8
- Info: https://github.com/firstmoversadvantage/hcaptcha<br/>
9
- Bugs: https://github.com/firstmoversadvantage/hcaptcha/issues<br/>
6
+ * https://github.com/Retrospring/hcaptcha
7
+ * https://github.com/firstmoversadvantage/hcaptcha
8
+ * https://github.com/ambethia/recaptcha
9
+
10
+ ## Overview
11
+
12
+ License: [MIT](http://creativecommons.org/licenses/MIT/)
13
+ Bugs: https://github.com/firstmoversadvantage/hcaptcha/issues
10
14
 
11
15
  This gem provides helper methods for the [hCaptcha API](https://hcaptcha.com). In your
12
16
  views you can use the `hcaptcha_tags` method to embed the needed javascript, and you can validate
@@ -18,27 +22,22 @@ Go to the [hCaptcha](https://hcaptcha.com/webmaster/signup) signup page to obtai
18
22
 
19
23
  The hostname you set it to must be a real hostname, since hCaptcha validates it when you create it in the portal. For example, `example.fmadata.com` does not have a DNS record, but `mydomain.com` does. The DNS record doesn't need to point to your application though, it just has to exist - that's why we added the record into the local hosts file.
20
24
 
21
- ## Rails Installation
25
+ ## Installation
22
26
 
23
- ```ruby
24
- gem "hcaptcha"
27
+ FIrst, add the gem to your bundle:
28
+ ```shell
29
+ bundle add hcaptcha
25
30
  ```
26
31
 
27
- You can keep keys out of the code base with environment variables or with Rails [secrets](https://api.rubyonrails.org/classes/Rails/Application.html#method-i-secrets).<br/>
32
+ Then, set the following environment variables:
33
+ * `HCAPTCHA_SECRET_KEY`
34
+ * `HCAPTCHA_SITE_KEY`
28
35
 
29
- In development, you can use the [dotenv](https://github.com/bkeepers/dotenv) gem. (Make sure to add it above `gem 'hcaptcha'`.)
36
+ > 💡 You should keep keys out of your codebase with external environment variables (using your shell's `export` command), Rails (< 5.2) [secrets](https://guides.rubyonrails.org/v5.1/security.html#custom-secrets), Rails (5.2+) [credentials](https://guides.rubyonrails.org/security.html#custom-credentials), the [dotenv](https://github.com/bkeepers/dotenv) or [figaro](https://github.com/laserlemon/figaro) gems,
30
37
 
31
- See [Alternative API key setup](#alternative-api-key-setup) for more ways to configure or override
32
- keys. See also the
33
- [Configuration](https://www.rubydoc.info/github/ambethia/recaptcha/master/Recaptcha/Configuration)
34
- documentation.
38
+ ## Usage
35
39
 
36
- ```shell
37
- export HCAPTCHA_SITE_KEY='6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
38
- export HCAPTCHA_SECRET_KEY='6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
39
- ```
40
-
41
- Add `hcaptcha_tags` to the forms you want to protect:
40
+ First, add `hcaptcha_tags` to the forms you want to protect:
42
41
 
43
42
  ```erb
44
43
  <%= form_for @foo do |f| %>
@@ -60,26 +59,56 @@ else
60
59
  end
61
60
  ```
62
61
 
63
- ## Sinatra / Rack / Ruby installation
64
-
65
- See [sinatra demo](/demo/sinatra) for details.
62
+ If you are **not using Rails**, you should:
63
+ * `include Hcaptcha::Adapters::ViewMethods` where you need `recaptcha_tags`
64
+ * `include Hcaptcha::Adapters::ControllerMethods` where you need `verify_hcaptcha`
66
65
 
67
- - add `gem 'hcaptcha'` to `Gemfile`
68
- - set env variables
69
- - `include Hcaptcha::Adapters::ViewMethods` where you need `recaptcha_tags`
70
- - `include Hcaptcha::Adapters::ControllerMethods` where you need `verify_recaptcha`
66
+ ### API details
71
67
 
72
-
73
- ## hCaptcha API and Usage
74
-
75
- ### `recaptcha_tags`
68
+ ### `hcaptcha_tags(options = {})`
76
69
 
77
70
  Use in your views to render the JavaScript widget.
78
71
 
72
+ Available options:
73
+
74
+ | Option | Description |
75
+ |-------------------------|-------------|
76
+ | `:badge` | _legacy, ignored_
77
+ | `:callback` | _see [official documentation](https://docs.hcaptcha.com/configuration)_
78
+ | `:chalexpired_callback` | _see [official documentation](https://docs.hcaptcha.com/configuration)_
79
+ | `:class` | Additional CSS classes added to `h-captcha` on the placeholder
80
+ | `:close_callback` | _see [official documentation](https://docs.hcaptcha.com/configuration)_
81
+ | `:error_callback` | _see [official documentation](https://docs.hcaptcha.com/configuration)_
82
+ | `:expired_callback` | _see [official documentation](https://docs.hcaptcha.com/configuration)_
83
+ | `:external_script` | _alias for `:script` option_
84
+ | `:hl` | _see [official documentation](https://docs.hcaptcha.com/configuration) and [available language codes](https://docs.hcaptcha.com/languages)_
85
+ | `:open_callback` | _see [official documentation](https://docs.hcaptcha.com/configuration)_
86
+ | `:nonce` | Add a `nonce="…"` attribute to the `<script>` tag
87
+ | `:onload` | _see [official documentation](https://docs.hcaptcha.com/configuration)_
88
+ | `:recaptchacompat` | _see [official documentation](https://docs.hcaptcha.com/configuration)_
89
+ | `:render` | _see [official documentation](https://docs.hcaptcha.com/configuration)_
90
+ | `:script_async` | Add `async` attribute to the `<script>` tag (default: `true`)
91
+ | `:script_defer` | Add `defer` attribute to the `<script>` tag (default: `true`)
92
+ | `:script` | Generate the `<script>` tag (default: `true`)
93
+ | `:site_key` | Set hCaptcha Site Key (overrides `HCAPTCHA_SITE_KEY` environment variable)
94
+ | `:size` | _see [official documentation](https://docs.hcaptcha.com/configuration)_
95
+ | `:stoken` | _legacy, raises an exception_
96
+ | `:ssl` | _legacy, raises an exception_
97
+ | `:theme` | _see [official documentation](https://docs.hcaptcha.com/configuration)_ (default: `:dark`)
98
+ | `:type` | _legacy, ignored_
99
+ | `:ui` | _legacy, ignored_
100
+
101
+ > ℹ️ Unkown options will be passed directly as attributes to the placeholder element.
102
+ >
103
+ > For example, `hcaptcha_tags(foo: "bar")` will generate the default script tag and the following placeholder tag:
104
+ > ```html
105
+ > <div class="h-captcha" data-sitekey="…" foo="bar"></div>
106
+ > ```
107
+
79
108
  ### `verify_recaptcha`
80
109
 
81
110
  This method returns `true` or `false` after processing the response token from the hCaptcha widget.
82
- This is usually called from your controller, as seen [above](#rails-installation).
111
+ This is usually called from your controller.
83
112
 
84
113
  Passing in the ActiveRecord object via `model: object` is optional. If you pass a `model`—and the
85
114
  captcha fails to verify—an error will be added to the object for you to use (available as
@@ -120,41 +149,5 @@ en:
120
149
  By default, hCaptcha is skipped in "test" and "cucumber" env. To enable it during test:
121
150
 
122
151
  ```ruby
123
- Recaptcha.configuration.skip_verify_env.delete("test")
124
- ```
125
-
126
- ## Alternative API key setup
127
-
128
- ### Recaptcha.configure
129
-
130
- ```ruby
131
- # config/initializers/recaptcha.rb
132
- Recaptcha.configure do |config|
133
- config.site_key = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
134
- config.secret_key = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
135
- # Uncomment the following line if you are using a proxy server:
136
- # config.proxy = 'http://myproxy.com.au:8080'
137
- end
138
- ```
139
-
140
- ### Recaptcha.with_configuration
141
-
142
- For temporary overwrites (not thread safe).
143
-
144
- ```ruby
145
- Recaptcha.with_configuration(site_key: '12345') do
146
- # Do stuff with the overwritten site_key.
147
- end
148
- ```
149
-
150
- ### Per call
151
-
152
- Pass in keys as options at runtime, for code base with multiple hCaptcha setups:
153
-
154
- ```ruby
155
- recaptcha_tags site_key: '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
156
-
157
- # and
158
-
159
- verify_recaptcha secret_key: '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
160
- ```
152
+ Hcaptcha.configuration.skip_verify_env.delete("test")
153
+ ```
@@ -6,6 +6,13 @@ module Hcaptcha
6
6
  hcaptcha_unreachable: 'Oops, we failed to validate your hCaptcha response. Please try again.',
7
7
  verification_failed: 'hCaptcha verification failed, please try again.'
8
8
  }.freeze
9
+ DEFAULT_OPTIONS = {
10
+ external_script: true,
11
+ script: true,
12
+ script_async: true,
13
+ script_defer: true,
14
+ theme: :dark
15
+ }.freeze
9
16
 
10
17
  def self.hcaptcha(options)
11
18
  if options.key?(:stoken)
@@ -15,13 +22,7 @@ module Hcaptcha
15
22
  raise(HcaptchaError, "SSL is now always true. Please remove 'ssl' from your calls to hcaptcha_tags.")
16
23
  end
17
24
 
18
- html, tag_attributes = components(options.dup)
19
- html << %(<div #{tag_attributes}></div>\n)
20
-
21
- html << <<-HTML
22
- <div class="h-captcha" data-sitekey="#{Hcaptcha.configuration.site_key!}" data-theme="dark"></div>
23
- HTML
24
-
25
+ html = generate_tags(options)
25
26
  html.respond_to?(:html_safe) ? html.html_safe : html
26
27
  end
27
28
 
@@ -40,54 +41,63 @@ module Hcaptcha
40
41
  end
41
42
  end
42
43
 
43
- private_class_method def self.components(options)
44
- html = +''
45
- attributes = {}
46
-
44
+ private_class_method def self.generate_tags(options)
47
45
  options = options.dup
48
- class_attribute = options.delete(:class)
49
- site_key = options.delete(:site_key)
50
- hl = options.delete(:hl)
51
- onload = options.delete(:onload)
52
- render = options.delete(:render)
53
- script_async = options.delete(:script_async)
54
- script_defer = options.delete(:script_defer)
55
- nonce = options.delete(:nonce)
56
- skip_script = (options.delete(:script) == false) || (options.delete(:external_script) == false)
57
- ui = options.delete(:ui)
58
-
59
- data_attribute_keys = [:badge, :theme, :type, :callback, :expired_callback, :error_callback, :size]
60
- data_attribute_keys << :tabindex unless ui == :button
61
- data_attributes = {}
62
- data_attribute_keys.each do |data_attribute|
63
- value = options.delete(data_attribute)
64
- data_attributes["data-#{data_attribute.to_s.tr('_', '-')}"] = value if value
46
+ DEFAULT_OPTIONS.each do |name, value|
47
+ options[name] = value unless options.key?(name)
65
48
  end
49
+ generate_script_tag(options) + generate_placeholder_tag(options)
50
+ end
66
51
 
67
- site_key ||= Hcaptcha.configuration.site_key!
68
- script_url = Hcaptcha.configuration.api_server_url
52
+ private_class_method def self.generate_script_tag(options)
53
+ # Forge script URL
54
+ url = Hcaptcha.configuration.api_server_url
69
55
  query_params = hash_to_query(
70
- hl: hl,
71
- onload: onload,
72
- render: render
56
+ hl: options.delete(:hl),
57
+ onload: options.delete(:onload),
58
+ recaptchacompat: options.delete(:recaptchacompat),
59
+ render: options.delete(:render)
73
60
  )
74
- script_url += "?#{query_params}" unless query_params.empty?
75
- async_attr = "async" if script_async != false
76
- defer_attr = "defer" if script_defer != false
61
+ url += "?#{query_params}" unless query_params.empty?
62
+
63
+ # Forge additional attributes
64
+ nonce = options.delete(:nonce)
77
65
  nonce_attr = " nonce='#{nonce}'" if nonce
78
- html << %(<script src="#{script_url}" #{async_attr} #{defer_attr} #{nonce_attr}></script>\n) unless skip_script
79
- attributes["data-sitekey"] = site_key
80
- attributes.merge! data_attributes
66
+ async_attr = "async" if options.delete(:script_async)
67
+ defer_attr = "defer" if options.delete(:script_defer)
68
+ additional_attributes = [async_attr, defer_attr, nonce_attr].compact.join(" ")
81
69
 
82
- # The remaining options will be added as attributes on the tag.
83
- attributes["class"] = "hcaptcha #{class_attribute}"
84
- tag_attributes = attributes.merge(options).map { |k, v| %(#{k}="#{v}") }.join(" ")
70
+ return "" if options.delete(:script) == false || options.delete(:external_script) == false
85
71
 
86
- [html, tag_attributes]
72
+ %(<script src="#{url}" #{additional_attributes}></script>)
73
+ end
74
+
75
+ private_class_method def self.generate_placeholder_tag(options)
76
+ attributes = {}
77
+
78
+ # Forge data-* attributes
79
+ %i[
80
+ callback close_callback error_callback chalexpired_callback
81
+ expired_callback open_callback size tabindex theme
82
+ ].each do |data_attribute|
83
+ value = options.delete(data_attribute)
84
+ attributes["data-#{data_attribute.to_s.tr('_', '-')}"] = value if value
85
+ end
86
+ attributes["data-sitekey"] = options.delete(:site_key) || Hcaptcha.configuration.site_key!
87
+
88
+ # Forge CSS classes
89
+ attributes["class"] = "h-captcha #{options.delete(:class)}"
90
+
91
+ # Remaining options will be added as attributes on the tag.
92
+ %(<div #{html_attributes(attributes)} #{html_attributes(options)}></div>)
87
93
  end
88
94
 
89
95
  private_class_method def self.hash_to_query(hash)
90
96
  hash.delete_if { |_, val| val.nil? || val.empty? }.to_a.map { |pair| pair.join('=') }.join('&')
91
97
  end
98
+
99
+ private_class_method def self.html_attributes(hash)
100
+ hash.map { |k, v| %(#{k}="#{v}") }.join(" ")
101
+ end
92
102
  end
93
103
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Hcaptcha
4
- VERSION = '7.0.1'
4
+ VERSION = '7.1.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hcaptcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.0.1
4
+ version: 7.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Harrison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-30 00:00:00.000000000 Z
11
+ date: 2020-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json