yunpian-captcha 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9f7bdd8215548f166170b031bff70fc47f69307b5be0ea26fef0127a7e90d2de
4
+ data.tar.gz: c601a9cce96a56685a18a516bd84df266804527412dd8f72d830780a0d33f7f5
5
+ SHA512:
6
+ metadata.gz: e5aff6299b16c75c9e96edb1d5ade4b9f6c43923713e86a7d13dee000d35cd4734a7f18b0567f224230dcb8f1e9e28df670c1778971d97bab68495314646738c
7
+ data.tar.gz: 4a9edecefb116184d6ae31299539bfc1b27314af889c45de892cd377519fdada1078b920736dbb17a4e22e865501ac038fcef25d5984b680b3453c2b211445f4
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --format documentation
2
+ --color
3
+ --require pry
4
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.5
7
+ before_install: gem install bundler -v 2.0.2
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in yunpian-captcha.gemspec
4
+ gemspec
5
+
6
+ gem "pry", "~> 0.12.2"
7
+ gem 'vcr', '~> 5.0'
8
+ gem 'webmock', '~> 3.7', '>= 3.7.6'
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ yunpian-captcha (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.7.0)
10
+ public_suffix (>= 2.0.2, < 5.0)
11
+ coderay (1.1.2)
12
+ crack (0.4.3)
13
+ safe_yaml (~> 1.0.0)
14
+ diff-lcs (1.3)
15
+ hashdiff (1.0.0)
16
+ method_source (0.9.2)
17
+ pry (0.12.2)
18
+ coderay (~> 1.1.0)
19
+ method_source (~> 0.9.0)
20
+ public_suffix (4.0.1)
21
+ rake (10.5.0)
22
+ rspec (3.9.0)
23
+ rspec-core (~> 3.9.0)
24
+ rspec-expectations (~> 3.9.0)
25
+ rspec-mocks (~> 3.9.0)
26
+ rspec-core (3.9.0)
27
+ rspec-support (~> 3.9.0)
28
+ rspec-expectations (3.9.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.9.0)
31
+ rspec-mocks (3.9.0)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.9.0)
34
+ rspec-support (3.9.0)
35
+ safe_yaml (1.0.5)
36
+ vcr (5.0.0)
37
+ webmock (3.7.6)
38
+ addressable (>= 2.3.6)
39
+ crack (>= 0.3.2)
40
+ hashdiff (>= 0.4.0, < 2.0.0)
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ bundler (~> 2.0)
47
+ pry (~> 0.12.2)
48
+ rake (~> 10.0)
49
+ rspec (~> 3.0)
50
+ vcr (~> 5.0)
51
+ webmock (~> 3.7, >= 3.7.6)
52
+ yunpian-captcha!
53
+
54
+ BUNDLED WITH
55
+ 2.0.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 xiaohui
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.
@@ -0,0 +1,56 @@
1
+ # YunPian Captcha
2
+
3
+ Ruby Client for YunPian.com Captcha service https://www.yunpian.com/doc/zh_CN/captcha/captcha_service.html
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'yunpian-captcha'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install yunpian-captcha
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ # config/initializers/yunpian.rb
25
+ Yunpian::Captcha.configure do |config|
26
+ config.app_id = ENV['YUNPIAN_CAPTCHA_APP_ID']
27
+ config.secret_id = ENV['YUNPIAN_CAPTCHA_SECRET_ID']
28
+ config.secret_key = ENV['YUNPIAN_CAPTCHA_SECRET_KEY']
29
+ end
30
+
31
+ # app/controllers/sessions_controller.rb
32
+
33
+ def create
34
+ if Yunpian::Captcha.verify(params[:captcha_token], params[:captcha_authenticate])
35
+ # verify username/password
36
+ # sign_in!
37
+ else
38
+ flash[:alert] = 'invalid captcha'
39
+ render :new
40
+ end
41
+ end
42
+ ```
43
+
44
+ ## Development
45
+
46
+ 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.
47
+
48
+ 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).
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/xiaohui-zhangxh/yunpian-captcha.
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "yunpian/captcha"
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__)
@@ -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,68 @@
1
+ <html>
2
+
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <!--依赖-->
6
+ <script src="https://www.yunpian.com/static/official/js/libs/riddler-sdk-0.2.2.js"></script>
7
+ <script>
8
+ window.onload = function () {
9
+ // 初始化
10
+ new YpRiddler({
11
+ expired: 10,
12
+ mode: 'dialog',
13
+ winWidth: 500,
14
+ lang: 'zh-cn', // 界面语言, 目前支持: 中文简体 zh-cn, 英语 en
15
+ // langPack: LANG_OTHER, // 你可以通过该参数自定义语言包, 其优先级高于lang
16
+ container: document.getElementById('cbox'),
17
+ appId: '450c5c58b47f423d9eb0d2412760d20e',
18
+ version: 'v1',
19
+ onError: function (param) {
20
+ if (!param.code) {
21
+ console.error('错误请求');
22
+ }
23
+ else if (parseInt(param.code / 100) == 5) {
24
+ // 服务不可用时,开发者可采取替代方案
25
+ console.error('验证服务暂不可用');
26
+ }
27
+ else if (param.code == 429) {
28
+ console.warn('请求过于频繁,请稍后再试');
29
+ }
30
+ else if (param.code == 403) {
31
+ console.warn('请求受限,请稍后再试');
32
+ }
33
+ else if (param.code == 400) {
34
+ console.warn('非法请求,请检查参数');
35
+ }
36
+ // 异常回调
37
+ console.error('验证服务异常')
38
+ },
39
+ onSuccess: function (validInfo, close, useDefaultSuccess) {
40
+ // 成功回调
41
+ alert('验证通过! token=' + validInfo.token + ', authenticate=' + validInfo.authenticate)
42
+ // 验证成功默认样式
43
+ useDefaultSuccess(true)
44
+ close()
45
+ },
46
+ onFail: function (code, msg, retry) {
47
+ // 失败回调
48
+ alert('出错啦:' + msg + ' code: ' + code)
49
+ retry()
50
+ },
51
+ beforeStart: function (next) {
52
+ console.log('验证马上开始')
53
+ next()
54
+ },
55
+ onExit: function () {
56
+ // 退出验证 (仅限dialog模式有效)
57
+ console.log('退出验证')
58
+ }
59
+ })
60
+ }
61
+ </script>
62
+ </head>
63
+
64
+ <body>
65
+ <div id="cbox"></div>
66
+ </body>
67
+
68
+ </html>
@@ -0,0 +1,22 @@
1
+ require "yunpian/captcha/version"
2
+ require 'yunpian/captcha/auth_service'
3
+ module Yunpian
4
+ module Captcha
5
+ Configuration = Struct.new(:app_id, :secret_id, :secret_key, :exception_recorder, :logger)
6
+ API_URI = URI('https://captcha.yunpian.com/v1/api/authenticate')
7
+
8
+ class << self
9
+ def config
10
+ @config ||= Configuration.new
11
+ end
12
+
13
+ def configure
14
+ yield config
15
+ end
16
+
17
+ def verify(token, authenticate)
18
+ Yunpian::Captcha::AuthService.new(token, authenticate, config).call
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,73 @@
1
+ require 'digest/md5'
2
+ require 'net/http'
3
+ require 'json'
4
+ module Yunpian::Captcha
5
+ class AuthService
6
+ PARAMS = %w[captchaId token authenticate secretId version user timestamp nonce signature]
7
+
8
+ attr_reader :token, :authenticate, :config
9
+ def initialize(token, authenticate, config = Yunpian::Captcha.config)
10
+ @config, @token, @authenticate = config, token, authenticate
11
+ end
12
+
13
+ def call
14
+ log :debug, 'verifying'
15
+ signed_params = sign(build_params)
16
+ valid?(signed_params)
17
+ end
18
+
19
+ private
20
+
21
+ def valid?(signed_params)
22
+ log :debug, "signed params is #{signed_params}"
23
+ resp = Net::HTTP.post_form(API_URI, signed_params)
24
+ log :debug, "API response => #{resp.body}"
25
+ result = JSON.parse(resp.body) rescue nil
26
+ return true if result && result['code'].zero?
27
+
28
+ record_exception!(result)
29
+ false
30
+ end
31
+
32
+ def sign(params)
33
+ params.tap do |p|
34
+ p[:signature] = signature(params)
35
+ end
36
+ end
37
+
38
+ def signature(params)
39
+ str = params.sort.inject("") { |ret, kv| ret << kv.join }
40
+ str << config.secret_key
41
+ Digest::MD5.hexdigest(str)
42
+ end
43
+
44
+ def build_params
45
+ {
46
+ captchaId: config.app_id,
47
+ token: token,
48
+ authenticate: authenticate,
49
+ secretId: config.secret_id,
50
+ version: '1.0',
51
+ timestamp: (Time.now.to_f * 1000).to_i,
52
+ nonce: rand(1..99_999)
53
+ }
54
+ end
55
+
56
+ def log(type, message)
57
+ return unless logger
58
+
59
+ logger.send type,
60
+ "[YunPian Captcha][token=#{token}, " \
61
+ "authenticate=#{authenticate}] " \
62
+ "#{message}"
63
+ end
64
+
65
+ def logger
66
+ @logger ||= config.logger
67
+ end
68
+
69
+ def record_exception!(result)
70
+ config.exception_recorder&.call(result)
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,5 @@
1
+ module Yunpian
2
+ module Captcha
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "yunpian/captcha/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "yunpian-captcha"
7
+ spec.version = Yunpian::Captcha::VERSION
8
+ spec.authors = ["xiaohui"]
9
+ spec.email = ["xiaohui@tanmer.com"]
10
+
11
+ spec.summary = %q{Captcha from YunPian.com}
12
+ spec.description = %q{Captcha Client for YunPian.com}
13
+ spec.homepage = "https://github.com/xiaohui-zhangxh/yunpian-captcha"
14
+ spec.license = "MIT"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency "bundler", "~> 2.0"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yunpian-captcha
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - xiaohui
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-20 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
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
+ description: Captcha Client for YunPian.com
56
+ email:
57
+ - xiaohui@tanmer.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - exampes/index.html
73
+ - lib/yunpian/captcha.rb
74
+ - lib/yunpian/captcha/auth_service.rb
75
+ - lib/yunpian/captcha/version.rb
76
+ - yunpian-captcha.gemspec
77
+ homepage: https://github.com/xiaohui-zhangxh/yunpian-captcha
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ homepage_uri: https://github.com/xiaohui-zhangxh/yunpian-captcha
82
+ source_code_uri: https://github.com/xiaohui-zhangxh/yunpian-captcha
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubygems_version: 3.0.6
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Captcha from YunPian.com
102
+ test_files: []