Files
mctf/chrome-ledger/backend/app/infrastructure/persistence/repositories.py
T
TakuyaYagam1 13f936bee9 chore: import MCTF services
- Added all seven issued service trees without omissions.
- Preserved the supplied runtime configuration, binaries, and assets.
2026-11-29 12:25:29 +03:00

127 lines
5.1 KiB
Python
Executable File

from backend.app.common.constants import LIFETIME
def get_user_by_username(connection, username):
return connection.execute(
"SELECT id, username, namespace, password_hash FROM users WHERE username = ?", (username,),
).fetchone()
def create_auth_session(connection, tid, user_id, expires_at):
connection.execute(
"INSERT INTO auth_sessions (tid, user_id, expires_at) VALUES (?, ?, ?)",
(tid, user_id, expires_at),
)
def get_auth_user(connection, tid, user_id, expires_at, now):
return connection.execute(
"SELECT u.id, u.username FROM auth_sessions s JOIN users u ON u.id = s.user_id "
"WHERE s.tid = ? AND s.user_id = ? AND s.expires_at = ? AND s.expires_at > ?",
(tid, user_id, expires_at, now),
).fetchone()
def delete_auth_session(connection, tid):
connection.execute("DELETE FROM auth_sessions WHERE tid = ?", (tid,))
def create_user(connection, user_id, username, namespace, password_hash, created_at):
connection.execute(
"INSERT INTO users (id, username, namespace, password_hash, created_at) VALUES (?, ?, ?, ?, ?)",
(user_id, username, namespace, password_hash, created_at),
)
def create_implant(connection, implant_id, user_id, display_name, model, details, created_at):
connection.execute(
"INSERT INTO implants "
"(id, user_id, display_name, model, details, created_at) "
"VALUES (?, ?, ?, ?, ?, ?)",
(implant_id, user_id, display_name, model, details, created_at),
)
def create_implant_telemetry(connection, implant_id, display_name, model, public_key_pem, project_codename, installed_by):
connection.execute(
"INSERT INTO implant_telemetry "
"(implant_id, display_name, model, public_key_pem, project_codename, installed_by) VALUES (?, ?, ?, ?, ?, ?)",
(implant_id, display_name, model, public_key_pem, project_codename, installed_by),
)
def get_implant(connection, implant_id):
return connection.execute(
"SELECT i.*, u.username AS owner "
"FROM implants i JOIN users u ON u.id = i.user_id WHERE i.id = ?", (implant_id,),
).fetchone()
def list_implants(connection, owner_id):
return connection.execute(
"SELECT i.id, i.display_name, i.model, i.created_at, u.username AS owner "
"FROM implants i JOIN users u ON u.id = i.user_id "
"WHERE i.user_id = ? ORDER BY i.created_at DESC, i.id DESC", (owner_id,),
).fetchall()
def list_telemetry(connection, owner_id):
return connection.execute(
"SELECT t.display_name, t.model, t.public_key_pem, t.project_codename, t.installed_by "
"FROM implants i JOIN implant_telemetry t ON t.implant_id = i.id "
"WHERE i.user_id = ? ORDER BY i.created_at DESC, i.id DESC", (owner_id,),
).fetchall()
def list_registry(connection):
return connection.execute(
"SELECT i.id, i.display_name, i.model, i.created_at, u.username AS owner, "
"c.cert_pem, c.fingerprint, c.revoked_at "
"FROM implants i JOIN users u ON u.id = i.user_id "
"JOIN implant_certificates c ON c.implant_id = i.id "
"ORDER BY i.created_at DESC, i.id DESC, c.rowid DESC",
).fetchall()
def create_certificate(connection, implant_id, certificate):
connection.execute(
"INSERT INTO implant_certificates "
"(serial, implant_id, cert_pem, fingerprint, created_at, not_after) VALUES (?, ?, ?, ?, ?, ?)",
(certificate["serial"], implant_id, certificate["cert_pem"], certificate["fingerprint"],
certificate["created_at"], certificate["not_after"]),
)
def revoke_certificates(connection, implant_id, now):
connection.execute(
"UPDATE implant_certificates SET revoked_at = ? WHERE implant_id = ? AND revoked_at IS NULL",
(now, implant_id),
)
def get_current_certificate(connection, implant_id):
return connection.execute(
"SELECT * FROM implant_certificates WHERE implant_id = ? AND revoked_at IS NULL "
"ORDER BY rowid DESC LIMIT 1", (implant_id,),
).fetchone()
def get_latest_certificate(connection, implant_id):
return connection.execute(
"SELECT * FROM implant_certificates WHERE implant_id = ? ORDER BY rowid DESC LIMIT 1",
(implant_id,),
).fetchone()
def get_certificate(connection, implant_id, serial):
return connection.execute(
"SELECT * FROM implant_certificates WHERE implant_id = ? AND serial = ?",
(implant_id, serial),
).fetchone()
def create_implant_challenge(connection, nonce, implant_id, serial, now):
connection.execute(
"DELETE FROM implant_challenges WHERE created_at <= ?", ((now - LIFETIME).isoformat(),),
)
connection.execute(
"INSERT INTO implant_challenges (nonce, implant_id, certificate_serial, created_at) "
"VALUES (?, ?, ?, ?)", (nonce, implant_id, serial, now.isoformat()),
)
def consume_implant_challenge(connection, nonce, implant_id, certificate_serial, cutoff):
return connection.execute(
"DELETE FROM implant_challenges WHERE nonce = ? AND implant_id = ? AND certificate_serial = ? AND created_at > ?",
(nonce, implant_id, certificate_serial, cutoff),
).rowcount