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 +4 -4
- data/lib/zhima/config.rb +19 -0
- data/lib/zhima/param.rb +31 -0
- data/lib/zhima/score.rb +47 -0
- data/lib/zhima/sign.rb +13 -0
- data/lib/zhima/util.rb +22 -0
- data/lib/zhima/version.rb +1 -1
- data/lib/zhima.rb +14 -1
- data/zhima.gemspec +1 -0
- metadata +21 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1f2a5dcde395a6dec95e6a934d13bf1e48dd6bd
|
4
|
+
data.tar.gz: f8242edbe588875639302fce434e14e6178b8bf3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 654e9ef37b9c96f11fc06812c4e80a859323830fc97937aa6c95c90bb7d5ec39195a018bbc5dff9a33aa91812a8f6e503101b30a80a205f3582a6f6a5230325f
|
7
|
+
data.tar.gz: 41d8a125c9f1baa284a7178d34783a50e2c4cd93101e2bb9523bfcf5ee13c7af212c09c35a4ed3245fc962392a859641db320736d54f01bcdc0ae8a2e5d60a10
|
data/lib/zhima/config.rb
ADDED
@@ -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
|
data/lib/zhima/param.rb
ADDED
@@ -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
|
data/lib/zhima/score.rb
ADDED
@@ -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
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
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
|
-
|
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
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.
|
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-
|
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
|