aliquot 2.3.1 → 3.1.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: e752bf7cf311e4fe611e3c868ed08510c54d9e1b05b327d5dfa390b3eeb8cee7
4
- data.tar.gz: 5c7df397cabccc5009c712546838c67329f4e2c835935ba56e62f71b6c368920
3
+ metadata.gz: b3a22d84f7027490499f290ffdeb5f36fd2c65ed9b341885b345e81b494e3a32
4
+ data.tar.gz: e577c181a9f3c4212d77493b45697e75ac9e49b003ed88eaafce05acc1f80ab5
5
5
  SHA512:
6
- metadata.gz: 648c2bb459b2a09b602c13dab8c8221fc726c886e047789c84ebf51d1436a3f5fece61255905744cf817dcf114d9833f3f0281d5c7ed18a6eb1c44a60b770521
7
- data.tar.gz: eb060ab6212f664bf3545a306dee4eaac7f63a6388921faf70cc28393e7bc606731d408edb3224ce737c3bc9ef8be8efa16942df717e157a1f3e0f746ad467f6
6
+ metadata.gz: 8c0b1463cd13dbeaf2ae44625dda2dcb5049a3b1f9080789ceba7d82fa10ca0ac2435c85652117602af91a5947a68b0419fef18e9f76a8e7793bbcca3c8421e7
7
+ data.tar.gz: 42442564d2fc0b143e0e337d611eb525d9c660e811fdcab512571f3d3a5a88d30f739b43f66ca00ab808482c5d290007bef6e6e2fb1212c387bb118fd7efe43a
data/lib/aliquot/error.rb CHANGED
@@ -23,9 +23,6 @@ module Aliquot
23
23
  # Thrown if there was an error validating the input data
24
24
  class ValidationError < Error; end
25
25
 
26
- # Thrown if JSON is invalid.
27
- class FormatError < Error; end
28
-
29
26
  # When shared_secret is invalid
30
27
  class InvalidSharedSecretError < Error; end
31
28
 
@@ -1,6 +1,5 @@
1
1
  require 'json'
2
2
  require 'base64'
3
- require 'hkdf'
4
3
  require 'openssl'
5
4
 
6
5
  require 'aliquot/error'
@@ -66,6 +65,9 @@ module Aliquot
66
65
 
67
66
  begin
68
67
  @message = JSON.parse(decrypt(aes_key, @signed_message[:encryptedMessage]))
68
+ @message['paymentMethodDetails'].merge!(
69
+ 'threedsCryptogram' => @message['paymentMethodDetails']
70
+ .delete('3dsCryptogram')) if @message['paymentMethodDetails']['3dsCryptogram']
69
71
  rescue JSON::JSONError => e
70
72
  raise InputError, "encryptedMessage JSON is invalid, #{e.message}"
71
73
  rescue => e
@@ -191,11 +193,7 @@ module Aliquot
191
193
 
192
194
  key_len = new_cipher.key_len
193
195
 
194
- key_bytes = if OpenSSL.const_defined?(:KDF) && OpenSSL::KDF.respond_to?(:hkdf)
195
- OpenSSL::KDF.hkdf(input_keying_material, hash: new_digest, salt: '', length: 2 * key_len, info: info)
196
- else
197
- HKDF.new(input_keying_material, algorithm: 'SHA256', info: info).next_bytes(2 * key_len)
198
- end
196
+ key_bytes = OpenSSL::KDF.hkdf(input_keying_material, hash: new_digest, salt: '', length: 2 * key_len, info: info)
199
197
 
200
198
  [key_bytes[0..key_len - 1], key_bytes[key_len..2 * key_len]]
201
199
  end
@@ -205,7 +203,11 @@ module Aliquot
205
203
  tag = @signed_message[:tag]
206
204
  mac = OpenSSL::HMAC.digest(new_digest, mac_key, data)
207
205
 
208
- compare(Base64.strict_encode64(mac), tag)
206
+ encoded_mac = Base64.strict_encode64(mac)
207
+
208
+ # fixed_length_secure_compare raises unless the lengths are equal.
209
+ encoded_mac.bytesize == tag.bytesize &&
210
+ OpenSSL.fixed_length_secure_compare(encoded_mac, tag)
209
211
  end
210
212
 
211
213
  def decrypt(key, encrypted)
@@ -221,7 +223,20 @@ module Aliquot
221
223
  validator.validate
