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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This package still supports Python 2.7 for Django 1.11, so that should be a subclass of object.

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

def __call__(self, request):
"""Wrap around actual Admin calls."""
Copy link
Collaborator

Choose a reason for hiding this comment

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

This code won't limit itself to just the admin site as it makes no such check. Either code needs to do what the comment says or the comment needs changing.

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