Repository Public
1# =============================================================================2# SP BOUCHER | INFRASTRUCTURE & AUTOMATION3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# Project : MacLustr Identity7# File : sdk/python/examples/fastapi_webhook.py8# Description : FastAPI webhook endpoint for MacLustr Identity.9# Module : sdk10# Repository : https://git.spboucher.ai/maclustr-identity11# Project Website : https://identity.spboucher.ai12# Created : 2026-12-0113# Last Modified : 2026-12-0114#15# Author : Simon-Pierre Boucher16# Position : Professor of Finance17# Institution : Université du Québec en Outaouais (UQO)18# Organization : SP Boucher19# Website : https://spboucher.ai20# Git : https://git.spboucher.ai21#22# Preferred Contact: admin@spboucher.ai23# Technical Contact: dev@spboucher.ai24# Security/Admin : admin@spboucher.ai25#26# Copyright (c) 202627# Simon-Pierre Boucher28# All rights reserved where applicable. This file is governed by the29# applicable project license, copyright notices and relevant law.30# Existing third-party rights and notices remain unaffected.31#32# License : None specified (no license file in this repository)33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041"""FastAPI webhook endpoint for MacLustr Identity.4243Run: MLID_WEBHOOK_SECRET=whsec_... uvicorn fastapi_webhook:app --port 900044The signature covers the exact bytes received: read ``await request.body()``, never a parsed model.45"""46from __future__ import annotations4748import os4950from fastapi import FastAPI, HTTPException, Request5152from maclustr_identity import SignatureVerificationError, Webhook5354app = FastAPI()5556# Replace both with your database. Store only these fields: never ask for or keep identity documents.57SEEN_EVENTS: set[str] = set() # unique constraint on event_id in a real application58USER_STATUS: dict[str, dict] = {} # external_user_id -> {verification_id, status, assurance_level, expires_at}596061@app.post("/webhooks/identity")62async def identity_webhook(request: Request) -> dict:63 payload = await request.body()64 try:65 event = Webhook.construct_event(payload, request.headers.get("mlid-signature"), os.environ["MLID_WEBHOOK_SECRET"])66 except SignatureVerificationError:67 raise HTTPException(status_code=400, detail="invalid signature")68 if event.id in SEEN_EVENTS: # deliveries are at-least-once69 return {"received": True, "duplicate": True}70 SEEN_EVENTS.add(event.id)71 v = event.verification # None for created / processing / expired / cancelled72 if v is not None and v.external_user_id:73 USER_STATUS[v.external_user_id] = {"verification_id": v.verification_id, "status": v.status,74 "assurance_level": v.assurance_level, "expires_at": v.expires_at}75 return {"received": True}76