Python 请求 - SAML 登录重定向

标签 python python-requests saml

我正在尝试从此 URL 登录网站:“https://pollev.com/login ”。由于我使用的是学校电子邮件,因此门户会重定向到学校的登录门户,并使用该门户来验证登录。当您输入 uw.edu 电子邮件(例如:myname@uw.edu)时,它就会显示。登录后,UW向https://www.polleverywhere.com/auth/washington/callback发送POST请求回调带有 SAMLResponse header like this 。我想我需要模拟来自 pollev 登录页面的 GET 请求,然后将登录 header 发送到 UW 登录页面,但我现在所做的不起作用。

这是我的代码:

import requests

with requests.session() as s:
     header_data = {
    'user - agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
                    '(KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
    'referer': 'https://pollev.com/login'
    }
    login_data = {
    'j_username' : 'username',
    'j_password' : 'password',
    '_eventId_proceed' : 'Sign in'
    }

    r = s.get('https://idp.u.washington.edu/idp/profile/SAML2/Redirect/SSO?execution=e2s1',
          headers=header_data, data=login_data)
    print(r.text)

现在,r.text 显示一个 NoSuchFlowExecutionException html 页面。我缺少什么?登录网站通常需要登录名、密码、Referrer 和 X-CSRF token ,我可以做到这一点,但我不知道如何导航重定向以进行身份​​验证。

最佳答案

老问题,但我有几乎相同的需求,并继续直到我解决它。就我而言,可能仍然是OP的情况,我拥有所需的凭据。我确信这可以变得更加高效/Pythonic,并且非常感谢这些提示/更正。

import re
import requests

# start HTTP request session
s = requests.Session()

# Prepare for first request - This is the ultimate target URL
url1 = '/URL/needing/shibbolethSAML/authentication'
header_data = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'}

# Make first request
r1 = s.get(url1, headers = header_data)

# Prepare for second request - extract URL action for next POST from response, append header, and add login credentials
ss1 = re.search('action="', r1.text)
ss2 = re.search('" autocomplete', r1.text)
url2 = 'https://idp.u.washington.edu' + r1.text[ss1.span(0)[1]:ss2.span(0)[0]]
header_data.update({'Accept-Encoding': 'gzip, deflate, br', 'Content-Type': 'application/x-www-form-urlencoded'})
cred = {'j_username': 'username', 'j_password':'password', '_eventId_proceed' : 'Sign in'}

# Make second request
r2 = s.post(url2, data = cred)

# Prepare for third request - format and extract URL, RelayState, and SAMLResponse
ss3 = re.search('<form action="',r2.text) # expect only one instance of this pattern in string
ss4 = re.search('" method="post">',r2.text) # expect only one instance of this pattern in string
url3 = r2.text[ss3.span(0)[1]:ss4.span(0)[0]].replace('&#x3a;',':').replace('&#x2f;','/')

ss4 = re.search('name="RelayState" value="', r2.text) # expect only one instance of this pattern in string
ss5 = re.search('"/>', r2.text)
relaystate_value = r2.text[ss4.span(0)[1]:ss5.span(0)[0]].replace('&#x3a;',':')

ss6 = re.search('name="SAMLResponse" value="', r2.text)
ss7 = [m.span for m in re.finditer('"/>',r2.text)] # expect multiple matches with the second match being desired
saml_value = r2.text[ss6.span(0)[1]:ss7[1](0)[0]]

data = {'RelayState': relaystate_value, 'SAMLResponse': [saml_value, 'Continue']}
header_data.update({'Host': 'training.ehs.washington.edu', 'Referer': 'https://idp.u.washington.edu/', 'Connection': 'keep-alive'})

# Make third request
r3 = s.post(url3, headers=header_data, data = data)

# You should now be at the intended URL

关于Python 请求 - SAML 登录重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52618451/

相关文章:

python - 从 Visual Studio Code 调试 Python - 导入 Numpy

python - sympy 符号矩阵平方根

single-sign-on - OpenID 不是 SSO 机制吗?

python - 如何计算代码中关键字的出现次数,但忽略注释/文档字符串中的关键字?

python - HTTP API 的嵌套 GET 参数

python - 请求中的数据和参数有什么区别?

python 请求 SSL 错误(证书验证失败)

saml - 在公共(public) URL 上提供 SAML 元数据的安全问题

amazon-cognito - AWS Cognito - 从 ADFS 创建组作为 Cognito 组

python - python re模块中split函数的空匹配