tonglian-ruby-sdk 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 +4 -4
- data/lib/tonglian-ruby-sdk.rb +35 -10
- 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: 7f4d54ded35c1a345b7c1523f59e561e70d08d6bad618e2c83a14d33ab532065
|
4
|
+
data.tar.gz: d37d2482450c7a156674fe5ec2b71e5b69eb415ee949aaea55392068113357c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d3afc6f5ad630dae3dfca5346a196493c7ef3ed217a862dc2946e766515434e8bef43bc22d4b21339d57f9df122f2e939f50af5ca4167e2ee0fbabe5f584557
|
7
|
+
data.tar.gz: f82d4d303edf28781dfa7f86bc0df7ead862f6dcb14dda96bfa00e775128f8088346ff8be1aa14a364e0c84dbded75be3eaf6ac60ea4acd91c9bd9673db803c7
|
data/lib/tonglian-ruby-sdk.rb
CHANGED
@@ -33,7 +33,6 @@ module TonglianRubySdk
|
|
33
33
|
data['sign'] = @signer.sign(data)
|
34
34
|
|
35
35
|
url = URI(@api_end_point)
|
36
|
-
|
37
36
|
http = Net::HTTP.new(url.host, url.port)
|
38
37
|
http.use_ssl = true if @api_end_point.downcase.starts_with?('https') # Enable SSL for HTTPS
|
39
38
|
|
@@ -42,9 +41,9 @@ module TonglianRubySdk
|
|
42
41
|
request.body = URI.encode_www_form(data)
|
43
42
|
response = http.request(request)
|
44
43
|
|
45
|
-
|
46
|
-
|
47
|
-
|
44
|
+
object = JSON.parse(response.body)
|
45
|
+
@signer.verify?(object) || raise('Invalid response signature!')
|
46
|
+
{ 'code' => response.code, 'data' => object }
|
48
47
|
end
|
49
48
|
|
50
49
|
private
|
@@ -65,18 +64,20 @@ module TonglianRubySdk
|
|
65
64
|
end
|
66
65
|
|
67
66
|
def sign(params)
|
68
|
-
|
67
|
+
message = make_sign_message(params)
|
69
68
|
rsa = OpenSSL::PKey::RSA.new private_key
|
70
|
-
Base64.strict_encode64(rsa.sign(
|
69
|
+
Base64.strict_encode64(rsa.sign(OpenSSL::Digest.new('SHA256'), message))
|
71
70
|
end
|
72
71
|
|
73
72
|
def verify?(params, signature = nil)
|
74
73
|
signature = params['sign'] if signature.nil? || signature.to_s.empty?
|
75
|
-
|
74
|
+
params.delete('sign')
|
75
|
+
message = make_verify_message(params)
|
76
|
+
|
76
77
|
public_file = File.open(@public_path)
|
77
78
|
public_key = OpenSSL::X509::Certificate.new(public_file).public_key.export
|
78
79
|
rsa = OpenSSL::PKey::RSA.new(public_key)
|
79
|
-
rsa.verify('
|
80
|
+
rsa.verify(OpenSSL::Digest.new('SHA256'), Base64.decode64(signature), message)
|
80
81
|
end
|
81
82
|
|
82
83
|
private
|
@@ -100,11 +101,35 @@ module TonglianRubySdk
|
|
100
101
|
params.keys.sort.map do |k|
|
101
102
|
next if %w[sign signType].include? k
|
102
103
|
next if params[k].nil? || params[k].to_s.empty?
|
103
|
-
|
104
|
+
|
104
105
|
sorted_params.push("#{k}=#{params[k]}")
|
105
106
|
end
|
106
107
|
|
107
|
-
|
108
|
+
flattened_params = sorted_params.join('&')
|
109
|
+
md5_digest = Digest::MD5.digest(flattened_params)
|
110
|
+
Base64.strict_encode64(md5_digest)
|
111
|
+
end
|
112
|
+
|
113
|
+
def make_verify_message(params)
|
114
|
+
params = sort_object(params)
|
115
|
+
flattened_params = params.to_json
|
116
|
+
Base64.strict_encode64(Digest::MD5.digest(flattened_params))
|
117
|
+
end
|
118
|
+
|
119
|
+
# In Ruby 3, a hash preserves the order the keys are inserted
|
120
|
+
# So we can make a 'sorted' hash and generate a sorted json later
|
121
|
+
def sort_object(obj)
|
122
|
+
result = nil
|
123
|
+
if obj.is_a? Hash
|
124
|
+
result = {}
|
125
|
+
obj.keys.sort.each { |k| result[k] = sort_object(obj[k]) }
|
126
|
+
elsif obj.is_a? Array
|
127
|
+
result = []
|
128
|
+
obj.sort.each { |k| result.push(k) }
|
129
|
+
else
|
130
|
+
result = obj
|
131
|
+
end
|
132
|
+
result
|
108
133
|
end
|
109
134
|
end
|
110
135
|
end
|