-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import logging | ||
from typing import Annotated | ||
|
||
from fastapi import Depends, HTTPException, Request, status | ||
from fastapi.security import HTTPBasic, HTTPBasicCredentials | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
security = HTTPBasic() | ||
|
||
|
||
def get_current_username( | ||
request: Request, | ||
credentials: Annotated[HTTPBasicCredentials, Depends(security)], | ||
): | ||
if user := request.app.users.get(credentials.username): | ||
if user.verify_password(credentials.password): | ||
return credentials.username | ||
else: | ||
logger.warning("Invalid password for user %s", credentials.username) | ||
else: | ||
logger.warning("Unknown user %s", credentials.username) | ||
|
||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Incorrect username or password", | ||
headers={"WWW-Authenticate": "Basic"}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters