Skip to content

Commit e809b0b

Browse files
committed
docs: add Python demo
1 parent 77116bb commit e809b0b

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

demo/demo.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env -S uv run
2+
3+
# /// script
4+
# dependencies = [
5+
# "beautifulsoup4",
6+
# "requests",
7+
# ]
8+
# ///
9+
10+
import os
11+
import re
12+
import sys
13+
14+
import requests
15+
import bs4
16+
17+
session = requests.Session()
18+
19+
VERIFY_TLS_CERTS = os.getenv("VERIFY_TLS_CERTS") != "false"
20+
21+
url, front_entity_id, api_key, andrvotr_authority_token = sys.argv[1:]
22+
post_data = None
23+
24+
while True:
25+
print("Requesting", ("POST" if post_data else "GET"), url, *(["with", list(post_data)] if post_data else []))
26+
27+
if re.match(r'^https://[^/]+/idp/profile/', url):
28+
idp_host = url.split("/")[2]
29+
assert post_data is None, post_data
30+
assert url.startswith("https://" + idp_host + '/idp/profile/SAML2/Redirect/SSO?'), url
31+
post_data = { 'front_entity_id': front_entity_id, 'api_key': api_key, 'andrvotr_authority_token': andrvotr_authority_token, 'target_url': url }
32+
url = "https://" + idp_host + "/idp/profile/andrvotr/fabricate"
33+
print("Nevermind, requesting POST", url, "with", list(post_data))
34+
35+
response = session.request("POST" if post_data else "GET", url, data=post_data, verify=VERIFY_TLS_CERTS, allow_redirects=False)
36+
37+
print("Received", response.status_code, response.reason)
38+
print()
39+
40+
if 300 <= response.status_code <= 399 and 'location' in response.headers:
41+
url = response.headers['location']
42+
post_data = None
43+
continue
44+
45+
if response.status_code == 200 and b"document.forms[0].submit()" in response.content:
46+
soup = bs4.BeautifulSoup(response.text)
47+
url = soup.form['action']
48+
post_data = { input['name']: input['value'] for input in soup.find_all('input') if input['type'] == 'hidden' }
49+
continue
50+
51+
print("Final headers:", response.headers)
52+
sys.stdout.buffer.write(b"Final content: [" + response.content + b"]\n")
53+
break

0 commit comments

Comments
 (0)