34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
|
|
"""Administrative commands run inside the backend container."""
|
||
|
|
|
||
|
|
import argparse
|
||
|
|
import asyncio
|
||
|
|
import getpass
|
||
|
|
|
||
|
|
from sqlalchemy import delete
|
||
|
|
|
||
|
|
from .config import Settings
|
||
|
|
from .db import create_database
|
||
|
|
from .models import Admin, LoginSession
|
||
|
|
from .security import password_hasher
|
||
|
|
|
||
|
|
|
||
|
|
async def reset_password():
|
||
|
|
password = getpass.getpass("New admin password (12+ characters): ")
|
||
|
|
if len(password) < 12 or password != getpass.getpass("Confirm password: "):
|
||
|
|
raise SystemExit("Password too short or confirmation does not match")
|
||
|
|
engine, sessions = create_database(Settings().database_url)
|
||
|
|
async with sessions() as db:
|
||
|
|
admin = await db.get(Admin, 1)
|
||
|
|
admin.password_hash = password_hasher.hash(password)
|
||
|
|
await db.execute(delete(LoginSession))
|
||
|
|
await db.commit()
|
||
|
|
await engine.dispose()
|
||
|
|
print("Admin password updated; all system sessions revoked.")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
parser = argparse.ArgumentParser()
|
||
|
|
parser.add_argument("command", choices=["reset-password"])
|
||
|
|
parser.parse_args()
|
||
|
|
asyncio.run(reset_password())
|