Skip to content

Commit bc3d90a

Browse files
author
Michał Kopydłowski
committed
WIP.
1 parent 5ee1d0c commit bc3d90a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

trench/decorators.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from django.contrib.auth import REDIRECT_FIELD_NAME
2+
from django.contrib.auth.decorators import user_passes_test
3+
4+
from trench.command.authenticate_second_factor import authenticate_second_step_command
5+
6+
7+
def mfa_login_required(
8+
function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None
9+
):
10+
"""
11+
Decorator for views that checks that the user is logged in, redirecting
12+
to the log-in page if necessary.
13+
"""
14+
15+
def test(user):
16+
# return user.is_verified() or (user.is_authenticated and not user_has_device(user))
17+
return authenticate_second_step_command
18+
19+
actual_decorator = user_passes_test(
20+
lambda u: u.is_authenticated,
21+
# test,
22+
login_url=login_url,
23+
redirect_field_name=redirect_field_name,
24+
)
25+
if function:
26+
return actual_decorator(function)
27+
return actual_decorator

trench/views/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.contrib.auth.decorators import login_required
12
from django.contrib.auth.models import User
23
from django.db.models import QuerySet
34
from django.utils.translation import gettext_lazy as _
@@ -24,6 +25,7 @@
2425
regenerate_backup_codes_for_mfa_method_command,
2526
)
2627
from trench.command.set_primary_mfa_method import set_primary_mfa_method_command
28+
from trench.decorators import mfa_login_required
2729
from trench.exceptions import MFAMethodDoesNotExistError, MFAValidationError
2830
from trench.query.get_mfa_config_by_name import get_mfa_config_by_name_query
2931
from trench.responses import ErrorResponse
@@ -210,6 +212,7 @@ class MFAMethodRequestCodeView(APIView):
210212
permission_classes = (IsAuthenticated,)
211213

212214
@staticmethod
215+
@login_required
213216
def post(request: Request) -> Response:
214217
serializer = MFAMethodCodeSerializer(data=request.data)
215218
serializer.is_valid(raise_exception=True)

0 commit comments

Comments
 (0)