-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #233 from Ivareh/212-privatize-the-api-in-deployed…
…-remote-version 212 privatize the api in deployed remote version
Showing
22 changed files
with
553 additions
and
127 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
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
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
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,50 @@ | ||
import secrets | ||
from typing import Annotated | ||
from fastapi import Depends, HTTPException, status | ||
from fastapi.security import HTTPBasic, HTTPBasicCredentials | ||
|
||
import os | ||
|
||
|
||
PRIVATIZE_API = os.getenv("PRIVATIZE_API") | ||
FIRST_SUPERUSER = os.getenv("FIRST_SUPERUSER") | ||
FIRST_SUPERUSER_PASSWORD = os.getenv("FIRST_SUPERUSER_PASSWORD") | ||
|
||
security = HTTPBasic() | ||
|
||
|
||
def verification( | ||
credentials: HTTPBasicCredentials = Depends(security), | ||
) -> None: | ||
"""Verify the username and password for the API. | ||
Args: | ||
credentials (HTTPBasicCredentials, optional): HTTP Credentials. Defaults to Depends(security). | ||
Raises: | ||
HTTPException: If the username or password is incorrect. | ||
Returns: | ||
None | ||
""" | ||
if PRIVATIZE_API: | ||
current_username_bytes = credentials.username.encode("utf8") | ||
correct_username_bytes = FIRST_SUPERUSER.encode("utf8") | ||
is_correct_username = secrets.compare_digest( | ||
current_username_bytes, correct_username_bytes | ||
) | ||
current_password_bytes = credentials.password.encode("utf8") | ||
correct_password_bytes = FIRST_SUPERUSER_PASSWORD.encode("utf8") | ||
is_correct_password = secrets.compare_digest( | ||
current_password_bytes, correct_password_bytes | ||
) | ||
if is_correct_password and is_correct_username: | ||
return True | ||
else: | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Incorrect username or password", | ||
headers={"WWW-Authenticate": "Basic"}, | ||
) | ||
else: | ||
return True |
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
44 changes: 44 additions & 0 deletions
44
src/backend_api/app/app/tests/crud/crud_models/test_stash.py
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,44 @@ | ||
from typing import Callable, Dict, Tuple, List, Union | ||
import pytest | ||
|
||
from app.crud import ( | ||
CRUD_stash, | ||
CRUD_account, | ||
) | ||
from app.core.models.models import Stash, Account | ||
from app.crud.base import CRUDBase | ||
import app.tests.crud.cascade_tests as cascade_test | ||
from app.tests.utils.model_utils.stash import generate_random_stash | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def object_generator_func() -> Callable[[], Dict]: | ||
return generate_random_stash | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def object_generator_func_w_deps() -> ( | ||
Callable[[], Tuple[Dict, Stash, List[Union[Dict, Account]]]] | ||
): | ||
def generate_random_stash_w_deps( | ||
db, | ||
) -> Callable[[], Tuple[Dict, Stash, List[Union[Dict, Account]]]]: | ||
return generate_random_stash(db, retrieve_dependencies=True) | ||
|
||
return generate_random_stash_w_deps | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def crud_instance() -> CRUDBase: | ||
return CRUD_stash | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def crud_deps_instances() -> CRUDBase: | ||
return [ | ||
CRUD_account, | ||
] | ||
|
||
|
||
class TestStashCRUD(cascade_test.TestCascade): | ||
pass |
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,52 @@ | ||
import asyncio | ||
from typing import Dict, List, Optional, Tuple, Union | ||
from sqlalchemy.orm import Session | ||
|
||
from app import crud | ||
from app.tests.conftest import db | ||
from app.core.models.models import Account, Stash | ||
from app.core.schemas.stash import StashCreate | ||
from app.tests.utils.utils import random_lower_string, random_bool | ||
from app.tests.utils.model_utils.account import generate_random_account | ||
|
||
|
||
async def create_random_stash_dict( | ||
db: Session, retrieve_dependencies: Optional[bool] = False | ||
) -> Union[Dict, Tuple[Dict, List[Union[Dict, Account]]]]: | ||
stashId = random_lower_string() | ||
public: bool = random_bool() | ||
league = random_lower_string() | ||
|
||
account_dict, account = await generate_random_account(db) | ||
accountName = account.accountName | ||
|
||
stash = { | ||
"stashId": stashId, | ||
"accountName": accountName, | ||
"public": public, | ||
"league": league, | ||
} | ||
|
||
if not retrieve_dependencies: | ||
return stash | ||
else: | ||
deps = [] | ||
deps += [account_dict, account] | ||
return stash, deps | ||
|
||
|
||
async def generate_random_stash( | ||
db: Session, retrieve_dependencies: Optional[bool] = False | ||
) -> Tuple[Dict, Stash, Optional[List[Union[Dict, Account]]]]: | ||
output = await create_random_stash_dict(db, retrieve_dependencies) | ||
if not retrieve_dependencies: | ||
stash_dict = output | ||
else: | ||
stash_dict, deps = output | ||
stash_create = StashCreate(**stash_dict) | ||
stash = await crud.CRUD_stash.create(db, obj_in=stash_create) | ||
|
||
if not retrieve_dependencies: | ||
return stash_dict, stash | ||
else: | ||
return stash_dict, stash, deps |
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
17 changes: 17 additions & 0 deletions
17
src/backend_data_retrieval/data_retrieval/pom_api_authentication.py
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,17 @@ | ||
from requests.auth import HTTPBasicAuth | ||
import os | ||
|
||
|
||
FIRST_SUPERUSER = os.getenv("FIRST_SUPERUSER") | ||
FIRST_SUPERUSER_PASSWORD = os.getenv("FIRST_SUPERUSER_PASSWORD") | ||
|
||
|
||
def get_super_authentication() -> HTTPBasicAuth: | ||
""" | ||
Get the super authentication for the Path of Modifiers API. | ||
Returns: | ||
HTTPBasicAuth: POM user and password authentication. | ||
""" | ||
authentication = HTTPBasicAuth(FIRST_SUPERUSER, FIRST_SUPERUSER_PASSWORD) | ||
return authentication |