#!/usr/bin/env python3
import base64, hashlib, hmac, subprocess, time
from pathlib import Path
secret=Path('/srv/apps/sotlab-users/data/session_secret').read_bytes()
def cookie(username):
    ts=str(int(time.time()))
    payload=f'{username}:{ts}'
    sig=hmac.new(secret, payload.encode(), hashlib.sha256).hexdigest()
    return base64.urlsafe_b64encode(f'{payload}:{sig}'.encode()).decode()
for user in ['denas','sotgee']:
    r=subprocess.run(['curl','-sS','-D','-','-o','/tmp/portal_gate_body','--max-time','10','-H','Host: sotlab.co.uk','-H',f'Cookie: sotlab_session={cookie(user)}','http://127.0.0.1:8090/'],capture_output=True,text=True)
    body=Path('/tmp/portal_gate_body').read_text(errors='replace')[:1200]
    print('USER',user,'exit',r.returncode)
    print('HEAD',' | '.join(r.stdout.splitlines()[:12]))
    print('MARKERS',{m:(m in body or m in r.stdout) for m in ['Profile is disabled','not enrolled','Sot Lab','Sign in','HTTP/1.0 401','HTTP/1.0 200']})
