mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-07-09 03:04:54 -04:00
Add failed login & IP to log (#2365)
* Add failed login & IP to log * Small change for human readability/intuitiveness
This commit is contained in:
parent
a2e69b5565
commit
2e43b51882
@ -1,12 +1,12 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, Form, status
|
from fastapi import APIRouter, Depends, Form, Request, status
|
||||||
from fastapi.exceptions import HTTPException
|
from fastapi.exceptions import HTTPException
|
||||||
from fastapi.security import OAuth2PasswordRequestForm
|
from fastapi.security import OAuth2PasswordRequestForm
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from sqlalchemy.orm.session import Session
|
from sqlalchemy.orm.session import Session
|
||||||
|
|
||||||
from mealie.core import security
|
from mealie.core import root_logger, security
|
||||||
from mealie.core.dependencies import get_current_user
|
from mealie.core.dependencies import get_current_user
|
||||||
from mealie.core.security import authenticate_user
|
from mealie.core.security import authenticate_user
|
||||||
from mealie.core.security.security import UserLockedOut
|
from mealie.core.security.security import UserLockedOut
|
||||||
@ -16,6 +16,7 @@ from mealie.schema.user import PrivateUser
|
|||||||
|
|
||||||
public_router = APIRouter(tags=["Users: Authentication"])
|
public_router = APIRouter(tags=["Users: Authentication"])
|
||||||
user_router = UserAPIRouter(tags=["Users: Authentication"])
|
user_router = UserAPIRouter(tags=["Users: Authentication"])
|
||||||
|
logger = root_logger.get_logger("auth")
|
||||||
|
|
||||||
|
|
||||||
class CustomOAuth2Form(OAuth2PasswordRequestForm):
|
class CustomOAuth2Form(OAuth2PasswordRequestForm):
|
||||||
@ -48,16 +49,18 @@ class MealieAuthToken(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
@public_router.post("/token")
|
@public_router.post("/token")
|
||||||
def get_token(data: CustomOAuth2Form = Depends(), session: Session = Depends(generate_session)):
|
def get_token(request: Request, data: CustomOAuth2Form = Depends(), session: Session = Depends(generate_session)):
|
||||||
email = data.username
|
email = data.username
|
||||||
password = data.password
|
password = data.password
|
||||||
|
|
||||||
try:
|
try:
|
||||||
user = authenticate_user(session, email, password) # type: ignore
|
user = authenticate_user(session, email, password) # type: ignore
|
||||||
except UserLockedOut as e:
|
except UserLockedOut as e:
|
||||||
|
logger.error(f"User is locked out from {request.client.host}")
|
||||||
raise HTTPException(status_code=status.HTTP_423_LOCKED, detail="User is locked out") from e
|
raise HTTPException(status_code=status.HTTP_423_LOCKED, detail="User is locked out") from e
|
||||||
|
|
||||||
if not user:
|
if not user:
|
||||||
|
logger.error(f"Incorrect username or password from {request.client.host}")
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user