222
224
 
223
225
  # Output is hashed with symbolized keys.
224
- validator.output
226
+ message_hash = validator.output
227
+
228
+ payment_method_details_message = message_hash[:paymentMethodDetails]
229
+ message_details_validator =
230
+ if message_hash[:paymentMethod] == 'TOKENIZED_CARD' ||
231
+ message_hash[:paymentMethodDetails]['authMethod'] == 'CRYPTOGRAM_3DS'
232
+ Aliquot::Validator::PaymentMethodDetailsValidator.new(payment_method_details_message, protocol_version, true)
233
+ else
234
+ Aliquot::Validator::PaymentMethodDetailsValidator.new(payment_method_details_message, protocol_version, false)
235
+ end
236
+ message_details_validator.validate
237
+ message_hash[:paymentMethodDetails] = message_details_validator.output
238
+
239
+ message_hash
225
240
  end
226
241
 
227
242
  ##
@@ -243,19 +258,5 @@ module Aliquot
243
258
  def new_digest
244
259
  OpenSSL::Digest::SHA256.new
245
260
  end
246
-
247
- def compare(a, b)
248
- return false unless a.length == b.length
249
-
250
- diffs = 0
251
-
252
- ys = b.unpack('C*')
253
-
254
- a.each_byte do |x|
255
- diffs |= x ^ ys.shift
256
- end
257
-
258
- diffs.zero?
259
- end
260
261
  end
261
262
  end
@@ -18,11 +18,11 @@ module Aliquot
18
18
  integer_string?: 'must be string encoded integer',
19
19
  month?: 'must be a month (1..12)',
20
20
  year?: 'must be a year (2000..3000)',
21
- base64_asn1?: 'must be base64-encoded ANS.1 value',
21
+ base64_asn1?: 'must be base64-encoded ASN.1 value',
22
22
  json?: 'must be valid JSON',
23
23
 
24
- is_authMethodCryptogram3DS: 'authMethod CRYPTOGRAM_3DS requires eciIndicator',
25
- is_authMethodCard: 'eciIndicator/cryptogram must be omitted when PAN_ONLY',
24
+ is_authMethodCryptogram3DS: 'authMethod CRYPTOGRAM_3DS or 3DS requires eciIndicator',
25
+ is_authMethodCard: 'eciIndicator/cryptogram/3dsCryptogram must be omitted when PAN_ONLY',
26
26
  }.freeze
27
27
 
28
28
  def self.base64_check(value)
@@ -40,13 +40,13 @@ module Aliquot
40
40
  end
41
41
  end
42
42
 
43
- def self.ans1_check(value)
43
+ def self.asn1_check(value)
44
44
  OpenSSL::ASN1.decode(Base64.strict_decode64(value)) rescue false
45
45
  end
46
46
 
47
47
  Dry::Validation.register_macro(:base64_asn1?) do
48
48
  if key?
49
- unless Aliquot::Validator.ans1_check(value)
49
+ unless Aliquot::Validator.asn1_check(value)
50
50
  key.failure(CUSTOM_ERRORS[:base64_asn1?])
51
51
  end
52
52
  end
@@ -118,8 +118,6 @@ module Aliquot
118
118
  rule(:keyValue).validate(:ec_public_key?)
119
119
  end
120
120
 
121
- SignedKeySchema = SignedKeyContract.new
122
-
123
121
  # Schema used for the 'intermediateSigningKey' hash included in ECv2.
124
122
  class IntermediateSigningKeyContract < Dry::Validation::Contract
125
123
  json do
@@ -129,12 +127,10 @@ module Aliquot
129
127
  rule(:signedKey).validate(:json?)
130
128
  rule(:signatures).each do
131
129
  key.failure('must be Base64') unless Aliquot::Validator.base64_check(value) &&
132
- Aliquot::Validator.ans1_check(value)
130
+ Aliquot::Validator.asn1_check(value)
133
131
  end
134
132
  end
135
133
 
136
- IntermediateSigningKeySchema = IntermediateSigningKeyContract.new
137
-
138
134
  # DRY-Validation schema for Google Pay token
139
135
  class TokenContract < Dry::Validation::Contract
140
136
  json do
@@ -147,13 +143,11 @@ module Aliquot
147
143
  rule(:signedMessage).validate(:json?)
148
144
 
149
145
  rule(:intermediateSigningKey) do
