Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7800962eb6 | ||
|
|
3ac3e101a3 | ||
|
|
4c31e1815e | ||
|
|
64dafb8d2e | ||
|
|
9086f47456 | ||
|
|
03ab360939 | ||
|
|
3a85efc1e6 | ||
|
|
9555c7820b |
17
CHANGELOG.md
Normal file
17
CHANGELOG.md
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# Change Log
|
||||||
|
|
||||||
|
## v 1.6.0
|
||||||
|
* Fixed some issues for django>= 2.0
|
||||||
|
* Added example app.
|
||||||
|
|
||||||
|
## v.1.5.0
|
||||||
|
* Added id the key used to validate to the session dictionary as 'id'
|
||||||
|
## v1.4.0
|
||||||
|
* Updated to FIDO == 0.7
|
||||||
|
|
||||||
|
## v1.3.0
|
||||||
|
* Updated to FIDO2 == 0.6
|
||||||
|
* Windows Hello is now supported.
|
||||||
|
|
||||||
|
## v1.2.0
|
||||||
|
* Added: MFA_HIDE_DISABLE setting option to disable users from deactivating their keys.
|
||||||
@@ -53,6 +53,7 @@ Depends on
|
|||||||
MFA_RECHECK_MAX=30 # Maximum in seconds
|
MFA_RECHECK_MAX=30 # Maximum in seconds
|
||||||
MFA_QUICKLOGIN=True # Allow quick login for returning users by provide only their 2FA
|
MFA_QUICKLOGIN=True # Allow quick login for returning users by provide only their 2FA
|
||||||
MFA_HIDE_DISABLE=('FIDO2',) # Can the user disable his key (Added in 1.2.0).
|
MFA_HIDE_DISABLE=('FIDO2',) # Can the user disable his key (Added in 1.2.0).
|
||||||
|
MFA_OWNED_BY_ENTERPRISE = FALSE # Who ownes security keys
|
||||||
|
|
||||||
TOKEN_ISSUER_NAME="PROJECT_NAME" #TOTP Issuer name
|
TOKEN_ISSUER_NAME="PROJECT_NAME" #TOTP Issuer name
|
||||||
|
|
||||||
@@ -68,8 +69,9 @@ Depends on
|
|||||||
* Trusted_Devices
|
* Trusted_Devices
|
||||||
* Email
|
* Email
|
||||||
|
|
||||||
**Note**: Starting version 1.1, ~~FIDO_LOGIN_URL~~ isn't required for FIDO2 anymore.
|
**Notes**:
|
||||||
|
* Starting version 1.1, ~~FIDO_LOGIN_URL~~ isn't required for FIDO2 anymore.
|
||||||
|
* Starting version 1.7.0, Key owners can be specified.
|
||||||
1. Break your login function
|
1. Break your login function
|
||||||
|
|
||||||
Usually your login function will check for username and password, log the user in if the username and password are correct and create the user session, to support mfa, this has to change
|
Usually your login function will check for username and password, log the user in if the username and password are correct and create the user session, to support mfa, this has to change
|
||||||
|
|||||||
@@ -127,8 +127,8 @@ LOGIN_URL="/auth/login"
|
|||||||
EMAIL_FROM='Test App'
|
EMAIL_FROM='Test App'
|
||||||
EMAIL_HOST="smtp.gmail.com"
|
EMAIL_HOST="smtp.gmail.com"
|
||||||
EMAIL_PORT=587
|
EMAIL_PORT=587
|
||||||
EMAIL_HOST_USER="mkalioby@gmail.com"
|
EMAIL_HOST_USER=""
|
||||||
EMAIL_HOST_PASSWORD='wanted85'
|
EMAIL_HOST_PASSWORD=''
|
||||||
EMAIL_USE_TLS=True
|
EMAIL_USE_TLS=True
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
# Change Log
|
|
||||||
|
|
||||||
## v.1.5.0
|
|
||||||
* Added id the key used to validate to the session dictionary as 'id'
|
|
||||||
## v1.4.0
|
|
||||||
* Updated to FIDO == 0.7
|
|
||||||
|
|
||||||
## v1.3.0
|
|
||||||
* Updated to FIDO2 == 0.6
|
|
||||||
* Windows Hello is now supported.
|
|
||||||
|
|
||||||
## v1.2.0
|
|
||||||
* Added: MFA_HIDE_DISABLE setting option to disable users from deactivating their keys.
|
|
||||||
41
mfa/FIDO2.py
41
mfa/FIDO2.py
@@ -12,14 +12,15 @@ from django.conf import settings
|
|||||||
from .models import *
|
from .models import *
|
||||||
from fido2.utils import websafe_decode,websafe_encode
|
from fido2.utils import websafe_decode,websafe_encode
|
||||||
from fido2.ctap2 import AttestedCredentialData
|
from fido2.ctap2 import AttestedCredentialData
|
||||||
from .views import login
|
from .views import login,reset_cookie
|
||||||
import datetime
|
import datetime
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
def recheck(request):
|
def recheck(request):
|
||||||
context = csrf(request)
|
context = csrf(request)
|
||||||
context["mode"]="recheck"
|
context["mode"]="recheck"
|
||||||
return request("FIDO2/recheck.html", context)
|
request.session["mfa_recheck"]=True
|
||||||
|
return render(request,"FIDO2/recheck.html", context)
|
||||||
|
|
||||||
|
|
||||||
def getServer():
|
def getServer():
|
||||||
@@ -52,13 +53,16 @@ def complete_reg(request):
|
|||||||
uk=User_Keys()
|
uk=User_Keys()
|
||||||
uk.username = request.user.username
|
uk.username = request.user.username
|
||||||
uk.properties = {"device":encoded,"type":att_obj.fmt,}
|
uk.properties = {"device":encoded,"type":att_obj.fmt,}
|
||||||
|
uk.owned_by_enterprise=getattr(settings,"MFA_OWNED_BY_ENTERPRISE",False)
|
||||||
uk.key_type = "FIDO2"
|
uk.key_type = "FIDO2"
|
||||||
uk.save()
|
uk.save()
|
||||||
return HttpResponse(simplejson.dumps({'status': 'OK'}))
|
return HttpResponse(simplejson.dumps({'status': 'OK'}))
|
||||||
except Exception as exp:
|
except Exception as exp:
|
||||||
|
try:
|
||||||
from raven.contrib.django.raven_compat.models import client
|
from raven.contrib.django.raven_compat.models import client
|
||||||
import traceback
|
|
||||||
client.captureException()
|
client.captureException()
|
||||||
|
except:
|
||||||
|
pass
|
||||||
return HttpResponse(simplejson.dumps({'status': 'ERR',"message":"Error on server, please try again later"}))
|
return HttpResponse(simplejson.dumps({'status': 'ERR',"message":"Error on server, please try again later"}))
|
||||||
def start(request):
|
def start(request):
|
||||||
context = csrf(request)
|
context = csrf(request)
|
||||||
@@ -83,6 +87,7 @@ def authenticate_begin(request):
|
|||||||
|
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
def authenticate_complete(request):
|
def authenticate_complete(request):
|
||||||
|
try:
|
||||||
credentials = []
|
credentials = []
|
||||||
username=request.session.get("base_username",request.user.username)
|
username=request.session.get("base_username",request.user.username)
|
||||||
server=getServer()
|
server=getServer()
|
||||||
@@ -92,7 +97,7 @@ def authenticate_complete(request):
|
|||||||
client_data = ClientData(data['clientDataJSON'])
|
client_data = ClientData(data['clientDataJSON'])
|
||||||
auth_data = AuthenticatorData(data['authenticatorData'])
|
auth_data = AuthenticatorData(data['authenticatorData'])
|
||||||
signature = data['signature']
|
signature = data['signature']
|
||||||
|
try:
|
||||||
cred = server.authenticate_complete(
|
cred = server.authenticate_complete(
|
||||||
request.session.pop('fido_state'),
|
request.session.pop('fido_state'),
|
||||||
credentials,
|
credentials,
|
||||||
@@ -101,8 +106,27 @@ def authenticate_complete(request):
|
|||||||
auth_data,
|
auth_data,
|
||||||
signature
|
signature
|
||||||
)
|
)
|
||||||
keys = User_Keys.objects.filter(username=username, key_type="FIDO2",enabled=1)
|
except ValueError:
|
||||||
|
return HttpResponse(simplejson.dumps({'status': "ERR", "message": "Wrong challenge received, make sure that this is your security and try again."}),
|
||||||
|
content_type = "application/json")
|
||||||
|
except Exception as excep:
|
||||||
|
try:
|
||||||
|
from raven.contrib.django.raven_compat.models import client
|
||||||
|
client.captureException()
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
return HttpResponse(simplejson.dumps({'status': "ERR",
|
||||||
|
"message": excep.message}),
|
||||||
|
content_type = "application/json")
|
||||||
|
|
||||||
|
if request.session.get("mfa_recheck",False):
|
||||||
|
import time
|
||||||
|
request.session["mfa"]["rechecked_at"]=time.time()
|
||||||
|
return HttpResponse(simplejson.dumps({'status': "OK"}),
|
||||||
|
content_type="application/json")
|
||||||
|
else:
|
||||||
import random
|
import random
|
||||||
|
keys = User_Keys.objects.filter(username=username, key_type="FIDO2", enabled=1)
|
||||||
for k in keys:
|
for k in keys:
|
||||||
if AttestedCredentialData(websafe_decode(k.properties["device"])).credential_id == cred.credential_id:
|
if AttestedCredentialData(websafe_decode(k.properties["device"])).credential_id == cred.credential_id:
|
||||||
k.last_used = timezone.now()
|
k.last_used = timezone.now()
|
||||||
@@ -112,6 +136,11 @@ def authenticate_complete(request):
|
|||||||
mfa["next_check"] = int((datetime.datetime.now()+ datetime.timedelta(
|
mfa["next_check"] = int((datetime.datetime.now()+ datetime.timedelta(
|
||||||
seconds=random.randint(settings.MFA_RECHECK_MIN, settings.MFA_RECHECK_MAX))).strftime("%s"))
|
seconds=random.randint(settings.MFA_RECHECK_MIN, settings.MFA_RECHECK_MAX))).strftime("%s"))
|
||||||
request.session["mfa"] = mfa
|
request.session["mfa"] = mfa
|
||||||
|
if not request.user.is_authenticated():
|
||||||
res=login(request)
|
res=login(request)
|
||||||
|
if not "location" in res: return reset_cookie(request)
|
||||||
return HttpResponse(simplejson.dumps({'status':"OK","redirect":res["location"]}),content_type="application/json")
|
return HttpResponse(simplejson.dumps({'status':"OK","redirect":res["location"]}),content_type="application/json")
|
||||||
return HttpResponse(simplejson.dumps({'status': "err"}),content_type="application/json")
|
return HttpResponse(simplejson.dumps({'status': "OK"}),
|
||||||
|
content_type = "application/json")
|
||||||
|
except Exception as exp:
|
||||||
|
return HttpResponse(simplejson.dumps({'status': "ERR","message":exp.message}),content_type="application/json")
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ from django.conf import settings
|
|||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from .models import *
|
from .models import *
|
||||||
from .views import login
|
from .views import login
|
||||||
|
import datetime
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
def recheck(request):
|
def recheck(request):
|
||||||
@@ -26,6 +27,8 @@ def recheck(request):
|
|||||||
def process_recheck(request):
|
def process_recheck(request):
|
||||||
x=validate(request,request.user.username)
|
x=validate(request,request.user.username)
|
||||||
if x==True:
|
if x==True:
|
||||||
|
import time
|
||||||
|
request.session["mfa"]["rechecked_at"] = time.time()
|
||||||
return HttpResponse(simplejson.dumps({"recheck":True}),content_type="application/json")
|
return HttpResponse(simplejson.dumps({"recheck":True}),content_type="application/json")
|
||||||
return x
|
return x
|
||||||
|
|
||||||
@@ -89,6 +92,7 @@ def bind(request):
|
|||||||
User_Keys.objects.filter(username=request.user.username,key_type="U2F").delete()
|
User_Keys.objects.filter(username=request.user.username,key_type="U2F").delete()
|
||||||
uk = User_Keys()
|
uk = User_Keys()
|
||||||
uk.username = request.user.username
|
uk.username = request.user.username
|
||||||
|
uk.owned_by_enterprise = getattr(settings, "MFA_OWNED_BY_ENTERPRISE", False)
|
||||||
uk.properties = {"device":simplejson.loads(device.json),"cert":cert_hash}
|
uk.properties = {"device":simplejson.loads(device.json),"cert":cert_hash}
|
||||||
uk.key_type = "U2F"
|
uk.key_type = "U2F"
|
||||||
uk.save()
|
uk.save()
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
__version__="1.5.0"
|
__version__="1.6.0"
|
||||||
|
|||||||
20
mfa/migrations/0009_user_keys_owned_by_enterprise.py
Normal file
20
mfa/migrations/0009_user_keys_owned_by_enterprise.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import models, migrations
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('mfa', '0008_user_keys_last_used'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='user_keys',
|
||||||
|
name='owned_by_enterprise',
|
||||||
|
field=models.NullBooleanField(default=None),
|
||||||
|
),
|
||||||
|
migrations.RunSQL("update mfa_user_keys set owned_by_enterprise = %s where key_type='FIDO2'"%(1 if getattr(settings,"MFA_OWNED_BY_ENTERPRISE",False) else 0 ))
|
||||||
|
]
|
||||||
@@ -13,6 +13,8 @@ class User_Keys(models.Model):
|
|||||||
enabled=models.BooleanField(default=True)
|
enabled=models.BooleanField(default=True)
|
||||||
expires=models.DateTimeField(null=True,default=None,blank=True)
|
expires=models.DateTimeField(null=True,default=None,blank=True)
|
||||||
last_used=models.DateTimeField(null=True,default=None,blank=True)
|
last_used=models.DateTimeField(null=True,default=None,blank=True)
|
||||||
|
owned_by_enterprise=models.NullBooleanField(default=None,null=True,blank=True)
|
||||||
|
|
||||||
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
|
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
|
||||||
if self.key_type == "Trusted Device" and self.properties.get("signature","") == "":
|
if self.key_type == "Trusted Device" and self.properties.get("signature","") == "":
|
||||||
self.properties["signature"]= jwt.encode({"username": self.username, "key": self.properties["key"]}, settings.SECRET_KEY)
|
self.properties["signature"]= jwt.encode({"username": self.username, "key": self.properties["key"]}, settings.SECRET_KEY)
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
{% extends "mfa_auth_base.html" %}
|
{% extends "mfa_auth_base.html" %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<br/>
|
|
||||||
<br/>
|
|
||||||
{% include 'FIDO2/recheck.html' with mode='auth' %}
|
{% include 'FIDO2/recheck.html' with mode='auth' %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
@@ -18,10 +18,11 @@
|
|||||||
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<p style="color: green">please press the button on your security key to prove it is you.</p>
|
<p style="color: green">please press the button on your security key to prove it is you.</p>
|
||||||
|
<div id="msgdiv"></div>
|
||||||
{% if mode == "auth" %}
|
{% if mode == "auth" %}
|
||||||
<form id="u2f_login" action="{% url 'fido2_complete_auth' %}" method="post" enctype="multipart/form-data">
|
<form id="u2f_login" action="{% url 'fido2_complete_auth' %}" method="post" enctype="multipart/form-data">
|
||||||
{% elif mode == "recheck" %}
|
{% elif mode == "recheck" %}
|
||||||
<form id="u2f_login" action="{% url 'u2f_recheck' %}" method="post">
|
<form id="u2f_login" action="{% url 'fido2_recheck' %}" method="post" enctype="multipart/form-data">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<input type="hidden" name="response" id="response" value=""/>
|
<input type="hidden" name="response" id="response" value=""/>
|
||||||
@@ -71,6 +72,8 @@
|
|||||||
}).then(function (response) {if (response.ok) return res = response.json()}).then(function (res) {
|
}).then(function (response) {if (response.ok) return res = response.json()}).then(function (res) {
|
||||||
if (res.status=="OK")
|
if (res.status=="OK")
|
||||||
{
|
{
|
||||||
|
$("#msgdiv").addClass("alert alert-success").removeClass("alert-danger")
|
||||||
|
$("#msgdiv").html("Verified....please wait")
|
||||||
{% if mode == "auth" %}
|
{% if mode == "auth" %}
|
||||||
window.location.href=res.redirect;
|
window.location.href=res.redirect;
|
||||||
{% elif mode == "recheck" %}
|
{% elif mode == "recheck" %}
|
||||||
@@ -78,10 +81,13 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
$("#msgdiv").addClass("alert alert-danger").removeClass("alert-success")
|
||||||
|
$("#msgdiv").html("Verification Failed as " + res.message + ", <a href='javascript:void(0)' onclick='authen())'> try again</a> or <a href='javascript:void(0)' onclick='history.back()'> Go Back</a>")
|
||||||
|
|
||||||
{% if mode == "auth" %}
|
{% if mode == "auth" %}
|
||||||
alert("Error occured, please try again")
|
|
||||||
login()
|
|
||||||
{% elif mode == "recheck" %}
|
{% elif mode == "recheck" %}
|
||||||
|
|
||||||
mfa_failed_function();
|
mfa_failed_function();
|
||||||
{% endif %}
|
{% endif %}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ def recheck(request):
|
|||||||
context["mode"]="recheck"
|
context["mode"]="recheck"
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
if verify_login(request,request.user.username, token=request.POST["otp"]):
|
if verify_login(request,request.user.username, token=request.POST["otp"]):
|
||||||
|
import time
|
||||||
|
request.session["mfa"]["rechecked_at"] = time.time()
|
||||||
return HttpResponse(simplejson.dumps({"recheck": True}), content_type="application/json")
|
return HttpResponse(simplejson.dumps({"recheck": True}), content_type="application/json")
|
||||||
else:
|
else:
|
||||||
return HttpResponse(simplejson.dumps({"recheck": False}), content_type="application/json")
|
return HttpResponse(simplejson.dumps({"recheck": False}), content_type="application/json")
|
||||||
|
|||||||
@@ -27,10 +27,7 @@ urlpatterns = [
|
|||||||
url(r'fido2/complete_auth', FIDO2.authenticate_complete, name="fido2_complete_auth"),
|
url(r'fido2/complete_auth', FIDO2.authenticate_complete, name="fido2_complete_auth"),
|
||||||
url(r'fido2/begin_reg', FIDO2.begin_registeration, name="fido2_begin_reg"),
|
url(r'fido2/begin_reg', FIDO2.begin_registeration, name="fido2_begin_reg"),
|
||||||
url(r'fido2/complete_reg', FIDO2.complete_reg, name="fido2_complete_reg"),
|
url(r'fido2/complete_reg', FIDO2.complete_reg, name="fido2_complete_reg"),
|
||||||
url(r'u2f/bind', U2F.bind, name="bind_u2f"),
|
url(r'fido2/recheck', FIDO2.recheck, name="fido2_recheck"),
|
||||||
url(r'u2f/auth', U2F.auth, name="u2f_auth"),
|
|
||||||
url(r'u2f/process_recheck', U2F.process_recheck, name="u2f_recheck"),
|
|
||||||
url(r'u2f/verify', U2F.verify, name="u2f_verify"),
|
|
||||||
|
|
||||||
|
|
||||||
url(r'td/$', TrustedDevice.start, name="start_td"),
|
url(r'td/$', TrustedDevice.start, name="start_td"),
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -4,7 +4,7 @@ from setuptools import find_packages, setup
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='django-mfa2',
|
name='django-mfa2',
|
||||||
version='1.5.0',
|
version='1.7.11',
|
||||||
description='Allows user to add 2FA to their accounts',
|
description='Allows user to add 2FA to their accounts',
|
||||||
long_description=open("README.md").read(),
|
long_description=open("README.md").read(),
|
||||||
long_description_content_type="text/markdown",
|
long_description_content_type="text/markdown",
|
||||||
|
|||||||
Reference in New Issue
Block a user