# Path: django_mfa/models.py
# def is_mfa_enabled(user):
#     """
#     Determine if a user has MFA enabled
#     """
#     return hasattr(user, 'userotp')
# 
# def is_u2f_enabled(user):
#     """
#     Determine if a user has U2F enabled
#     """
#     return user.u2f_keys.all().exists()
# 
# Path: django_mfa/views.py
# def verify_rmb_cookie(request):
#     try:
#         remember_my_browser = settings.MFA_REMEMBER_MY_BROWSER
#         max_cookie_age = settings.MFA_REMEMBER_DAYS * 24 * 3600
#     except:
#         return False
#     if not remember_my_browser:
#         return False
# 
#     cookie_name = MFA_COOKIE_PREFIX + str(request.user.pk)
#     cookie_salt = _generate_cookie_salt(request.user)
#     if not cookie_salt:
#         return False
#     cookie_value = request.get_signed_cookie(
#         cookie_name, False, max_age=max_cookie_age, salt=cookie_salt)
#     # if the cookie value is True and the signature is good than the browser can be trusted
#     return cookie_value


from django.urls import reverse
from django.shortcuts import resolve_url
from django.contrib.auth import REDIRECT_FIELD_NAME as redirect_field_name
from .models import is_mfa_enabled, is_u2f_enabled
from .views import verify_rmb_cookie
    from django.utils.deprecation import MiddlewareMixin
                    from django.contrib.auth.views import redirect_to_login

try:
except ImportError:  # Django < 1.10
    # Works perfectly for everyone using MIDDLEWARE_CLASSES
    MiddlewareMixin = object


class MfaMiddleware(MiddlewareMixin):

    def process_request(self, request):

# Path: django_mfa/models.py
# def is_mfa_enabled(user):
#     """
#     Determine if a user has MFA enabled
#     """
#     return hasattr(user, 'userotp')
# 
# def is_u2f_enabled(user):
#     """
#     Determine if a user has U2F enabled
#     """
#     return user.u2f_keys.all().exists()
# 
# Path: django_mfa/views.py
# def verify_rmb_cookie(request):
#     try:
#         remember_my_browser = settings.MFA_REMEMBER_MY_BROWSER
#         max_cookie_age = settings.MFA_REMEMBER_DAYS * 24 * 3600
#     except:
#         return False
#     if not remember_my_browser:
#         return False
# 
#     cookie_name = MFA_COOKIE_PREFIX + str(request.user.pk)
#     cookie_salt = _generate_cookie_salt(request.user)
#     if not cookie_salt:
#         return False
#     cookie_value = request.get_signed_cookie(
#         cookie_name, False, max_age=max_cookie_age, salt=cookie_salt)
#     # if the cookie value is True and the signature is good than the browser can be trusted
#     return cookie_value


# Path: django_mfa/middleware.py
from django.urls import reverse
from django.shortcuts import resolve_url
from django.contrib.auth import REDIRECT_FIELD_NAME as redirect_field_name
from .models import is_mfa_enabled, is_u2f_enabled
from .views import verify_rmb_cookie
    from django.utils.deprecation import MiddlewareMixin
                    from django.contrib.auth.views import redirect_to_login


try:
except ImportError:  # Django < 1.10
    # Works perfectly for everyone using MIDDLEWARE_CLASSES
    MiddlewareMixin = object


class MfaMiddleware(MiddlewareMixin):

    def process_request(self, request):

        if request.user.is_authenticated and ((is_mfa_enabled(request.user) and not verify_rmb_cookie(request)) or is_u2f_enabled(request.user)):