reformat; add expiration time to tokens

This commit is contained in:
bain 2023-10-13 20:57:15 +02:00
parent a2ad9ea428
commit f85d0a0f20
Signed by: bain
GPG key ID: 31F0F25E3BED0B9B
6 changed files with 25 additions and 16 deletions

View file

@ -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())

View file

@ -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:

View file

@ -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)
)

View file

@ -245,5 +245,3 @@ class Bot:
# TODO: better restart system
logger.exception("Restarting")
await asyncio.sleep(15)

View file

@ -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:

View file

@ -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}"')