django sso提供程序应用程序
dj-sso-server的Python项目详细描述
dj-sso-server是一个django应用程序,它为您的项目提供单点登录功能。
dj-sso-server应用程序用作SSO提供程序,您可以在其他需要SSO的项目中使用dj-sso-client(https://github.com/feifangit/dj-sso-client)作为SSO客户端
工作原理
基于dj-api-auth模块,我们可以创建一个最初包含sso相关api的api密钥。dj-sso-server和dj-sso-client之间的所有api通信都受dj-api-auth
保护。api密钥还将与host绑定,后者用于限制sso请求的来源。
使用dj-sso-clientSSO工作流
Firstly, ^{tt3}$ applies a request key via API ^{tt12}$ on ^{tt1}$
The request key in ^{tt1}$ side will be kept in cache for 5 minutes, so the whole SSO login process should be done in 5 minutes.
With the request key, ^{tt3}$ redirects user to SSO login page on SSO provider, and get auth token if login success. ^{tt1}$ will
- verify the request origin
- verify request key validity (expired?)
- save user information in cache
dj-sso-client通过apiauthtoken/使用dj-sso-server验证auth令牌,并获取SSOUser对象。
dj-sso-server调用authtoken/后,从缓存中删除请求键。
如果在dj-sso-server上有一个已登录的帐户(例如,放置sso提供程序的项目也提供了其他功能,并且浏览器端有一个有效的cookies,服务器端有一个有效的会话),则用户可以选择继续使用该已登录的帐户。
通过dj-sso-server进行SSO登录,不影响dj-sso-server上的登录状态。
注意
因为请求键存储在缓存中,等待验证或过期。如果在部署中运行多个应用程序进程(Gunicorn等),请使用可以在进程之间共享的适当缓存系统。
memcached和redis都非常适合缓存,请注意,本地内存缓存(django.core.cache.backends.locmem.LocMemCache)是本地调试的玩具。
将dj-sso-server添加到项目
- 将djapiauth和djssoserver添加到sttings.py中的INSTALLED_APPS中
- 为模块分配URL
# add auth for a browser-oriented viewurl(r'^sso/',include("djssoserver.urls"))#...
设置
- sso_server_user_model_to_dict_cls
- 可选,json编码器类的路径
- 默认编码器处理datetime、date、time和decimal.Decimal
# DjangoJSONEncoder from Django 1.7 source codeclassDjangoJSONEncoder(json.JSONEncoder):""" JSONEncoder subclass that knows how to encode date/time and decimal types. """defdefault(self,o):# See "Date Time String Format" in the ECMA-262 specification.ifisinstance(o,datetime.datetime):r=o.isoformat()ifo.microsecond:r=r[:23]+r[26:]ifr.endswith('+00:00'):r=r[:-6]+'Z'returnrelifisinstance(o,datetime.date):returno.isoformat()elifisinstance(o,datetime.time):ifis_aware(o):raiseValueError("JSON can't represent timezone-aware times.")r=o.isoformat()ifo.microsecond:r=r[:12]returnrelifisinstance(o,decimal.Decimal):returnstr(o)else:returnsuper(DjangoJSONEncoder,self).default(o)
扫描API
为了发现和管理api,在可访问的urls.py中添加dj-sso-server之后,运行命令python manage.py reloadentrypoints将api收集到数据库。
为SSO创建API密钥
- 从您的管理站点,在Single sign-on/SSO credential创建一个api密钥。所有与SSO相关的API将自动分配给此API密钥。
- 准备好SSO的API密钥后,您可以在管理站点的API Auth/Credential为该API密钥分配更多的API
自定义SSO登录页
您可以将样式添加到自己的SSO登录页。只需在templates文件夹下创建djsso/ssologin.html。通过模仿 original page
ssouser对象
dj-sso-client获取一个SSOUser对象,无论sso提供程序项目中使用的用户模型是什么。
详见dj-sso-client(https://github.com/feifangit/dj-sso-client)自述文件
演示
我们在Heroku(https://dj-sso-sample.herokuapp.com/)上运行了一个SSO提供程序应用程序
源代码:在example文件夹下
要尝试演示,请查看dj-sso-client(https://github.com/feifangit/dj-sso-client)的自述文件