Skip to content

Commit c616233

Browse files
committed
chore: reimplement flexible LoadConfig w/o tokens
1 parent 3b5e7d3 commit c616233

1 file changed

Lines changed: 28 additions & 9 deletions

File tree

src/fastapi_csrf_protect/flexible/load_config.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,39 @@
1010
# *************************************************************
1111

1212
### Standard packages ###
13-
from typing import Tuple
13+
from __future__ import annotations
14+
from typing import Literal, Optional, Set, Tuple
1415

15-
### Local modules ###
16-
from fastapi_csrf_protect.load_config import LoadConfig as BaseLoadConfig
16+
### Third-party packages ###
17+
from pydantic import (
18+
BaseModel,
19+
StrictBool,
20+
StrictInt,
21+
StrictStr,
22+
model_validator,
23+
)
1724

1825

19-
class LoadConfig(BaseLoadConfig):
26+
class LoadConfig(BaseModel):
2027
"""Same as the base LoadConfig, but no token_location & token_key validations."""
2128

22-
def __post_init__(self) -> None:
23-
self.validate_attribute_types()
24-
self.validate_cookie_samesite()
25-
self.validate_cookie_samesite_none_secure()
26-
self.validate_methods()
29+
cookie_key: Optional[StrictStr] = "fastapi-csrf-token"
30+
cookie_path: Optional[StrictStr] = "/"
31+
cookie_domain: Optional[StrictStr] = None
32+
cookie_samesite: Optional[Literal["lax", "none", "strict"]] = "lax"
33+
cookie_secure: Optional[StrictBool] = False
34+
header_name: Optional[StrictStr] = "X-CSRF-Token"
35+
header_type: Optional[StrictStr] = None
36+
httponly: Optional[StrictBool] = True
37+
max_age: Optional[StrictInt] = 3600
38+
methods: Optional[Set[Literal["DELETE", "GET", "OPTIONS", "PATCH", "POST", "PUT"]]] = None
39+
secret_key: Optional[StrictStr] = None
40+
41+
@model_validator(mode="after")
42+
def validate_cookie_samesite_none_secure(self) -> LoadConfig:
43+
if self.cookie_samesite in {None, "none"} and self.cookie_secure is not True:
44+
raise ValueError('The "cookie_secure" must be True if "cookie_samesite" set to "none".')
45+
return self
2746

2847

2948
__all__: Tuple[str, ...] = ("LoadConfig",)

0 commit comments

Comments
 (0)