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

2fa flow #9

Merged
merged 11 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
11 changes: 9 additions & 2 deletions backend/api/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ def test_post__otp(self):

assert response.status_code == 200
self.assertDictEqual(
response.json(), {"auth_factors": [AuthFactor.Type.OTP]}
response.json(),
{
"auth_factors": [AuthFactor.Type.OTP],
"otp_bypass_token_exists": False,
},
)

self.user.userprofile.otp_secret = pyotp.random_base32()
Expand All @@ -45,7 +49,10 @@ def test_post__otp(self):
)

assert response.status_code == 200
self.assertDictEqual(response.json(), {"auth_factors": []})
self.assertDictEqual(
response.json(),
{"auth_factors": []},
)


class TestClearExpiredView(CronTestCase):
Expand Down
22 changes: 14 additions & 8 deletions backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from codeforlife.mixins import CronMixin
from codeforlife.request import HttpRequest
from codeforlife.user.models import AuthFactor
from common.models import UserSession
from django.contrib.auth import login
from django.contrib.auth.views import LoginView as _LoginView
Expand Down Expand Up @@ -58,15 +59,20 @@ def form_valid(self, form: BaseAuthForm):
# Save session (with data).
self.request.session.save()

return JsonResponse(
{
"auth_factors": list(
self.request.user.session.session_auth_factors.values_list(
"auth_factor__type", flat=True
)
response_data = {
"auth_factors": list(
self.request.user.session.session_auth_factors.values_list(
"auth_factor__type", flat=True
)
}
)
)
}

if AuthFactor.Type.OTP in response_data["auth_factors"]:
response_data[
"otp_bypass_token_exists"
] = self.request.user.otp_bypass_tokens.exists()

return JsonResponse(response_data)

def form_invalid(self, form: BaseAuthForm):
return JsonResponse(form.errors, status=status.HTTP_400_BAD_REQUEST)
Expand Down