From 4a5e16aa0c9071163cfb72d7847657cdef3822cd Mon Sep 17 00:00:00 2001 From: Rohan Date: Wed, 11 Dec 2024 18:52:48 +0530 Subject: [PATCH] fix: gracefully handle DoesNotExist exceptions for tokens --- backend/api/utils/rest.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/backend/api/utils/rest.py b/backend/api/utils/rest.py index d133efa51..ddc0b9296 100644 --- a/backend/api/utils/rest.py +++ b/backend/api/utils/rest.py @@ -87,11 +87,22 @@ def token_is_expired_or_deleted(auth_token): prefix, token_type, token_value = auth_token.split(" ") if token_type == "User": - token = UserToken.objects.get(token=token_value) + try: + token = UserToken.objects.get(token=token_value) + except UserToken.DoesNotExist: + return True + elif token_type == "Service": - token = ServiceToken.objects.get(token=token_value) + try: + token = ServiceToken.objects.get(token=token_value) + except ServiceToken.DoesNotExist: + return True + elif token_type == "ServiceAccount": - token = ServiceAccountToken.objects.get(token=token_value) + try: + token = ServiceAccountToken.objects.get(token=token_value) + except ServiceAccountToken.DoesNotExist: + return True return token.deleted_at is not None or ( token.expires_at is not None and token.expires_at < timezone.now()