150
- key.failure('is missing') if 'ECv2'.eql?(values[:protocolVersion]) &&
151
- values[:intermediateSigningKey].nil?
146
+ key.failure('is missing') if values[:protocolVersion] == 'ECv2' &&
147
+ values[:intermediateSigningKey].nil?
152
148
  end
153
149
  end
154
150
 
155
- TokenSchema = TokenContract.new
156
-
157
151
  # DRY-Validation schema for signedMessage component Google Pay token
158
152
  class SignedMessageContract < Dry::Validation::Contract
159
153
  json do
@@ -166,41 +160,55 @@ module Aliquot
166
160
  rule(:tag).validate(:base64?)
167
161
  end
168
162
 
169
- SignedMessageSchema = SignedMessageContract.new
170
-
171
- # DRY-Validation schema for paymentMethodDetails component Google Pay token
172
- class PaymentMethodDetailsContract < Dry::Validation::Contract
163
+ class CommonPaymentMethodDetailsContract < Dry::Validation::Contract
173
164
  json do
174
- required(:pan).filled(:str?)
175
165
  required(:expirationMonth).filled(:int?)
176
166
  required(:expirationYear).filled(:int?)
177
- required(:authMethod).filled(:str?, included_in?: %w[PAN_ONLY CRYPTOGRAM_3DS])
178
-
179
- optional(:cryptogram).filled(:str?)
180
- optional(:eciIndicator).filled(:str?)
181
167
  end
182
- rule(:pan).validate(:integer_string?, :pan?)
183
168
  rule(:expirationMonth).validate(:month?)
184
169
  rule(:expirationYear).validate(:year?)
185
- rule(:eciIndicator).validate(:eci?)
170
+ end
186
171
 
187
- rule(:cryptogram) do
188
- key.failure('is missing') if 'CRYPTOGRAM_3DS'.eql?(values[:authMethod]) &&
189
- values[:cryptogram].nil?
172
+ class ECv1_PaymentMethodDetailsContract < CommonPaymentMethodDetailsContract
173
+ json(CommonPaymentMethodDetailsContract.schema) do
174
+ required(:pan).filled(:str?)
190
175
  end
191
176
 
192
- rule(:cryptogram) do
193
- key.failure('cannot be defined') if 'PAN_ONLY'.eql?(values[:authMethod]) &&
194
- !values[:cryptogram].nil?
177
+ rule(:pan).validate(:integer_string?, :pan?)
178
+ end
179
+
180
+ class ECv1_TokenizedPaymentMethodDetailsContract < CommonPaymentMethodDetailsContract
181
+ json(CommonPaymentMethodDetailsContract.schema) do
182
+ required(:dpan).filled(:str?)
183
+ required(:threedsCryptogram).filled(:str?)
184
+ required(:eciIndicator).filled(:str?)
185
+ required(:authMethod).filled(:str?, included_in?: %w[3DS])
195
186
  end
196
187
 
197
- rule(:eciIndicator) do
198
- key.failure('cannot be defined') if 'PAN_ONLY'.eql?(values[:authMethod]) &&
199
- !values[:eciIndicator].nil?
188
+ rule(:dpan).validate(:integer_string?, :pan?)
189
+ rule(:eciIndicator).validate(:eci?)
190
+ end
191
+
192
+ class ECv2_PaymentMethodDetailsContract < CommonPaymentMethodDetailsContract
193
+ json(CommonPaymentMethodDetailsContract.schema) do
194
+ required(:pan).filled(:str?)
195
+ required(:authMethod).filled(:str?, included_in?: %w[PAN_ONLY])
200
196
  end
197
+
198
+ rule(:pan).validate(:integer_string?, :pan?)
201
199
  end
202
200
 
203
- PaymentMethodDetailsSchema = PaymentMethodDetailsContract.new
201
+ class ECv2_TokenizedPaymentMethodDetailsContract < CommonPaymentMethodDetailsContract
202
+ json(CommonPaymentMethodDetailsContract.schema) do
203
+ required(:pan).filled(:str?)
204
+ required(:cryptogram).filled(:str?)
205
+ required(:eciIndicator).filled(:str?)
206
+ required(:authMethod).filled(:str?, included_in?: %w[CRYPTOGRAM_3DS])
207
+ end
208
+
209
+ rule(:pan).validate(:integer_string?, :pan?)
210
+ rule(:eciIndicator).validate(:eci?)
211
+ end
204
212
 
