zhima 0.1.1 → 0.2.2

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: 3d4635cf216406eec296ffb79145df637d5851f2
4
- data.tar.gz: d0c4accc311070fe02872ade7ab36e33adaadb71
3
+ metadata.gz: d1f2a5dcde395a6dec95e6a934d13bf1e48dd6bd
4
+ data.tar.gz: f8242edbe588875639302fce434e14e6178b8bf3
5
5
  SHA512:
6
- metadata.gz: 6366688abd1045e25b24c32d15ff101a259204df517dc222c5b50d66e43e47960ce17d8cc9be3778a46d193253c1d42fd33423180b9e06da0c22f71d40bdfc93
7
- data.tar.gz: a3d053bd747c0dd2f146f77b96c6da09608a7cac817390b2e82db9d0585c2d919e83f269396b2964a1a43e52800aaf934d617f8a89bb21ee2a2a908be67e6a74
6
+ metadata.gz: 654e9ef37b9c96f11fc06812c4e80a859323830fc97937aa6c95c90bb7d5ec39195a018bbc5dff9a33aa91812a8f6e503101b30a80a205f3582a6f6a5230325f
7
+ data.tar.gz: 41d8a125c9f1baa284a7178d34783a50e2c4cd93101e2bb9523bfcf5ee13c7af212c09c35a4ed3245fc962392a859641db320736d54f01bcdc0ae8a2e5d60a10
@@ -0,0 +1,19 @@
1
+ module Zhima
2
+ class Config
3
+ class << self
4
+ attr_accessor :app_id, :private_key, :public_key
5
+
6
+ def configure
7
+ yield self
8
+ end
9
+
10
+ def mech_rsa
11
+ @mech_rsa ||= OpenSSL::PKey::RSA.new private_key
12
+ end
13
+
14
+ def zm_rsa
15
+ @zm_rsa ||= OpenSSL::PKey::RSA.new public_key
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,31 @@
1
+ module Zhima
2
+ # 业务参数params加解密
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)}
5
+ # 返回两个参数:加密后的params,及sign
6
+ # .slice(:identity_type, :identity_param, :biz_params)
7
+ def self.encrypt(params)
8
+ params = Util.symbolize_hash_keys(params)
9
+ params.each { |key, value| params[key] = URI.encode(value.to_json) if value.is_a? Hash }
10
+ 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
+ [
17
+ Util.base64_and_uri_encode(encrypted_str),
18
+ Util.base64_and_uri_encode(Sign.sign(param_str))
19
+ ]
20
+ end
21
+
22
+ def self.encrypt_str(str)
23
+ Config.zm_rsa.public_encrypt str
24
+ end
25
+
26
+ def self.decrypt(param_str)
27
+ # strict_decode64 decode64
28
+ Config.mech_rsa.private_decrypt(Base64.decode64 param_str)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,47 @@
1
+ require "rest-client"
2
+
3
+ module Zhima
4
+ class Score
5
+ GATEWAY = 'https://zmopenapi.zmxy.com.cn/openapi.do'
6
+ SYSTEM_OPTIONS = {charset: 'UTF-8', version: '1.0', channel: 'app'}
7
+ AUTHORIZE_METHOD = 'zhima.auth.info.authorize'
8
+ SCORE_METHOD = 'zhima.credit.score.get'
9
+ AUTH_QUERY_METHOD = 'zhima.auth.info.authquery'
10
+
11
+ # params参数 请参考 https://b.zmxy.com.cn/technology/openDoc.htm?id=67
12
+ # 系统参数 SYSTEM_OPTIONS,可自己传入,一般只需要配置channel参数(与auth_code不对应芝麻信用会报错)
13
+ def self.auth_url(params, sys_options = {})
14
+ url_by(params, sys_options.merge(method: AUTHORIZE_METHOD))
15
+ end
16
+
17
+ # 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
18
+ # TODO verify sign
19
+ def self.get(params, sys_options = {})
20
+ score_url = url_by(params, sys_options.merge(method: SCORE_METHOD))
21
+ response = RestClient.get score_url
22
+ parse_response(response.body)
23
+ end
24
+
25
+ # https://b.zmxy.com.cn/technology/openDoc.htm?id=453
26
+ def self.auth_query(params, sys_options = {})
27
+ query_url = url_by(params, sys_options.merge(method: AUTH_QUERY_METHOD))
28
+ response = RestClient.get query_url
29
+ parse_response(response.body)
30
+ end
31
+
32
+ def self.url_by(params, sys_options)
33
+ params_value, sign = Param.encrypt(params)
34
+ opts = SYSTEM_OPTIONS.merge(sys_options)
35
+ .merge(params: params_value, sign: sign, app_id: Config.app_id)
36
+ query_str = Util.to_query(opts)
37
+ [GATEWAY, query_str].join('?')
38
+ end
39
+
40
+ def self.parse_response(response_str)
41
+ response_hash = JSON.parse(response_str)
42
+ biz_response = response_hash["biz_response"]
43
+ biz_response = Param.decrypt(biz_response) if response_hash["encrypted"]
44
+ JSON.parse(biz_response)
45
+ end
46
+ end
47
+ end
data/lib/zhima/sign.rb ADDED
@@ -0,0 +1,13 @@
1
+ module Zhima
2
+ module Sign
3
+ SIGN_METHOD = 'sha1'.freeze
4
+
5
+ def self.sign(str)
6
+ Config.mech_rsa.sign(SIGN_METHOD, str)
7
+ end
8
+
9
+ # TODO
10
+ def self.verify()
11
+ end
12
+ end
13
+ end
data/lib/zhima/util.rb ADDED
@@ -0,0 +1,22 @@
1
+ module Zhima
2
+ module Util
3
+ def self.symbolize_hash_keys(hash)
4
+ return hash.symbolize_keys! if hash.respond_to?(:symbolize_keys!)
5
+
6
+ new_hash = {}
7
+ hash.each do |key, value|
8
+ new_hash[key.to_sym] = value
9
+ end
10
+ new_hash
11
+ end
12
+
13
+ def self.base64_and_uri_encode(str)
14
+ URI.encode_www_form_component(Base64.strict_encode64(str))
15
+ end
16
+
17
+ # 暂时只做了一级hash的处理
18
+ def self.to_query(hash)
19
+ hash.map{ |pair| pair.join("=") }.join("&")
20
+ end
21
+ end
22
+ end
data/lib/zhima/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Zhima
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.2"
3
3
  end
