yandex_captcha 0.4.3.4 → 0.4.3.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.
- checksums.yaml +4 -4
- data/.travis.yml +0 -2
- data/README.md +36 -4
- data/lib/yandex_captcha.rb +8 -1
- data/lib/yandex_captcha/configuration.rb +10 -4
- data/lib/yandex_captcha/helpers/rails.rb +2 -2
- data/lib/yandex_captcha/helpers/sinatra.rb +4 -1
- data/lib/yandex_captcha/verify.rb +37 -7
- data/lib/yandex_captcha/version.rb +1 -1
- data/test/yandex_captcha_test.rb +52 -26
- data/yandex_captcha.gemspec +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: dec49d32aa312a91762b280c934c38aa8fc44890
|
|
4
|
+
data.tar.gz: 42b4f188c419dc203b5742e5bbbd0c8d5f0e560f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ba4557d65c6bbd374c806e01c846f6ff3c0e4f8e1df1523ced84905d754bb43af1afed921f52e300ca0cc18b3636b0789de68031dd6167d055daad3df0b9cc74
|
|
7
|
+
data.tar.gz: 0b64cb323d9b11471b83c648b20263159e6436b35911cbce215e3a6fd8995ec16ddb34f755a4946994428336cbaa766878149917c72be7945876cd9a7d0b445a
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://codeclimate.com/github/merqlove/yandex-captcha)
|
|
4
4
|
[](https://travis-ci.org/merqlove/yandex-captcha)
|
|
5
|
+
[](http://inch-ci.org/github/merqlove/yandex-captcha)
|
|
6
|
+
[](https://gemnasium.com/merqlove/yandex-captcha)
|
|
5
7
|
|
|
6
8
|
Ruby wrapper for [Yandex Cleanweb](http://api.yandex.ru/cleanweb/) spam detector.
|
|
7
9
|
|
|
@@ -35,6 +37,17 @@ Tested:
|
|
|
35
37
|
|
|
36
38
|
Get the api key: [http://api.yandex.ru/cleanweb/getkey.xml](http://api.yandex.ru/cleanweb/getkey.xml)
|
|
37
39
|
|
|
40
|
+
### Configuration options
|
|
41
|
+
|
|
42
|
+
| Name | Value |
|
|
43
|
+
| :------------- |:-------------|
|
|
44
|
+
| api_key | Yandex.CleanWeb API key. (default `nil`) |
|
|
45
|
+
| captcha_type (optional) | Type of captcha (std,estd,lite,elite,rus,latl,elatl,latu,elatu,latm, elatm). (default `std`) |
|
|
46
|
+
| api_server_url (optional) | Yandex.CleanWeb API server URL. (default `http://cleanweb-api.yandex.ru/1.0`) |
|
|
47
|
+
| skip_verify_env (optional) | Custom error provider for non ajax mode. (default `['test', 'cucumber']`) |
|
|
48
|
+
| handle_timeouts_gracefull (optional) | Graceful error's on timeouts. (default `true`) |
|
|
49
|
+
| current_env (optional) | Current environment (default `ENV['RACK_ENV'] || ENV['RAILS_ENV']`) |
|
|
50
|
+
|
|
38
51
|
### Code
|
|
39
52
|
|
|
40
53
|
```ruby
|
|
@@ -52,10 +65,13 @@ end
|
|
|
52
65
|
|
|
53
66
|
### In Views
|
|
54
67
|
|
|
55
|
-
####
|
|
68
|
+
#### `captcha_tag` options
|
|
56
69
|
|
|
57
|
-
|
|
58
|
-
|
|
70
|
+
| Name | Value |
|
|
71
|
+
| :------------- |:-------------|
|
|
72
|
+
| :ajax (optional) | Ajaxify your captcha, don't wait for it on any page. (default `false`) |
|
|
73
|
+
| :noscript (optional) | Adding <noscript></noscript> wrap around captcha. Works only for non ajax mode. (default `false`) |
|
|
74
|
+
| :error (optional) | Custom error provider for non ajax mode. (default `flash` or `nil`) |
|
|
59
75
|
|
|
60
76
|
#### Code
|
|
61
77
|
|
|
@@ -75,6 +91,22 @@ or
|
|
|
75
91
|
|
|
76
92
|
### In Controllers
|
|
77
93
|
|
|
94
|
+
#### `valid_captcha?` options
|
|
95
|
+
|
|
96
|
+
| Name | Value |
|
|
97
|
+
| :------------- |:-------------|
|
|
98
|
+
| :captcha_id | Yandex.CleanWeb captcha id. (default `nil`) |
|
|
99
|
+
| :value | User response value. (default `nil`) |
|
|
100
|
+
| :request_id (optional) | Some unique request id (default `nil`) |
|
|
101
|
+
|
|
102
|
+
#### `get_captcha` options
|
|
103
|
+
|
|
104
|
+
| Name | Value |
|
|
105
|
+
| :------------- |:-------------|
|
|
106
|
+
| :request_id (optional) | Some unique request id (default `nil`) |
|
|
107
|
+
|
|
108
|
+
#### Example
|
|
109
|
+
|
|
78
110
|
```ruby
|
|
79
111
|
# Rails / Sinatra / or if you included helpers
|
|
80
112
|
if valid_captcha?(params[:captcha_response_id], params[:captcha_response_field])
|
|
@@ -133,4 +165,4 @@ If you use Yandex Captcha in Rails app, we recommend to set up the api key in `c
|
|
|
133
165
|
|
|
134
166
|
---
|
|
135
167
|
|
|
136
|
-
Special respect to Evrone which gem `yandex-cleanweb` include most of code in `lib/yandex_captcha/verify.rb` and some examples on this page.
|
|
168
|
+
Special respect to Evrone which gem `yandex-cleanweb` include most of code in `lib/yandex_captcha/verify.rb` and some examples on this page.
|
data/lib/yandex_captcha.rb
CHANGED
|
@@ -3,7 +3,6 @@ require 'yandex_captcha/verify'
|
|
|
3
3
|
require 'yandex_captcha/helpers/base'
|
|
4
4
|
|
|
5
5
|
module YandexCaptcha
|
|
6
|
-
module Helpers ; end
|
|
7
6
|
class NoApiKeyException < Exception; end
|
|
8
7
|
class BadResponseException < Exception; end
|
|
9
8
|
class YandexCaptchaError < StandardError; end
|
|
@@ -24,6 +23,14 @@ module YandexCaptcha
|
|
|
24
23
|
def self.configuration
|
|
25
24
|
@configuration ||= Configuration.new
|
|
26
25
|
end
|
|
26
|
+
|
|
27
|
+
def self.skip_env
|
|
28
|
+
if configuration.skip_verify_env.include? configuration.current_env
|
|
29
|
+
true
|
|
30
|
+
else
|
|
31
|
+
false
|
|
32
|
+
end
|
|
33
|
+
end
|
|
27
34
|
end
|
|
28
35
|
|
|
29
36
|
if defined?(Rails)
|
|
@@ -25,6 +25,10 @@ module YandexCaptcha
|
|
|
25
25
|
# YandexCaptcha.configure do |config|
|
|
26
26
|
# config.api_key = 'cw.1.1.gsdjdgskjhsdgjkgsdjsdjgkskhsgjkgsjhdkgsdghskd.sdgjhgsdsgdkjgdshkgds'
|
|
27
27
|
# config.captcha_type = 'elite'
|
|
28
|
+
# config.api_server_url = 'http://cleanweb-api.yandex.ru/1.0'
|
|
29
|
+
# config.skip_verify_env = ["test", "cucumber"]
|
|
30
|
+
# config.handle_timeouts_gracefull = true'
|
|
31
|
+
# config.current_env = "test"
|
|
28
32
|
# end
|
|
29
33
|
#
|
|
30
34
|
class Configuration
|
|
@@ -32,14 +36,16 @@ module YandexCaptcha
|
|
|
32
36
|
:api_key,
|
|
33
37
|
:skip_verify_env,
|
|
34
38
|
:handle_timeouts_gracefully,
|
|
35
|
-
:captcha_type
|
|
39
|
+
:captcha_type,
|
|
40
|
+
:current_env
|
|
36
41
|
|
|
37
42
|
def initialize #:nodoc:
|
|
38
|
-
@api_server_url
|
|
39
|
-
@captcha_type
|
|
40
|
-
@api_key
|
|
43
|
+
@api_server_url = API_URL
|
|
44
|
+
@captcha_type = CAPTCHA_TYPE
|
|
45
|
+
@api_key = ENV['CAPTCHA_KEY']
|
|
41
46
|
@skip_verify_env = SKIP_VERIFY_ENV
|
|
42
47
|
@handle_timeouts_gracefully = HANDLE_TIMEOUTS_GRACEFULLY
|
|
48
|
+
@current_env = ENV['RACK_ENV'] || ENV['RAILS_ENV']
|
|
43
49
|
end
|
|
44
50
|
|
|
45
51
|
end
|
|
@@ -2,16 +2,16 @@ module YandexCaptcha
|
|
|
2
2
|
module Helpers
|
|
3
3
|
module Rails
|
|
4
4
|
def captcha_tags(options = {})
|
|
5
|
+
return if YandexCaptcha.skip_env
|
|
5
6
|
|
|
6
|
-
error = options[:error] ||= ((defined? flash) ? flash[:captcha_error] : "")
|
|
7
7
|
if options[:ajax]
|
|
8
8
|
render partial: "yandex_captcha/captcha_ajax"
|
|
9
9
|
else
|
|
10
|
+
error = options[:error] ||= ((defined? flash) ? flash[:yandex_captcha_error] : "")
|
|
10
11
|
captcha = YandexCaptcha::Verify.get_captcha
|
|
11
12
|
render partial: "yandex_captcha/captcha", locals: { captcha: captcha, error: error, noscript: options[:noscript] } if captcha
|
|
12
13
|
end
|
|
13
14
|
end
|
|
14
|
-
|
|
15
15
|
end
|
|
16
16
|
end
|
|
17
17
|
end
|
|
@@ -2,12 +2,15 @@ module YandexCaptcha
|
|
|
2
2
|
module Helpers
|
|
3
3
|
module Sinatra
|
|
4
4
|
def captcha_tags(options = {})
|
|
5
|
+
return if YandexCaptcha.skip_env
|
|
6
|
+
|
|
5
7
|
template = settings.captcha_ajax_template.to_s.to_sym
|
|
6
8
|
if options[:ajax]
|
|
7
9
|
render(current_engine, template, {layout: false}).to_s.html_safe
|
|
8
10
|
else
|
|
11
|
+
error = options[:error] ||= ((defined? flash) ? flash[:yandex_captcha_error] : "")
|
|
9
12
|
captcha = YandexCaptcha::Verify.get_captcha
|
|
10
|
-
render(current_engine, template, {layout: false}, { captcha: captcha, noscript: options[:noscript] }).to_s.html_safe if captcha
|
|
13
|
+
render(current_engine, template, {layout: false}, { captcha: captcha, error: error, noscript: options[:noscript] }).to_s.html_safe if captcha
|
|
11
14
|
end
|
|
12
15
|
end
|
|
13
16
|
|
|
@@ -11,15 +11,18 @@ module YandexCaptcha
|
|
|
11
11
|
doc = Nokogiri::XML(response)
|
|
12
12
|
|
|
13
13
|
spam_result = doc.xpath('//check-spam-result')
|
|
14
|
+
|
|
15
|
+
raise BadResponseException unless spam_result
|
|
16
|
+
|
|
14
17
|
request_id_tag = spam_result.xpath('id')
|
|
15
18
|
spam_flag_tag = spam_result.xpath('text')
|
|
16
19
|
|
|
17
20
|
raise BadResponseException if request_id_tag.size.zero?
|
|
18
21
|
|
|
19
|
-
request_id = request_id_tag.first
|
|
20
|
-
spam_flag = spam_flag_tag.first.attributes["spam-flag"]
|
|
22
|
+
request_id = request_id_tag.first.content
|
|
23
|
+
spam_flag = spam_flag_tag.first.attributes["spam-flag"].content
|
|
21
24
|
|
|
22
|
-
|
|
25
|
+
spam_flag_check request_id, spam_result, spam_flag
|
|
23
26
|
end
|
|
24
27
|
|
|
25
28
|
def get_captcha(request_id=nil)
|
|
@@ -33,6 +36,8 @@ module YandexCaptcha
|
|
|
33
36
|
end
|
|
34
37
|
|
|
35
38
|
def valid_captcha?(captcha_id=nil, value=nil, request_id=nil)
|
|
39
|
+
true if YandexCaptcha.skip_env
|
|
40
|
+
|
|
36
41
|
response = api_check_captcha(request_id, captcha_id, value)
|
|
37
42
|
doc = Nokogiri::XML(response)
|
|
38
43
|
doc.xpath('//check-captcha-result/ok').any?
|
|
@@ -62,7 +67,7 @@ module YandexCaptcha
|
|
|
62
67
|
uri = URI.parse(get_captcha_url)
|
|
63
68
|
uri.query = URI.encode_www_form(params)
|
|
64
69
|
|
|
65
|
-
|
|
70
|
+
request_uri(uri)
|
|
66
71
|
end
|
|
67
72
|
|
|
68
73
|
def api_check_spam(options)
|
|
@@ -78,12 +83,12 @@ module YandexCaptcha
|
|
|
78
83
|
|
|
79
84
|
check_spam_url = "#{API_URL}/check-spam"
|
|
80
85
|
uri = URI.parse(check_spam_url)
|
|
81
|
-
response =
|
|
86
|
+
response = request_uri(uri, cleanweb_options, :post)
|
|
82
87
|
response.body
|
|
83
88
|
end
|
|
84
89
|
|
|
85
|
-
def
|
|
86
|
-
return
|
|
90
|
+
def spam_flag_check(request_id, spam_result, spam_flag)
|
|
91
|
+
return {id: request_id, links: []} unless spam_flag
|
|
87
92
|
|
|
88
93
|
if spam_flag == 'yes'
|
|
89
94
|
links = spam_result.xpath('links')
|
|
@@ -99,6 +104,31 @@ module YandexCaptcha
|
|
|
99
104
|
end
|
|
100
105
|
end
|
|
101
106
|
|
|
107
|
+
def request_uri(uri, options={}, type=:get)
|
|
108
|
+
begin
|
|
109
|
+
case type
|
|
110
|
+
when :post
|
|
111
|
+
Net::HTTP.post_form(uri, options)
|
|
112
|
+
else
|
|
113
|
+
Net::HTTP.get(uri)
|
|
114
|
+
end
|
|
115
|
+
rescue Timeout::Error
|
|
116
|
+
if YandexCaptcha.configuration.handle_timeouts_gracefully
|
|
117
|
+
if defined?(flash)
|
|
118
|
+
flash[:captcha_error] = if defined?(I18n)
|
|
119
|
+
I18n.translate('yandex_captcha.errors.cleanweb_unreachable', {:default => 'Yandex.CleanWeb unreachable.'})
|
|
120
|
+
else
|
|
121
|
+
'Yandex.CleanWeb unreachable.'
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
else
|
|
125
|
+
raise YandexCaptchaError, "Yandex.CleanWeb unreachable."
|
|
126
|
+
end
|
|
127
|
+
rescue Exception => e
|
|
128
|
+
raise YandexCaptchaError, e.message, e.backtrace
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
102
132
|
def prepare_api_key
|
|
103
133
|
raise NoApiKeyException if YandexCaptcha.configuration.api_key.nil? || YandexCaptcha.configuration.api_key.empty?
|
|
104
134
|
|
data/test/yandex_captcha_test.rb
CHANGED
|
@@ -2,6 +2,33 @@
|
|
|
2
2
|
require "test_helper"
|
|
3
3
|
|
|
4
4
|
describe YandexCaptcha do
|
|
5
|
+
context "with test environment" do
|
|
6
|
+
before do
|
|
7
|
+
YandexCaptcha.configure do |config|
|
|
8
|
+
config.current_env = "test"
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
describe "#skip_env" do
|
|
13
|
+
it "skipping" do
|
|
14
|
+
YandexCaptcha.skip_env.must_equal true
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
context "with dev environment" do
|
|
20
|
+
before do
|
|
21
|
+
YandexCaptcha.configure do |config|
|
|
22
|
+
config.current_env = "development"
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
describe "#skip_env" do
|
|
27
|
+
it "working" do
|
|
28
|
+
YandexCaptcha.skip_env.must_equal false
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
5
32
|
|
|
6
33
|
context "without api key" do
|
|
7
34
|
before do
|
|
@@ -29,7 +56,7 @@ describe YandexCaptcha do
|
|
|
29
56
|
describe "#valid_captcha?" do
|
|
30
57
|
it "raise an error" do
|
|
31
58
|
-> {
|
|
32
|
-
YandexCaptcha::Verify.valid_captcha?("anything", "anything"
|
|
59
|
+
YandexCaptcha::Verify.valid_captcha?("anything", 123, "anything")
|
|
33
60
|
}.must_raise YandexCaptcha::NoApiKeyException
|
|
34
61
|
end
|
|
35
62
|
end
|
|
@@ -58,39 +85,38 @@ describe YandexCaptcha do
|
|
|
58
85
|
end
|
|
59
86
|
end
|
|
60
87
|
|
|
61
|
-
describe "#spam?" do
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
end
|
|
88
|
+
# describe "#spam?" do
|
|
89
|
+
#
|
|
90
|
+
# describe "simple check" do
|
|
91
|
+
# it "works" do
|
|
92
|
+
# YandexCaptcha::Verify.spam?("фраза").must_equal false
|
|
93
|
+
# YandexCaptcha::Verify.spam?("недорого увеличение пениса проститутки").must_equal false
|
|
94
|
+
# end
|
|
95
|
+
# end
|
|
96
|
+
#
|
|
97
|
+
# describe "advanced mode" do
|
|
98
|
+
# it "works" do
|
|
99
|
+
# YandexCaptcha::Verify.spam?(body_plain: "my text", ip: "80.80.40.3").must_equal false
|
|
100
|
+
# end
|
|
101
|
+
#
|
|
102
|
+
# it "with some html" do
|
|
103
|
+
# result = YandexCaptcha::Verify.spam?(body_html: "some spam <a href='http://spam.com'>spam link</a>")
|
|
104
|
+
#
|
|
105
|
+
# result[:id].wont_be_empty
|
|
106
|
+
# result[:links].must_be_empty
|
|
107
|
+
# end
|
|
108
|
+
# end
|
|
109
|
+
# end
|
|
83
110
|
|
|
84
111
|
describe "#get_captcha + #valid_captcha?" do
|
|
85
112
|
|
|
86
113
|
it "works for not valid captchas" do
|
|
87
|
-
|
|
88
|
-
captcha = YandexCaptcha::Verify.get_captcha(result[:id])
|
|
114
|
+
captcha = YandexCaptcha::Verify.get_captcha
|
|
89
115
|
|
|
90
116
|
captcha[:url].wont_be_empty
|
|
91
117
|
captcha[:captcha].wont_be_empty
|
|
92
118
|
|
|
93
|
-
valid = YandexCaptcha::Verify.valid_captcha?(
|
|
119
|
+
valid = YandexCaptcha::Verify.valid_captcha?(captcha[:captcha], "1234")
|
|
94
120
|
valid.must_equal false
|
|
95
121
|
end
|
|
96
122
|
end
|
data/yandex_captcha.gemspec
CHANGED
|
@@ -20,7 +20,7 @@ Gem::Specification.new do |gem|
|
|
|
20
20
|
gem.add_dependency 'nokogiri'
|
|
21
21
|
|
|
22
22
|
gem.add_development_dependency "rake"
|
|
23
|
-
gem.add_development_dependency "minitest", "~>
|
|
23
|
+
gem.add_development_dependency "minitest", "~> 5.0"
|
|
24
24
|
gem.add_development_dependency "minitest-spec-context"
|
|
25
25
|
gem.add_development_dependency "fakeweb"
|
|
26
26
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: yandex_captcha
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.4.3.
|
|
4
|
+
version: 0.4.3.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Alexander Merkulov
|
|
@@ -45,14 +45,14 @@ dependencies:
|
|
|
45
45
|
requirements:
|
|
46
46
|
- - "~>"
|
|
47
47
|
- !ruby/object:Gem::Version
|
|
48
|
-
version: '
|
|
48
|
+
version: '5.0'
|
|
49
49
|
type: :development
|
|
50
50
|
prerelease: false
|
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
|
52
52
|
requirements:
|
|
53
53
|
- - "~>"
|
|
54
54
|
- !ruby/object:Gem::Version
|
|
55
|
-
version: '
|
|
55
|
+
version: '5.0'
|
|
56
56
|
- !ruby/object:Gem::Dependency
|
|
57
57
|
name: minitest-spec-context
|
|
58
58
|
requirement: !ruby/object:Gem::Requirement
|