zhima_auth 0.2.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 72c6022d4ec7ad10934852d37960bb357b2ab091
4
- data.tar.gz: 5f84fb04bd7dfe979934a63f6cab99c42b904dd0
3
+ metadata.gz: 20c07328831c323f52ed18319126106ea7db50e5
4
+ data.tar.gz: 42594b30d81fc95e15ac28218f5c6ffdf74a6660
5
5
  SHA512:
6
- metadata.gz: 6bb8167eb89b62dc30abfaabcbedd564c39b17a42d1e74e7e9fda30efb4c469608f9c338c91fc852c8a79fdaa2daa67a434d86ed3a6addad7ea7738d53327b0e
7
- data.tar.gz: 2c29257fa3673bc9a935adef2075a93fec5791e07a3650ef0d108f1cba67eb52754d326fb886105f5d551bae42474e58cc2c958ea5cff879dab9109128cee0ea
6
+ metadata.gz: ad8cb899a198e178f6d4ba583e886f544955891842268ba9be2c14258b9721caf405c3b5b6199314937086f7bd61adb5967b7000298d8eedd23c5a51b681bc66
7
+ data.tar.gz: d92bb317756319d2a361c0c2c51336d72a021718b98c5ca5e2e644ba41bb328e729d1074590e9b45b144ece82a9af5fff0fbc21ba5d8dbefbb1f6bf292b3d591
data/.gitignore CHANGED
@@ -5,5 +5,6 @@
5
5
  /coverage/
6
6
  /doc/
7
7
  /pkg/
8
+ /spec/zhima.yml
8
9
  /spec/reports/
9
10
  /tmp/
data/README.md CHANGED
@@ -1,41 +1,71 @@
1
1
  # ZhimaAuth
2
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/zhima_auth`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [芝麻认证](https://doc.open.alipay.com/docs/doc.htm?treeId=271&articleId=105915&docType=1)是通过人脸识别、眼纹识别、银行卡验证等手段,对用户进行实人验证,可有效地核实用户身份,防止身份冒用、欺诈等风险。简单说就是验证“张三是张三”。
4
+ 因为官方并没有发布针对Ruby的SDK同时市面上并没有现成的可用于Ruby的第三方开发包,于是这个针对该空缺的Ruby就诞生了,Yeah:D
5
+ 由于官方暂时只支持身份证 + 人脸识别的方案。所以此Gem也不列外。使用方式非常简单,具体如下,如果觉得这个Gem对你有用请别忘了点个赞,Happy coding! Everyone!
4
6
 
5
- TODO: Delete this and the text above, and describe your gem
7
+ ## Usage
6
8
 
7
- ## Installation
9
+ 首先[官方文档链接](https://doc.open.alipay.com/docs/doc.htm?treeId=271&articleId=105913&docType=1), 如果遇到任何意外的问题,首先建议请一定参阅官方文档,毕竟文档才是唯一的Truth。
8
10
 
9
- Add this line to your application's Gemfile:
11
+ ### STEP 1
12
+ [产品注册](https://doc.open.alipay.com/docs/doc.htm?treeId=271&articleId=105913&docType=1), 这是第一步,注册产品并获取必要的app_id, 接着[配置密钥](https://doc.open.alipay.com/docs/doc.htm?treeId=291&articleId=106103&docType=1),其中自己的private_key是需要配置进自己的app的。完成上述两部获得相应的app_id及private_key,一切就准备就绪了。
10
13
 
14
+ ### STEP 2
15
+ 如果你用的是Rails(下面都会以此为假设)
11
16
  ```ruby
17
+ # Gemfile
12
18
  gem 'zhima_auth'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install zhima_auth
22
-
23
- ## Usage
24
19
 
