【发布时间】:2020-04-09 03:09:30
【问题描述】:
我正在用 Python 3.7 为我们的 TWiki、Bugzilla 和 SuiteCRM 内部站点编写一个 SSO Web 应用程序。所有 3 个站点都使用 LDAP 进行身份验证,我可以使用 LDAP 中设置的相同凭据通过 Firefox 和 Chrome 直接对所有 3 个站点进行身份验证。
在 Python 中,我可以使用 Requests 库对所有 3 个站点进行身份验证,并将 cookie 传递回浏览器。但是,只有 TWiki 和 Bugzilla 接受从我的 Python 脚本传递给浏览器的 cookie。 SuiteCRM 说我的会话已过期并需要登录。我知道身份验证成功,因为 cookie 在使用正确凭据时从 WSGI 脚本传递到浏览器,而在使用错误凭据时它们不存在。
所有三个站点都在同一个 dev.domain.com 域下,Python 脚本在同一个 dev.domain.com 域下运行。
服务器正在使用 WSGI Apache 模块。该脚本使用 requests 库进行身份验证,将 cookie 保存为变量中的字典,并将字典附加到响应标头,然后将响应返回给浏览器。我在 Centos 7.5 上使用 SuiteCRM 版本 7.8.20、Python 3.7 和 Apache 2.4.6。 SeLinux 在许可模式下运行。我有一个静态 Web 登录页面,它只是将身份验证凭据发送到 sso.py 脚本:
登录.html
<form action="https://dev.domain.com/sso" class="login-form" method="post">
<input name=username type="text" placeholder="username"/>
<input name=password type="password" placeholder="password"/>
<button>login</button>
</form>
sso.py
import requests
import Cookie
...
def auth(url,auth_params,custom_headers):
try:
headers = requests.utils.default_headers()
headers.update(custom_headers)
session = requests.session()
response = session.get(url,headers=headers,params=auth_params,verify=False)
response = session.post(url,headers=headers,params=auth_params,verify=False)
cookies = requests.utils.dict_from_cookiejar(response.cookies)
return cookies
except:
return "failed auth"
# authenticate and save auth cookies
bz_cookies = auth(bz_url,bz_auth_params,bz_custom_headers)
twiki_cookies = auth(twiki_url,twiki_auth_params,twiki_custom_headers)
sugar_cookies = auth(sugar_url,sugar_auth_params,sugar_custom_headers)
...
# iteritems to add bugzilla cookies to response
try:
for key, value in bz_cookies.iteritems():
cookie = key + "=" + value
response_headers.append(('Set-Cookie',cookie))
except:
output = bytes("no bz_cookie")
## iteritems to add twiki cookies to response
try:
for key, value in twiki_cookies.iteritems():
cookie = key + "=" + value
response_headers.append(('Set-Cookie',cookie))
except:
output = bytes("no twiki cookie")
## iteritems to add sugarcrm cookies to response
try:
for key, value in sugar_cookies.iteritems():
cookie = key + "=" + value
response_headers.append(('Set-Cookie',cookie))
except:
output = bytes("no sugar cookie")
...
start_response(status, response_headers)
return [bytes(output),response_headers]
如果需要,我可以提供更多详细信息,但这是我为我们的网络应用构建内部 SSO 所采用的基本方法。
如果有人知道我应该检查什么,请告诉我。我在网上搜索了 SSO 帮助,但我发现的所有内容大多与我的设置无关。
有没有更好的方法将身份验证令牌传递给浏览器,或者这是将这些网站捆绑在一起的唯一方法?
我是在重新发明轮子吗?
感谢您的宝贵时间。
【问题讨论】:
标签: python authentication cookies single-sign-on