jieshun-parking 0.3.1 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dcff311fa412b01113a075ad7622765777928ad3aed9b23f04dc5e4c405b3f19
4
- data.tar.gz: 278c470b20aa79112ba66894adaaeb4e3aac5b15ccf187ef165375d7ef38d95c
3
+ metadata.gz: df11031bd27943deeac558a8e200255fad232335c3b8bf143b987aa70560964c
4
+ data.tar.gz: 8f11af9940434a8fd38cce628f6d01ab0f87fd18cecd3f546e328ecd28166803
5
5
  SHA512:
6
- metadata.gz: 766149c652dfc911a6684baf6072f06a8e4c9d4c9b85b31c704efb02cd5f13b0df08bddc91db51a06a24780958fc27e4d2ccfb21d70b346b8327528f9e2b6746
7
- data.tar.gz: 1f7a0b2961316ea2c081f2b1f9a8514e62791af84c0c3ecb0bc56e3454ddd32c38cf43aed86ddac314005d3f18b36ef833c3c2f8c3a9f1002586b97376d18dbb
6
+ metadata.gz: a7df802493278b25dc262c992e89349842ceea36b90c9d204e9e71db922f532e5cf2ee6df7d1544668c92efa0f6f16be25e94c3ccfda1d788a78cde289f028ec
7
+ data.tar.gz: 467e451adbbcccd4cb52c7e46c342ef192713877bc0141d5c9725045eaafbd05bfd8ee33fc0d64f0669da2c6493a161cfccc31b1c5b04f12f93d48e050f1e7c7
@@ -0,0 +1,53 @@
1
+ module Jieshun
2
+ module Parking
3
+ class DataReceiver
4
+
5
+ def initialize(secret)
6
+ @secret = secret
7
+ end
8
+
9
+ def receive(params, options)
10
+ error_handler = options[:error_handler]
11
+ error_occurred = false
12
+ internal_error_handler = lambda do |error|
13
+ error_occurred = true
14
+ if error.is_a?(Jieshun::Parking::InvalidSignatureError)
15
+ error_handler.call({ resultCode: 205, message: '验签失败' })
16
+ else
17
+ #TODO process other errors like ip unbind
18
+ error_handler.call({ resultCode: 1, message: '处理失败' })
19
+ end
20
+ end
21
+ verify_signature(params, { error_handler: internal_error_handler })
22
+ return nil if error_occurred
23
+ JSON.parse(params['dataItems'])
24
+ end
25
+
26
+ def verify_signature(params, options)
27
+ signature_param = params['sn']
28
+ signature = generate_signature(params)
29
+ unless signature_param == signature
30
+ error_handler = options[:error_handler]
31
+ if error_handler
32
+ error_handler.call(Jieshun::Parking::InvalidSignatureError.new)
33
+ else
34
+ raise Jieshun::Parking::InvalidSignatureError.new
35
+ end
36
+ end
37
+ end
38
+
39
+ require 'digest'
40
+
41
+ def generate_signature(params)
42
+ # sn:签名(sign),使用md5算法对密钥和ts以及dataItem进行签名,签名后的值为大写,规则如下:
43
+ # sn=md5(secret+ts+dataItems)
44
+ timestamp = params['ts']
45
+ data_items = params['dataItems']
46
+ payload = "#{@secret}#{timestamp}#{data_items}"
47
+ md5_string = Digest::MD5.hexdigest(payload)
48
+ md5_string.upcase
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,6 @@
1
+ module Jieshun
2
+ module Parking
3
+ class InvalidSignatureError < RuntimeError
4
+ end
5
+ end
6
+ end
@@ -21,6 +21,7 @@ class Retry
21
21
  retry
22
22
  else
23
23
  Jieshun::Parking.logger.error("Retried #{tried_times} times, aborted.")
24
+ raise RuntimeError.new("Failed to execute operation with Retrier.")
24
25
  end
25
26
  end
26
27
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jieshun
4
4
  module Parking
5
- VERSION = "0.3.1"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
@@ -8,6 +8,8 @@ require_relative "parking/http/http_client"
8
8
  require_relative "parking/http/standard_http_client"
9
9
  require_relative "parking/redis/redis_client"
10
10
  require_relative "parking/helpers/retry"
11
+ require_relative "parking/data_receiver"
12
+ require_relative "parking/errors/invalid_signature_error"
11
13
  require 'logger'
12
14
 
13
15
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jieshun-parking
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - LCola
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-03 00:00:00.000000000 Z
11
+ date: 2023-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -50,6 +50,8 @@ files:
50
50
  - lib/jieshun/parking.rb
51
51
  - lib/jieshun/parking/client.rb
52
52
  - lib/jieshun/parking/configuration.rb
53
+ - lib/jieshun/parking/data_receiver.rb
54
+ - lib/jieshun/parking/errors/invalid_signature_error.rb
53
55
  - lib/jieshun/parking/helpers/retry.rb
54
56
  - lib/jieshun/parking/http/http_client.rb
55
57
  - lib/jieshun/parking/http/standard_http_client.rb