From b2f00f00c847f9552c137c4dfb54c81e87902c75 Mon Sep 17 00:00:00 2001 From: Nikita Melkozerov Date: Wed, 4 Sep 2024 11:46:38 +0200 Subject: [PATCH] Use the correct metric name when the user does not exist (#609) --- fixbackend/auth/router.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/fixbackend/auth/router.py b/fixbackend/auth/router.py index 20001348..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 @@ -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 UserNotExists: + pass + metric.inc() raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST,