From c07479256c7ce8b559100e56b6b5106fc485a54f Mon Sep 17 00:00:00 2001 From: Szabo Zoltan Date: Tue, 13 Oct 2026 10:26:51 +0200 Subject: [PATCH] Return an accurate Account Deactivated error when user is deactivated --- CHANGELOG.md | 3 +++ api/test_views.py | 40 ++++++++++++++++++++++++++++++++++++++++ api/views.py | 24 +++++++++++++++++++++--- main/error_codes.py | 1 + 4 files changed, 65 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8354d358..4dd831c2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## Unreleased ### Added + - Return an accurate "account deactivated" error (with `error_code`) from `/get_auth_token` + instead of the generic "Invalid username or password" when the credentials are correct + but the account is inactive - Add potential funding (`PotentialFunding`) ingestion and expose it on Appeal/AppealHistory - EAP: add RCRC Climate Centre to registration and lift activity limit - Use APP_subtype instead of APD_TYP_Id in ingest_appeals diff --git a/api/test_views.py b/api/test_views.py index b34720e91..5e8dddc84 100644 --- a/api/test_views.py +++ b/api/test_views.py @@ -35,6 +35,7 @@ IdentifiedNeedFactory, ) from dref.models import Dref, DrefFile, DrefSummary +from main import error_codes from main.test_case import APITestCase from per.factories import OpsLearningFactory @@ -389,6 +390,45 @@ def test_get_auth(self): self.assertIsNotNone(response.get("token")) self.assertIsNotNone(response.get("expires")) + def test_get_auth_deactivated_user(self): + user = User.objects.create(username="deactivated", email="deactivated@example.com", is_active=False) + user.set_password("12345678") + user.save() + + headers = {"CONTENT_TYPE": "application/json"} + + # Correct credentials, but the account is deactivated. + response = self.client.post( + "/get_auth_token", + {"username": "deactivated", "password": "12345678"}, + format="json", + headers=headers, + ) + self.assertEqual(response.status_code, 400) + data = response.json() + self.assertEqual(data.get("error_code"), error_codes.USER_DEACTIVATED) + self.assertIn("deactivated", data.get("error_message")) + + # Works with the email too. + response = self.client.post( + "/get_auth_token", + {"username": "deactivated@example.com", "password": "12345678"}, + format="json", + headers=headers, + ) + self.assertEqual(response.status_code, 400) + self.assertEqual(response.json().get("error_code"), error_codes.USER_DEACTIVATED) + + # Wrong password on a deactivated account should still look like a generic failure. + response = self.client.post( + "/get_auth_token", + {"username": "deactivated", "password": "wrong-password"}, + format="json", + headers=headers, + ) + self.assertEqual(response.status_code, 400) + self.assertIsNone(response.json().get("error_code")) + class EventApiTest(APITestCase): diff --git a/api/views.py b/api/views.py index 39293ac87..0dab917c8 100644 --- a/api/views.py +++ b/api/views.py @@ -60,6 +60,7 @@ Statuses, ) from flash_update.models import FlashUpdate +from main import error_codes from main.permissions import DenyGuestUserPermission from notifications.models import Subscription, SurgeAlert, SurgeAlertStatus from notifications.notification import send_notification @@ -86,8 +87,11 @@ # get_predefined_search_urls, -def bad_request(message): - return JsonResponse({"statusCode": 400, "error_message": message}, status=400) +def bad_request(message, error_code=None): + body = {"statusCode": 400, "error_message": message} + if error_code is not None: + body["error_code"] = error_code + return JsonResponse(body, status=400) def bad_http_request(header, message): @@ -946,6 +950,15 @@ def post(self, request): # We get the first one if there are still multiple available is_active: user = authenticate(username=users[0].username, password=password) + # authenticate() also returns None for correct credentials on a deactivated account + # (ModelBackend rejects inactive users), so check that case separately to give an + # accurate message instead of the generic "invalid credentials" one. + deactivated_user = None + if user is None: + candidate = User.objects.filter(Q(username__iexact=username) | Q(email__iexact=username), is_active=False).first() + if candidate is not None and candidate.check_password(password): + deactivated_user = candidate + # Determining the client IP is not always straightforward: clientIP = "" # if 'REMOTE_ADDR' in request.META: clientIP += 'R' + request.META['REMOTE_ADDR'] @@ -961,7 +974,7 @@ def post(self, request): % ( username, clientIP, - "ok" if user else "ERR", + "ok" if user else ("DEACTIVATED" if deactivated_user else "ERR"), request.META["HTTP_ACCEPT_LANGUAGE"] if "HTTP_ACCEPT_LANGUAGE" in request.META else "", request.META["HTTP_USER_AGENT"] if "HTTP_USER_AGENT" in request.META else "", ) @@ -989,6 +1002,11 @@ def post(self, request): "id": user.id, } ) + elif deactivated_user is not None: + return bad_request( + "Your account is deactivated, please contact the IM team at im@ifrc.org for more information.", + error_code=error_codes.USER_DEACTIVATED, + ) else: return bad_request("Invalid username or password") # most probably password issue diff --git a/main/error_codes.py b/main/error_codes.py index 7d80e654c..987b40113 100644 --- a/main/error_codes.py +++ b/main/error_codes.py @@ -1,3 +1,4 @@ TOKEN_INVALID = 4001 NOT_AUTHENTICATED = 4011 AUTHENTICATION_FAILED = 4012 +USER_DEACTIVATED = 4013