tonglian-ruby-sdk 0.1.0 → 0.2.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 +4 -4
- data/lib/tonglian-ruby-sdk.rb +71 -8
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d110ad2c605228b2bcad375d09bdb1054e03f252880adff36ac6f573f3ed01df
|
4
|
+
data.tar.gz: c116187cd8e449a0965694d7154acf6c0f22ea232f133f3590101255f3c96c26
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 017d35a06757cc2375cea897f41a3a8da499e77ae770e71b605e505b2b290d91558686305bd0119d930927d8614bd06e999e33b1c0c120bef9aef599281a37b5
|
7
|
+
data.tar.gz: 17eb8000f9961dfb76dfe89199831a3364b4befed84973e2250c0145e2e9df15015dece33c01f6937ac79bf128849f0a050df90d4c84da29925721c6d9133e38
|
data/lib/tonglian-ruby-sdk.rb
CHANGED
@@ -5,8 +5,58 @@ require 'openssl'
|
|
5
5
|
require 'cgi'
|
6
6
|
require 'digest'
|
7
7
|
require 'base64'
|
8
|
+
require 'net/http'
|
9
|
+
require 'active_support/all'
|
8
10
|
|
9
11
|
module TonglianRubySdk
|
12
|
+
# Client class to handle request and responses to and from Tonglian gateway
|
13
|
+
class Client
|
14
|
+
REQUEST_STUB = {
|
15
|
+
'charset' => 'utf-8',
|
16
|
+
'format' => 'JSON',
|
17
|
+
'signType' => 'SHA256WithRSA',
|
18
|
+
'version' => '1.0'
|
19
|
+
}.freeze
|
20
|
+
|
21
|
+
def initialize(api_end_point, app_id, private_path, private_passwd, public_path)
|
22
|
+
@api_end_point = api_end_point
|
23
|
+
@app_id = app_id
|
24
|
+
@signer = Signer.new(private_path, private_passwd, public_path)
|
25
|
+
end
|
26
|
+
|
27
|
+
def request(method, params)
|
28
|
+
data = REQUEST_STUB.dup
|
29
|
+
data['appId'] = @app_id
|
30
|
+
data['method'] = method
|
31
|
+
data['timestamp'] = timestamp
|
32
|
+
data['bizContent'] = params.to_json
|
33
|
+
data['sign'] = @signer.sign(data)
|
34
|
+
|
35
|
+
url = URI(@api_end_point)
|
36
|
+
|
37
|
+
http = Net::HTTP.new(url.host, url.port)
|
38
|
+
http.use_ssl = true if @api_end_point.downcase.starts_with?('https') # Enable SSL for HTTPS
|
39
|
+
|
40
|
+
request = Net::HTTP::Post.new(url.request_uri)
|
41
|
+
request['Content-Type'] = 'application/x-www-form-urlencoded'
|
42
|
+
request.body = URI.encode_www_form(data)
|
43
|
+
response = http.request(request)
|
44
|
+
|
45
|
+
# Handle response
|
46
|
+
puts response.code
|
47
|
+
puts response.body
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def timestamp
|
53
|
+
timezone = ActiveSupport::TimeZone.new('Asia/Shanghai')
|
54
|
+
current_time = Time.now.in_time_zone(timezone)
|
55
|
+
current_time.strftime('%Y-%m-%d %H:%M:%S')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# To sign client request message and verify tonglian's response message
|
10
60
|
class Signer
|
11
61
|
def initialize(private_path, private_passwd, public_path)
|
12
62
|
@private_path = private_path
|
@@ -16,32 +66,45 @@ module TonglianRubySdk
|
|
16
66
|
|
17
67
|
def sign(params)
|
18
68
|
str = make_sign_message(params)
|
19
|
-
private_file = File.open(@private_path)
|
20
|
-
private_key = OpenSSL::PKCS12.new(private_file, @private_passwd).key.export
|
21
69
|
rsa = OpenSSL::PKey::RSA.new private_key
|
22
|
-
rsa.sign('sha1', str.force_encoding('UTF-8'))
|
70
|
+
Base64.strict_encode64(rsa.sign('sha1', str.force_encoding('UTF-8')))
|
23
71
|
end
|
24
72
|
|
25
|
-
def verify?(params,
|
73
|
+
def verify?(params, signature = nil)
|
74
|
+
signature = params['sign'] if signature.nil? || signature.to_s.empty?
|
26
75
|
str = make_sign_message(params)
|
27
76
|
public_file = File.open(@public_path)
|
28
77
|
public_key = OpenSSL::X509::Certificate.new(public_file).public_key.export
|
29
78
|
rsa = OpenSSL::PKey::RSA.new(public_key)
|
30
|
-
rsa.verify('sha1',
|
79
|
+
rsa.verify('sha1', Base64.decode64(signature), str)
|
31
80
|
end
|
32
81
|
|
33
82
|
private
|
34
83
|
|
84
|
+
def private_key
|
85
|
+
return @private_key if @private_key
|
86
|
+
|
87
|
+
private_file = File.open(@private_path)
|
88
|
+
@private_key = OpenSSL::PKCS12.new(private_file, @private_passwd).key.export
|
89
|
+
end
|
90
|
+
|
91
|
+
def public_key
|
92
|
+
return @public_key if @public_key
|
93
|
+
|
94
|
+
public_file = File.open(@public_path)
|
95
|
+
@public_key = OpenSSL::X509::Certificate.new(public_file).public_key.export
|
96
|
+
end
|
97
|
+
|
35
98
|
def make_sign_message(params)
|
36
99
|
sorted_params = []
|
37
100
|
params.keys.sort.map do |k|
|
38
101
|
next if %w[sign signType].include? k
|
39
102
|
next if params[k].nil? || params[k].to_s.empty?
|
40
|
-
|
41
|
-
sorted_params.push("#{k}=#{
|
103
|
+
#sorted_params.push("#{k}=#{CGI.escape(params[k])}")
|
104
|
+
sorted_params.push("#{k}=#{params[k]}")
|
42
105
|
end
|
43
106
|
|
44
|
-
Base64.
|
107
|
+
Base64.strict_encode64(Digest::MD5.hexdigest(sorted_params.join('&')))
|
45
108
|
end
|
46
109
|
end
|
47
110
|
end
|