-
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.
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Enhanced security by introducing basic authentication for member and video management actions. - **Tests** - Added tests to validate the functionality of security measures and default values in settings. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
9 changed files
with
146 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,36 @@ | ||
"""Security module for the FastAPI application.""" | ||
|
||
import secrets | ||
from typing import Annotated | ||
|
||
from fastapi import Depends, HTTPException, status | ||
from fastapi.security import HTTPBasic, HTTPBasicCredentials | ||
|
||
from .settings import settings | ||
|
||
security = HTTPBasic() | ||
|
||
|
||
def authorize(credentials: Annotated[HTTPBasicCredentials, Depends(security)]): | ||
""" | ||
Authorize the request with the correct username and password. | ||
The correct username and password are stored in the settings. | ||
:param credentials: the credentials from the request | ||
:return: | ||
""" | ||
current_username_bytes = credentials.username.encode("utf8") | ||
correct_username_bytes = settings.username.encode("utf8") | ||
is_correct_username = secrets.compare_digest( | ||
current_username_bytes, correct_username_bytes | ||
) | ||
current_password_bytes = credentials.password.encode("utf8") | ||
correct_password_bytes = settings.password.encode("utf8") | ||
is_correct_password = secrets.compare_digest( | ||
current_password_bytes, correct_password_bytes | ||
) | ||
if not (is_correct_username and is_correct_password): | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import pytest | ||
from fastapi import Depends, FastAPI | ||
from fastapi.security import HTTPBasicCredentials | ||
from fastapi.testclient import TestClient | ||
|
||
from src.security import authorize | ||
|
||
|
||
@pytest.fixture | ||
def client(): | ||
app = FastAPI() | ||
|
||
@app.get("/secure-endpoint") | ||
def secure_endpoint(credentials: HTTPBasicCredentials = Depends(authorize)): | ||
return {"message": "You are authorized"} | ||
|
||
return TestClient(app) | ||
|
||
|
||
def test_authorize_correct_credentials(mocker, client): | ||
credentials = HTTPBasicCredentials(username="admin", password="password") | ||
mocker.patch("src.security.security", return_value=credentials) | ||
|
||
response = client.get("/secure-endpoint", auth=("admin", "password")) | ||
|
||
assert response.status_code == 200 | ||
assert response.json() == {"message": "You are authorized"} | ||
|
||
|
||
def test_authorize_incorrect_credentials(mocker, client): | ||
credentials = HTTPBasicCredentials(username="admin", password="password") | ||
mocker.patch("src.security.security", return_value=credentials) | ||
|
||
response = client.get("/secure-endpoint", auth=("wrong", "wrong")) | ||
|
||
assert response.status_code == 401 | ||
assert response.json() == {"detail": "Incorrect username or password"} |
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,8 @@ | ||
from src.settings import Settings | ||
|
||
|
||
def test_settings_default_values(): | ||
settings = Settings() | ||
assert settings.server_base_path == "./assets/" | ||
assert settings.username == "admin" | ||
assert settings.password == "password" |