SPB Git

Repository Public

flask_webhook.py 3.1 KB 76 lines python

1# =============================================================================2#                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION3#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# Project          : MacLustr Identity7# File             : sdk/python/examples/flask_webhook.py8# Description      : Flask 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"""Flask webhook endpoint for MacLustr Identity.4243Run:  MLID_WEBHOOK_SECRET=whsec_... flask --app flask_webhook run --port 900044The signature covers the exact bytes received: use ``request.get_data()``, never ``request.json``.45"""46from __future__ import annotations4748import os4950from flask import Flask, jsonify, request5152from maclustr_identity import SignatureVerificationError, Webhook5354app = Flask(__name__)5556# Replace both with your database. Store only these fields: never ask for or keep identity documents.57SEEN_EVENTS: set[str] = set()58USER_STATUS: dict[str, dict] = {}596061@app.post("/webhooks/identity")62def identity_webhook():63    payload = request.get_data()             # raw bytes64    try:65        event = Webhook.construct_event(payload, request.headers.get("MLID-Signature"), os.environ["MLID_WEBHOOK_SECRET"])66    except SignatureVerificationError:67        return jsonify(error="invalid signature"), 40068    if event.id in SEEN_EVENTS:              # deliveries are at-least-once69        return jsonify(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 jsonify(received=True)76
Files main
Menu

Theme