|
10 | 10 | # ************************************************************* |
11 | 11 |
|
12 | 12 | ### Standard packages ### |
13 | | -from typing import Tuple |
| 13 | +from __future__ import annotations |
| 14 | +from typing import Literal, Optional, Set, Tuple |
14 | 15 |
|
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 | +) |
17 | 24 |
|
18 | 25 |
|
19 | | -class LoadConfig(BaseLoadConfig): |
| 26 | +class LoadConfig(BaseModel): |
20 | 27 | """Same as the base LoadConfig, but no token_location & token_key validations.""" |
21 | 28 |
|
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 |
27 | 46 |
|
28 | 47 |
|
29 | 48 | __all__: Tuple[str, ...] = ("LoadConfig",) |
0 commit comments