25
- TODO: Write usage instructions here
20
+ bundle install
21
+
22
+ # config/initializers/zhima.rb
23
+ ZhimaAuth.configure do |config|
24
+ config.app_id = "1223232"
25
+ config.private_key = private_key
26
+ end
27
+
28
+ # private_key looks like this
29
+ private_key = <<-KEY
30
+ -----BEGIN RSA PRIVATE KEY-----
31
+ MIIEogIBAAKCAQEAtSq73+gTT7OI7psSTvXGvKo6PtHoJ3NX3KS/vcSMIju9A0uT
32
+ uBZXA3Rsz/dUTlgAZOqkNWLHDoDj+pcjX1Oj99wkKDWAfui1xcrj5qgljBKPxiYo
33
+ nJUxDrmiHwFeO6KiNs4Z+FutAposxioz1+JhHYnssuL2lm4kwc7eJxYaDls5shlg
34
+ ...many lines here...
35
+ LMFBAoGAUAgyFOusxxcXaKdJY+LzMNyQLmbmvVxF0l5V0syE1a8Mv5YivFsXqIFT
36
+ x+26iF4G8GcrZtejwVrkgr9i6B4CIHa90Qs8PpAuFgzspwU4ALOsBujfC1fk+iVl
37
+ d3S+mDxT/A+hxMFsZr8CQUj/CJtG/SYcLpAWAW3akvVqacHgqOQ=
38
+ -----END RSA PRIVATE KEY-----
39
+ KEY
40
+ ```
26
41
 
27
- ## Development
42
+ ### STEP 3
43
+ 配置完成之后,最简单的调用方法如下
44
+ ```ruby
45
+ # 获取biz_no, certify_url
46
+ params = { cert_name: "王大锤", cert_no: "32012345678901234X", return_url: "http://www.liangboyuan.pub", transanction_id: "12345" }
47
+ # transanction_id should be given for the moment, problem will be solved in the next release
48
+ ZhimaAuth.certify params
49
+
50
+ # expected response, following response is a fake one.
51
+ {:biz_no=>"ZM201704153000000767600024912345",
52
+ :certify_url=>
53
+ "https://openapi.alipay.com/gateway.do?app_id=2017021605123456&charset=utf-8&format=JSON&sign_type=RSA2&version=1.0&method=zhima.customer.certification.certify&return_url=http%3A%2F%2Fwww.liangboyuan.pub&timestamp=2017-04-15+14%3A37%3A30&biz_content=%7B%22biz_no%22%3A%22ZM201704153000000767600024912345%22%7D&sign=lXzkmaNfv%2FwZppNItxTBGIJK%2Fpw2sKwhofNf9UtN3RMl%2FFo2FSKY4LaA3Vf6J04VXNI6LK7Vkw0OiTVVfKwG2P%2F5AbpUxNeC2uevE%2FIBjOvq6QeQwZNcfjhc8M87umMWUqTpsDzye6W2KaRR7HAbDOeEG8iizVXwADqf842nkWyviFj7Jh8YK6726DsleZTb%2BQybNWmPgJ4Y7wLeSmhNSe8aP9vmVuXVbVshTU1I50BgoaSpvvFLwHzmKstrKkdgpVwQgMcloGMMK3z90vbdNNn85KamxwF4u2reUSYeavkKBvgQyog%3D%3D"}
54
+ ```
55
+ 获取认证结果的调用方法如下
56
+ ```ruby
57
+ # 参数为之前一部获取的biz_no
58
+ ZhimaAuth::QueryRequest.new("MK8789789789787978").get_certify_result
28
59
 
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.
60
+ # expected result
61
+ "true" or "false"
62
+ ```
30
63
 
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
64
 
33
65
  ## Contributing
34
66
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/zhima_auth. 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
-
67
+ If you find any bug or want to contribute, feel free to make a pull request or create a issue, I will try my best to work with that. Or you can live me a message to my email: lby89757@hotmail.com
37
68
 
38
69
  ## License
39
70
 
40
71
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
@@ -1,6 +1,6 @@
1
1
  module ZhimaAuth
2
2
  class Configuration
3
- attr_accessor :app_id, :private_key, :sign_type, :format, :charset, :version, :biz_code
3
+ attr_accessor :app_id, :private_key, :sign_type, :format, :charset, :version, :biz_code, :admittance_score
4
4
 
5
5
  def initialize
6
6
  @app_id = "app_id"
@@ -10,6 +10,7 @@ module ZhimaAuth
10
10
  @charset = "utf-8"
11
11
  @version = "1.0"
12
12
  @biz_code = "FACE"
13
+ @admittance_score = 650
13
14
  end
14
15
  end
15
16
  end
@@ -1,12 +1,12 @@
1
1
  module ZhimaAuth
2
- class ZhimaAuthInvalidParams < StandardError
2
+ class InvalidParams < StandardError
3
3
  attr_accessor :message
4
4
  def initialize(msg=nil)
5
5
  @message = msg
6
6
  end
7
7
  end
8
8
 
9
- class ZhimaAuthInvalidResponse < StandardError
9
+ class InvalidResponse < StandardError
10
10
  attr_accessor :message
11
11
  def initialize(msg=nil)
12
12
  @message = msg
@@ -128,4 +128,56 @@ module ZhimaAuth
128
128
  end
129
129
 
130
130
  end
131
+
132
+ class CreditRequest < BaseRequest
133
+
134
+ def initialize biz_params
135
+ # support cert_type: IDENTITY_CARD(身份证),PASSPORT(护照),ALIPAY_USER_ID(支付宝uid)
136
+ @cert_type = biz_params[:cert_type]
137
+ @cert_name = biz_params[:cert_name]
138
+ @cert_no = biz_params[:cert_no]
139
+ @transaction_id = biz_params[:transaction_id]
140
+ @admittance_score = biz_params[:admittance_score]
141
+ end
142
+
143
+ def excute
144
+ @response ||= RestClient.post url_with_params, {}
145
+ end
146
+
147
+ def get_result
148
+ res = JSON.parse(excute)
149
+ Validation.check_credit_response res
150
+ result = res["zhima_credit_score_brief_get_response"]["is_admittance"]
151
+ biz_no = res["zhima_credit_score_brief_get_response"]["biz_no"]
152
+ {
153
+ passed: result == "Y" ? true : false,
154
+ biz_no: biz_no
155
+ }
156
+ end
157
+
158
+ private
159
+
160
+ def url_with_params
161
+ [url, WebUtil.to_query(params_with_sign)].join("?")
162
+ end
163
+
164
+ def params
165
+ @params ||= base_params.merge({
166
+ method: "zhima.credit.score.brief.get",
167
+ timestamp: Time.now.strftime("%Y-%m-%d %H:%M:%S"),
168
+ biz_content: biz_content.to_json
169
+ })
170
+ end
171
+
172
+ def biz_content
173
+ {
174
+ transaction_id: @transaction_id,
175
+ product_code: "w1010100000000002733",
176
+ cert_type: @cert_type,
177
+ cert_no: @cert_no,
178
+ name: @cert_name,
179
+ admittance_score: @admittance_score
180
+ }.delete_if { |key, value| value.to_s.strip == '' }
181
+ end
182
+ end
131
183
  end
@@ -2,43 +2,53 @@ module ZhimaAuth
2
2
  class Validation
3
3
  class << self
4
4
  def check_initialize_params biz_params
5
- raise ZhimaAuthInvalidParams, "Params should be a hash" unless biz_params.is_a? Hash
5
+ raise ZhimaAuth::InvalidParams, "Params should be a hash" unless biz_params.is_a? Hash
6
6
  unless biz_params[:cert_name] && biz_params[:cert_no] && biz_params[:transaction_id]
7
- raise ZhimaAuthInvalidParams, "Params should include cert_name, cert_no and transaction_id"
7
+ raise ZhimaAuth::InvalidParams, "Params should include cert_name, cert_no and transaction_id"
8
8
  end
9
9
  check_name biz_params[:cert_name]
10
10
  check_id_no biz_params[:cert_no]
11
11
  end
12
12
 
13
13
  def check_certify_params biz_params
14
- raise ZhimaAuthInvalidParams, "Params should be a hash" unless biz_params.is_a? Hash
14
+ raise ZhimaAuth::InvalidParams, "Params should be a hash" unless biz_params.is_a? Hash
15
15
  unless biz_params[:biz_no] && biz_params[:return_url]
16
- raise ZhimaAuthInvalidParams, "Params should include biz_no and return_url"
16
+ raise ZhimaAuth::InvalidParams, "Params should include biz_no and return_url"
17
17
  end
18
18
  check_url biz_params[:return_url]
19
19
  check_biz_no biz_params[:biz_no]
20
20
  end
21
21
 
22
22
  def check_biz_no biz_no
23
- raise ZhimaAuthInvalidParams, "Invalid biz no" unless biz_no.length == 32
23
+ raise ZhimaAuth::InvalidParams, "Invalid biz no" unless biz_no.length == 32
24
24
  end
25
25
 
26
26
  def check_initialize_response response
27
- raise ZhimaAuthInvalidResponse, "Initialize request failed" unless (response.is_a? Hash) && response["zhima_customer_certification_initialize_response"]
27
+ raise ZhimaAuth::InvalidResponse, "Initialize request failed" unless (response.is_a? Hash) && response["zhima_customer_certification_initialize_response"]
28
28
  response_code = response["zhima_customer_certification_initialize_response"]["code"]
29
29
  if response_code != "10000"
30
30
  response_msg = response["zhima_customer_certification_initialize_response"]["sub_msg"]
31
- raise ZhimaAuthInvalidResponse, "#{response_code}#{response_msg}"
31
+ raise ZhimaAuth::InvalidResponse, "#{response_code}#{response_msg}"
32
32
  end
33
33
  true
34
34
  end
35
35
 
36
36
  def check_query_response response
37
- raise ZhimaAuthInvalidResponse, "Query request failed" unless (response.is_a? Hash) && response["zhima_customer_certification_query_response"]
37
+ raise ZhimaAuth::InvalidResponse, "Query request failed" unless (response.is_a? Hash) && response["zhima_customer_certification_query_response"]
38
38
  response_code = response["zhima_customer_certification_query_response"]["code"]
39
39
  if response_code != "10000"
40
40
  response_msg = response["zhima_customer_certification_query_response"]["sub_msg"]
41
- raise ZhimaAuthInvalidResponse, "#{response_code}#{response_msg}"
41
+ raise ZhimaAuth::InvalidResponse, "#{response_code}#{response_msg}"
42
+ end
43
+ true
44
+ end
45
+
46
+ def check_credit_response response
47
+ raise ZhimaAuth::InvalidResponse, "Credit request failed" unless (response.is_a? Hash) && response["zhima_credit_score_brief_get_response"]
48
+ response_code = response["zhima_credit_score_brief_get_response"]["code"]
49
+ if response_code != "10000"
50
+ response_msg = response["zhima_credit_score_brief_get_response"]["sub_msg"]
51
+ raise ZhimaAuth::InvalidResponse, "#{response_code}#{response_msg}"
42
52
  end
43
53
  true
44
54
  end
@@ -46,16 +56,16 @@ module ZhimaAuth
46
56
  private
47
57
 
48
58
  def check_url url
49
- raise ZhimaAuthInvalidParams, "Invalid return url" unless url =~ URI::regexp
59
+ raise ZhimaAuth::InvalidParams, "Invalid return url" unless url =~ URI::regexp
50
60
  end
51
61
 
52
62
  def check_name name
53
- raise ZhimaAuthInvalidParams, "Invalid name" if (name.length > 5 || name.length <= 1)
63
+ raise ZhimaAuth::InvalidParams, "Invalid name" if (name.length > 10 || name.length <= 1)
54
64
  true
55
65
  end
56
66
 
57
67
  def check_id_no id_no
58
- raise ZhimaAuthInvalidParams, "Invalid identity number" unless (id_no.length == 15 || id_no.length == 18)
68
+ raise ZhimaAuth::InvalidParams, "Invalid identity number" unless (id_no.length == 15 || id_no.length == 18)
59
69
  true
60
70
  end
61
71
  end
@@ -1,3 +1,3 @@
1
1
  module ZhimaAuth
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zhima_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - BranLiang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-15 00:00:00.000000000 Z
11
+ date: 2017-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client