First commit
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
"""Enable Banking (PSD2 aggregator) client: link ING / Revolut and pull transactions automatically.
|
||||
|
||||
Free "restricted mode": create an application at https://enablebanking.com/cp/applications,
|
||||
link your own accounts there, download the private key, set EB_APP_ID and mount the .pem.
|
||||
"""
|
||||
import logging
|
||||
import os
|
||||
import secrets
|
||||
import time
|
||||
from datetime import date, datetime, timedelta, timezone
|
||||
|
||||
import httpx
|
||||
import jwt
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from importers import parse_enablebanking
|
||||
from models import Account, Setting, Transaction
|
||||
from services import ingest
|
||||
|
||||
API = "https://api.enablebanking.com"
|
||||
log = logging.getLogger("enablebanking")
|
||||
|
||||
|
||||
def configured() -> bool:
|
||||
return bool(os.getenv("EB_APP_ID")) and os.path.exists(os.getenv("EB_KEY_PATH", "/run/secrets/enablebanking.pem"))
|
||||
|
||||
|
||||
def _headers() -> dict:
|
||||
app_id = os.environ["EB_APP_ID"]
|
||||
with open(os.getenv("EB_KEY_PATH", "/run/secrets/enablebanking.pem")) as f:
|
||||
key = f.read()
|
||||
iat = int(time.time())
|
||||
token = jwt.encode({"iss": "enablebanking.com", "aud": "api.enablebanking.com", "iat": iat, "exp": iat + 3600},
|
||||
key, algorithm="RS256", headers={"kid": app_id})
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
def _req(method: str, path: str, **kw) -> dict:
|
||||
with httpx.Client(timeout=30) as c:
|
||||
r = c.request(method, API + path, headers=_headers(), **kw)
|
||||
if r.status_code >= 400:
|
||||
raise RuntimeError(f"Enable Banking {method} {path}: {r.status_code} {r.text[:300]}")
|
||||
return r.json()
|
||||
|
||||
|
||||
def _set(db: Session, key: str, value: str) -> None:
|
||||
row = db.get(Setting, key) or Setting(key=key)
|
||||
row.value = value
|
||||
db.add(row)
|
||||
|
||||
|
||||
def get_setting(db: Session, key: str, default: str = "") -> str:
|
||||
row = db.get(Setting, key)
|
||||
return row.value if row else default
|
||||
|
||||
|
||||
def aspsps(country: str = "NL") -> list[dict]:
|
||||
return _req("GET", "/aspsps", params={"country": country, "psu_type": "personal"}).get("aspsps", [])
|
||||
|
||||
|
||||
def start_auth(db: Session, name: str, country: str, redirect_url: str) -> str:
|
||||
"""Returns the bank's consent URL to redirect the browser to."""
|
||||
max_days = 90
|
||||
for a in aspsps(country):
|
||||
if a["name"] == name:
|
||||
max_days = max(1, int(a.get("maximum_consent_validity", 90 * 86400)) // 86400)
|
||||
break
|
||||
valid_until = datetime.now(timezone.utc) + timedelta(days=min(max_days, 180))
|
||||
state = secrets.token_urlsafe(24)
|
||||
_set(db, "eb_state", state)
|
||||
db.commit()
|
||||
body = {"access": {"valid_until": valid_until.isoformat()},
|
||||
"aspsp": {"name": name, "country": country},
|
||||
"state": state, "redirect_url": redirect_url, "psu_type": "personal"}
|
||||
return _req("POST", "/auth", json=body)["url"]
|
||||
|
||||
|
||||
def finish_auth(db: Session, code: str, state: str) -> list[Account]:
|
||||
if not state or state != get_setting(db, "eb_state"):
|
||||
raise RuntimeError("Invalid state; start the bank connection again.")
|
||||
s = _req("POST", "/sessions", json={"code": code})
|
||||
aspsp = (s.get("aspsp") or {}).get("name", "bank")
|
||||
valid_until = (s.get("access") or {}).get("valid_until", "")
|
||||
linked = []
|
||||
for acc in s.get("accounts", []):
|
||||
iban = ((acc.get("account_id") or {}).get("iban") or "").replace(" ", "").upper() or None
|
||||
a = db.scalar(select(Account).where(Account.iban == iban)) if iban else None
|
||||
if a is None:
|
||||
bank = "revolut" if "revolut" in aspsp.lower() else ("ing" if "ing" in aspsp.lower() else "other")
|
||||
a = Account(name=acc.get("name") or f"{aspsp} {iban or ''}".strip(), bank=bank, iban=iban)
|
||||
db.add(a)
|
||||
a.eb_account_uid = acc["uid"]
|
||||
linked.append(a)
|
||||
_set(db, f"eb_valid_until:{aspsp}", valid_until)
|
||||
_set(db, "eb_state", "")
|
||||
db.commit()
|
||||
return linked
|
||||
|
||||
|
||||
def _transactions(uid: str, date_from: date) -> list[dict]:
|
||||
out, cont = [], None
|
||||
for _ in range(50):
|
||||
params = {"date_from": date_from.isoformat()}
|
||||
if cont:
|
||||
params["continuation_key"] = cont
|
||||
page = _req("GET", f"/accounts/{uid}/transactions", params=params)
|
||||
out += page.get("transactions", [])
|
||||
cont = page.get("continuation_key")
|
||||
if not cont:
|
||||
break
|
||||
return out
|
||||
|
||||
|
||||
def _balance(uid: str):
|
||||
from decimal import Decimal
|
||||
bals = _req("GET", f"/accounts/{uid}/balances").get("balances", [])
|
||||
pref = ["CLBD", "ITBD", "ITAV", "XPCD", "CLAV"]
|
||||
bals.sort(key=lambda b: pref.index(b.get("balance_type")) if b.get("balance_type") in pref else 99)
|
||||
return Decimal(str(bals[0]["balance_amount"]["amount"])) if bals else None
|
||||
|
||||
|
||||
def sync(db: Session) -> list[str]:
|
||||
"""Pull new transactions for every linked account. Returns human-readable results."""
|
||||
if not configured():
|
||||
return ["Enable Banking is not configured."]
|
||||
msgs = []
|
||||
for a in db.scalars(select(Account).where(Account.eb_account_uid.is_not(None))):
|
||||
try:
|
||||
last = db.scalar(select(func.max(Transaction.date)).where(Transaction.account_id == a.id))
|
||||
date_from = (last - timedelta(days=3)) if last else date.today() - timedelta(days=89)
|
||||
raw = [t for t in _transactions(a.eb_account_uid, date_from)
|
||||
if (t.get("status") or "BOOK") == "BOOK"]
|
||||
parsed = [parse_enablebanking(t) for t in raw]
|
||||
bal = _balance(a.eb_account_uid)
|
||||
r = ingest(db, a, parsed, source="api", balance=bal, balance_date=date.today() if bal is not None else None)
|
||||
a.eb_last_sync = datetime.now()
|
||||
db.commit()
|
||||
msgs.append(f"{a.name}: {r['inserted']} new, {r['duplicates']} already known")
|
||||
except Exception as e: # noqa: BLE001
|
||||
db.rollback()
|
||||
log.warning("sync %s failed: %s", a.name, e)
|
||||
msgs.append(f"{a.name}: {e}")
|
||||
return msgs
|
||||
Reference in New Issue
Block a user