forked from mtusi2026/mtusi2026lab7
48 lines
2.0 KiB
Python
48 lines
2.0 KiB
Python
"""Verify that login upgrades an older hash for a synthetic local account."""
|
|
import base64
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
|
|
from client import Client, arguments, check, run
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
CHECK = """
|
|
import json,os,sys
|
|
from argon2 import PasswordHasher
|
|
import psycopg
|
|
|
|
user_id,password,operation=json.load(sys.stdin)
|
|
with psycopg.connect(os.environ['DATABASE_URL']) as conn:
|
|
if operation=='seed':
|
|
old=PasswordHasher(memory_cost=8192,time_cost=1,parallelism=1).hash(password)
|
|
assert conn.execute('UPDATE users SET password_hash=%s WHERE id=%s',
|
|
(old,user_id)).rowcount==1
|
|
else:
|
|
updated=conn.execute('SELECT password_hash FROM users WHERE id=%s',(user_id,)).fetchone()[0]
|
|
current=PasswordHasher(memory_cost=19456,time_cost=2,parallelism=1)
|
|
assert current.verify(updated,password)
|
|
assert not current.check_needs_rehash(updated)
|
|
"""
|
|
|
|
|
|
def main():
|
|
args = arguments('Local password rehash')
|
|
client = Client(args.base_url, args.ca_file)
|
|
user = client.user('rehash')
|
|
registration = user['registration']
|
|
password = registration['password']
|
|
command = ['docker','compose','exec','-T','--index','1','api','python','secret_entry.py','python','-c',CHECK]
|
|
subprocess.run(command, cwd=ROOT, input=json.dumps([user['id'],password,'seed']), text=True, check=True)
|
|
basic = base64.b64encode(f"{registration['username']}:{password}".encode()).decode()
|
|
session = client.call('POST','/api/v1/auth/sessions',201,headers={'Authorization':'Basic '+basic},schema='Session')
|
|
check(session['json']['user']['id'] == user['id'], 'Login returned a different account')
|
|
subprocess.run(command, cwd=ROOT, input=json.dumps([user['id'],password,'verify']), text=True, check=True)
|
|
(ROOT / 'evidence/password-rehash.json').write_text('{"rehash_on_login": true}\n', encoding='utf-8')
|
|
print('PASS: successful login upgraded the synthetic account to the current Argon2id parameters')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|