Public source file

backend/scripts/narrowed_verifier_poc_demo.py

Documentation home
289 lines9,864 bytesread-only generated view
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""
Demonstrate the integrated narrowed verifier pipeline.

Usage:
    ./.venv/bin/python scripts/narrowed_verifier_poc_demo.py
"""

from __future__ import annotations

import asyncio
from datetime import datetime, timedelta, timezone
import logging
import sys
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parents[2]
if str(REPO_ROOT) not in sys.path:
    sys.path.insert(0, str(REPO_ROOT))

from backend.app.services.narrowed_verifier_poc import (  # noqa: E402
    IssuerVerificationState,
    NarrowedVerifierService,
)
from backend.app.services.replay_guard_poc import InMemoryReplayGuard  # noqa: E402
from backend.app.services.signed_schema_poc import (  # noqa: E402
    SUPPORTED_ALGORITHM_ID,
    SignedQRCodeClaims,
    build_demo_certificate,
    create_signed_envelope,
)


class FinalizeFailingReplayGuard(InMemoryReplayGuard):
    async def finalize(
        self,
        nonce: str,
        owner_token: str,
        consumed_ttl_seconds: int,
    ) -> bool:
        return False


class ReleaseFailingReplayGuard(InMemoryReplayGuard):
    async def release(self, nonce: str, owner_token: str) -> bool:
        return False


def make_claims(
    nonce: str,
    payload: str = "https://acme.example/pay",
    *,
    usage_policy: str = "one_time",
    issued_offset_minutes: int = -1,
    expires_offset_minutes: int = 5,
) -> SignedQRCodeClaims:
    now = datetime.now(timezone.utc)
    return SignedQRCodeClaims(
        version="1",
        usage_policy=usage_policy,
        certificate_ref="cert:acme-demo:2026-01",
        issued_at=(now + timedelta(minutes=issued_offset_minutes)).isoformat(),
        expires_at=(now + timedelta(minutes=expires_offset_minutes)).isoformat(),
        nonce=nonce,
        payload=payload,
    )


async def run_concurrent_worker(
    verifier: NarrowedVerifierService,
    envelope,
    certificate,
    issuer_state,
    start: asyncio.Event,
):
    await start.wait()
    return await verifier.verify_presented_code(envelope, certificate, issuer_state)


async def main() -> None:
    logging.getLogger("qrcode_api").setLevel(logging.ERROR)

    certificate, private_key_pem = build_demo_certificate(
        certificate_ref="cert:acme-demo:2026-01",
        algorithm_id=SUPPORTED_ALGORITHM_ID,
    )

    # Case 1: valid first scan, then replay block on second attempt.
    guard_a = InMemoryReplayGuard()
    verifier_a = NarrowedVerifierService(guard_a)
    issuer_state_valid = IssuerVerificationState(verified_domains=["acme.example"])
    claims_a = make_claims("demo-nonce-101")
    envelope_a = create_signed_envelope(
        claims_a,
        private_key_pem,
        code_algorithm_id=SUPPORTED_ALGORITHM_ID,
    )

    first_scan = await verifier_a.verify_presented_code(
        envelope_a,
        certificate,
        issuer_state_valid,
    )
    second_scan = await verifier_a.verify_presented_code(
        envelope_a,
        certificate,
        issuer_state_valid,
    )

    # Case 2: issuer-state mismatch releases reservation, then later retry succeeds.
    guard_b = InMemoryReplayGuard()
    verifier_b = NarrowedVerifierService(guard_b)
    claims_b = make_claims("demo-nonce-202")
    envelope_b = create_signed_envelope(
        claims_b,
        private_key_pem,
        code_algorithm_id=SUPPORTED_ALGORITHM_ID,
    )
    issuer_state_missing_domain = IssuerVerificationState(verified_domains=[])
    issuer_state_restored = IssuerVerificationState(verified_domains=["acme.example"])

    failed_then_released = await verifier_b.verify_presented_code(
        envelope_b,
        certificate,
        issuer_state_missing_domain,
    )
    retry_after_release = await verifier_b.verify_presented_code(
        envelope_b,
        certificate,
        issuer_state_restored,
    )

    # Case 3: expired credential is blocked before nonce reservation.
    guard_expired = InMemoryReplayGuard()
    verifier_expired = NarrowedVerifierService(guard_expired)
    claims_expired = make_claims(
        "demo-nonce-expired",
        issued_offset_minutes=-10,
        expires_offset_minutes=-1,
    )
    envelope_expired = create_signed_envelope(
        claims_expired,
        private_key_pem,
        code_algorithm_id=SUPPORTED_ALGORITHM_ID,
    )
    expired_result = await verifier_expired.verify_presented_code(
        envelope_expired,
        certificate,
        issuer_state_valid,
    )

    # Case 4: revoked certificate is blocked before nonce reservation.
    guard_revoked = InMemoryReplayGuard()
    verifier_revoked = NarrowedVerifierService(guard_revoked)
    claims_revoked = make_claims("demo-nonce-revoked")
    envelope_revoked = create_signed_envelope(
        claims_revoked,
        private_key_pem,
        code_algorithm_id=SUPPORTED_ALGORITHM_ID,
    )
    issuer_state_revoked = IssuerVerificationState(
        verified_domains=["acme.example"],
        certificate_revoked=True,
        certificate_revocation_reason="Issuer revoked credential after merchant offboarding",
    )
    revoked_result = await verifier_revoked.verify_presented_code(
        envelope_revoked,
        certificate,
        issuer_state_revoked,
    )

    # Case 5: release failure is surfaced when payload revalidation fails.
    guard_release_fail = ReleaseFailingReplayGuard()
    verifier_release_fail = NarrowedVerifierService(guard_release_fail)
    claims_release_fail = make_claims("demo-nonce-release-fail")
    envelope_release_fail = create_signed_envelope(
        claims_release_fail,
        private_key_pem,
        code_algorithm_id=SUPPORTED_ALGORITHM_ID,
    )
    release_failed_result = await verifier_release_fail.verify_presented_code(
        envelope_release_fail,
        certificate,
        issuer_state_missing_domain,
    )

    # Case 6: finalize failure is surfaced when consume cannot complete.
    guard_finalize_fail = FinalizeFailingReplayGuard()
    verifier_finalize_fail = NarrowedVerifierService(guard_finalize_fail)
    claims_finalize_fail = make_claims("demo-nonce-finalize-fail")
    envelope_finalize_fail = create_signed_envelope(
        claims_finalize_fail,
        private_key_pem,
        code_algorithm_id=SUPPORTED_ALGORITHM_ID,
    )
    finalize_failed_result = await verifier_finalize_fail.verify_presented_code(
        envelope_finalize_fail,
        certificate,
        issuer_state_valid,
    )

    # Case 7: concurrent valid scans share one nonce; only one may win.
    guard_c = InMemoryReplayGuard()
    verifier_c = NarrowedVerifierService(guard_c)
    claims_c = make_claims("demo-nonce-303")
    envelope_c = create_signed_envelope(
        claims_c,
        private_key_pem,
        code_algorithm_id=SUPPORTED_ALGORITHM_ID,
    )
    start = asyncio.Event()
    tasks = [
        asyncio.create_task(
            run_concurrent_worker(
                verifier_c,
                envelope_c,
                certificate,
                issuer_state_valid,
                start,
            )
        )
        for _ in range(10)
    ]
    start.set()
    concurrent_results = await asyncio.gather(*tasks)
    accepted_count = sum(1 for result in concurrent_results if result.allowed)
    replay_blocked_count = sum(
        1 for result in concurrent_results
        if not result.allowed and result.stage == "replay_guard"
    )

    print("Narrowed Verifier PoC")
    print("=====================")
    print(f"First valid scan: {'ALLOW' if first_scan.allowed else 'BLOCK'}")
    print(f"  stage: {first_scan.stage}")
    print(f"  reason: {first_scan.reason}")
    print(f"  reservation_state: {first_scan.reservation_state}")

    print(f"Second scan of same code: {'ALLOW' if second_scan.allowed else 'BLOCK'}")
    print(f"  stage: {second_scan.stage}")
    print(f"  reason: {second_scan.reason}")
    print(f"  reservation_state: {second_scan.reservation_state}")

    print(
        "Failed verification releases reservation:"
        f" {'PASS' if failed_then_released.reservation_state == 'released' else 'FAIL'}"
    )
    print(f"  stage: {failed_then_released.stage}")
    print(f"  reason: {failed_then_released.reason}")
    print(f"  reservation_state: {failed_then_released.reservation_state}")

    print(f"Retry after issuer state is restored: {'ALLOW' if retry_after_release.allowed else 'BLOCK'}")
    print(f"  stage: {retry_after_release.stage}")
    print(f"  reason: {retry_after_release.reason}")
    print(f"  reservation_state: {retry_after_release.reservation_state}")

    print(f"Expired credential: {'ALLOW' if expired_result.allowed else 'BLOCK'}")
    print(f"  stage: {expired_result.stage}")
    print(f"  reason: {expired_result.reason}")
    print(f"  reservation_state: {expired_result.reservation_state}")

    print(f"Revoked certificate: {'ALLOW' if revoked_result.allowed else 'BLOCK'}")
    print(f"  stage: {revoked_result.stage}")
    print(f"  reason: {revoked_result.reason}")
    print(f"  reservation_state: {revoked_result.reservation_state}")

    print(
        "Release failure is surfaced:"
        f" {'PASS' if release_failed_result.reservation_state == 'release_failed' else 'FAIL'}"
    )
    print(f"  stage: {release_failed_result.stage}")
    print(f"  reason: {release_failed_result.reason}")
    print(f"  reservation_state: {release_failed_result.reservation_state}")

    print(
        "Finalize failure is surfaced:"
        f" {'PASS' if finalize_failed_result.reservation_state == 'finalize_failed' else 'FAIL'}"
    )
    print(f"  stage: {finalize_failed_result.stage}")
    print(f"  reason: {finalize_failed_result.reason}")
    print(f"  reservation_state: {finalize_failed_result.reservation_state}")

    print("Concurrent first scans")
    print(f"  accepted_count: {accepted_count}")
    print(f"  replay_blocked_count: {replay_blocked_count}")


if __name__ == "__main__":
    asyncio.run(main())