205
213
  # DRY-Validation schema for encryptedMessage component Google Pay token
206
214
  class EncryptedMessageContract < Dry::Validation::Contract
@@ -208,17 +216,40 @@ module Aliquot
208
216
  required(:messageExpiration).filled(:str?)
209
217
  required(:messageId).filled(:str?)
210
218
  required(:paymentMethod).filled(:str?)
211
- required(:paymentMethodDetails).filled(:hash).schema(PaymentMethodDetailsContract.schema)
219
+ required(:paymentMethodDetails).filled(:hash)
212
220
  optional(:gatewayMerchantId).filled(:str?)
213
221
  end
214
222
  rule(:messageExpiration).validate(:integer_string?)
223
+
224
+ rule(:paymentMethodDetails).validate do
225
+ contract =
226
+ if values[:protocolVersion] == 'ECv1'
227
+ if values[:paymentMethod] == 'TOKENIZED_CARD'
228
+ ECv1_TokenizedPaymentMethodDetailsContract.new
229
+ else
230
+ ECv1_PaymentMethodDetailsContract.new
231
+ end
232
+ else
233
+ if values[:authMethod] == 'CRYPTOGRAM_3DS'
234
+ ECv2_TokenizedPaymentMethodDetailsContract.new
235
+ else
236
+ ECv2_PaymentMethodDetailsContract.new
237
+ end
238
+ end
239
+ contract.call(values[:paymentMethodDetails])
240
+ end
241
+
215
242
  rule(:paymentMethod) do
216
- key.failure('must be equal to CARD') unless 'CARD'.eql?(value)
243
+ if values[:paymentMethodDetails].is_a?(Hash)
244
+ if '3DS'.eql?(values[:paymentMethodDetails]['authMethod']) # Tokenized ECv1
245
+ key.failure('must be equal to TOKENIZED_CARD') unless value == 'TOKENIZED_CARD'
246
+ else
247
+ key.failure('must be equal to CARD') unless value == 'CARD'
248
+ end
249
+ end
217
250
  end
218
251
  end
219
252
 
220
- EncryptedMessageSchema = EncryptedMessageContract.new
221
-
222
253
  module InstanceMethods
223
254
  attr_reader :output
224
255
 
@@ -259,7 +290,7 @@ module Aliquot
259
290
 
260
291
  def initialize(input)
261
292
  @input = input
262
- @schema = TokenSchema
293
+ @schema = TokenContract.new
263
294
  end
264
295
  end
265
296
 
@@ -271,7 +302,7 @@ module Aliquot
271
302
 
272
303
  def initialize(input)
273
304
  @input = input
274
- @schema = SignedMessageSchema
305
+ @schema = SignedMessageContract.new
275
306
  end
276
307
  end
277
308
 
@@ -283,7 +314,32 @@ module Aliquot
283
314
 
284
315
  def initialize(input)
285
316
  @input = input
286
- @schema = EncryptedMessageSchema
317
+ @schema = EncryptedMessageContract.new
318
+ end
319
+ end
320
+
321
+ # Class for validating the encryptedMessage component of a Google Pay token
322
+ class PaymentMethodDetailsValidator
323
+ include InstanceMethods
324
+
325
+ class Error < ::Aliquot::Error; end
326
+
327
+ def initialize(input, version, tokenized)
328
+ @input = input
329
+ @schema =
330
+ if version == 'ECv1'
331
+ if tokenized
332
+ Aliquot::Validator::ECv1_TokenizedPaymentMethodDetailsContract.new
333
+ else
334
+ Aliquot::Validator::ECv1_PaymentMethodDetailsContract.new
335
+ end
336
+ else
337
+ if tokenized
338
+ Aliquot::Validator::ECv2_TokenizedPaymentMethodDetailsContract.new
339
+ else
340
+ Aliquot::Validator::ECv2_PaymentMethodDetailsContract.new
341
+ end
342
+ end
287
343
  end
288
344
  end
289
345
 
@@ -294,7 +350,7 @@ module Aliquot
294
350
 
295
351
  def initialize(input)
296
352
  @input = input
297
- @schema = SignedKeySchema
353
+ @schema = SignedKeyContract.new
298
354
  end
299
355
  end
300
356
  end