data/lib/zhima.rb CHANGED
@@ -1,5 +1,18 @@
1
1
  require "zhima/version"
2
+ require "openssl"
3
+ require "base64"
4
+ require "uri"
2
5
 
3
6
  module Zhima
4
- # Your code goes here...
7
+ class << self
8
+ def configure(&block)
9
+ Config.configure(&block)
10
+ end
11
+ end
5
12
  end
13
+
14
+ require_relative "zhima/score"
15
+ require_relative "zhima/param"
16
+ require_relative "zhima/sign"
17
+ require_relative "zhima/util"
18
+ require_relative "zhima/config"
data/zhima.gemspec CHANGED
@@ -30,4 +30,5 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency "bundler", "~> 1.12"
31
31
  spec.add_development_dependency "rake", "~> 10.0"
32
32
  spec.add_development_dependency "rspec", "~> 3.0"
33
+ spec.add_development_dependency "rest-client"
33
34
  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.1.1
4
+ version: 0.2.2
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-19 00:00:00.000000000 Z
11
+ date: 2016-10-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rest-client
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'
55
69
  description: "芝麻信用"
56
70
  email:
57
71
  - zhchsf@gmail.com
@@ -70,6 +84,11 @@ files:
70
84
  - bin/console
71
85
  - bin/setup
72
86
  - lib/zhima.rb
87
+ - lib/zhima/config.rb
88
+ - lib/zhima/param.rb
89
+ - lib/zhima/score.rb
90
+ - lib/zhima/sign.rb
91
+ - lib/zhima/util.rb
73
92
  - lib/zhima/version.rb
74
93
  - zhima.gemspec
75
94
  homepage: https://github.com/zhchsf/zhima