允许用户向其帐户添加2fa
django-mfa2的Python项目详细描述
Django-MFA2
一个处理mfa的django应用程序,它支持totp、u2f、fido2u2f(web authn)、电子邮件令牌和可信设备
web认证api(webauthn)是最先进的技术,有望取代密码。
对于fido2,支持以下内容
- 安全密钥(Firefox 60+,Chrome 67+,Edge 18+,
- windows你好(火狐67+,Chrome 72+,edge),
- Apple的Touch ID(Mac OS X上的Chrome 70+),
- android安全网(chrome 70+)
- nfc设备使用pcsc(未测试,但在fido2中受支持)
英文:),它允许您通过电脑、笔记本电脑或手机上的安全密钥、Windows 10 Build 1903+(2019年5月更新)上的Windows Hello(指纹、PIN)和MacBooks(Chrome)上的Touch ID以及Andriod手机上的指纹/人脸/虹膜/PIN验证用户。
可信设备是用户添加不支持安全密钥(如iOS和无指纹或NFC的Andriod)的设备的模式。
注释:U2F and FIDO2 can only be served under secure context (https)
包在Python2.7和Python3.5+上用django 1.8、django 2.1进行了测试,但没有在这两个版本之间进行任何检查,而是打开查看是否有问题。
取决于
- Pyotp
- python-u2flib-server
- UA分析器
- 用户代理
- Python何塞
- fido2==0.7
安装
pip install django-mfa2
在settings.py中将应用程序添加到已安装的应用程序中
INSTALLED_APPS=('......','mfa','......')
收集静态文件
python manage.py collectstatic
将下列设置添加到文件中
MFA_UNALLOWED_METHODS=()# Methods that shouldn't be allowed for the userMFA_LOGIN_CALLBACK=""# A function that should be called by username to login the user in sessionMFA_RECHECK=True# Allow random rechecking of the userMFA_RECHECK_MIN=10# Minimum interval in secondsMFA_RECHECK_MAX=30# Maximum in secondsMFA_QUICKLOGIN=True# Allow quick login for returning users by provide only their 2FAMFA_HIDE_DISABLE=('FIDO2',)# Can the user disable his key (Added in 1.2.0). TOKEN_ISSUER_NAME="PROJECT_NAME"#TOTP Issuer nameU2F_APPID="https://localhost"#URL For U2FFIDO_SERVER_ID=u"localehost"# Server rp id for FIDO2, it the full domain of your projectFIDO_SERVER_NAME=u"PROJECT_NAME"FIDO_LOGIN_URL=BASE_URL
方法名
- U2F
- 氢火焰离子化
- 总计
- 可信设备
- 电子邮件
注意:从1.1版开始,fido2不再需要使用fido登录url。
中断登录功能
通常您的登录功能会检查用户名和密码,如果用户名和密码正确,请登录并创建用户会话,要支持MFA,必须更改此设置
- 对用户进行身份验证
- 如果用户名和密码正确,请检查用户是否有mfa
- 如果用户有MFA,则重定向到MFA页
- 如果用户没有mfa,则调用函数创建用户会话
deflogin(request):# this function handles the login form POSTuser=auth.authenticate(username=username,password=password)ifuserisnotNone:# if the user object existfrommfa.helpersimporthas_mfares=has_mfa(username=username,request=request)# has_mfa returns false or HttpResponseRedirectifres:returnresreturnlog_user_in(request,username=user.username)#log_user_in is a function that handles creatung user session, it should be in the setting file as MFA_CALLBACK
将mfa添加到url.py
importmfaimportmfa.TrustedDeviceurls_patterns=['...',url(r'^mfa/',include('mfa.urls')),url(r'devices/add$',mfa.TrustedDevice.add,name="mfa_add_new_trusted_device"),# This short link to add new trusted device'....',]
在模板中提供名为“head”和“content”的块
mfa_auth_base.html
模板将在用户登录期间包含。 如果要使用email token方法,则必须提供名为mfa_email_token_template.html
的模板,该模板将使用名为user
和otp
的参数来满足电子邮件的格式。要匹配项目的外观,mfa包含
base.html
,但它需要名为head
&;content
的块来向其中添加内容。在应用程序的某个位置,添加指向“mfa_home”的链接
<li><a href="{% url 'mfa_home' %}">Security</a> </li>
例如,请参见autdeploy项目中的https://github.com/mkalioby/AutoDeploy/commit/5f1d94b1804e0aa33c79e9e8530ce849d9eb78cc
无密码
要使返回的用户能够无密码,请创建一个名为“base_username”的cookie,其中包含用户名,如下面的代码片段所示
response=render(request,'Dashboard.html',context))ifrequest.session.get("mfa",{}).get("verified",False)andgetattr(settings,"MFA_QUICKLOGIN",False):ifrequest.session["mfa"]["method"]!="Trusted Device":response.set_cookie("base_username",request.user.username,path="/",max_age=15*24*60*60)returnresponse
其次,更新登录视图的get部分
if"mfa"insettings.INSTALLED_APPSandgetattr(settings,"MFA_QUICKLOGIN",False)andrequest.COOKIES.get('base_username'):username=request.COOKIES.get('base_username')frommfa.helpersimporthas_mfares=has_mfa(username=username,request=request,)ifres:returnres## continue and return the form.
检查客户端的MFA
有时,您希望验证用户是否仍然存在,因此您可以让django-mfa2为您检查它
{% include 'mfa_check.html' %}
functionsuccess_func(){//logic if mfa check succeeds}functionfail_func(){//logic if mfa check fails}functionsome_func(){recheck_mfa(success_func,fail_func,MUST_BE_MFA)//MUST_BE_MFA true or false, if the user must has with MFA}