geetest_ruby_sdk 4.0.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f4f67d9fe2e2bbb890315f13c0214eed4701469d9ec0d44f83dc5b27857e5e68
4
+ data.tar.gz: 1e036fc6881bd36939e8906e7d654de5c3a1c3a5b8a18622b2235ef60faa9bfc
5
+ SHA512:
6
+ metadata.gz: 0af8da7eab3fe0bcda683c48b5e0a5684c889374ba94cd6da3b7a747fccb360054c88deef110f3d4d108d8aafb31daaa21df57153c6b9305919b27024635086e
7
+ data.tar.gz: 66094c37b4d7dd3b9e636b89851a3bfaf9aa8d2c86c7ee5dec32e6e918ee3b00adb3515e40a2ff61245e3cbb24b226e700005dd2a9a5f133caf57314fe6cbdd9
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /.idea
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /tags
11
+ .DS_Store
12
+
13
+ # rspec failure tracking
14
+ .rspec_status
15
+ /Gemfile.lock
16
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,38 @@
1
+ # https://github.com/bbatsov/rubocop/blob/master/config/default.yml
2
+ # https://github.com/bbatsov/rubocop/blob/master/config/disabled.yml
3
+ # https://github.com/bbatsov/rubocop/blob/master/config/enabled.yml
4
+
5
+ require:
6
+ - rubocop-rspec
7
+
8
+ AllCops:
9
+ TargetRubyVersion: 2.5
10
+ Exclude:
11
+ - 'vendor/**/*'
12
+ - 'test/**/*'
13
+ - 'bin/console'
14
+ - 'bin/setup'
15
+ - 'Rakefile'
16
+ - 'lib/geetest_ruby_sdk/version.rb'
17
+
18
+ # This cop checks whether the source file has a utf-8 encoding comment or not.
19
+ # This check makes sense only for code that should support Ruby 1.9,
20
+ # since in 2.0+ utf-8 is the default source file encoding.
21
+ Style/Encoding:
22
+ Enabled: false
23
+
24
+ Style/FrozenStringLiteralComment:
25
+ Enabled: false
26
+
27
+ Style/NumericPredicate:
28
+ Enabled: false
29
+
30
+ Style/PercentLiteralDelimiters:
31
+ Enabled: false
32
+
33
+ Style/MutableConstant:
34
+ Enabled: false
35
+
36
+ Metrics/BlockLength:
37
+ Exclude:
38
+ - 'spec/**/*'
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.5.1
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at huayuehao@jinshuju.net. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in geetest_ruby_sdk.gemspec
4
+ gemspec
5
+
6
+ gem 'logger'
7
+ gem 'openssl', '~> 2.2'
8
+ gem 'rest-client'
9
+
10
+ group :test do
11
+ gem 'rake', '~> 12.0'
12
+ gem 'rspec', '~> 3.0'
13
+ gem 'rubocop', '~> 1.25', require: false
14
+ gem 'rubocop-rspec', require: false
15
+ gem 'webmock'
16
+ gem 'timecop'
17
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,96 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ geetest_ruby_sdk (4.0.3)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.8.1)
10
+ public_suffix (>= 2.0.2, < 6.0)
11
+ ast (2.4.2)
12
+ crack (0.4.5)
13
+ rexml
14
+ diff-lcs (1.5.0)
15
+ domain_name (0.5.20190701)
16
+ unf (>= 0.0.5, < 1.0.0)
17
+ hashdiff (1.0.1)
18
+ http-accept (1.7.0)
19
+ http-cookie (1.0.5)
20
+ domain_name (~> 0.5)
21
+ ipaddr (1.2.4)
22
+ logger (1.5.1)
23
+ mime-types (3.4.1)
24
+ mime-types-data (~> 3.2015)
25
+ mime-types-data (3.2022.0105)
26
+ netrc (0.11.0)
27
+ openssl (2.2.2)
28
+ ipaddr
29
+ parallel (1.22.1)
30
+ parser (3.1.2.1)
31
+ ast (~> 2.4.1)
32
+ public_suffix (4.0.7)
33
+ rainbow (3.1.1)
34
+ rake (12.3.3)
35
+ regexp_parser (2.5.0)
36
+ rest-client (2.1.0)
37
+ http-accept (>= 1.7.0, < 2.0)
38
+ http-cookie (>= 1.0.2, < 2.0)
39
+ mime-types (>= 1.16, < 4.0)
40
+ netrc (~> 0.8)
41
+ rexml (3.2.5)
42
+ rspec (3.11.0)
43
+ rspec-core (~> 3.11.0)
44
+ rspec-expectations (~> 3.11.0)
45
+ rspec-mocks (~> 3.11.0)
46
+ rspec-core (3.11.0)
47
+ rspec-support (~> 3.11.0)
48
+ rspec-expectations (3.11.1)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.11.0)
51
+ rspec-mocks (3.11.1)
52
+ diff-lcs (>= 1.2.0, < 2.0)
53
+ rspec-support (~> 3.11.0)
54
+ rspec-support (3.11.1)
55
+ rubocop (1.28.2)
56
+ parallel (~> 1.10)
57
+ parser (>= 3.1.0.0)
58
+ rainbow (>= 2.2.2, < 4.0)
59
+ regexp_parser (>= 1.8, < 3.0)
60
+ rexml
61
+ rubocop-ast (>= 1.17.0, < 2.0)
62
+ ruby-progressbar (~> 1.7)
63
+ unicode-display_width (>= 1.4.0, < 3.0)
64
+ rubocop-ast (1.17.0)
65
+ parser (>= 3.1.1.0)
66
+ rubocop-rspec (2.10.0)
67
+ rubocop (~> 1.19)
68
+ ruby-progressbar (1.11.0)
69
+ timecop (0.9.5)
70
+ unf (0.1.4)
71
+ unf_ext
72
+ unf_ext (0.0.8.2)
73
+ unicode-display_width (2.3.0)
74
+ webmock (3.18.1)
75
+ addressable (>= 2.8.0)
76
+ crack (>= 0.3.2)
77
+ hashdiff (>= 0.4.0, < 2.0.0)
78
+
79
+ PLATFORMS
80
+ ruby
81
+
82
+ DEPENDENCIES
83
+ bundler (~> 2.0)
84
+ geetest_ruby_sdk!
85
+ logger
86
+ openssl (~> 2.2)
87
+ rake (~> 12.0)
88
+ rest-client
89
+ rspec (~> 3.0)
90
+ rubocop (~> 1.25)
91
+ rubocop-rspec
92
+ timecop
93
+ webmock
94
+
95
+ BUNDLED WITH
96
+ 2.3.13
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Yuehao Hua
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # GeetestRubySdk
2
+
3
+ This is A gem for Geetest SDK using Ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'geetest_ruby_sdk'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install geetest_ruby_sdk
20
+
21
+ ## Usage
22
+
23
+ This is A gem for Geetest SDK using Ruby
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/geetest_ruby_sdk. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/geetest_ruby_sdk/blob/master/CODE_OF_CONDUCT.md).
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
39
+
40
+ ## Code of Conduct
41
+
42
+ Everyone interacting in the GeetestRubySdk project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/geetest_ruby_sdk/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "geetest_ruby_sdk"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ require_relative 'lib/geetest_ruby_sdk/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'geetest_ruby_sdk'
5
+ spec.version = GeetestRubySdk::VERSION
6
+ spec.authors = ['Yuehao Hua', 'Yii Chou']
7
+ spec.email = ['huayuehao@jinshuju.net, zhouyi@jinshuju,net']
8
+
9
+ spec.summary = 'Ruby version of Geetest SDK'
10
+ spec.homepage = 'https://github.com/jinshuju/geetest-ruby-sdk'
11
+ spec.license = 'MIT'
12
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
13
+
14
+ spec.metadata['homepage_uri'] = spec.homepage
15
+ spec.metadata['source_code_uri'] = 'https://github.com/jinshuju/geetest-ruby-sdk'
16
+ spec.metadata['changelog_uri'] = 'https://github.com/jinshuju/geetest-ruby-sdk'
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+ spec.add_development_dependency 'bundler', '~> 2.0'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ end
@@ -0,0 +1,79 @@
1
+ require 'openssl'
2
+
3
+ module Geetest
4
+ # This class is used internally by Geetest to encrypt message
5
+ # It prefers to be used with chain methods
6
+ #
7
+ # Encryptor.encrypt(string).with(secret).by(digest_mod).to_s
8
+ #
9
+ # chain methods
10
+ # * :encrypt(string) => the message to be encrypted
11
+ # * :by(digest_mod) => encrypt mode
12
+ # * :with(secret) => secret_key used by #to_challenge/#to_validate and hmac_sha256 encryption
13
+ #
14
+ # return methods
15
+ # * :to_challenge => generate a geetest challenge string
16
+ # * :to_validate => generate a geetest validate string
17
+ # * :to_s => just digest the given string by given digest_mod
18
+ #
19
+ # assert methods
20
+ # * :eq?(string)
21
+ #
22
+ class Encryptor
23
+
24
+ def self.encrypt(str)
25
+ new.encrypt(str)
26
+ end
27
+
28
+ def encrypt(str)
29
+ @str = str.to_s
30
+ self
31
+ end
32
+
33
+ def with(secret)
34
+ @secret = secret.to_s
35
+ self
36
+ end
37
+
38
+ def by(digest_mod)
39
+ @digest_mod = digest_mod.to_s
40
+ self
41
+ end
42
+
43
+ def to_challenge
44
+ return if @str.to_s.empty? || @secret.to_s.empty?
45
+
46
+ hexdigest(@str + @secret)
47
+ end
48
+
49
+ def to_validate
50
+ return if @str.to_s.empty? || @secret.to_s.empty?
51
+
52
+ hexdigest(@secret + 'geetest' + @str)
53
+ end
54
+
55
+ def to_s
56
+ return if @str.to_s.empty?
57
+
58
+ hexdigest(@str)
59
+ end
60
+ alias inspect to_s
61
+
62
+ def eq?(string)
63
+ self.to_s == string
64
+ end
65
+
66
+ private
67
+
68
+ def hexdigest(payload)
69
+ case @digest_mod
70
+ when 'sha256'
71
+ OpenSSL::Digest::SHA256.hexdigest payload
72
+ when 'hmac_sha256'
73
+ OpenSSL::HMAC.hexdigest('SHA256', @secret, payload)
74
+ else
75
+ OpenSSL::Digest::MD5.hexdigest payload
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Geetest
4
+ module V3
5
+ # This class is used internally by Geetest to send the request
6
+ # Mandatory parameters to initialize an instance:
7
+ # * :captcha_id
8
+ # * :captcha_key
9
+ #
10
+ # => register: to register a geetest service
11
+ # Optional parameters
12
+ # * :digest_mod => encrypt mode
13
+ # * :options => some options for callback like user_name, password
14
+ #
15
+ # => validate?: to validate if the geetest is success
16
+ # Required parameters
17
+ # * :challenge => the code from client generated by geetest
18
+ # * :validate => the code from client generated by geetest
19
+ # * :seccode => the code from client generated by geetest
20
+ # Optional parameters
21
+ # * :digest_mod => encrypt mode
22
+ # * :options => some options for callback like user_name, password
23
+ class Account
24
+ BASE_URL = 'http://api.geetest.com'
25
+ JSON_FORMAT = '1'
26
+ DEFAULT_DIGEST_MOD = 'md5'
27
+
28
+ attr_accessor :captcha_id, :captcha_key, :digest_mod
29
+
30
+ def initialize(captcha_id, captcha_key, **options)
31
+ @captcha_id = captcha_id
32
+ @captcha_key = captcha_key
33
+ @digest_mod = options.fetch(:digest_mod, Account::DEFAULT_DIGEST_MOD)
34
+ end
35
+
36
+ def register(**options)
37
+ Register.new(account: self).register(options)
38
+ end
39
+
40
+ def validate?(challenge: nil, validate: nil, seccode: nil, **options)
41
+ Validator.new(account: self).valid?(challenge, validate, seccode, options)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Geetest
4
+ module V3
5
+ # Create and validate a challenge under degraded model
6
+ # When a degraded_challenge is created, it will keep valid at least in 21.6 minutes and no more than 43.2 minutes
7
+ # 21.6 = 36 * 36 / 60 (that means the last 2 chars of base36 timestamp)
8
+ # By default, the validity of geetest challenge is 10 minutes, so 21.6 minutes is enough in degraded model.
9
+ module DegradedMode
10
+ CHALLENGE_LENGTH = 32
11
+
12
+ def degraded_challenge
13
+ timestamp = Time.now.to_i.to_s(36)
14
+ index = rand(20..26)
15
+ random = [*'a'..'z', *'0'..'9'].sample(6).join
16
+ sha = Encryptor.encrypt(timestamp).with(account.captcha_key).by(digest_mod).to_challenge
17
+ (sha[0,index] + timestamp).ljust(CHALLENGE_LENGTH, random)
18
+ end
19
+
20
+ def degraded?(challenge)
21
+ return false unless challenge.length == CHALLENGE_LENGTH
22
+
23
+ valid_marks = [Time.now.to_i, Time.now.to_i - 36 * 36].map { |t| t.to_s(36)[0,4] }
24
+ valid_marks.any? do |mark|
25
+ index = challenge.index(mark)
26
+ next unless (20..26).include?(index)
27
+
28
+ timestamp = challenge[index,6]
29
+ sha = Encryptor.encrypt(timestamp).with(account.captcha_key).by(digest_mod).to_challenge
30
+ sha[0, index] == challenge[0, index]
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Geetest
4
+ module V3
5
+ # This class is used internally by Geetest to send a register request
6
+ # => register: to send a get request to geetest service and return a valid result
7
+ # parameters
8
+ # * :options => some options for callback like user_name, password
9
+ class Register
10
+ include DegradedMode
11
+
12
+ REGISTER_URL = '/register.php'.freeze
13
+
14
+ attr_reader :account, :digest_mod, :response
15
+
16
+ def initialize(account:, digest_mod: nil)
17
+ @account = account
18
+ @digest_mod = digest_mod || account.digest_mod
19
+ end
20
+
21
+ def register(**options)
22
+ payload = { gt: account.captcha_id }
23
+ options = { json_format: Account::JSON_FORMAT }.merge options.transform_keys(&:to_sym)
24
+ @response = request! payload, options
25
+ on_remote_mode
26
+ rescue *Geetest.exceptions_to_degraded_mode => e
27
+ Geetest.logger.info "Geetest register request failed for #{e.message}, fall back to degraded mode"
28
+ on_degraded_mode
29
+ end
30
+
31
+ private
32
+
33
+ def request!(payload, options = {})
34
+ response = RestClient.get "#{Account::BASE_URL}#{REGISTER_URL}", params: payload.merge(options)
35
+ JSON.parse response.body
36
+ end
37
+
38
+ def on_remote_mode
39
+ {
40
+ success: 1,
41
+ gt: account.captcha_id,
42
+ challenge: Encryptor.encrypt(@response['challenge']).with(account.captcha_key).by(digest_mod).to_challenge
43
+ }
44
+ end
45
+
46
+ def on_degraded_mode
47
+ {
48
+ success: 0,
49
+ gt: account.captcha_id,
50
+ challenge: degraded_challenge
51
+ }
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Geetest
4
+ module V3
5
+ # This class is used internally by Geetest to validate by request or local
6
+ # => valid?: to send a post request to geetest service and return if a geetest is valid
7
+ # parameters
8
+ # * :challenge
9
+ # * :validate
10
+ # * :seccode
11
+ # * :options => some options for callback like user_name, password
12
+ class Validator
13
+ include DegradedMode
14
+
15
+ VALIDATE_URL = '/validate.php'
16
+
17
+ attr_reader :account, :digest_mod, :response
18
+
19
+ def initialize(account:, digest_mod: nil)
20
+ @account = account
21
+ @digest_mod = digest_mod || account.digest_mod
22
+ end
23
+
24
+ def valid?(challenge, validate, seccode, **options)
25
+ return false if validate.to_s.empty? || challenge.to_s.empty?
26
+
27
+ degraded?(challenge) ? degraded_valid?(challenge, validate) : remote_valid?(challenge, validate, seccode, options)
28
+ end
29
+
30
+ private
31
+
32
+ def degraded_valid?(challenge, validate)
33
+ Encryptor.encrypt(challenge).by(digest_mod).eq? validate
34
+ end
35
+
36
+ def remote_valid?(challenge, validate, seccode, options)
37
+ return false if seccode.to_s.empty?
38
+ return false unless validate == Encryptor.encrypt(challenge).with(account.captcha_key).by(digest_mod).to_validate
39
+
40
+ remote_seccode_valid?(challenge, validate, seccode, **options)
41
+ end
42
+
43
+ def remote_seccode_valid?(challenge, validate, seccode, **options)
44
+ payload = { challenge: challenge, validate: validate, seccode: seccode }
45
+ options = { json_format: Account::JSON_FORMAT }.merge options.transform_keys(&:to_sym)
46
+ @response = request! payload, options
47
+
48
+ Encryptor.encrypt(seccode).with(account.captcha_key).by(digest_mod).eq? @response['seccode']
49
+ end
50
+
51
+ def request!(payload, options = {})
52
+ response = RestClient.post "#{Account::BASE_URL}#{VALIDATE_URL}", payload.merge(options)
53
+ JSON.parse response.body
54
+ end
55
+ end
56
+ end
57
+ end