Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds middleware to enforce 2fa #284

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions two_factor/middleware/enforce2fa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""Middleware to check if staff user has 2FA unabled."""

from django.shortcuts import redirect
from django.urls import resolve, reverse


class Enforce2FAMiddleware(object):
"""Enforce 2FA middleware."""
def __init__(self, get_response):
"""Initialize the middleware."""
self.get_response = get_response

def __call__(self, request):
"""Redirect to two_factor URL if two_factor enforced."""
response = self.get_response(request)
if resolve(request.path).app_name == 'two_factor' or (
resolve(request.path).url_name == 'logout'):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we know the logout URL is called logout?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moggers87 Is there any way of knowing this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I know of.

return response
if not request.user.is_verified():
return redirect(reverse('two_factor:profile'))
return response