ap2_mandate_verifier 1.3.0 → 1.5.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.ja.md +24 -8
- data/README.md +24 -8
- data/SECURITY.md +4 -2
- data/docs/PAYMENT_ACCEPTANCE.md +88 -0
- data/docs/PAYMENT_POLICY.md +72 -0
- data/docs/PUBLIC_API.md +13 -3
- data/docs/RELEASE_130_CANDIDATE.md +2 -0
- data/docs/RELEASE_140_CANDIDATE.md +19 -0
- data/docs/RELEASE_150_CANDIDATE.md +21 -0
- data/docs/SELECTIVE_CHECKOUT.md +4 -2
- data/docs/SUPPORT_CONTRACT.md +5 -1
- data/examples/payment_acceptance.rb +77 -0
- data/examples/payment_policy.rb +101 -0
- data/lib/ap2_mandate_verifier/payment.rb +102 -54
- data/lib/ap2_mandate_verifier/payment_acceptance/sqlite_store.rb +316 -0
- data/lib/ap2_mandate_verifier/payment_acceptance.rb +263 -0
- data/lib/ap2_mandate_verifier/payment_policy.rb +115 -0
- data/lib/ap2_mandate_verifier/payment_receipt.rb +6 -5
- data/lib/ap2_mandate_verifier/selective_payment.rb +40 -31
- data/lib/ap2_mandate_verifier.rb +3 -0
- metadata +14 -4
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AP2MandateVerifier
|
|
4
|
+
# Explicit, narrower operational-plan profile. No scheduling or QWAC proof.
|
|
5
|
+
module PaymentPolicy
|
|
6
|
+
PROFILE_REF = "ap2.payment.pisp_execution.complete.1".freeze
|
|
7
|
+
SELECTIVE_PROFILE_REF = "ap2.payment.pisp_execution.selective.1".freeze
|
|
8
|
+
FIELDS = %w[pisp execution_date].freeze
|
|
9
|
+
TYPES = %w[payment.allowed_pisps payment.execution_date].freeze
|
|
10
|
+
def self.enabled?(profile)
|
|
11
|
+
[PROFILE_REF, SELECTIVE_PROFILE_REF].include?(profile)
|
|
12
|
+
end
|
|
13
|
+
def self.profile!(value, selective: false)
|
|
14
|
+
profile = InputValidator.snapshot!(value, max_bytes: 256)
|
|
15
|
+
old, current = selective ? [SelectivePayment::PROFILE_REF, SELECTIVE_PROFILE_REF] : [Payment::PROFILE_REF, PROFILE_REF]
|
|
16
|
+
raise Payment::Invalid unless [old, current].include?(profile)
|
|
17
|
+
profile
|
|
18
|
+
end
|
|
19
|
+
def self.candidate_policy!(profile, policy)
|
|
20
|
+
raise Payment::Invalid unless enabled?(profile) ? policy == :independent : policy.nil?
|
|
21
|
+
end
|
|
22
|
+
def self.fields(profile) = Payment::FIELDS + (enabled?(profile) ? FIELDS : [])
|
|
23
|
+
def self.pisp!(value)
|
|
24
|
+
Payment.send(:exact!, value, %w[brand_name domain_name legal_name])
|
|
25
|
+
%w[legal_name brand_name].each { |f| Payment.send(:text!, value[f], 512) }
|
|
26
|
+
domain = value["domain_name"]
|
|
27
|
+
raise Payment::Invalid unless domain.instance_of?(String) && domain.bytesize.between?(1, 253) && domain.ascii_only? &&
|
|
28
|
+
domain.split(".", -1).all? { |label| label.bytesize.between?(1, 63) && label.match?(/\A[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z/) }
|
|
29
|
+
end
|
|
30
|
+
# Exact decimal arithmetic; no permissive DateTime parsing/normalization.
|
|
31
|
+
def self.instant!(source)
|
|
32
|
+
value = InputValidator.snapshot!(source, max_bytes: 40)
|
|
33
|
+
m = /\A(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,9}))?(Z|[+-]\d{2}:\d{2})\z/.match(value)
|
|
34
|
+
raise Payment::Invalid unless m
|
|
35
|
+
y, month, day, hour, minute, second = m.captures.first(6).map(&:to_i)
|
|
36
|
+
leap = y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)
|
|
37
|
+
days = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
|
38
|
+
raise Payment::Invalid unless y.between?(1, 9999) && month.between?(1, 12) && day.between?(1, days[month - 1]) &&
|
|
39
|
+
hour.between?(0, 23) && minute.between?(0, 59) && second.between?(0, 59) && m[8] != "-00:00"
|
|
40
|
+
offset = 0
|
|
41
|
+
unless m[8] == "Z"
|
|
42
|
+
oh, om = m[8][1..].split(":").map(&:to_i)
|
|
43
|
+
raise Payment::Invalid unless oh <= 23 && om <= 59
|
|
44
|
+
offset = (oh * 3600 + om * 60) * (m[8][0] == "-" ? -1 : 1)
|
|
45
|
+
end
|
|
46
|
+
# Gregorian days relative to 0001-01-01, with fractional seconds exact.
|
|
47
|
+
before = y - 1
|
|
48
|
+
count = before * 365 + before / 4 - before / 100 + before / 400 + days.first(month - 1).sum + day - 1
|
|
49
|
+
fraction = m[7] ? Rational(m[7].to_i, 10**m[7].size) : 0
|
|
50
|
+
count * 86400 + hour * 3600 + minute * 60 + second - offset + fraction
|
|
51
|
+
end
|
|
52
|
+
def self.details!(value)
|
|
53
|
+
pisp!(value.fetch("pisp")); instant!(value.fetch("execution_date"))
|
|
54
|
+
end
|
|
55
|
+
def self.constraints!(opening)
|
|
56
|
+
cs = opening.fetch("constraints")
|
|
57
|
+
raise Payment::Invalid unless cs.instance_of?(Array) && cs.size.between?(5, 6) && cs.all? { |c| c.instance_of?(Hash) }
|
|
58
|
+
extra = cs.select { |c| TYPES.include?(c["type"]) }
|
|
59
|
+
raise Payment::Invalid unless extra.size.between?(1, 2) && extra.map { |c| c["type"] }.uniq.size == extra.size
|
|
60
|
+
extra.each do |c|
|
|
61
|
+
if c["type"] == TYPES[0]
|
|
62
|
+
Payment.send(:exact!, c, %w[type allowed])
|
|
63
|
+
list = c["allowed"]
|
|
64
|
+
raise Payment::Invalid unless list.instance_of?(Array) && list.size.between?(1, 8) && list.uniq.size == list.size
|
|
65
|
+
list.each { |p| pisp!(p) }
|
|
66
|
+
else
|
|
67
|
+
raise Payment::Invalid unless (c.keys - %w[type not_before not_after]).empty? &&
|
|
68
|
+
c.key?("type") && (c.key?("not_before") || c.key?("not_after"))
|
|
69
|
+
lower = instant!(c["not_before"]) if c.key?("not_before")
|
|
70
|
+
upper = instant!(c["not_after"]) if c.key?("not_after")
|
|
71
|
+
raise Payment::Invalid if lower && upper && lower > upper
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
pisp!(opening["pisp"]) if opening.key?("pisp")
|
|
75
|
+
instant!(opening["execution_date"]) if opening.key?("execution_date")
|
|
76
|
+
# Reject already contradictory fixed plans before any signer effects.
|
|
77
|
+
extra.each do |c|
|
|
78
|
+
if c["type"] == TYPES[0] && opening.key?("pisp")
|
|
79
|
+
raise Payment::Invalid unless c["allowed"].include?(opening["pisp"])
|
|
80
|
+
elsif c["type"] == TYPES[1] && opening.key?("execution_date")
|
|
81
|
+
instant = instant!(opening["execution_date"])
|
|
82
|
+
raise Payment::Invalid if c.key?("not_before") && instant < instant!(c["not_before"])
|
|
83
|
+
raise Payment::Invalid if c.key?("not_after") && instant > instant!(c["not_after"])
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
opening.reject { |k,_| FIELDS.include?(k) }.merge("constraints" => cs.reject { |c| TYPES.include?(c["type"]) })
|
|
87
|
+
end
|
|
88
|
+
def self.conditions!(opening, closed)
|
|
89
|
+
details!(closed)
|
|
90
|
+
FIELDS.each { |f| raise Payment::Invalid if opening.key?(f) && opening[f] != closed[f] }
|
|
91
|
+
opening.fetch("constraints").each do |c|
|
|
92
|
+
if c["type"] == TYPES[0]
|
|
93
|
+
raise Payment::Invalid unless c["allowed"].include?(closed["pisp"])
|
|
94
|
+
elsif c["type"] == TYPES[1]
|
|
95
|
+
instant = instant!(closed["execution_date"])
|
|
96
|
+
raise Payment::Invalid if c.key?("not_before") && instant < instant!(c["not_before"])
|
|
97
|
+
raise Payment::Invalid if c.key?("not_after") && instant > instant!(c["not_after"])
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
# Never fills either expected context from claims or expected_payment.
|
|
102
|
+
def self.context!(profile, trusted_pisp, execution_at)
|
|
103
|
+
unless enabled?(profile)
|
|
104
|
+
raise Payment::Invalid unless trusted_pisp.nil? && execution_at.nil?
|
|
105
|
+
return nil
|
|
106
|
+
end
|
|
107
|
+
pisp = InputBuilder.send(:copy!, trusted_pisp)
|
|
108
|
+
pisp!(pisp)
|
|
109
|
+
[ResearchEvidence.send(:copy_frozen, pisp), instant!(execution_at)].freeze
|
|
110
|
+
end
|
|
111
|
+
def self.bind_context!(value, context)
|
|
112
|
+
raise Payment::Invalid unless value.fetch("pisp") == context[0] && instant!(value.fetch("execution_date")) == context[1]
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
@@ -29,10 +29,11 @@ module AP2MandateVerifier
|
|
|
29
29
|
def payment_rejected? = verified? && outcome == "Error"
|
|
30
30
|
private_class_method :new
|
|
31
31
|
end
|
|
32
|
-
def self.reference(closed_sd_jwt:) = Payment.reference(closed_sd_jwt: closed_sd_jwt)
|
|
32
|
+
def self.reference(closed_sd_jwt:, profile_ref: Payment::PROFILE_REF) = Payment.reference(closed_sd_jwt: closed_sd_jwt, profile_ref: profile_ref)
|
|
33
33
|
def self.sign(closed_sd_jwt:, issuer:, outcome:, payment_id:, issued_at:, signer:, public_key_pem:,
|
|
34
|
-
psp_confirmation_id: nil, network_confirmation_id: nil, error: nil, error_description: nil
|
|
35
|
-
|
|
34
|
+
psp_confirmation_id: nil, network_confirmation_id: nil, error: nil, error_description: nil,
|
|
35
|
+
profile_ref: Payment::PROFILE_REF)
|
|
36
|
+
ref = reference(closed_sd_jwt: closed_sd_jwt, profile_ref: profile_ref)
|
|
36
37
|
claims = {"iss" => issuer, "status" => outcome, "payment_id" => payment_id, "iat" => issued_at, "reference" => ref}
|
|
37
38
|
{"psp_confirmation_id" => psp_confirmation_id, "network_confirmation_id" => network_confirmation_id,
|
|
38
39
|
"error" => error, "error_description" => error_description}.each { |k, v| claims[k] = v unless v.nil? }
|
|
@@ -41,13 +42,13 @@ module AP2MandateVerifier
|
|
|
41
42
|
Signed.send(:new, MerchantSignature.sign!(claims, signer, public_key_pem), ref)
|
|
42
43
|
end
|
|
43
44
|
def self.verify(jwt:, closed_sd_jwt:, trusted_public_key_pem:, expected_issuer:, expected_payment_id:,
|
|
44
|
-
not_before:, not_after:, verified_at:)
|
|
45
|
+
not_before:, not_after:, verified_at:, profile_ref: Payment::PROFILE_REF)
|
|
45
46
|
[not_before, not_after, verified_at].each { |t| CheckoutReceipt.send(:epoch!, t) }
|
|
46
47
|
raise Invalid unless not_before <= verified_at && verified_at <= not_after
|
|
47
48
|
expected = InputValidator.snapshot!(expected_issuer, max_bytes: 512)
|
|
48
49
|
id = InputValidator.snapshot!(expected_payment_id, max_bytes: 512)
|
|
49
50
|
text!(expected, 512); text!(id, 512)
|
|
50
|
-
ref = reference(closed_sd_jwt: closed_sd_jwt)
|
|
51
|
+
ref = reference(closed_sd_jwt: closed_sd_jwt, profile_ref: profile_ref)
|
|
51
52
|
claims = RootSignature.verified_payload!(jwt, trusted_public_key_pem, expected_typ: "JWT", max_bytes: 16_384)
|
|
52
53
|
validate!(claims)
|
|
53
54
|
raise Invalid unless claims["iss"] == expected && claims["payment_id"] == id && claims["reference"] == ref &&
|
|
@@ -16,7 +16,9 @@ module AP2MandateVerifier
|
|
|
16
16
|
# protected approval record. This confidential issuance MUST NOT be sent.
|
|
17
17
|
def self.issue_open(open_checkout_sd_jwt:, constraints:, fixed:, approved_combinations:, selective:,
|
|
18
18
|
agent_public_key_pem:, issued_at:, expires_at:, signer:, public_key_pem:,
|
|
19
|
-
checkout_profile_ref: ScopedConstraints::MULTI_PROFILE_REF)
|
|
19
|
+
checkout_profile_ref: ScopedConstraints::MULTI_PROFILE_REF, profile_ref: PROFILE_REF, candidate_policy: nil)
|
|
20
|
+
profile_ref = PaymentPolicy.profile!(profile_ref, selective: true)
|
|
21
|
+
PaymentPolicy.candidate_policy!(profile_ref, candidate_policy)
|
|
20
22
|
RootSignature.send(:dependencies!)
|
|
21
23
|
raise Invalid unless %i[payees instruments both].include?(selective)
|
|
22
24
|
checkout = Payment.send(:checkout_open!, open_checkout_sd_jwt, profile_ref: checkout_profile_ref)
|
|
@@ -24,10 +26,10 @@ module AP2MandateVerifier
|
|
|
24
26
|
raise Invalid unless cs.instance_of?(Array)
|
|
25
27
|
cs << {"type" => "payment.reference", "conditional_transaction_id" => digest(checkout)}
|
|
26
28
|
value = InputBuilder.send(:copy!, fixed)
|
|
27
|
-
raise Invalid unless value.instance_of?(Hash) && (value.keys -
|
|
29
|
+
raise Invalid unless value.instance_of?(Hash) && (value.keys - PaymentPolicy.fields(profile_ref).drop(1)).empty?
|
|
28
30
|
opening = value.merge("vct" => "mandate.payment.open.1", "iat" => issued_at, "exp" => expires_at,
|
|
29
31
|
"cnf" => {"jwk" => RootSignature.public_jwk!(agent_public_key_pem)}, "constraints" => cs)
|
|
30
|
-
Payment.send(:open!, opening)
|
|
32
|
+
Payment.send(:open!, opening, profile_ref: profile_ref)
|
|
31
33
|
payees = constraint!(opening, "payment.allowed_payees").fetch("allowed")
|
|
32
34
|
instruments = constraint!(opening, "payment.allowed_payment_instruments").fetch("allowed")
|
|
33
35
|
pairs = InputBuilder.send(:copy!, approved_combinations)
|
|
@@ -47,18 +49,19 @@ module AP2MandateVerifier
|
|
|
47
49
|
end
|
|
48
50
|
content = []
|
|
49
51
|
pointer = disclose!(opening, content)
|
|
50
|
-
payload = {"ap2_profile" =>
|
|
52
|
+
payload = {"ap2_profile" => profile_ref, "_sd_alg" => "sha-256", "delegate_payload" => [pointer]}
|
|
51
53
|
preview = [encode({"alg" => "ES256", "typ" => "example+sd-jwt"}), encode(payload), encode("\0" * 64)].join(".")
|
|
52
54
|
wire = ([preview] + content + disclosures + [""]).join("~")
|
|
53
|
-
parse!(wire, complete: true) # Full wire/disclosure bounds BEFORE signer effects.
|
|
55
|
+
parse!(wire, complete: true, profile_ref: profile_ref) # Full wire/disclosure bounds BEFORE signer effects.
|
|
54
56
|
jwt = MerchantSignature.sign!(payload, signer, public_key_pem, typ: "example+sd-jwt")
|
|
55
57
|
([jwt] + content + disclosures + [""]).join("~").freeze
|
|
56
58
|
end
|
|
57
59
|
|
|
58
60
|
# Each selective index selects one original signed candidate slot. For an
|
|
59
61
|
# inline/nonselective axis, pass nil: every approved inline value is visible.
|
|
60
|
-
def self.select(open_sd_jwt:, payee_index:, instrument_index:)
|
|
61
|
-
|
|
62
|
+
def self.select(open_sd_jwt:, payee_index:, instrument_index:, profile_ref: PROFILE_REF)
|
|
63
|
+
profile_ref = PaymentPolicy.profile!(profile_ref, selective: true)
|
|
64
|
+
parsed = parse!(open_sd_jwt, complete: true, profile_ref: profile_ref)
|
|
62
65
|
keep = [parsed.fetch(:content_digest)]
|
|
63
66
|
{"payment.allowed_payees" => payee_index, "payment.allowed_payment_instruments" => instrument_index}.each do |type, index|
|
|
64
67
|
c = constraint!(parsed.fetch(:original), type)
|
|
@@ -74,42 +77,47 @@ module AP2MandateVerifier
|
|
|
74
77
|
end
|
|
75
78
|
end
|
|
76
79
|
wire = ([parsed.fetch(:compact)] + parsed.fetch(:disclosures).select { |d| keep.include?(digest(d)) } + [""]).join("~")
|
|
77
|
-
parse!(wire, complete: false)
|
|
80
|
+
parse!(wire, complete: false, profile_ref: profile_ref)
|
|
78
81
|
wire.freeze
|
|
79
82
|
end
|
|
80
83
|
|
|
81
84
|
def self.issue_closed(open_presentation:, payment:, trusted_provider_public_key_pem:, agent_public_key_pem:,
|
|
82
|
-
audience:, nonce:, verified_at:, signer:)
|
|
85
|
+
audience:, nonce:, verified_at:, signer:, profile_ref: PROFILE_REF)
|
|
86
|
+
profile_ref = PaymentPolicy.profile!(profile_ref, selective: true)
|
|
83
87
|
agent = InputValidator.snapshot!(agent_public_key_pem, max_bytes: 1_024)
|
|
84
88
|
provider = InputValidator.snapshot!(trusted_provider_public_key_pem, max_bytes: 1_024)
|
|
85
89
|
aud = InputValidator.snapshot!(audience, max_bytes: 512)
|
|
86
90
|
challenge = InputValidator.snapshot!(nonce, max_bytes: 512)
|
|
87
|
-
parsed = authenticated!(open_presentation, provider)
|
|
91
|
+
parsed = authenticated!(open_presentation, provider, profile_ref: profile_ref)
|
|
88
92
|
opening = parsed.fetch(:opening)
|
|
89
93
|
Payment.send(:valid_now!, opening, verified_at)
|
|
90
94
|
raise Invalid unless opening.fetch("cnf") == {"jwk" => RootSignature.public_jwk!(agent)}
|
|
91
|
-
closed = Payment.send(:copy_details!, payment).merge("vct" => "mandate.payment.1", "iat" => verified_at, "exp" => opening.fetch("exp"))
|
|
92
|
-
Payment.send(:closed!, closed); Payment.send(:conditions!, opening, closed)
|
|
95
|
+
closed = Payment.send(:copy_details!, payment, profile_ref: profile_ref).merge("vct" => "mandate.payment.1", "iat" => verified_at, "exp" => opening.fetch("exp"))
|
|
96
|
+
Payment.send(:closed!, closed, profile_ref: profile_ref); Payment.send(:conditions!, opening, closed, profile_ref: profile_ref)
|
|
93
97
|
Payment.send(:text!, aud, 512); Payment.send(:text!, challenge, 512)
|
|
94
98
|
claims = {"delegate_payload" => [closed], "iat" => verified_at, "aud" => aud, "nonce" => challenge,
|
|
95
99
|
"sd_hash" => digest(parsed.fetch(:wire))}
|
|
96
|
-
|
|
100
|
+
claims["ap2_profile"] = profile_ref if PaymentPolicy.enabled?(profile_ref)
|
|
101
|
+
Payment.send(:preflight!, claims, "kb+sd-jwt", profile_ref: profile_ref)
|
|
97
102
|
terminal = MerchantSignature.sign!(claims, signer, agent, typ: "kb+sd-jwt")
|
|
98
103
|
Payment::Presentation.send(:new, parsed.fetch(:wire), terminal + "~")
|
|
99
104
|
end
|
|
100
105
|
|
|
101
106
|
# Structural restoration only. It never authenticates a stored presentation.
|
|
102
|
-
def self.present(open_presentation:, closed_sd_jwt:)
|
|
103
|
-
|
|
104
|
-
|
|
107
|
+
def self.present(open_presentation:, closed_sd_jwt:, profile_ref: PROFILE_REF)
|
|
108
|
+
profile_ref = PaymentPolicy.profile!(profile_ref, selective: true)
|
|
109
|
+
root = parse!(open_presentation, complete: false, profile_ref: profile_ref)
|
|
110
|
+
terminal, wire = Payment.send(:final!, closed_sd_jwt, conditional: true, profile_ref: profile_ref)
|
|
105
111
|
closed = terminal.fetch("delegate_payload").first
|
|
106
|
-
bind_terminal!(root, terminal, closed)
|
|
112
|
+
bind_terminal!(root, terminal, closed, profile_ref: profile_ref)
|
|
107
113
|
Payment::Presentation.send(:new, root.fetch(:wire), wire)
|
|
108
114
|
end
|
|
109
115
|
|
|
110
116
|
def self.verify(open_presentation:, closed_sd_jwt:, checkout_inputs:, expected_payment:,
|
|
111
117
|
trusted_provider_public_key_pem:, expected_agent_public_key_pem:,
|
|
112
|
-
expected_audience:, expected_nonce:, verified_at:)
|
|
118
|
+
expected_audience:, expected_nonce:, verified_at:, profile_ref: PROFILE_REF, trusted_pisp: nil, execution_at: nil)
|
|
119
|
+
profile_ref = PaymentPolicy.profile!(profile_ref, selective: true)
|
|
120
|
+
context = PaymentPolicy.context!(profile_ref, trusted_pisp, execution_at)
|
|
113
121
|
CheckoutReceipt.send(:epoch!, verified_at)
|
|
114
122
|
agent = InputValidator.snapshot!(expected_agent_public_key_pem, max_bytes: 1_024)
|
|
115
123
|
provider = InputValidator.snapshot!(trusted_provider_public_key_pem, max_bytes: 1_024)
|
|
@@ -123,7 +131,7 @@ module AP2MandateVerifier
|
|
|
123
131
|
[key, key == :verified_at ? value : InputValidator.snapshot!(value, max_bytes: limits.fetch(key, 131_072))]
|
|
124
132
|
end.freeze
|
|
125
133
|
raise Invalid unless inputs.size == fields.size && fields.all? { |f| inputs.key?(f) } && inputs[:verified_at] == verified_at
|
|
126
|
-
expected = Payment.send(:copy_details!, expected_payment)
|
|
134
|
+
expected = Payment.send(:copy_details!, expected_payment, profile_ref: profile_ref)
|
|
127
135
|
checkout = AP2MandateVerifier.verify_checkout(**inputs)
|
|
128
136
|
raise Invalid unless checkout.success?
|
|
129
137
|
raise Invalid unless expected.fetch("transaction_id") == digest(inputs.fetch(:host_checkout_jws))
|
|
@@ -132,39 +140,40 @@ module AP2MandateVerifier
|
|
|
132
140
|
raise Invalid unless expected.fetch("payee").fetch("id") == binding.fetch("merchant_id") &&
|
|
133
141
|
amount.fetch("currency") == binding.fetch("currency") && amount.fetch("amount") == binding.fetch("expected_total")
|
|
134
142
|
Payment.send(:bind_checkout!, expected, Payment.send(:jwt_payload!, inputs.fetch(:host_checkout_jws), "JWT"))
|
|
135
|
-
parsed = authenticated!(open_presentation, provider)
|
|
143
|
+
parsed = authenticated!(open_presentation, provider, profile_ref: profile_ref)
|
|
136
144
|
opening = parsed.fetch(:opening)
|
|
137
145
|
Payment.send(:valid_now!, opening, verified_at)
|
|
138
146
|
raise Invalid unless opening.fetch("cnf") == {"jwk" => RootSignature.public_jwk!(agent)}
|
|
139
|
-
terminal, wire = Payment.send(:final!, closed_sd_jwt, conditional: true)
|
|
147
|
+
terminal, wire = Payment.send(:final!, closed_sd_jwt, conditional: true, profile_ref: profile_ref)
|
|
140
148
|
RootSignature.verified_payload!(wire.split("~", -1).first, agent,
|
|
141
149
|
expected_typ: "kb+sd-jwt", max_bytes: 32_768)
|
|
142
150
|
Payment.send(:text!, aud, 512); Payment.send(:text!, nonce, 512)
|
|
143
151
|
raise Invalid unless terminal.fetch("aud") == aud && terminal.fetch("nonce") == nonce &&
|
|
144
152
|
terminal.fetch("iat") <= verified_at && verified_at - terminal.fetch("iat") <= 300
|
|
145
153
|
closed = terminal.fetch("delegate_payload").first
|
|
146
|
-
bind_terminal!(parsed, terminal, closed)
|
|
154
|
+
bind_terminal!(parsed, terminal, closed, profile_ref: profile_ref)
|
|
147
155
|
Payment.send(:valid_now!, closed, verified_at)
|
|
148
156
|
ref = constraint!(opening, "payment.reference").fetch("conditional_transaction_id")
|
|
149
|
-
|
|
157
|
+
PaymentPolicy.bind_context!(closed, context) if context
|
|
158
|
+
raise Invalid unless ref == digest(inputs.fetch(:root_presentation)) && closed.slice(*PaymentPolicy.fields(profile_ref)) == expected
|
|
150
159
|
Payment::Verification.send(:new, closed, wire)
|
|
151
160
|
rescue StandardError, LoadError
|
|
152
161
|
Payment::Verification.send(:new)
|
|
153
162
|
end
|
|
154
163
|
|
|
155
|
-
def self.bind_terminal!(root, terminal, closed)
|
|
164
|
+
def self.bind_terminal!(root, terminal, closed, profile_ref: PROFILE_REF)
|
|
156
165
|
opening = root.fetch(:opening)
|
|
157
|
-
Payment.send(:closed!, closed)
|
|
166
|
+
Payment.send(:closed!, closed, profile_ref: profile_ref)
|
|
158
167
|
raise Invalid unless terminal.fetch("sd_hash") == digest(root.fetch(:wire)) && terminal.fetch("iat") == closed.fetch("iat") &&
|
|
159
168
|
closed.fetch("iat") >= opening.fetch("iat") && closed.fetch("exp") <= opening.fetch("exp")
|
|
160
|
-
Payment.send(:conditions!, opening, closed)
|
|
169
|
+
Payment.send(:conditions!, opening, closed, profile_ref: profile_ref)
|
|
161
170
|
end
|
|
162
|
-
def self.authenticated!(wire, key)
|
|
163
|
-
parsed = parse!(wire, complete: false)
|
|
171
|
+
def self.authenticated!(wire, key, profile_ref: PROFILE_REF)
|
|
172
|
+
parsed = parse!(wire, complete: false, profile_ref: profile_ref)
|
|
164
173
|
RootSignature.verified_payload!(parsed.fetch(:compact), key, max_bytes: 16_384)
|
|
165
174
|
parsed
|
|
166
175
|
end
|
|
167
|
-
def self.parse!(source, complete:)
|
|
176
|
+
def self.parse!(source, complete:, profile_ref: PROFILE_REF)
|
|
168
177
|
RootSignature.send(:dependencies!)
|
|
169
178
|
wire = InputValidator.snapshot!(source, max_bytes: MAX_WIRE)
|
|
170
179
|
raise Invalid unless wire.ascii_only? && wire.end_with?("~")
|
|
@@ -174,7 +183,7 @@ module AP2MandateVerifier
|
|
|
174
183
|
compact = InputValidator.snapshot!(compact, max_bytes: 16_384)
|
|
175
184
|
claims = Payment.send(:jwt_payload!, compact, "example+sd-jwt")
|
|
176
185
|
Payment.send(:exact!, claims, %w[ap2_profile _sd_alg delegate_payload])
|
|
177
|
-
raise Invalid unless claims["ap2_profile"] ==
|
|
186
|
+
raise Invalid unless claims["ap2_profile"] == profile_ref && claims["_sd_alg"] == "sha-256" &&
|
|
178
187
|
claims["delegate_payload"].instance_of?(Array) && claims["delegate_payload"].size == 1
|
|
179
188
|
pointer = claims.fetch("delegate_payload").first
|
|
180
189
|
Payment.send(:exact!, pointer, ["..."])
|
|
@@ -218,7 +227,7 @@ module AP2MandateVerifier
|
|
|
218
227
|
end
|
|
219
228
|
raise Invalid if slots.values.all?(&:empty?)
|
|
220
229
|
raise Invalid unless used.sort == values.keys.sort
|
|
221
|
-
Payment.send(:open!, opening) # Only candidate markers were resolved/omitted.
|
|
230
|
+
Payment.send(:open!, opening, profile_ref: profile_ref) # Only candidate markers were resolved/omitted.
|
|
222
231
|
{wire: wire, compact: compact, disclosures: disclosures, opening: opening,
|
|
223
232
|
original: original, content_digest: content_digest, slots: slots, values: values}
|
|
224
233
|
end
|
data/lib/ap2_mandate_verifier.rb
CHANGED
|
@@ -19,6 +19,9 @@ require_relative "ap2_mandate_verifier/payment"
|
|
|
19
19
|
require_relative "ap2_mandate_verifier/payment_receipt"
|
|
20
20
|
require_relative "ap2_mandate_verifier/selective_payment"
|
|
21
21
|
require_relative "ap2_mandate_verifier/selective_checkout"
|
|
22
|
+
require_relative "ap2_mandate_verifier/payment_policy"
|
|
23
|
+
require_relative "ap2_mandate_verifier/payment_acceptance"
|
|
24
|
+
require_relative "ap2_mandate_verifier/payment_acceptance/sqlite_store"
|
|
22
25
|
|
|
23
26
|
module AP2MandateVerifier
|
|
24
27
|
# Accepts original JSON bytes only. Syntax validity is not AP2 validity,
|
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: 1.
|
|
4
|
+
version: 1.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- AP2 Mandate Verifier contributors
|
|
@@ -82,9 +82,10 @@ dependencies:
|
|
|
82
82
|
description: 'Framework-independent Ruby SDK for supported AP2 Checkout profiles:
|
|
83
83
|
Merchant JWT signing, Direct and conditional Mandate issuance and presentation,
|
|
84
84
|
full verification with immutable Evidence, Checkout Receipts, Direct and conditional
|
|
85
|
-
Payment Mandates, and Payment Receipts. Adds
|
|
86
|
-
|
|
87
|
-
does not grant purchase or
|
|
85
|
+
Payment Mandates, and Payment Receipts. Adds selective Checkout/Payment, PISP/date
|
|
86
|
+
constraints and opt-in transactional Payment acceptance/idempotency support. Requires
|
|
87
|
+
independently trusted application inputs; verification does not grant purchase or
|
|
88
|
+
payment permission.'
|
|
88
89
|
executables: []
|
|
89
90
|
extensions: []
|
|
90
91
|
extra_rdoc_files: []
|
|
@@ -101,6 +102,8 @@ files:
|
|
|
101
102
|
- docs/MERCHANT_SIGNING.md
|
|
102
103
|
- docs/MULTI_ITEM_CHECKOUT.md
|
|
103
104
|
- docs/PAYMENT.md
|
|
105
|
+
- docs/PAYMENT_ACCEPTANCE.md
|
|
106
|
+
- docs/PAYMENT_POLICY.md
|
|
104
107
|
- docs/POST_RELEASE_110.md
|
|
105
108
|
- docs/POST_RELEASE_MAINTENANCE.md
|
|
106
109
|
- docs/PUBLIC_API.md
|
|
@@ -108,12 +111,16 @@ files:
|
|
|
108
111
|
- docs/RELEASE_110_CANDIDATE.md
|
|
109
112
|
- docs/RELEASE_120_CANDIDATE.md
|
|
110
113
|
- docs/RELEASE_130_CANDIDATE.md
|
|
114
|
+
- docs/RELEASE_140_CANDIDATE.md
|
|
115
|
+
- docs/RELEASE_150_CANDIDATE.md
|
|
111
116
|
- docs/SELECTIVE_CHECKOUT.md
|
|
112
117
|
- docs/SELECTIVE_PAYMENT.md
|
|
113
118
|
- docs/SUPPORT_CONTRACT.md
|
|
114
119
|
- docs/evidence/1.1.0_publication.json
|
|
115
120
|
- examples/conditional_checkout.rb
|
|
116
121
|
- examples/payment.rb
|
|
122
|
+
- examples/payment_acceptance.rb
|
|
123
|
+
- examples/payment_policy.rb
|
|
117
124
|
- examples/selective_checkout.rb
|
|
118
125
|
- examples/selective_checkout_payment.rb
|
|
119
126
|
- examples/selective_payment.rb
|
|
@@ -128,6 +135,9 @@ files:
|
|
|
128
135
|
- lib/ap2_mandate_verifier/input_validator.rb
|
|
129
136
|
- lib/ap2_mandate_verifier/merchant_signature.rb
|
|
130
137
|
- lib/ap2_mandate_verifier/payment.rb
|
|
138
|
+
- lib/ap2_mandate_verifier/payment_acceptance.rb
|
|
139
|
+
- lib/ap2_mandate_verifier/payment_acceptance/sqlite_store.rb
|
|
140
|
+
- lib/ap2_mandate_verifier/payment_policy.rb
|
|
131
141
|
- lib/ap2_mandate_verifier/payment_receipt.rb
|
|
132
142
|
- lib/ap2_mandate_verifier/purchase_binding.rb
|
|
133
143
|
- lib/ap2_mandate_verifier/research_result.rb
|