metadata CHANGED
@@ -1,100 +1,98 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aliquot
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.1
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Clearhaus
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2023-09-29 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
- name: dry-validation
13
+ name: base64
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
- - - "~>"
16
+ - - ">="
18
17
  - !ruby/object:Gem::Version
19
- version: '1.8'
18
+ version: '0.2'
20
19
  type: :runtime
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
- - - "~>"
23
+ - - ">="
25
24
  - !ruby/object:Gem::Version
26
- version: '1.8'
25
+ version: '0.2'
27
26
  - !ruby/object:Gem::Dependency
28
- name: excon
27
+ name: dry-validation
29
28
  requirement: !ruby/object:Gem::Requirement
30
29
  requirements:
31
- - - "~>"
30
+ - - ">="
32
31
  - !ruby/object:Gem::Version
33
- version: 0.71.0
32
+ version: '1.9'
34
33
  type: :runtime
35
34
  prerelease: false
36
35
  version_requirements: !ruby/object:Gem::Requirement
37
36
  requirements:
38
- - - "~>"
37
+ - - ">="
39
38
  - !ruby/object:Gem::Version
40
- version: 0.71.0
39
+ version: '1.9'
41
40
  - !ruby/object:Gem::Dependency
42
- name: hkdf
41
+ name: openssl
43
42
  requirement: !ruby/object:Gem::Requirement
44
43
  requirements:
45
- - - "~>"
44
+ - - ">="
46
45
  - !ruby/object:Gem::Version
47
- version: '0.3'
46
+ version: '2.2'
48
47
  type: :runtime
49
48
  prerelease: false
50
49
  version_requirements: !ruby/object:Gem::Requirement
51
50
  requirements:
52
- - - "~>"
51
+ - - ">="
53
52
  - !ruby/object:Gem::Version
54
- version: '0.3'
53
+ version: '2.2'
55
54
  - !ruby/object:Gem::Dependency
56
55
  name: aliquot-pay
57
56
  requirement: !ruby/object:Gem::Requirement
58
57
  requirements:
59
- - - "~>"
58
+ - - ">="
60
59
  - !ruby/object:Gem::Version
61
- version: '3.0'
60
+ version: '4.0'
62
61
  type: :development
63
62
  prerelease: false
64
63
  version_requirements: !ruby/object:Gem::Requirement
65
64
  requirements:
66
- - - "~>"
65
+ - - ">="
67
66
  - !ruby/object:Gem::Version
68
- version: '3.0'
67
+ version: '4.0'
69
68
  - !ruby/object:Gem::Dependency
70
69
  name: rspec
71
70
  requirement: !ruby/object:Gem::Requirement
72
71
  requirements:
73
- - - "~>"
72
+ - - ">="
74
73
  - !ruby/object:Gem::Version
75
74
  version: '3'
76
75
  type: :development
77
76
  prerelease: false
78
77
  version_requirements: !ruby/object:Gem::Requirement
79
78
  requirements:
80
- - - "~>"
79
+ - - ">="
81
80
  - !ruby/object:Gem::Version
82
81
  version: '3'
83
82
  - !ruby/object:Gem::Dependency
84
83
  name: pry
85
84
  requirement: !ruby/object:Gem::Requirement
86
85
  requirements:
87
- - - "~>"
86
+ - - ">="
88
87
  - !ruby/object:Gem::Version
89
- version: 0.14.1
88
+ version: '0.15'
90
89
  type: :development
91
90
  prerelease: false
92
91
  version_requirements: !ruby/object:Gem::Requirement
93
92
  requirements:
94
- - - "~>"
93
+ - - ">="
95
94
  - !ruby/object:Gem::Version
96
- version: 0.14.1
97
- description:
95
+ version: '0.15'
98
96
  email: hello@clearhaus.com
99
97
  executables: []
100
98
  extensions: []
@@ -108,7 +106,6 @@ homepage: https://github.com/clearhaus/aliquot
108
106
  licenses:
109
107
  - MIT
110
108
  metadata: {}
111
- post_install_message:
112
109
  rdoc_options: []
113
110
  require_paths:
114
111
  - lib
@@ -123,8 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
120
  - !ruby/object:Gem::Version
124
121
  version: '0'
125
122
  requirements: []
126
- rubygems_version: 3.3.5
127
- signing_key:
123
+ rubygems_version: 3.6.9
128
124
  specification_version: 4
129
125
  summary: Validates Google Pay tokens
130
126
  test_files: []