|
| 1 | +import requests |
| 2 | +from bs4 import BeautifulSoup |
| 3 | + |
| 4 | + |
| 5 | +class AdfsLoginFailed(Exception): |
| 6 | + pass |
| 7 | + |
| 8 | + |
| 9 | +def adfslogin(email, password): |
| 10 | + s = requests.Session() |
| 11 | + |
| 12 | + # First, request SMS to be redirected to ADFS |
| 13 | + r = s.get('https://sms.eursc.eu/sso.php') |
| 14 | + |
| 15 | + # Second, log in to ADFS |
| 16 | + # Find the URL to continue the flow |
| 17 | + soup = BeautifulSoup(r.text, "html.parser") |
| 18 | + form = soup.select('#loginForm') |
| 19 | + form_url = 'https://sts.eursc.eu' + form[0]['action'] |
| 20 | + |
| 21 | + payload = {'AuthMethod': 'FormsAuthentication', 'UserName': email, 'Password': password} |
| 22 | + |
| 23 | + r2 = s.post(form_url, data=payload) |
| 24 | + |
| 25 | + # Third, follow ADFS's form to be redirected back to SMS |
| 26 | + soup2 = BeautifulSoup(r2.text, "html.parser") |
| 27 | + form2 = soup2.select('form') |
| 28 | + form_url2 = form2[0]['action'] |
| 29 | + |
| 30 | + # ADFS's intermediary form has a bunch of inputs we need to finish the SAML flow |
| 31 | + payload2 = {} |
| 32 | + |
| 33 | + try: |
| 34 | + |
| 35 | + for input in soup2.select('input[type=\'hidden\']'): |
| 36 | + payload2[input['name']] = input['value'] |
| 37 | + |
| 38 | + r3 = s.post(form_url2, data=payload2) |
| 39 | + |
| 40 | + # Epic success! |
| 41 | + return s.cookies['PHPSESSID'] |
| 42 | + except KeyError as e: |
| 43 | + raise AdfsLoginFailed(e) |
0 commit comments