zhima 0.3.0 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5ae4098fe5acc3b45025e6cd8603676362d4c7c3
4
- data.tar.gz: b64c2616fa053692d59d8e86a30c3d256e2167e4
3
+ metadata.gz: 43037687ae73f8b71a5753c0996ea7bfa0cce3d0
4
+ data.tar.gz: 1286fc66a6246181c7ac8b0527ef8e78107bef9a
5
5
  SHA512:
6
- metadata.gz: 7c0ed4896d352c4302ca8dbcf640f8fedbf4c2e4123c67d600eaa952cd292196917b1108554ae0d9da5f70141d58fde4084b3da53d758aa8ceabc43f11784f52
7
- data.tar.gz: 2602e39b06060ba1d44970b8d9e50ebde5aa63b8e929f542045779ad4acef5e1a6bb7eb8c6c538479ae7ebb17672ec5ffd3c506032de6f97d1db62e72739fdf2
6
+ metadata.gz: 8c6fff8c94c128deef4616452768df00695351251daf63619841f1d7077c100e8a9670d2ce785c5eafe1b718643f83c1f7c2389edb91c1c6c665f0bc829b62cb
7
+ data.tar.gz: f555b189fc5da2a159de5c6bf09f3b54907b6ac3c62876c8cc5c6c6a5b3bca3b1c1407a8604585dd59fd523b2a20fa331770535ff5800e0380faeea145a3b5a8
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --format documentation
2
+ --require spec_helper
2
3
  --color
data/README.md CHANGED
@@ -25,22 +25,39 @@ Or install it yourself as:
25
25
  ```ruby
26
26
  Zhima.configure do |config|
27
27
  config.app_id = 'xxxxxxx' # 应用id
28
- config.private_key = private_key # 自己的私钥
29
- config.public_key = public_key # 芝麻给的公钥
28
+ config.private_key = File.read('private_key.pkcs8') # 自己的私钥
29
+ config.public_key = File.read('public_key.pkcs8') # 芝麻给的公钥
30
30
  end
31
31
  ```
32
32
 
33
33
  芝麻信用分,调用方法如下(请自行查阅芝麻文档的业务参数组织params):
34
34
  ```ruby
35
35
  # 认证url 业务参数请参考 https://b.zmxy.com.cn/technology/openDoc.htm?id=67
36
- Zhima::Score.auth_url(params) # 第二个参数可hash传入芝麻需要的系统参数,不传亦可(下同,省略)
36
+ params = {
37
+ identity_type: '2',
38
+ identity_param: {certNo: 'idcard_no', name: 'name', certType: 'IDENTITY_CARD'},
39
+ biz_params: {auth_code: 'M_H5', channelType: 'app', state: '100111211'}
40
+ }
41
+ system_options = {charset: 'UTF-8', version: '1.0', channel: 'app'} # 可省略,默认为这些参数
42
+ Zhima::Score.auth_url(params, system_options) # 第二个参数system_options传入芝麻需要的系统参数,不传亦可(下同,省略)
37
43
 
38
44
  # 获取芝麻分
39
45
  # https://b.zmxy.com.cn/technology/openDoc.htm?relInfo=zhima.credit.score.get@1.0@1.4&relType=API_DOC&type=API_INFO_DOC&LEFT_MENU_MODEnull#Seq_1
46
+ params = {
47
+ transaction_id: 'transaction_id',
48
+ product_code: 'w1010100100000000001',
49
+ open_id: 'open_id'
50
+ }
40
51
  Zhima::Score.get(params)
41
52
 
42
53
  # auth_query
43
54
  # https://b.zmxy.com.cn/technology/openDoc.htm?id=453
55
+ params = {
56
+ identity_type: '2',
57
+ identity_param: {
58
+ certNo: 'id_card_no', name: 'name', certType: 'IDENTITY_CARD'
59
+ }
60
+ }
44
61
  Zhima::Score.auth_query(params)
45
62
 
46
63
  # 芝麻callback url中的params参数解密
@@ -50,7 +67,23 @@ Zhima::Score.param_decrypt(params_str)
50
67
  反欺诈信息验证:
51
68
  ```ruby
52
69
  # 参数 https://b.zmxy.com.cn/technology/openDoc.htm?relInfo=zhima.credit.ivs.detail.get@1.0@1.2&relType=API_DOC&type=API_INFO_DOC&LEFT_MENU_MODEnull
70
+ params = {
71
+ product_code: 'w1010100000000000103',
72
+ transaction_id: 'transaction_id',
73
+ cert_no: '身份证',
74
+ cert_type: '100',
75
+ name: 'name',
76
+ mobile: '18888888888'
77
+ }
53
78
  Zhima::Ivs.get(params)
