gt_ruby_sdk 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8003ddff42711dbc8a07120af1c62d3895158bcf
4
+ data.tar.gz: 6a5ff021c9676e75b6e019a8d994cb48098818b2
5
+ SHA512:
6
+ metadata.gz: 755ec71342d3c9b644b6b74ffbd2dbb7efa651b774d2e21ff74dd46d39c98c7e0485794e8e51de28cad2e2f161f572ebe576855ef9f4507d9ea90fc3f571824c
7
+ data.tar.gz: 188f20418c59ddde8e0b8134c3e0dc5860c645d32b2f96917fb4c488b250948837353ebc1de0cab71bdc27b9b789d46b7f2f637432af9e15c5c05d13a15e13e5
data/.editorconfig ADDED
@@ -0,0 +1,13 @@
1
+ # editorconfig.org
2
+ root = true
3
+
4
+ [*]
5
+ indent_style = space
6
+ indent_size = 2
7
+ end_of_line = lf
8
+ charset = utf-8
9
+ trim_trailing_whitespace = true
10
+ insert_final_newline = true
11
+
12
+ [*.md]
13
+ trim_trailing_whitespace = false
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,35 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ DisplayStyleGuide: true
4
+ TargetRubyVersion: 2.0
5
+
6
+ Style/AsciiComments:
7
+ Enabled: false
8
+
9
+ Style/ClassAndModuleChildren:
10
+ Enabled: false
11
+
12
+ Style/Documentation:
13
+ Enabled: false
14
+
15
+ Metrics/MethodLength:
16
+ Max: 30
17
+
18
+ Metrics/LineLength:
19
+ Max: 90
20
+
21
+ Metrics/AbcSize:
22
+ Max: 30
23
+
24
+ Metrics/ClassLength:
25
+ Max: 600
26
+
27
+ Metrics/ParameterLists:
28
+ Max: 6
29
+
30
+ Style/NumericLiterals:
31
+ Description: >-
32
+ Add underscores to large numeric literals to improve their
33
+ readability.
34
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#underscores-in-numerics'
35
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.13.6
@@ -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 lcpmarvel@gmail.com. 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 [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gt_ruby_sdk.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 LcpMarvel
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,41 @@
1
+ # GtRubySdk
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/gt_ruby_sdk`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'gt_ruby_sdk'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install gt_ruby_sdk
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/gt_ruby_sdk. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
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 'gt_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
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,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gt_ruby_sdk/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'gt_ruby_sdk'
8
+ spec.version = GtRubySdk::VERSION
9
+ spec.authors = ['LcpMarvel']
10
+ spec.email = ['lcpmarvel@gmail.com']
11
+
12
+ spec.summary = 'GtWeb Ruby SDK'
13
+ spec.description = 'GtWeb Ruby SDK'
14
+ spec.homepage = 'https://github.com/LcpMarvel/gt-ruby-sdk'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.13'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'rubocop'
29
+ spec.add_development_dependency 'pry'
30
+
31
+ spec.add_dependency 'faraday'
32
+ spec.add_dependency 'activesupport'
33
+ end
@@ -0,0 +1,89 @@
1
+ require 'gt_ruby_sdk/version'
2
+ require 'gt_ruby_sdk/remote_server'
3
+ require 'gt_ruby_sdk/local_server'
4
+
5
+ module GtRubySdk
6
+ class Base
7
+ attr_reader :captcha_id, :private_key, :response
8
+
9
+ RESPONSE_STATUS = {
10
+ success: 1,
11
+ failure: 0
12
+ }.freeze
13
+
14
+ # 构造函数
15
+ def initialize(captcha_id, private_key)
16
+ @captcha_id = captcha_id
17
+ @private_key = private_key
18
+ end
19
+
20
+ # 预处理接口: 判断极验服务器是否down机
21
+ def pretreat(user_id = nil)
22
+ challenge = GtRubySdk::RemoteServer.register(captcha_id, user_id)
23
+
24
+ return failed_pretreatment if challenge.size != 32
25
+
26
+ successful_pretreatment(challenge)
27
+ rescue Faraday::Error::ClientError
28
+ failed_pretreatment
29
+ end
30
+
31
+ # 获取预处理结果的接口
32
+ def response_json
33
+ return if response.blank?
34
+
35
+ JSON.generate(response)
36
+ end
37
+
38
+ # 极验服务器状态正常的二次验证接口
39
+ def remote_validate(challenge, pin_code, seccode, user_id = nil)
40
+ return false unless pin_code_validated?(challenge, pin_code)
41
+
42
+ data = {
43
+ seccode: seccode,
44
+ sdk: GtRubySdk::VERSION
45
+ }
46
+
47
+ data[:user_id] = user_id if user_id.present?
48
+
49
+ GtRubySdk::RemoteServer.validate(data) == Digest::MD5.hexdigest(seccode)
50
+ end
51
+
52
+ # 极验服务器状态宕机的二次验证接口
53
+ def local_validate(challenge, pin_code, seccode)
54
+ GtRubySdk::LocalServer.validate(challenge, pin_code, seccode)
55
+ end
56
+
57
+ private
58
+
59
+ def failed_pretreatment
60
+ challenge = GtRubySdk::LocalServer.register
61
+
62
+ @response = build_response(false, challenge)
63
+
64
+ RESPONSE_STATUS[:failure]
65
+ end
66
+
67
+ def successful_pretreatment(challenge)
68
+ encrypted_challenge = Digest::MD5.hexdigest([challenge, private_key].join)
69
+
70
+ @response = build_response(true, encrypted_challenge)
71
+
72
+ RESPONSE_STATUS[:success]
73
+ end
74
+
75
+ def build_response(success, challenge)
76
+ {
77
+ success: success ? RESPONSE_STATUS[:success] : RESPONSE_STATUS[:failure],
78
+ gt: captcha_id,
79
+ challenge: challenge
80
+ }
81
+ end
82
+
83
+ def pin_code_validated?(challenge, pin_code)
84
+ return false if pin_code.size != 32
85
+
86
+ Digest::MD5.hexdigest([private_key, 'geetest'.freeze, challenge].join) == pin_code
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,84 @@
1
+ require 'active_support/core_ext/object/blank'
2
+
3
+ module GtRubySdk
4
+ class LocalServer
5
+ TOLERANCE = 4
6
+
7
+ class << self
8
+ def register
9
+ rnd1 = Digest::MD5.hexdigest(Random.rand(100).to_s)
10
+ rnd2 = Digest::MD5.hexdigest(Random.rand(100).to_s)
11
+
12
+ [
13
+ rnd1,
14
+ rnd2.to_s[0..1]
15
+ ].join
16
+ end
17
+
18
+ def validate(challenge, pin_code, _seccode)
19
+ return false if pin_code.blank?
20
+
21
+ code_fragments = pin_code.split('_')
22
+
23
+ ans = decode(challenge, code_fragments[0])
24
+ bg_image_index = decode(challenge, code_fragments[1])
25
+ image_grp_index = decode(challenge, code_fragments[2])
26
+
27
+ validate_image(ans, bg_image_index, image_grp_index)
28
+ end
29
+
30
+ def validate_image(ans, bg_image_index, image_grp_index)
31
+ full_bg_name = Digest::MD5.hexdigest(bg_image_index.to_s)[0..8]
32
+ bg_name = Digest::MD5.hexdigest(image_grp_index.to_s)[10..18]
33
+
34
+ array = 9.times.inject([]) do |collector, i|
35
+ collector.push(
36
+ i.odd? ? bg_name[i] : full_bg_name[i]
37
+ )
38
+ end
39
+
40
+ x_decode = array[4..-1].join
41
+ x_int = x_decode.to_i(16)
42
+
43
+ result = x_int % 200
44
+ result = result < 40 ? 40 : result
45
+
46
+ (ans - result).abs < 4
47
+ end
48
+
49
+ # 解码随机参数
50
+ def decode(challenge, code_fragment)
51
+ return 0 if code_fragment.length > 100
52
+
53
+ hash = {}
54
+ duplicated_array = []
55
+ numbers = [1, 2, 5, 10, 50]
56
+ count = 0
57
+
58
+ challenge.chars.each do |item|
59
+ next if duplicated_array.include?(item)
60
+
61
+ value = numbers[count % 5]
62
+ duplicated_array.push(item)
63
+ count += 1
64
+
65
+ hash[item] = value
66
+ end
67
+
68
+ offset = decode_offset(challenge[32..33])
69
+ code_fragment.chars.inject(0) { |_acc, elem| +hash[elem] } - offset
70
+ end
71
+
72
+ # 输入的两位的随机数字,解码出偏移量
73
+ def decode_offset(number)
74
+ number = number[0..1]
75
+
76
+ array = number.codepoints.to_a.map do |codepoint|
77
+ codepoint > 57 ? (codepoint - 87) : (codepoint - 48)
78
+ end
79
+
80
+ array[0] * 36 + array[1]
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,55 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module GtRubySdk
5
+ class RemoteServer
6
+ REQUEST_URL = 'http://api.geetest.com/'.freeze
7
+ REGISTER_PATH = '/register.php'.freeze
8
+ VALIDATE_PATH = '/validate.php'.freeze
9
+
10
+ CONNECTION_TIMEOUT = 1
11
+ READ_TIMEOUT = 1
12
+
13
+ class << self
14
+ def register(captcha_id, user_id)
15
+ conn = Faraday.new(url: REQUEST_URL) do |faraday|
16
+ faraday.request :url_encoded
17
+ faraday.response :raise_error
18
+ faraday.adapter Faraday.default_adapter
19
+ end
20
+
21
+ params = { gt: captcha_id }.tap do |query|
22
+ query[:user_id] = user_id if user_id.present?
23
+ end
24
+
25
+ request_response = conn.get do |req|
26
+ req.url REGISTER_PATH, params
27
+ req.options.timeout = READ_TIMEOUT
28
+ req.options.open_timeout = CONNECTION_TIMEOUT
29
+ end
30
+
31
+ request_response.body
32
+ end
33
+
34
+ def validate(data)
35
+ request_response = request_connection.post do |req|
36
+ req.url VALIDATE_PATH
37
+ req.options.timeout = READ_TIMEOUT
38
+ req.options.open_timeout = CONNECTION_TIMEOUT
39
+
40
+ req.body = data
41
+ end
42
+
43
+ request_response.body
44
+ end
45
+
46
+ def request_connection
47
+ Faraday.new(url: REQUEST_URL) do |faraday|
48
+ faraday.request :url_encoded
49
+ faraday.response :raise_error
50
+ faraday.adapter Faraday.default_adapter
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module GtRubySdk
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/web/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'gt_ruby_sdk', path: '../'
4
+ gem 'sinatra'
data/web/Gemfile.lock ADDED
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ gt_ruby_sdk (0.1.0)
5
+ activesupport
6
+ faraday
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (5.0.1)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (~> 0.7)
14
+ minitest (~> 5.1)
15
+ tzinfo (~> 1.1)
16
+ coderay (1.1.1)
17
+ concurrent-ruby (1.0.4)
18
+ faraday (0.10.0)
19
+ multipart-post (>= 1.2, < 3)
20
+ i18n (0.7.0)
21
+ method_source (0.8.2)
22
+ minitest (5.10.1)
23
+ multipart-post (2.0.0)
24
+ pry (0.10.4)
25
+ coderay (~> 1.1.0)
26
+ method_source (~> 0.8.1)
27
+ slop (~> 3.4)
28
+ rack (1.6.5)
29
+ rack-protection (1.5.3)
30
+ rack
31
+ sinatra (1.4.7)
32
+ rack (~> 1.5)
33
+ rack-protection (~> 1.4)
34
+ tilt (>= 1.3, < 3)
35
+ slop (3.6.0)
36
+ thread_safe (0.3.5)
37
+ tilt (2.0.5)
38
+ tzinfo (1.2.2)
39
+ thread_safe (~> 0.1)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ gt_ruby_sdk!
46
+ pry
47
+ sinatra
48
+
49
+ BUNDLED WITH
50
+ 1.13.6
data/web/demo.rb ADDED
@@ -0,0 +1,62 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'sinatra'
5
+ require 'gt_ruby_sdk'
6
+ require 'json'
7
+
8
+ CAPTCHA_ID = 'b46d1900d0a894591916ea94ea91bd2c'.freeze
9
+ PRIVATE_KEY = '36fc3fe98530eea08dfc6ce76e3d24c4'.freeze
10
+ MOBILE_CAPTCHA_ID = '7c25da6fe21944cfe507d2f9876775a9'.freeze
11
+ MOBILE_PRIVATE_KEY = 'f5883f4ee3bd4fa8caec67941de1b903'.freeze
12
+
13
+ enable :sessions
14
+
15
+ get '/' do
16
+ erb :index
17
+ end
18
+
19
+ get '/register' do
20
+ content_type :json
21
+
22
+ user_id = 'test'
23
+ status = gt_client.pretreat(user_id)
24
+ session['gtserver'] = status
25
+ session['user_id'] = user_id
26
+
27
+ gt_client.response_json
28
+ end
29
+
30
+ post '/validate' do
31
+ content_type :json
32
+
33
+ p params
34
+ p '------------------------------------------------------------'
35
+ user_id = session['user_id']
36
+ result = false
37
+
38
+ if session['gtserver'] == 1
39
+ result = gt_client.remote_validate(
40
+ params[:geetest_challenge],
41
+ params[:geetest_validate],
42
+ params[:geetest_seccode],
43
+ user_id
44
+ )
45
+ else
46
+ result = gt_client.local_validate(
47
+ params[:geetest_challenge],
48
+ params[:geetest_validate],
49
+ params[:geetest_seccode]
50
+ )
51
+ end
52
+
53
+ { status: result ? 'success' : 'fail' }.to_json
54
+ end
55
+
56
+ def gt_client
57
+ @gt_client ||= if params[:type] == 'pc'
58
+ GtRubySdk::Base.new(CAPTCHA_ID, PRIVATE_KEY)
59
+ else
60
+ GtRubySdk::Base.new(MOBILE_CAPTCHA_ID, MOBILE_PRIVATE_KEY)
61
+ end
62
+ end
@@ -0,0 +1,293 @@
1
+ <!-- Copied from https://github.com/GeeTeam/gt-php-sdk/blob/master/static/login.html -->
2
+ <!DOCTYPE html>
3
+ <html>
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1">
7
+ <title>gt-php-sdk-demo</title>
8
+ <style>
9
+ body {
10
+ margin: 50px 0;
11
+ text-align: center;
12
+ }
13
+ .inp {
14
+ border: 1px solid gray;
15
+ padding: 0 10px;
16
+ width: 200px;
17
+ height: 30px;
18
+ font-size: 18px;
19
+ }
20
+ .btn {
21
+ border: 1px solid gray;
22
+ width: 100px;
23
+ height: 30px;
24
+ font-size: 18px;
25
+ cursor: pointer;
26
+ }
27
+ #embed-captcha {
28
+ width: 300px;
29
+ margin: 0 auto;
30
+ }
31
+ .show {
32
+ display: block;
33
+ }
34
+ .hide {
35
+ display: none;
36
+ }
37
+ #notice {
38
+ color: red;
39
+ }
40
+ /* 以下遮罩层为demo.用户可自行设计实现 */
41
+ #mask {
42
+ display: none;
43
+ position: fixed;
44
+ text-align: center;
45
+ left: 0;
46
+ top: 0;
47
+ width: 100%;
48
+ height: 100%;
49
+ background-color: rgba(0, 0, 0, 0.5);
50
+ overflow: auto;
51
+ }
52
+ /* 可自行设计实现captcha的位置大小 */
53
+ .popup-mobile {
54
+ position: relative;
55
+ }
56
+ #popup-captcha-mobile {
57
+ position: fixed;
58
+ display: none;
59
+ left: 50%;
60
+ top: 50%;
61
+ transform: translate(-50%, -50%);
62
+ -webkit-transform: translate(-50%, -50%);
63
+ z-index: 9999;
64
+ }
65
+ </style>
66
+ </head>
67
+ <body>
68
+ <h1>极验验证SDKDemo</h1>
69
+ <br><br>
70
+ <hr>
71
+ <br><br>
72
+
73
+ <!-- 为使用方便,直接使用jquery.js库,如您代码中不需要,可以去掉 -->
74
+ <script src="http://code.jquery.com/jquery-1.12.3.min.js"></script>
75
+ <!-- 引入封装了failback的接口--initGeetest -->
76
+ <script src="http://static.geetest.com/static/tools/gt.js"></script>
77
+
78
+ <!-- 若是https,使用以下接口 -->
79
+ <!-- <script src="https://code.jquery.com/jquery-1.12.3.min.js"></script> -->
80
+ <!-- <script src="https://static.geetest.com/static/tools/gt.js"></script> -->
81
+
82
+ <div class="popup">
83
+ <h2>弹出式Demo,使用ajax形式提交二次验证码所需的验证结果值</h2>
84
+ <br>
85
+ <p>
86
+ <labe>用户名:</labe>
87
+ <input id="username1" class="inp" type="text" value="极验验证">
88
+ </p>
89
+ <br>
90
+ <p>
91
+ <label>密&nbsp;&nbsp;&nbsp;&nbsp;码:</label>
92
+ <input id="password1" class="inp" type="password" value="123456">
93
+ </p>
94
+
95
+ <br>
96
+ <input class="btn" id="popup-submit" type="submit" value="提交">
97
+
98
+ <div id="popup-captcha"></div>
99
+ </div>
100
+
101
+ <script>
102
+ var handlerPopup = function (captchaObj) {
103
+ // 成功的回调
104
+ captchaObj.onSuccess(function () {
105
+ var validate = captchaObj.getValidate();
106
+ $.ajax({
107
+ url: "<%= url('/validate') %>", // 进行二次验证
108
+ type: "post",
109
+ dataType: "json",
110
+ data: {
111
+ type: "pc",
112
+ username: $('#username1').val(),
113
+ password: $('#password1').val(),
114
+ geetest_challenge: validate.geetest_challenge,
115
+ geetest_validate: validate.geetest_validate,
116
+ geetest_seccode: validate.geetest_seccode
117
+ },
118
+ success: function (data) {
119
+ if (data && (data.status === "success")) {
120
+ $(document.body).html('<h1>登录成功</h1>');
121
+ } else {
122
+ $(document.body).html('<h1>登录失败</h1>');
123
+ }
124
+ }
125
+ });
126
+ });
127
+ $("#popup-submit").click(function () {
128
+ captchaObj.show();
129
+ });
130
+ // 将验证码加到id为captcha的元素里
131
+ captchaObj.appendTo("#popup-captcha");
132
+ // 更多接口参考:http://www.geetest.com/install/sections/idx-client-sdk.html
133
+ };
134
+ // 验证开始需要向网站主后台获取id,challenge,success(是否启用failback)
135
+ $.ajax({
136
+ url: "<%= url('/register') %>?type=pc&t=" + (new Date()).getTime(), // 加随机数防止缓存
137
+ type: "get",
138
+ dataType: "json",
139
+ success: function (data) {
140
+ // 使用initGeetest接口
141
+ // 参数1:配置参数
142
+ // 参数2:回调,回调的第一个参数验证码对象,之后可以使用它做appendTo之类的事件
143
+ initGeetest({
144
+ gt: data.gt,
145
+ challenge: data.challenge,
146
+ product: "popup", // 产品形式,包括:float,embed,popup。注意只对PC版验证码有效
147
+ offline: !data.success // 表示用户后台检测极验服务器是否宕机,一般不需要关注
148
+ // 更多配置参数请参见:http://www.geetest.com/install/sections/idx-client-sdk.html#config
149
+ }, handlerPopup);
150
+ }
151
+ });
152
+ </script>
153
+ <br><br>
154
+ <hr>
155
+ <br><br>
156
+ <form class="popup" action="<%= url('/validate') %>" method="post">
157
+ <h2>嵌入式Demo,使用表单形式提交二次验证所需的验证结果值</h2>
158
+ <input class="inp" name="type" type="hidden" value="pc">
159
+ <br>
160
+ <p>
161
+ <label for="username2">用户名:</label>
162
+ <input class="inp" id="username2" type="text" value="极验验证">
163
+ </p>
164
+ <br>
165
+ <p>
166
+ <label for="password2">密&nbsp;&nbsp;&nbsp;&nbsp;码:</label>
167
+ <input class="inp" id="password2" type="password" value="123456">
168
+ </p>
169
+
170
+ <div id="embed-captcha"></div>
171
+ <p id="wait" class="show">正在加载验证码......</p>
172
+ <p id="notice" class="hide">请先拖动验证码到相应位置</p>
173
+
174
+ <br>
175
+ <input class="btn" id="embed-submit" type="submit" value="提交">
176
+ </form>
177
+
178
+ <script>
179
+ var handlerEmbed = function (captchaObj) {
180
+ $("#embed-submit").click(function (e) {
181
+ var validate = captchaObj.getValidate();
182
+ if (!validate) {
183
+ $("#notice")[0].className = "show";
184
+ setTimeout(function () {
185
+ $("#notice")[0].className = "hide";
186
+ }, 2000);
187
+ e.preventDefault();
188
+ }
189
+ });
190
+ // 将验证码加到id为captcha的元素里,同时会有三个input的值:geetest_challenge, geetest_validate, geetest_seccode
191
+ captchaObj.appendTo("#embed-captcha");
192
+ captchaObj.onReady(function () {
193
+ $("#wait")[0].className = "hide";
194
+ });
195
+ // 更多接口参考:http://www.geetest.com/install/sections/idx-client-sdk.html
196
+ };
197
+ $.ajax({
198
+ // 获取id,challenge,success(是否启用failback)
199
+ url: "<%= url('/register') %>?type=pc&t=" + (new Date()).getTime(), // 加随机数防止缓存
200
+ type: "get",
201
+ dataType: "json",
202
+ success: function (data) {
203
+ // 使用initGeetest接口
204
+ // 参数1:配置参数
205
+ // 参数2:回调,回调的第一个参数验证码对象,之后可以使用它做appendTo之类的事件
206
+ initGeetest({
207
+ gt: data.gt,
208
+ challenge: data.challenge,
209
+ product: "embed", // 产品形式,包括:float,embed,popup。注意只对PC版验证码有效
210
+ offline: !data.success // 表示用户后台检测极验服务器是否宕机,一般不需要关注
211
+ // 更多配置参数请参见:http://www.geetest.com/install/sections/idx-client-sdk.html#config
212
+ }, handlerEmbed);
213
+ }
214
+ });
215
+ </script>
216
+ <br><br>
217
+ <hr>
218
+ <br><br>
219
+ <div class="popup-mobile">
220
+ <h2>移动端手动实现弹出式Demo</h2>
221
+ <br>
222
+ <p>
223
+ <labe for="username3">用户名:</labe>
224
+ <input id="username3" class="inp" type="text" value="极验验证">
225
+ </p>
226
+ <br>
227
+ <p>
228
+ <label for="password3">密&nbsp;&nbsp;&nbsp;&nbsp;码:</label>
229
+ <input id="password3" class="inp" type="password" value="123456">
230
+ </p>
231
+ <br>
232
+ <input class="btn" id="popup-submit-mobile" type="submit" value="提交">
233
+ <div id="mask"></div>
234
+ <div id="popup-captcha-mobile"></div>
235
+ </div>
236
+
237
+ <script>
238
+ $("#mask").click(function () {
239
+ $("#mask, #popup-captcha-mobile").hide();
240
+ });
241
+ $("#popup-submit-mobile").click(function () {
242
+ $("#mask, #popup-captcha-mobile").show();
243
+ });
244
+ var handlerPopupMobile = function (captchaObj) {
245
+ // 将验证码加到id为captcha的元素里
246
+ captchaObj.appendTo("#popup-captcha-mobile");
247
+ //拖动验证成功后两秒(可自行设置时间)自动发生跳转等行为
248
+ captchaObj.onSuccess(function () {
249
+ var validate = captchaObj.getValidate();
250
+ $.ajax({
251
+ url: "<%= url('/validate') %>", // 进行二次验证
252
+ type: "post",
253
+ dataType: "json",
254
+ data: {
255
+ // 二次验证所需的三个值
256
+ type: "mobile",
257
+ username: $('#username3').val(),
258
+ password: $('#password3').val(),
259
+ geetest_challenge: validate.geetest_challenge,
260
+ geetest_validate: validate.geetest_validate,
261
+ geetest_seccode: validate.geetest_seccode
262
+ },
263
+ success: function (data) {
264
+ if (data && (data.status === "success")) {
265
+ $(document.body).html('<h1>登录成功</h1>');
266
+ } else {
267
+ $(document.body).html('<h1>登录失败</h1>');
268
+ }
269
+ }
270
+ });
271
+ });
272
+ // 更多接口参考:http://www.geetest.com/install/sections/idx-client-sdk.html
273
+ };
274
+ $.ajax({
275
+ // 获取id,challenge,success(是否启用failback)
276
+ url: "<%= url('/register') %>?type=mobile&t=" + (new Date()).getTime(), // 加随机数防止缓存
277
+ type: "get",
278
+ dataType: "json",
279
+ success: function (data) {
280
+ // 使用initGeetest接口
281
+ // 参数1:配置参数
282
+ // 参数2:回调,回调的第一个参数验证码对象,之后可以使用它做appendTo之类的事件
283
+ initGeetest({
284
+ gt: data.gt,
285
+ challenge: data.challenge,
286
+ offline: !data.success // 表示用户后台检测极验服务器是否宕机,一般不需要关注
287
+ // 更多配置参数请参见:http://www.geetest.com/install/sections/idx-client-sdk.html#config
288
+ }, handlerPopupMobile);
289
+ }
290
+ });
291
+ </script>
292
+ </body>
293
+ </html>
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gt_ruby_sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - LcpMarvel
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: faraday
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activesupport
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: GtWeb Ruby SDK
112
+ email:
113
+ - lcpmarvel@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".editorconfig"
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".rubocop.yml"
122
+ - ".travis.yml"
123
+ - CODE_OF_CONDUCT.md
124
+ - Gemfile
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - bin/console
129
+ - bin/setup
130
+ - gt_ruby_sdk.gemspec
131
+ - lib/gt_ruby_sdk.rb
132
+ - lib/gt_ruby_sdk/local_server.rb
133
+ - lib/gt_ruby_sdk/remote_server.rb
134
+ - lib/gt_ruby_sdk/version.rb
135
+ - web/Gemfile
136
+ - web/Gemfile.lock
137
+ - web/demo.rb
138
+ - web/views/index.erb
139
+ homepage: https://github.com/LcpMarvel/gt-ruby-sdk
140
+ licenses:
141
+ - MIT
142
+ metadata: {}
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.5.2
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: GtWeb Ruby SDK
163
+ test_files: []