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

fix: include user id in session cookie #10

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 16 additions & 10 deletions backend/api/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json
import typing as t
from unittest.mock import patch

import pyotp
Expand All @@ -14,14 +16,14 @@ class TestLoginView(TestCase):
def setUp(self):
self.user = User.objects.get(id=2)

def _get_session_auth_factors(self, response: HttpResponse):
return [
auth_factor
for auth_factor in response.cookies[
"sessionid_httponly_false"
].value.split(",")
if auth_factor != ""
]
def _get_session(self, response: HttpResponse):
class Session(t.NamedTuple):
user_id: int
auth_factors: t.List[str]

return Session(
**json.loads(response.cookies["sessionid_httponly_false"].value)
)

def test_post__otp(self):
AuthFactor.objects.create(
Expand All @@ -38,7 +40,9 @@ def test_post__otp(self):
)

assert response.status_code == 200
assert self._get_session_auth_factors(response) == [AuthFactor.Type.OTP]
session = self._get_session(response)
assert session.user_id == self.user.id
assert session.auth_factors == [AuthFactor.Type.OTP]

self.user.userprofile.otp_secret = pyotp.random_base32()
self.user.userprofile.save()
Expand All @@ -53,7 +57,9 @@ def test_post__otp(self):
)

assert response.status_code == 200
assert self._get_session_auth_factors(response) == []
session = self._get_session(response)
assert session.user_id == self.user.id
assert session.auth_factors == []


class TestClearExpiredView(CronTestCase):
Expand Down
21 changes: 14 additions & 7 deletions backend/api/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging

from codeforlife.mixins import CronMixin
Expand Down Expand Up @@ -25,7 +26,6 @@
from .permissions import UserHasSessionAuthFactors


# TODO: add 2FA logic
class LoginView(_LoginView):
request: HttpRequest

Expand All @@ -48,6 +48,7 @@ def form_valid(self, form: BaseAuthForm):

# Create session (without data).
login(self.request, form.user)
user = self.request.user

# TODO: use google analytics
user_session = {"user": form.user}
Expand All @@ -61,15 +62,21 @@ def form_valid(self, form: BaseAuthForm):
# Save session (with data).
self.request.session.save()

response = HttpResponse()

# Create a non-HTTP-only session cookie with the pending auth factors.
response = HttpResponse()
response.set_cookie(
key="sessionid_httponly_false",
value=",".join(
self.request.user.session.session_auth_factors.values_list(
"auth_factor__type", flat=True
)
value=json.dumps(
{
"user_id": user.id,
"auth_factors": list(
user.session.session_auth_factors.values_list(
"auth_factor__type", flat=True
)
),
},
separators=(",", ":"),
indent=None,
),
max_age=(
None
Expand Down