Skip to content

Commit

Permalink
return 401 for login failures (#15432)
Browse files Browse the repository at this point in the history
* return 401 for login failures

* only setup the rate limiter when configured
  • Loading branch information
blakeblackshear authored Dec 10, 2024
1 parent 0b9c4c1 commit 6b12a45
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions frigate/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def login(request: Request, body: AppPostLoginBody):
try:
db_user: User = User.get_by_id(user)
except DoesNotExist:
return JSONResponse(content={"message": "Login failed"}, status_code=400)
return JSONResponse(content={"message": "Login failed"}, status_code=401)

password_hash = db_user.password_hash
if verify_password(password, password_hash):
Expand All @@ -340,7 +340,7 @@ def login(request: Request, body: AppPostLoginBody):
response, JWT_COOKIE_NAME, encoded_jwt, expiration, JWT_COOKIE_SECURE
)
return response
return JSONResponse(content={"message": "Login failed"}, status_code=400)
return JSONResponse(content={"message": "Login failed"}, status_code=401)


@router.get("/users")
Expand Down
6 changes: 5 additions & 1 deletion frigate/api/fastapi_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ async def startup():
logger.info("FastAPI started")

# Rate limiter (used for login endpoint)
auth.rateLimiter.set_limit(frigate_config.auth.failed_login_rate_limit or "")
if frigate_config.auth.failed_login_rate_limit is None:
limiter.enabled = False
else:
auth.rateLimiter.set_limit(frigate_config.auth.failed_login_rate_limit)

app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
app.add_middleware(SlowAPIMiddleware)
Expand Down
7 changes: 5 additions & 2 deletions web/src/api/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ export function ApiProvider({ children, options }: ApiProviderType) {
error.response &&
[401, 302, 307].includes(error.response.status)
) {
window.location.href =
error.response.headers.get("location") ?? "login";
// redirect to the login page if not already there
const loginPage = error.response.headers.get("location") ?? "login";
if (window.location.href !== loginPage) {
window.location.href = loginPage;
}
}
},
...options,
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/auth/AuthForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function UserAuthForm({ className, ...props }: UserAuthFormProps) {
toast.error("Exceeded rate limit. Try again later.", {
position: "top-center",
});
} else if (err.response?.status === 400) {
} else if (err.response?.status === 401) {
toast.error("Login failed", {
position: "top-center",
});
Expand Down

0 comments on commit 6b12a45

Please sign in to comment.