|
93 | 93 | pi_cache = create_pi_cache(PiCacheOptions(redis_url=os.environ.get("REDIS_URL"))) |
94 | 94 |
|
95 | 95 | app = FastAPI() |
96 | | -gate = AgentScoreGate( |
| 96 | +_gate = AgentScoreGate( |
97 | 97 | api_key=os.environ["AGENTSCORE_API_KEY"], |
98 | 98 | require_kyc=True, |
99 | 99 | require_sanctions_clear=True, |
100 | 100 | min_age=21, |
101 | 101 | allowed_jurisdictions=["US"], |
102 | 102 | ) |
103 | 103 |
|
| 104 | + |
| 105 | +# Conditional gate: fires only when a payment credential is already attached. Anonymous |
| 106 | +# requests (no payment header) fall through to the handler unauthenticated and receive |
| 107 | +# a clean 402 with all rails advertised — so any spec-compliant x402 wallet (Coinbase |
| 108 | +# awal, Phantom, Solflare, etc.) can discover prices before AgentScore identity exists. |
| 109 | +# Identity is verified at settle time (when X-Payment / Authorization: Payment arrives), |
| 110 | +# and `create_session_on_missing` then auto-mints a verification session. |
| 111 | +async def gate_on_settle(request: Request) -> None: |
| 112 | + has_payment_header = bool( |
| 113 | + request.headers.get("payment-signature") |
| 114 | + or request.headers.get("x-payment") |
| 115 | + or (request.headers.get("authorization") or "").startswith("Payment ") |
| 116 | + ) |
| 117 | + if not has_payment_header: |
| 118 | + return None |
| 119 | + return await _gate(request) |
| 120 | + |
| 121 | + |
104 | 122 | # Vendor-instantiated x402 server + pympp server are stubs in this example — |
105 | 123 | # replace with your `create_x402_server(...)` + `create_mppx_server(...)` setup. |
106 | 124 | x402_server: object = ... # type: ignore[assignment] |
107 | 125 |
|
108 | 126 |
|
109 | | -@app.post("/purchase", dependencies=[Depends(gate)]) |
| 127 | +@app.post("/purchase", dependencies=[Depends(gate_on_settle)]) |
110 | 128 | async def purchase(request: Request, assess: dict = Depends(get_assess_data)): |
111 | 129 | body = await request.json() |
112 | 130 |
|
|
0 commit comments