Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
- fix test
- don't allow empty keys^
  • Loading branch information
devkral committed Dec 27, 2024
1 parent ba34315 commit d76b986
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
20 changes: 18 additions & 2 deletions esmerald/config/session.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Union

from pydantic import BaseModel, ConfigDict, constr
from pydantic import BaseModel, ConfigDict, constr, field_validator
from typing_extensions import Annotated, Doc, Literal

from esmerald.datastructures import Secret
Expand Down Expand Up @@ -39,7 +39,7 @@ class SessionConfig(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)

secret_key: Annotated[
Union[str, Secret],
Union[str, bytes, Secret],
Doc(
"""
The string used for the encryption/decryption and used to create an HMAC to sign.
Expand Down Expand Up @@ -91,3 +91,19 @@ class SessionConfig(BaseModel):
"""
),
] = "lax"

@field_validator("secret_key")
def validate_secret(
cls,
value: Annotated[
Secret,
Doc(
"""
The string secret that will be evaluated.
"""
),
],
) -> Secret:
if not value:
raise ValueError("secret_key is empty")
return value
3 changes: 0 additions & 3 deletions tests/middleware/test_session_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
[os.urandom(16), False],
[os.urandom(24), False],
[os.urandom(32), False],
[os.urandom(17), True],
[os.urandom(4), True],
[os.urandom(100), True],
[b"", True],
],
)
Expand Down

0 comments on commit d76b986

Please sign in to comment.