Skip to content
Draft
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions api/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):

Expand Down
24 changes: 21 additions & 3 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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']
Expand All @@ -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 "",
)
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions main/error_codes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
TOKEN_INVALID = 4001
NOT_AUTHENTICATED = 4011
AUTHENTICATION_FAILED = 4012
USER_DEACTIVATED = 4013
Loading