FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
Commit f4c77c1a authored by Robin Goodall's avatar Robin Goodall :speech_balloon:
Browse files

feat: allow creation fake tokens in test/demo environments

parent 2faefd27
No related branches found
No related tags found
1 merge request!87feat: allow creation fake tokens in test/demo environments
Pipeline #686285 passed
...@@ -19,6 +19,7 @@ DATABASES = { ...@@ -19,6 +19,7 @@ DATABASES = {
DATA_MANAGER_ENABLED = False DATA_MANAGER_ENABLED = False
DATA_MANAGER_READ_ONLY = True DATA_MANAGER_READ_ONLY = True
FAKE_RESET_TOKEN_IF_MISSING = False
# If the EXTRA_SETTINGS_URLS environment variable is set, it is a comma-separated list of URLs from # If the EXTRA_SETTINGS_URLS environment variable is set, it is a comma-separated list of URLs from
# which to fetch additional settings as YAML-formatted documents. The documents should be # which to fetch additional settings as YAML-formatted documents. The documents should be
...@@ -44,6 +45,7 @@ externalsettings.load_external_settings( ...@@ -44,6 +45,7 @@ externalsettings.load_external_settings(
"EMAIL_PORT", "EMAIL_PORT",
"DATA_MANAGER_ENABLED", "DATA_MANAGER_ENABLED",
"DATA_MANAGER_READ_ONLY", "DATA_MANAGER_READ_ONLY",
"FAKE_RESET_TOKEN_IF_MISSING",
], ],
) )
......
...@@ -3,6 +3,10 @@ Views implementing the API endpoints. ...@@ -3,6 +3,10 @@ Views implementing the API endpoints.
""" """
import random
from string import ascii_uppercase, digits
from django.conf import settings
from drf_spectacular.utils import OpenApiResponse, extend_schema from drf_spectacular.utils import OpenApiResponse, extend_schema
from rest_framework import exceptions, generics, status from rest_framework import exceptions, generics, status
...@@ -75,6 +79,16 @@ class ResetTokenView(generics.RetrieveAPIView): ...@@ -75,6 +79,16 @@ class ResetTokenView(generics.RetrieveAPIView):
try: try:
return get_reset_token(self.request.user.crsid) return get_reset_token(self.request.user.crsid)
except PasswordAppNotFound: except PasswordAppNotFound:
# To aid demo/testing purposes, we can fake a reset token if the Password App can't
# find the user
if settings.FAKE_RESET_TOKEN_IF_MISSING:
return "-".join(
[
"".join([random.choice(digits + ascii_uppercase) for _ in range(4)])
for _ in range(3)
]
+ ["FAKE"] # Help developers identify fake tokens
)
# Raising a validation error here rather than a 404 which could be misunderstand as # Raising a validation error here rather than a 404 which could be misunderstand as
# the endpoint not existing # the endpoint not existing
raise exceptions.ValidationError({"crsid": "Password App was unable to find the user"}) raise exceptions.ValidationError({"crsid": "Password App was unable to find the user"})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment