-
Notifications
You must be signed in to change notification settings - Fork 0
Add basic auth #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add basic auth #76
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Storing default credentials in the source code can pose a security risk. Consider using environment variables for sensitive information like
username
andpassword
to enhance security. This approach allows for more secure and flexible configuration management.Committable suggestion