new_google_recaptcha 0.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/MIT-LICENSE +20 -0
- data/README.md +94 -0
- data/Rakefile +27 -0
- data/lib/generators/USAGE +8 -0
- data/lib/generators/new_google_recaptcha_generator.rb +6 -0
- data/lib/generators/templates/new_google_recaptcha.rb +6 -0
- data/lib/new_google_recaptcha.rb +21 -0
- data/lib/new_google_recaptcha/railtie.rb +9 -0
- data/lib/new_google_recaptcha/validator.rb +13 -0
- data/lib/new_google_recaptcha/version.rb +3 -0
- data/lib/new_google_recaptcha/view_ext.rb +25 -0
- data/lib/tasks/new_google_recaptcha_tasks.rake +4 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f5410e7d18af6d1a30e7b679a10975ecfa59eccd6902efaf1e569de236920ac1
|
4
|
+
data.tar.gz: 578c1cbf8e7c1743dc26d71c44358625b4dfab5818c4bd6a69474ae90828fe7f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c7298d1b499360f7051c2488858921eb3f52c37159bb34470b8362ddeb63bf0934dd55db85966ed87299ad845b5fc52ca518f4f31c61cc76303da8e89d88c398
|
7
|
+
data.tar.gz: 3e7b5476d418e5e2d441797a1fc9af65c9909c711a924778c160f815c08edb5f6ead189fddc13f23b973016feba8e9e222f44c26c1157a497d9487947f44a705
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2018 Igor Kasyanchuk
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# Google Recaptcha v3 + Rails
|
2
|
+
|
3
|
+
Integrate Google Recaptcha v3 with Rails app.
|
4
|
+
|
5
|
+
Google Recaptcha console: https://www.google.com/recaptcha/admin#list
|
6
|
+
|
7
|
+
Recaptcha v3 documentation: https://developers.google.com/recaptcha/docs/v3
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
- Open https://www.google.com/recaptcha/admin#list
|
12
|
+
- register a new site
|
13
|
+
- copy `site_key` and `secret_key` and put into config/initializer/new_google_recaptcha.rb
|
14
|
+
- in layout:
|
15
|
+
```erb
|
16
|
+
<head>
|
17
|
+
...
|
18
|
+
<%= include_recaptcha_js %>
|
19
|
+
</head>
|
20
|
+
```
|
21
|
+
- in view where you for example you have a form:
|
22
|
+
```erb
|
23
|
+
<form ...>
|
24
|
+
<%= recaptcha_action('checkout') %>
|
25
|
+
</form>
|
26
|
+
```
|
27
|
+
- in controller:
|
28
|
+
```ruby
|
29
|
+
def create
|
30
|
+
@post = Post.new(post_params)
|
31
|
+
if NewGoogleRecaptcha.human?(params[:new_google_recaptcha_token], @post) && @post.save
|
32
|
+
redirect_to @post, notice: 'Post was successfully created.'
|
33
|
+
else
|
34
|
+
render :new
|
35
|
+
end
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
Also you can verify token without adding error to model:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
NewGoogleRecaptcha.human?(params[:new_google_recaptcha_token])
|
43
|
+
```
|
44
|
+
|
45
|
+
## Installation
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
gem 'new_google_recaptcha'
|
49
|
+
```
|
50
|
+
|
51
|
+
And then execute:
|
52
|
+
```bash
|
53
|
+
$ bundle
|
54
|
+
```
|
55
|
+
|
56
|
+
And then run:
|
57
|
+
|
58
|
+
```bash
|
59
|
+
$ rails generate new_google_recaptcha initializer
|
60
|
+
```
|
61
|
+
|
62
|
+
And edit new_google_recaptcha.rb and enter your site_key and secret_key.
|
63
|
+
|
64
|
+
## API
|
65
|
+
|
66
|
+
**NewGoogleRecaptcha.human?(token, model)** in contoller
|
67
|
+
|
68
|
+
- token is received from google, must be sent to backend
|
69
|
+
- model optional parameter. if you want to add error to model.
|
70
|
+
|
71
|
+
**<%= include_recaptcha_js %>** in layout
|
72
|
+
|
73
|
+
Include Google Recaptcha v3 JS into your Rails app. In head, right before `</head>`.
|
74
|
+
|
75
|
+
**<%= recaptcha_action(action_name) %>** in view
|
76
|
+
|
77
|
+
Action where recaptcha action was executed. Actions could be viewed in Admin console. More docs: https://developers.google.com/recaptcha/docs/v3. Action name could be "comments", "checkout", etc. Put any name and check scores in console.
|
78
|
+
|
79
|
+
## TODO
|
80
|
+
|
81
|
+
- check everything works with turbolinks
|
82
|
+
- allow custom ID for input
|
83
|
+
- return score ?
|
84
|
+
- tests
|
85
|
+
- handle exceptions with timeouts, json is not parsed
|
86
|
+
- add support for non-Rails apps
|
87
|
+
|
88
|
+
## Contributing
|
89
|
+
|
90
|
+
You are welcome to contribute.
|
91
|
+
|
92
|
+
## License
|
93
|
+
|
94
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'NewGoogleRecaptcha'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'bundler/gem_tasks'
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
|
21
|
+
Rake::TestTask.new(:test) do |t|
|
22
|
+
t.libs << 'test'
|
23
|
+
t.pattern = 'test/**/*_test.rb'
|
24
|
+
t.verbose = false
|
25
|
+
end
|
26
|
+
|
27
|
+
task default: :test
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "new_google_recaptcha/railtie"
|
2
|
+
|
3
|
+
module NewGoogleRecaptcha
|
4
|
+
mattr_accessor :site_key
|
5
|
+
mattr_accessor :secret_key
|
6
|
+
|
7
|
+
def self.setup
|
8
|
+
yield(self)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.human?(token, model = nil)
|
12
|
+
is_valid = NewGoogleRecaptcha::Validator.valid?(token)
|
13
|
+
if model && !is_valid
|
14
|
+
model.errors.add(:base, "Looks like you are not a human")
|
15
|
+
end
|
16
|
+
is_valid
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
require_relative "new_google_recaptcha/view_ext"
|
21
|
+
require_relative "new_google_recaptcha/validator"
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
module NewGoogleRecaptcha
|
4
|
+
class Validator
|
5
|
+
|
6
|
+
def Validator.valid?(token)
|
7
|
+
uri = URI("https://www.google.com/recaptcha/api/siteverify?secret=#{NewGoogleRecaptcha.secret_key}&response=#{token}")
|
8
|
+
result = JSON.parse(Net::HTTP.get(uri))
|
9
|
+
!!result["success"]
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module NewGoogleRecaptcha
|
2
|
+
module ViewExt
|
3
|
+
include ActionView::Helpers::TagHelper
|
4
|
+
|
5
|
+
def include_recaptcha_js
|
6
|
+
raw %Q{
|
7
|
+
<script src="https://www.google.com/recaptcha/api.js?render=#{NewGoogleRecaptcha.site_key}"></script>
|
8
|
+
}
|
9
|
+
end
|
10
|
+
|
11
|
+
def recaptcha_action(action)
|
12
|
+
id = "new_google_recaptcha_token_#{SecureRandom.hex(10)}"
|
13
|
+
raw %Q{
|
14
|
+
<input name="new_google_recaptcha_token" type="hidden" id="#{id}"/>
|
15
|
+
<script>
|
16
|
+
grecaptcha.ready(function() {
|
17
|
+
grecaptcha.execute("#{NewGoogleRecaptcha.site_key}", {action: "#{action}"}).then(function(token) {
|
18
|
+
document.getElementById("#{id}").value = token;
|
19
|
+
});
|
20
|
+
});
|
21
|
+
</script>
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: new_google_recaptcha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Igor Kasyanchuk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Google reCAPTCHA v3 + Rails (integration)
|
42
|
+
email:
|
43
|
+
- igorkasyanchuk@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/generators/USAGE
|
52
|
+
- lib/generators/new_google_recaptcha_generator.rb
|
53
|
+
- lib/generators/templates/new_google_recaptcha.rb
|
54
|
+
- lib/new_google_recaptcha.rb
|
55
|
+
- lib/new_google_recaptcha/railtie.rb
|
56
|
+
- lib/new_google_recaptcha/validator.rb
|
57
|
+
- lib/new_google_recaptcha/version.rb
|
58
|
+
- lib/new_google_recaptcha/view_ext.rb
|
59
|
+
- lib/tasks/new_google_recaptcha_tasks.rake
|
60
|
+
homepage: https://github.com/igorkasyanchuk/new_google_recaptcha
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.7.6
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Google reCAPTCHA v3 + Rails
|
84
|
+
test_files: []
|