79
+
80
+ 行业关注名单2.0
81
+ params = {
82
+ product_code: 'w1010100100000000022',
83
+ transaction_id: 'transaction_id',
84
+ open_id: '268800000000000000000000'
85
+ }
86
+ Zhima::WatchList.get(params)
54
87
  ```
55
88
 
56
89
  ## Development
data/lib/zhima/param.rb CHANGED
@@ -1,26 +1,23 @@
1
1
  module Zhima
2
2
  # 业务参数params加解密
3
3
  class Param
4
- # {identity_type: 2, identity_param: URI.encode({certNo: '410526198605109070', name: '王智超', certType: 'IDENTITY_CARD'}.to_json), biz_params: URI.encode({auth_code: 'M_APPPC_CERT', channelType: 'apppc'}.to_json)}
4
+ # {identity_type: 2, identity_param: URI.encode({certNo: 'xxx', name: 'xxx', certType: 'IDENTITY_CARD'}.to_json), biz_params: URI.encode({auth_code: 'M_APPPC_CERT', channelType: 'apppc'}.to_json)}
5
5
  # 返回两个参数:加密后的params,及sign
6
- # .slice(:identity_type, :identity_param, :biz_params)
7
6
  def self.encrypt(params)
8
7
  params = Util.symbolize_hash_keys(params)
9
8
  params.each { |key, value| params[key] = value.to_json if value.is_a? Hash }
10
9
  param_str = Util.to_query(params)
11
- encrypted_str = param_str.split('').each_slice(117).inject('') do |str, bytes|
12
- str += encrypt_str(bytes.join())
13
- str
14
- end
15
-
16
10
  [
17
- Util.base64_encode(encrypted_str),
11
+ Util.base64_encode(rsa_encrypt(param_str)),
18
12
  Util.base64_encode(Sign.sign(param_str))
19
13
  ]
20
14
  end
21
15
 
22
- def self.encrypt_str(str)
23
- Config.zm_rsa.public_encrypt str
16
+ def self.rsa_encrypt(str)
17
+ str.split('').each_slice(117).inject('') do |s, bytes|
18
+ s += Config.zm_rsa.public_encrypt(bytes.join())
19
+ s
20
+ end
24
21
  end
25
22
 
26
23
  def self.decrypt(param_str)
data/lib/zhima/request.rb CHANGED
@@ -25,12 +25,17 @@ module Zhima
25
25
 
26
26
  private
27
27
  # 芝麻返回的json数据解析,结果为真正的业务参数
28
- # TODO verify sign
29
28
  def parse_response(response_str)
30
29
  response_hash = JSON.parse(response_str)
31
- biz_response = response_hash["biz_response"]
30
+ biz_response_sign = response_hash['biz_response_sign']
31
+ biz_response = response_hash['biz_response']
32
32
  biz_response = Param.decrypt(biz_response) if response_hash["encrypted"]
33
- JSON.parse(biz_response)
33
+
34
+ if response_hash["encrypted"] && !Sign.verify?(biz_response_sign, biz_response)
35
+ {error: true, error_msg: 'sign解签错误'}
36
+ else
37
+ JSON.parse(biz_response)
38
+ end
34
39
  end
35
40
  end
36
41
  end
data/lib/zhima/sign.rb CHANGED
@@ -6,8 +6,9 @@ module Zhima
6
6
  Config.mech_rsa.sign(SIGN_METHOD, str)
7
7
  end
8
8
 
9
- # TODO
10
- def self.verify()
9
+ def self.verify?(sign, str)
10
+ decode64_sign = Base64.strict_decode64(sign.to_s)
11
+ Config.zm_rsa.verify(SIGN_METHOD, decode64_sign, str.force_encoding("utf-8"))
11
12
  end
12
13
  end
13
14
  end
data/lib/zhima/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Zhima
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.3"
3
3
  end
@@ -0,0 +1,11 @@
1
+ module Zhima
2
+ # 行业关注名单2.0版
3
+ class WatchList
4
+ METHOD = 'zhima.credit.watchlistii.get'
5
+
6
+ # https://b.zmxy.com.cn/technology/openDoc.htm?relInfo=zhima.credit.watchlistii.get@1.0@1.0&relType=API_DOC&LEFT_MENU_MODE=null
7
+ def self.get(params, sys_options = {})
8
+ Request.new(params, sys_options.merge(method: METHOD)).execute
9
+ end
10
+ end
11
+ end
data/lib/zhima.rb CHANGED
@@ -14,6 +14,7 @@ end
14
14
  require_relative "zhima/config"
15
15
  require_relative "zhima/score"
16
16
  require_relative "zhima/ivs"
17
+ require_relative "zhima/watch_list"
17
18
  require_relative "zhima/request"
18
19
  require_relative "zhima/param"
19
20
  require_relative "zhima/sign"
data/zhima.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["zhchsf@gmail.com"]
11
11
 
12
12
  spec.summary = %q{芝麻信用}
13
- spec.description = %q{芝麻信用}
13
+ spec.description = %q{芝麻信用 非官方ruby sdk}
14
14
  spec.homepage = "https://github.com/zhchsf/zhima"
15
15
  spec.license = "MIT"
16
16
 
@@ -32,4 +32,5 @@ Gem::Specification.new do |spec|
32
32
  spec.add_development_dependency "bundler", "~> 1.12"
33
33
  spec.add_development_dependency "rake", "~> 10.0"
34
34
  spec.add_development_dependency "rspec", "~> 3.0"
35
+ spec.add_development_dependency "webmock", "~> 2.0"
35
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zhima
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - sam
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-21 00:00:00.000000000 Z
11
+ date: 2016-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -66,7 +66,21 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.0'
69
- description: "芝麻信用"
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.0'
83
+ description: "芝麻信用 非官方ruby sdk"
70
84
  email:
71
85
  - zhchsf@gmail.com
72
86
  executables: []
@@ -92,6 +106,7 @@ files:
92
106
  - lib/zhima/sign.rb
93
107
  - lib/zhima/util.rb
94
108
  - lib/zhima/version.rb
109
+ - lib/zhima/watch_list.rb
95
110
  - zhima.gemspec
96
111
  homepage: https://github.com/zhchsf/zhima
97
112
  licenses: