From f85d0a0f2098694ed8dd5553354b252cb877c68c Mon Sep 17 00:00:00 2001 From: bain Date: Fri, 13 Oct 2023 20:57:15 +0200 Subject: [PATCH] reformat; add expiration time to tokens --- matrix-invitation-dealer/__main__.py | 2 +- matrix-invitation-dealer/admin.py | 4 ++++ matrix-invitation-dealer/env.py | 11 +++++------ matrix-invitation-dealer/main.py | 2 -- matrix-invitation-dealer/migrate.py | 7 +++++-- matrix-invitation-dealer/setup.py | 15 ++++++++++----- 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/matrix-invitation-dealer/__main__.py b/matrix-invitation-dealer/__main__.py index 81e9122..b4e59cc 100644 --- a/matrix-invitation-dealer/__main__.py +++ b/matrix-invitation-dealer/__main__.py @@ -2,7 +2,6 @@ import asyncio import logging import os import json -import signal from .env import CREDENTIALS_FILE from .client import create_client @@ -31,4 +30,5 @@ async def go(): await bot.run() + asyncio.run(go()) diff --git a/matrix-invitation-dealer/admin.py b/matrix-invitation-dealer/admin.py index 946cbda..4795cec 100644 --- a/matrix-invitation-dealer/admin.py +++ b/matrix-invitation-dealer/admin.py @@ -1,6 +1,9 @@ from typing import Optional from aiohttp import ClientSession import logging +import time + +from .env import INVITE_CODE_EXPIRATION logger = logging.getLogger(__name__) @@ -46,6 +49,7 @@ class SynapseAdmin: }, json={ "uses_allowed": 1, + "expiry_time": int((time.time() + INVITE_CODE_EXPIRATION.total_seconds())*1000), }, ) if not resp.ok: diff --git a/matrix-invitation-dealer/env.py b/matrix-invitation-dealer/env.py index c2ffa60..17ec1d0 100644 --- a/matrix-invitation-dealer/env.py +++ b/matrix-invitation-dealer/env.py @@ -47,14 +47,10 @@ if os.path.isfile(_ENV_FILE): continue _DOTENV[split[0]] = "=".join(split[1:]) -USER_REQUIRED_AGE = env( - td_parse, "USER_REQUIRED_AGE", datetime.timedelta(days=14) -) +USER_REQUIRED_AGE = env(td_parse, "USER_REQUIRED_AGE", datetime.timedelta(days=14)) SYNAPSE_ADMIN_ACCESS_TOKEN = env(str, "SYNAPSE_ADMIN_ACCESS_TOKEN", "") -SYNAPSE_ADMIN_HOMESERVER = env( - str, "SYNAPSE_ADMIN_HOMESERVER", "http://127.0.0.1:8008" -) +SYNAPSE_ADMIN_HOMESERVER = env(str, "SYNAPSE_ADMIN_HOMESERVER", "http://127.0.0.1:8008") DATABASE_FILE = env(Path, "DATABASE_FILE", "data.sqlite") CREDENTIALS_FILE = env(Path, "CREDENTIALS_FILE", "credentials.json") @@ -68,3 +64,6 @@ icq_amount, icq_timespan = INVITE_CODE_QUOTA.split("/") INVITE_CODE_QUOTA_AMOUNT = int(icq_amount) INVITE_CODE_QUOTA_TIMESPAN = td_parse(icq_timespan) +INVITE_CODE_EXPIRATION = env( + td_parse, "INVITE_CODE_EXPIRATION", datetime.timedelta(days=7) +) diff --git a/matrix-invitation-dealer/main.py b/matrix-invitation-dealer/main.py index a26eca6..1cc8cfb 100644 --- a/matrix-invitation-dealer/main.py +++ b/matrix-invitation-dealer/main.py @@ -245,5 +245,3 @@ class Bot: # TODO: better restart system logger.exception("Restarting") await asyncio.sleep(15) - - diff --git a/matrix-invitation-dealer/migrate.py b/matrix-invitation-dealer/migrate.py index e9b91d3..c28c138 100644 --- a/matrix-invitation-dealer/migrate.py +++ b/matrix-invitation-dealer/migrate.py @@ -8,6 +8,7 @@ from .env import DATABASE_FILE logger = logging.getLogger(__name__) + async def db_apply_migrations(): if not os.path.exists(DATABASE_FILE): async with aiosqlite.connect(DATABASE_FILE) as db: @@ -24,9 +25,11 @@ async def db_apply_migrations(): if file.name not in already_applied: try: await db.executescript(file.read_text()) - await db.execute("INSERT INTO sch_updates (filename) VALUES (?)", file.name) + await db.execute( + "INSERT INTO sch_updates (filename) VALUES (?)", file.name + ) except Exception: - logger.exception('Failed to migrate!') + logger.exception("Failed to migrate!") await db.rollback() exit(1) else: diff --git a/matrix-invitation-dealer/setup.py b/matrix-invitation-dealer/setup.py index f31c575..cdb336e 100644 --- a/matrix-invitation-dealer/setup.py +++ b/matrix-invitation-dealer/setup.py @@ -35,7 +35,7 @@ async def matrix_account_setup(): homeserver = MATRIX_HOMESERVER if not (homeserver.startswith("https://") or homeserver.startswith("http://")): - homeserver = "https://"+MATRIX_HOMESERVER + homeserver = "https://" + MATRIX_HOMESERVER cfg = AsyncClientConfig( encryption_enabled=True, @@ -44,7 +44,9 @@ async def matrix_account_setup(): ) os.makedirs(STORE_PATH, exist_ok=True) - client = AsyncClient(homeserver, MATRIX_USER_ID, config=cfg, store_path=str(STORE_PATH)) + client = AsyncClient( + homeserver, MATRIX_USER_ID, config=cfg, store_path=str(STORE_PATH) + ) resp = await client.login(MATRIX_USER_PASSWORD, device_name=MATRIX_DEVICE_NAME) @@ -56,10 +58,13 @@ async def matrix_account_setup(): write_details_to_disk(resp, homeserver) assert client.olm is not None - key = client.olm.account.identity_keys['ed25519'] + key = client.olm.account.identity_keys["ed25519"] - logger.info('Logged in as %s. Please manually verify this session.') - logger.info('Session fingerprint %s', ' '.join([key[i:i+4] for i in range(0, len(key), 4)])) + logger.info("Logged in as %s. Please manually verify this session.") + logger.info( + "Session fingerprint %s", + " ".join([key[i : i + 4] for i in range(0, len(key), 4)]), + ) else: print(f'homeserver = "{homeserver}"; user = "{MATRIX_USER_ID}"')