-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #25 --------- Co-authored-by: Hans Keeler <[email protected]>
- Loading branch information
1 parent
ab24d11
commit 65d5d5c
Showing
10 changed files
with
364 additions
and
96 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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,61 @@ | ||
import os | ||
from typing import Dict, Any | ||
|
||
from pydantic import TypeAdapter | ||
from pydantic.networks import HttpUrl, PostgresDsn | ||
from pydantic.types import SecretStr | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
||
JWT_OPTS_PREFIX = "jwt_opts_" | ||
|
||
env_files_to_load = [".env"] | ||
if os.getenv("ENV", "LOCAL") == "LOCAL": | ||
env_files_to_load.append(".env.local") | ||
|
||
|
||
class Settings(BaseSettings): | ||
inst_conn: PostgresDsn | ||
inst_db_schema: str = "public" | ||
auth_client: str | ||
auth_url: HttpUrl | ||
token_url: HttpUrl | ||
certs_url: HttpUrl | ||
kc_url: HttpUrl | ||
kc_realm: str | ||
kc_admin_client_id: str | ||
kc_admin_client_secret: SecretStr | ||
kc_realm_url: HttpUrl | ||
jwt_opts: Dict[str, bool | int] = {} | ||
|
||
def __init__(self, **data): | ||
super().__init__(**data) | ||
self.set_jwt_opts() | ||
|
||
def set_jwt_opts(self) -> None: | ||
""" | ||
Converts `jwt_opts_` prefixed settings, and env vars into JWT options dictionary. | ||
all options are boolean, with exception of 'leeway' being int | ||
valid options can be found here: | ||
https://github.com/mpdavis/python-jose/blob/4b0701b46a8d00988afcc5168c2b3a1fd60d15d8/jose/jwt.py#L81 | ||
Because we're using model_extra to load in jwt_opts as a dynamic dictionary, | ||
normal env overrides does not take place on top of dotenv files, | ||
so we're merging settings.model_extra with environment variables. | ||
""" | ||
jwt_opts_adapter = TypeAdapter(int | bool) | ||
self.jwt_opts = { | ||
**self.parse_jwt_vars(jwt_opts_adapter, self.model_extra.items()), | ||
**self.parse_jwt_vars(jwt_opts_adapter, os.environ.items()), | ||
} | ||
|
||
def parse_jwt_vars(self, type_adapter: TypeAdapter, setting_variables: Dict[str, Any]) -> Dict[str, bool | int]: | ||
return { | ||
key.lower().replace(JWT_OPTS_PREFIX, ""): type_adapter.validate_python(value) | ||
for (key, value) in setting_variables | ||
if key.lower().startswith(JWT_OPTS_PREFIX) | ||
} | ||
|
||
model_config = SettingsConfigDict(env_file=env_files_to_load, extra="allow") | ||
|
||
|
||
settings = Settings() |
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 was deleted.
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,23 @@ | ||
import pytest | ||
from config import Settings | ||
|
||
|
||
def test_jwt_opts_valid_values(): | ||
mock_config = { | ||
"jwt_opts_test1": "true", | ||
"jwt_opts_test2": "true", | ||
"jwt_opts_test3": "12", | ||
} | ||
settings = Settings(**mock_config) | ||
assert settings.jwt_opts == {"test1": True, "test2": True, "test3": 12} | ||
|
||
|
||
def test_jwt_opts_invalid_values(): | ||
mock_config = { | ||
"jwt_opts_test1": "not a bool or int", | ||
"jwt_opts_test2": "true", | ||
"jwt_opts_test3": "12", | ||
} | ||
with pytest.raises(Exception) as e: | ||
Settings(**mock_config) | ||
assert "validation error" in str(e.value) |