From 6541b5f33be9e3c5c3f0b5d3ef377cefc3c72140 Mon Sep 17 00:00:00 2001 From: Nikita Melkozerov Date: Tue, 3 Sep 2024 11:36:35 +0000 Subject: [PATCH 1/2] Use the correct metric name when the user does not exist --- fixbackend/auth/router.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/fixbackend/auth/router.py b/fixbackend/auth/router.py index 20001348..03026fa9 100644 --- a/fixbackend/auth/router.py +++ b/fixbackend/auth/router.py @@ -182,10 +182,14 @@ async def login( user = await user_manager.authenticate(credentials) if user is None or not user.is_active: - maybe_existing = await user_manager.get_by_email(credentials.username) - metric = FailedLoginAttempts - if maybe_existing: - metric = FailedLoginAttempts.labels(user_id=maybe_existing.id) + metric = FailedLoginAttempts.labels(user_id=None) + try: + maybe_existing = await user_manager.get_by_email(credentials.username) + if maybe_existing: + metric = FailedLoginAttempts.labels(user_id=maybe_existing.id) + except Exception: + pass + metric.inc() raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, From 2e6e0980e0386b051ffbd061904ca6523536f33a Mon Sep 17 00:00:00 2001 From: Nikita Melkozerov Date: Wed, 4 Sep 2024 08:59:01 +0000 Subject: [PATCH 2/2] make code check happy --- fixbackend/auth/router.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fixbackend/auth/router.py b/fixbackend/auth/router.py index 03026fa9..41f7cc0b 100644 --- a/fixbackend/auth/router.py +++ b/fixbackend/auth/router.py @@ -17,7 +17,7 @@ from fastapi import APIRouter, Depends, HTTPException, Request, Response, status, Form from fastapi_users.authentication import AuthenticationBackend, Strategy -from fastapi_users.exceptions import UserAlreadyExists, InvalidPasswordException +from fastapi_users.exceptions import UserAlreadyExists, InvalidPasswordException, UserNotExists from fastapi_users.router import ErrorCode from fastapi_users.router.oauth import generate_state_token from httpx_oauth.clients.google import GoogleOAuth2 @@ -187,7 +187,7 @@ async def login( maybe_existing = await user_manager.get_by_email(credentials.username) if maybe_existing: metric = FailedLoginAttempts.labels(user_id=maybe_existing.id) - except Exception: + except UserNotExists: pass metric.inc()