98 lines
2.8 KiB
Python
98 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Use an operator connection only to seed a legacy hash and an expired test session."""
|
|
|
|
import base64
|
|
import hashlib
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
|
|
|
|
def inside():
|
|
import psycopg
|
|
|
|
from messenger.db_roles import admin_conninfo
|
|
|
|
fixture = json.load(sys.stdin)
|
|
with psycopg.connect(admin_conninfo()) as db:
|
|
if fixture["mode"] == "seed":
|
|
db.execute(
|
|
"UPDATE users SET password_hash=%s WHERE id=%s",
|
|
(fixture["hash"], fixture["user"]),
|
|
)
|
|
elif fixture["mode"] == "check":
|
|
row = db.execute(
|
|
"SELECT password_hash FROM users WHERE id=%s", (fixture["user"],)
|
|
).fetchone()
|
|
assert row and row[0].startswith("$argon2id$")
|
|
assert row[0].split("$")[3] == "m=19456,t=2,p=1"
|
|
db.execute(
|
|
"UPDATE sessions SET expires_at=created_at+interval '1 millisecond' "
|
|
"WHERE token_hash=%s",
|
|
(bytes.fromhex(fixture["digest"]),),
|
|
)
|
|
else:
|
|
raise AssertionError("Unknown fixture mode")
|
|
|
|
|
|
def db_fixture(**data):
|
|
subprocess.run(
|
|
[
|
|
"docker",
|
|
"compose",
|
|
"run",
|
|
"--rm",
|
|
"--no-deps",
|
|
"-T",
|
|
"-v",
|
|
f"{Path(__file__).resolve()}:/test/verify_sessions.py:ro",
|
|
"--entrypoint",
|
|
"python",
|
|
"migrate",
|
|
"/test/verify_sessions.py",
|
|
"--inside",
|
|
],
|
|
cwd=ROOT,
|
|
text=True,
|
|
input=json.dumps(data),
|
|
check=True,
|
|
)
|
|
|
|
|
|
def main():
|
|
if "--inside" in sys.argv:
|
|
inside()
|
|
return
|
|
from argon2 import PasswordHasher
|
|
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
from client import Client
|
|
|
|
client = Client("https://localhost:8443", str(ROOT / "secrets/tls/trust/ca.crt"))
|
|
actor = client.user("legacy")
|
|
reg = actor["registration"]
|
|
legacy = PasswordHasher(memory_cost=65536, time_cost=3, parallelism=4).hash(reg["password"])
|
|
db_fixture(mode="seed", user=actor["id"], hash=legacy)
|
|
auth = base64.b64encode((reg["username"] + ":" + reg["password"]).encode()).decode()
|
|
session = client.call(
|
|
"POST",
|
|
"/api/v1/auth/sessions",
|
|
201,
|
|
schema="Session",
|
|
headers={"Authorization": "Basic " + auth},
|
|
)["json"]
|
|
actor["token"] = session["token"]
|
|
client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
|
|
db_fixture(
|
|
mode="check", user=actor["id"], digest=hashlib.sha256(actor["token"].encode()).hexdigest()
|
|
)
|
|
client.call("GET", "/api/v1/users/me", 401, actor=actor)
|
|
print("PASS: successful login rehashed old Argon2id parameters; expired session rejected.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|