Skip to content

Commit

Permalink
fix: auto-create otp bypass tokens when enabling otp
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Feb 21, 2025
1 parent 2a9e6a7 commit 6312314
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

secrets = set_up_settings(BASE_DIR, service_name="portal")

# pylint: disable-next=wrong-import-position,wildcard-import,unused-wildcard-import
from codeforlife.settings import *

SECRET_KEY = secrets.SECRET_KEY

# ------------------------------------------------------------------------------
# TODO: Clean settings below
# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -80,7 +85,10 @@

if os.environ.get("STATIC_MODE", "") == "pipeline":
STATICFILES_FINDERS = ["pipeline.finders.PipelineFinder"]
STATICFILES_STORAGE = "pipeline.storage.PipelineStorage"
STORAGES = {
**STORAGES, # type: ignore[has-type]
"staticfiles": {"BACKEND": "pipeline.storage.PipelineStorage"},
}

PIPELINE = {
"COMPILERS": ("portal.pipeline_compilers.LibSassCompiler",),
Expand Down Expand Up @@ -251,8 +259,6 @@ def domain():
# TODO: Clean settings above
# ------------------------------------------------------------------------------

# pylint: disable-next=wrong-import-position,wildcard-import,unused-wildcard-import
from codeforlife.settings import *

ROOT_URLCONF = "src.urls"

Expand Down
9 changes: 7 additions & 2 deletions src/api/serializers/auth_factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import re

from codeforlife.serializers import ModelSerializer
from codeforlife.user.models import AuthFactor, User
from codeforlife.user.models import AuthFactor, OtpBypassToken, User
from rest_framework import serializers

# pylint: disable=missing-class-docstring
Expand Down Expand Up @@ -62,4 +62,9 @@ def validate(self, attrs):
def create(self, validated_data):
validated_data["user_id"] = self.request.auth_user.id
validated_data.pop("otp", None)
return super().create(validated_data)
auth_factor = super().create(validated_data)

if auth_factor.type == AuthFactor.Type.OTP:
OtpBypassToken.objects.bulk_create(auth_factor.user)

return auth_factor
9 changes: 8 additions & 1 deletion src/api/serializers/auth_factor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
from unittest.mock import Mock, patch

from codeforlife.tests import ModelSerializerTestCase
from codeforlife.user.models import AuthFactor, TeacherUser, User
from codeforlife.user.models import (
AuthFactor,
OtpBypassToken,
TeacherUser,
User,
)

from .auth_factor import AuthFactorSerializer

Expand Down Expand Up @@ -99,3 +104,5 @@ def test_create__otp(self):
new_data={"user": user.id},
context={"request": self.request_factory.post(user=user)},
)

assert user.otp_bypass_tokens.count() == OtpBypassToken.max_count
1 change: 0 additions & 1 deletion src/api/views/otp_bypass_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def get_permissions(self):
def get_queryset(self):
return OtpBypassToken.objects.filter(user=self.request.auth_user)

# TODO: replace this custom action with bulk create and list serializer.
@action(detail=False, methods=["post"])
def generate(self, request: Request):
"""
Expand Down

0 comments on commit 6312314

Please sign in to comment.