ap2_mandate_verifier 0.5.0 → 0.7.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/README.md +32 -11
- data/docs/BUYER_DIRECT_CHECKOUT.md +180 -0
- data/docs/MERCHANT_SIGNING.md +28 -4
- data/docs/MULTI_ITEM_CHECKOUT.md +119 -0
- data/docs/SUPPORT_CONTRACT.md +47 -7
- data/lib/ap2_mandate_verifier/buyer.rb +153 -0
- data/lib/ap2_mandate_verifier/checkout_jwt.rb +38 -5
- data/lib/ap2_mandate_verifier/checkout_receipt.rb +13 -6
- data/lib/ap2_mandate_verifier/host_binding.rb +2 -1
- data/lib/ap2_mandate_verifier/input_builder.rb +16 -11
- data/lib/ap2_mandate_verifier/merchant_signature.rb +4 -3
- data/lib/ap2_mandate_verifier/purchase_binding.rb +7 -2
- data/lib/ap2_mandate_verifier/root_disclosures.rb +33 -10
- data/lib/ap2_mandate_verifier/scoped_constraints.rb +143 -2
- data/lib/ap2_mandate_verifier/trust_context.rb +21 -4
- data/lib/ap2_mandate_verifier.rb +3 -2
- metadata +4 -1
|
@@ -6,6 +6,10 @@ module AP2MandateVerifier
|
|
|
6
6
|
module ScopedConstraints
|
|
7
7
|
MAX_AMOUNT = 9_007_199_254_740_991
|
|
8
8
|
MAX_QUANTITY = 1_000_000
|
|
9
|
+
MULTI_PROFILE_REF = "ap2.checkout.single_merchant_multi_item"
|
|
10
|
+
MAX_LINES = 8
|
|
11
|
+
MAX_REQUIREMENTS = 8
|
|
12
|
+
MAX_CANDIDATES = 8
|
|
9
13
|
CURRENCIES = %w[USD EUR JPY].freeze
|
|
10
14
|
CORE_INSTANCE_OF = Object.instance_method(:instance_of?)
|
|
11
15
|
class Rejected < RootSignature::Rejected; end
|
|
@@ -16,7 +20,7 @@ module AP2MandateVerifier
|
|
|
16
20
|
end
|
|
17
21
|
|
|
18
22
|
# Internal claims handoff after the complete existing limited profile.
|
|
19
|
-
def self.verified_constraints!(expected_currency:, expected_total_minor:, **binding_inputs)
|
|
23
|
+
def self.verified_constraints!(expected_currency:, expected_total_minor:, profile_ref: TrustContext::PROFILE_REF, **binding_inputs)
|
|
20
24
|
begin
|
|
21
25
|
currency = InputValidator.snapshot!(expected_currency, max_bytes: 3)
|
|
22
26
|
rescue InputValidator::Invalid
|
|
@@ -25,6 +29,12 @@ module AP2MandateVerifier
|
|
|
25
29
|
raise RootSignature::Unavailable, :constraint_context_unavailable unless
|
|
26
30
|
CURRENCIES.include?(currency) && CORE_INSTANCE_OF.bind_call(expected_total_minor, Integer) &&
|
|
27
31
|
expected_total_minor.between?(0, MAX_AMOUNT)
|
|
32
|
+
if profile_ref == MULTI_PROFILE_REF
|
|
33
|
+
details = PurchaseBinding.verified_details!(**binding_inputs, profile_ref: profile_ref)
|
|
34
|
+
multi_constraints!(details, currency, expected_total_minor)
|
|
35
|
+
return details
|
|
36
|
+
end
|
|
37
|
+
raise Rejected, :unsupported_scoped_profile unless profile_ref == TrustContext::PROFILE_REF
|
|
28
38
|
details = PurchaseBinding.verified_details!(**binding_inputs)
|
|
29
39
|
open, checkout = details.values_at(:open, :checkout)
|
|
30
40
|
constraints = open["constraints"]
|
|
@@ -76,6 +86,136 @@ module AP2MandateVerifier
|
|
|
76
86
|
details
|
|
77
87
|
end
|
|
78
88
|
|
|
89
|
+
# New profile only. IDs identify products; titles are descriptive text.
|
|
90
|
+
# Original arrays remain intact; this graph is solely a quantity proof.
|
|
91
|
+
def self.multi_constraints!(details, currency, expected_total)
|
|
92
|
+
open, checkout = details.values_at(:open, :checkout)
|
|
93
|
+
constraints = open["constraints"]
|
|
94
|
+
raise Rejected, :unsupported_constraint unless constraints.instance_of?(Array) && constraints.size == 2
|
|
95
|
+
required, allowed = constraints
|
|
96
|
+
exact!(required, %w[items type])
|
|
97
|
+
exact!(allowed, %w[allowed type])
|
|
98
|
+
raise Rejected, :unsupported_constraint unless required["type"] == "checkout.line_items" && allowed["type"] == "checkout.allowed_merchants"
|
|
99
|
+
requirements = bounded_array!(required["items"], MAX_REQUIREMENTS)
|
|
100
|
+
unique_ids!(requirements)
|
|
101
|
+
requirements.each do |requirement|
|
|
102
|
+
exact!(requirement, %w[acceptable_items id quantity])
|
|
103
|
+
quantity!(requirement["quantity"])
|
|
104
|
+
candidates = bounded_array!(requirement["acceptable_items"], MAX_CANDIDATES)
|
|
105
|
+
unique_ids!(candidates)
|
|
106
|
+
candidates.each do |candidate|
|
|
107
|
+
exact!(candidate, %w[id title])
|
|
108
|
+
text!(candidate["title"], 512)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
merchant = one!(allowed["allowed"])
|
|
112
|
+
exact!(merchant, %w[id name])
|
|
113
|
+
text!(merchant["id"])
|
|
114
|
+
text!(merchant["name"], 512)
|
|
115
|
+
raise Rejected, :constraint_mismatch unless checkout["merchant"] == merchant
|
|
116
|
+
raise Rejected, :currency_mismatch unless checkout["currency"] == currency
|
|
117
|
+
amount = multi_checkout!(checkout)
|
|
118
|
+
raise Rejected, :amount_mismatch unless amount == expected_total
|
|
119
|
+
quantities_match!(requirements, checkout["line_items"])
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Also shared with the explicitly selected merchant signing profile.
|
|
123
|
+
def self.multi_checkout!(checkout)
|
|
124
|
+
exact!(checkout, %w[currency id line_items links merchant status totals])
|
|
125
|
+
text!(checkout["id"])
|
|
126
|
+
exact!(checkout["merchant"], %w[id name])
|
|
127
|
+
text!(checkout["merchant"]["id"])
|
|
128
|
+
text!(checkout["merchant"]["name"], 512)
|
|
129
|
+
text!(checkout["currency"], 3)
|
|
130
|
+
raise Rejected, :unsupported_scoped_profile unless CURRENCIES.include?(checkout["currency"]) &&
|
|
131
|
+
checkout["status"] == "ready_for_complete"
|
|
132
|
+
raise Rejected, :invalid_scoped_schema unless checkout["links"].instance_of?(Array)
|
|
133
|
+
raise Rejected, :unsupported_scoped_profile unless checkout["links"].empty?
|
|
134
|
+
lines = bounded_array!(checkout["line_items"], MAX_LINES)
|
|
135
|
+
unique_ids!(lines)
|
|
136
|
+
amount = 0
|
|
137
|
+
lines.each do |line|
|
|
138
|
+
exact!(line, %w[id item quantity totals])
|
|
139
|
+
quantity!(line["quantity"])
|
|
140
|
+
item = line["item"]
|
|
141
|
+
exact!(item, %w[id price title])
|
|
142
|
+
text!(item["id"])
|
|
143
|
+
text!(item["title"], 512)
|
|
144
|
+
amount!(item["price"])
|
|
145
|
+
line_amount = item["price"] * line["quantity"]
|
|
146
|
+
amount!(line_amount)
|
|
147
|
+
totals!(line["totals"], line_amount)
|
|
148
|
+
amount += line_amount
|
|
149
|
+
amount!(amount)
|
|
150
|
+
end
|
|
151
|
+
totals!(checkout["totals"], amount)
|
|
152
|
+
amount
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def self.bounded_array!(value, limit)
|
|
156
|
+
raise Rejected, :invalid_scoped_schema unless value.instance_of?(Array)
|
|
157
|
+
raise Rejected, :constraint_limit_exceeded unless value.length.between?(1, limit)
|
|
158
|
+
value
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def self.unique_ids!(values)
|
|
162
|
+
ids = values.map do |value|
|
|
163
|
+
raise Rejected, :invalid_scoped_schema unless value.instance_of?(Hash)
|
|
164
|
+
text!(value["id"])
|
|
165
|
+
value["id"]
|
|
166
|
+
end
|
|
167
|
+
raise Rejected, :invalid_scoped_schema unless ids.uniq.length == ids.length
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Integral max flow with reverse residual edges avoids greedy false rejects.
|
|
171
|
+
# At most 18 vertices, 64 candidate edges; capacities never expand per unit.
|
|
172
|
+
def self.quantities_match!(requirements, lines)
|
|
173
|
+
demand = requirements.sum { |requirement| requirement["quantity"] }
|
|
174
|
+
supply = lines.sum { |line| line["quantity"] }
|
|
175
|
+
raise Rejected, :constraint_mismatch unless demand == supply
|
|
176
|
+
sink = 1 + requirements.length + lines.length
|
|
177
|
+
residual = Array.new(sink + 1) { Array.new(sink + 1, 0) }
|
|
178
|
+
requirements.each_with_index do |requirement, i|
|
|
179
|
+
r = i + 1
|
|
180
|
+
residual[0][r] = requirement["quantity"]
|
|
181
|
+
ids = requirement["acceptable_items"].map { |candidate| candidate["id"] }
|
|
182
|
+
lines.each_with_index do |line, j|
|
|
183
|
+
residual[r][requirements.length + j + 1] = demand if ids.include?(line["item"]["id"])
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
lines.each_with_index { |line, j| residual[requirements.length + j + 1][sink] = line["quantity"] }
|
|
187
|
+
flow = 0
|
|
188
|
+
loop do
|
|
189
|
+
parents = Array.new(sink + 1)
|
|
190
|
+
parents[0] = 0
|
|
191
|
+
queue = [0]
|
|
192
|
+
until queue.empty? || parents[sink]
|
|
193
|
+
u = queue.shift
|
|
194
|
+
residual[u].each_with_index do |capacity, v|
|
|
195
|
+
next unless capacity > 0 && parents[v].nil?
|
|
196
|
+
parents[v] = u
|
|
197
|
+
queue << v
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
break unless parents[sink]
|
|
201
|
+
amount, v = demand, sink
|
|
202
|
+
until v == 0
|
|
203
|
+
u = parents[v]
|
|
204
|
+
amount = [amount, residual[u][v]].min
|
|
205
|
+
v = u
|
|
206
|
+
end
|
|
207
|
+
v = sink
|
|
208
|
+
until v == 0
|
|
209
|
+
u = parents[v]
|
|
210
|
+
residual[u][v] -= amount
|
|
211
|
+
residual[v][u] += amount
|
|
212
|
+
v = u
|
|
213
|
+
end
|
|
214
|
+
flow += amount
|
|
215
|
+
end
|
|
216
|
+
raise Rejected, :constraint_mismatch unless flow == demand
|
|
217
|
+
end
|
|
218
|
+
|
|
79
219
|
def self.exact!(value, fields)
|
|
80
220
|
raise Rejected, :invalid_scoped_schema unless value.instance_of?(Hash) && (fields - value.keys).empty?
|
|
81
221
|
raise Rejected, :unsupported_scoped_profile unless (value.keys - fields).empty?
|
|
@@ -108,7 +248,8 @@ module AP2MandateVerifier
|
|
|
108
248
|
raise Rejected, :unsupported_scoped_profile unless values.map { |entry| entry["type"] }.sort == %w[subtotal total]
|
|
109
249
|
raise Rejected, :amount_mismatch unless values.all? { |entry| entry["amount"] == expected }
|
|
110
250
|
end
|
|
111
|
-
private_class_method :exact!, :one!, :text!, :quantity!, :amount!, :totals
|
|
251
|
+
private_class_method :exact!, :one!, :text!, :quantity!, :amount!, :totals!,
|
|
252
|
+
:multi_constraints!, :bounded_array!, :unique_ids!, :quantities_match!
|
|
112
253
|
private_constant :CORE_INSTANCE_OF
|
|
113
254
|
end
|
|
114
255
|
end
|
|
@@ -8,6 +8,22 @@ module AP2MandateVerifier
|
|
|
8
8
|
PROFILE_REF = "ap2.checkout.single_merchant_sku"
|
|
9
9
|
PROFILE_VERSION = "1"
|
|
10
10
|
PROFILE_HASH = Digest::SHA256.hexdigest(PROFILE_BODY).freeze
|
|
11
|
+
MULTI_PROFILE_REF = "ap2.checkout.single_merchant_multi_item"
|
|
12
|
+
MULTI_PROFILE_VERSION = "1"
|
|
13
|
+
MULTI_PROFILE_BODY = '{"profile_ref":"ap2.checkout.single_merchant_multi_item","version":"1","ap2_source":"b4587ac1d055888a73b4b21750973cffba961793","algorithm":"ES256","key_format":"canonical-public-SPKI-P256-SHA256","root_type":"example+sd-jwt","terminal_type":"kb+sd-jwt","checkout_type":"JWT","disclosure_path_semantics":"root-walker-with-synthetic-disclosed-edges","disclosure_positions":["delegate_payload[0]","delegate_payload[0].disclosed.constraints[0].items[*].acceptable_items[*]","delegate_payload[0].disclosed.constraints[1].allowed[0]"],"constraints":["checkout.line_items","checkout.allowed_merchants"],"max_disclosures":66,"max_disclosure_segment_bytes":8192,"max_root_bytes":131072,"max_graph_nodes":2048,"max_graph_depth":24,"max_array_entries":16,"max_line_items":8,"max_requirements":8,"max_candidates_per_requirement":8,"quantity_matching":"integer-max-flow-product-id-exact-coverage","duplicate_line_ids":"reject","duplicate_requirement_ids":"reject","same_product_multiple_lines":"allow","amount_model":"integer-minor-unit-no-tax-shipping-discount"}'.freeze
|
|
14
|
+
MULTI_PROFILE_HASH = Digest::SHA256.hexdigest(MULTI_PROFILE_BODY).freeze
|
|
15
|
+
|
|
16
|
+
# Explicit Host policy registration. Signed token claims never select this.
|
|
17
|
+
def self.profile!(reference)
|
|
18
|
+
case reference
|
|
19
|
+
when PROFILE_REF
|
|
20
|
+
{ref: PROFILE_REF, version: PROFILE_VERSION, body: PROFILE_BODY, hash: PROFILE_HASH}.freeze
|
|
21
|
+
when MULTI_PROFILE_REF
|
|
22
|
+
{ref: MULTI_PROFILE_REF, version: MULTI_PROFILE_VERSION, body: MULTI_PROFILE_BODY, hash: MULTI_PROFILE_HASH}.freeze
|
|
23
|
+
else
|
|
24
|
+
reject!(:unsupported_trust_profile)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
11
27
|
ROLES = {"Issuer" => :trusted_issuer_public_key_pem,
|
|
12
28
|
"Agent" => :expected_agent_public_key_pem,
|
|
13
29
|
"Merchant" => :trusted_merchant_public_key_pem}.freeze
|
|
@@ -73,9 +89,10 @@ module AP2MandateVerifier
|
|
|
73
89
|
selection.values_at("trust_snapshot_id", "policy_version", "trust_content_hash")
|
|
74
90
|
live!(snapshot, verified_at, :inactive_trust_snapshot, :trust_snapshot_time_mismatch)
|
|
75
91
|
_profile, wire = body!(trust["profile"], %w[profile_ref version], "definition_hash", 8_192)
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
92
|
+
profile = profile!(selection["profile_ref"])
|
|
93
|
+
reject!(:unsupported_trust_profile) unless wire == profile[:body] &&
|
|
94
|
+
trust["profile"].values_at("profile_ref", "version", "definition_hash") == profile.values_at(:ref, :version, :hash) &&
|
|
95
|
+
selection.values_at("profile_ref", "profile_version", "profile_definition_hash") == profile.values_at(:ref, :version, :hash)
|
|
79
96
|
records = snapshot["records"]
|
|
80
97
|
reject! unless records.instance_of?(Array) && records.length.between?(1, 16)
|
|
81
98
|
records.each do |record|
|
|
@@ -83,7 +100,7 @@ module AP2MandateVerifier
|
|
|
83
100
|
%w[identity tenant_id purpose key_ref profile_ref profile_version].each { |f| id!(record[f]) }
|
|
84
101
|
reject! unless ROLES.key?(record["role"])
|
|
85
102
|
window!(record)
|
|
86
|
-
reject!(:unsupported_trust_profile) unless record.values_at("profile_ref", "profile_version", "algorithm") == [
|
|
103
|
+
reject!(:unsupported_trust_profile) unless record.values_at("profile_ref", "profile_version", "algorithm") == [profile[:ref], profile[:version], "ES256"]
|
|
87
104
|
digest!(record["key_fingerprint"])
|
|
88
105
|
begin
|
|
89
106
|
RootSignature.public_jwk!(record["public_key_pem"])
|
data/lib/ap2_mandate_verifier.rb
CHANGED
|
@@ -13,6 +13,7 @@ require_relative "ap2_mandate_verifier/input_builder"
|
|
|
13
13
|
require_relative "ap2_mandate_verifier/merchant_signature"
|
|
14
14
|
require_relative "ap2_mandate_verifier/checkout_jwt"
|
|
15
15
|
require_relative "ap2_mandate_verifier/checkout_receipt"
|
|
16
|
+
require_relative "ap2_mandate_verifier/buyer"
|
|
16
17
|
|
|
17
18
|
module AP2MandateVerifier
|
|
18
19
|
# Accepts original JSON bytes only. Syntax validity is not AP2 validity,
|
|
@@ -98,9 +99,9 @@ module AP2MandateVerifier
|
|
|
98
99
|
inputs = inputs.to_h do |key, value|
|
|
99
100
|
[key, %i[verified_at expected_total_minor].include?(key) ? value : InputValidator.snapshot!(value)]
|
|
100
101
|
end
|
|
101
|
-
|
|
102
|
+
context = TrustContext.verified_context!(trust_json: trust_json, selection_json: selection_json,
|
|
102
103
|
verified_at: inputs[:verified_at], expected_merchant_id: inputs[:expected_merchant_id])
|
|
103
|
-
ScopedConstraints.check!(**inputs, **keys)
|
|
104
|
+
ScopedConstraints.check!(**inputs, **context.fetch(:keys), profile_ref: context.fetch(:references).fetch("profile_ref"))
|
|
104
105
|
Result.new(status: :unverified, code: :scoped_constraints_verified_only)
|
|
105
106
|
rescue InputValidator::Unavailable, RootSignature::Unavailable => error
|
|
106
107
|
Result.new(status: :unavailable, code: error.code)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ap2_mandate_verifier
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.7.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- AP2 Mandate Verifier contributors
|
|
@@ -89,11 +89,14 @@ extra_rdoc_files: []
|
|
|
89
89
|
files:
|
|
90
90
|
- LICENSE
|
|
91
91
|
- README.md
|
|
92
|
+
- docs/BUYER_DIRECT_CHECKOUT.md
|
|
92
93
|
- docs/DIAGNOSTICS.ja.md
|
|
93
94
|
- docs/DIAGNOSTICS.md
|
|
94
95
|
- docs/MERCHANT_SIGNING.md
|
|
96
|
+
- docs/MULTI_ITEM_CHECKOUT.md
|
|
95
97
|
- docs/SUPPORT_CONTRACT.md
|
|
96
98
|
- lib/ap2_mandate_verifier.rb
|
|
99
|
+
- lib/ap2_mandate_verifier/buyer.rb
|
|
97
100
|
- lib/ap2_mandate_verifier/checkout_jwt.rb
|
|
98
101
|
- lib/ap2_mandate_verifier/checkout_receipt.rb
|
|
99
102
|
- lib/ap2_mandate_verifier/diagnostic.rb
|