Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pydantic v2 #27

Merged
merged 3 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9, "3.10", "3.11.6", "3.12"]
python-version: [3.8, 3.9, "3.10", "3.11.6", "3.12"]
fail-fast: false

steps:
Expand Down
48 changes: 27 additions & 21 deletions fastapi_another_jwt_auth/config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from datetime import timedelta
from typing import Optional, Union, Sequence, List
from typing import Optional, Union, Sequence, List, Set, Any
from pydantic import (
BaseModel,
field_validator, ConfigDict, BaseModel,
validator,
StrictBool,
StrictInt,
StrictStr
)

MyType = Union[Sequence[Any], Set[Any]]

class LoadConfig(BaseModel):
authjwt_token_location: Optional[Sequence[StrictStr]] = {'headers'}
authjwt_token_location: Optional[Union[Sequence[StrictStr], Set[StrictStr]]] = {'headers'}
authjwt_secret_key: Optional[StrictStr] = None
authjwt_public_key: Optional[StrictStr] = None
authjwt_private_key: Optional[StrictStr] = None
Expand All @@ -20,7 +22,7 @@ class LoadConfig(BaseModel):
authjwt_decode_issuer: Optional[StrictStr] = None
authjwt_decode_audience: Optional[Union[StrictStr,Sequence[StrictStr]]] = None
authjwt_denylist_enabled: Optional[StrictBool] = False
authjwt_denylist_token_checks: Optional[Sequence[StrictStr]] = {'access','refresh'}
authjwt_denylist_token_checks: Optional[Union[Sequence[StrictStr], Set[StrictStr]]] = {'access','refresh'}
authjwt_header_name: Optional[StrictStr] = "Authorization"
authjwt_header_type: Optional[StrictStr] = "Bearer"
authjwt_access_token_expires: Optional[Union[StrictBool,StrictInt,timedelta]] = timedelta(minutes=15)
Expand All @@ -42,44 +44,48 @@ class LoadConfig(BaseModel):
authjwt_refresh_csrf_cookie_path: Optional[StrictStr] = "/"
authjwt_access_csrf_header_name: Optional[StrictStr] = "X-CSRF-Token"
authjwt_refresh_csrf_header_name: Optional[StrictStr] = "X-CSRF-Token"
authjwt_csrf_methods: Optional[Sequence[StrictStr]] = {'POST','PUT','PATCH','DELETE'}
authjwt_csrf_methods: Optional[Union[Sequence[StrictStr], Set[StrictStr]]] = {'POST','PUT','PATCH','DELETE'}

@validator('authjwt_access_token_expires')
@field_validator('authjwt_access_token_expires')
@classmethod
def validate_access_token_expires(cls, v):
if v is True:
raise ValueError("The 'authjwt_access_token_expires' only accept value False (bool)")
return v

@validator('authjwt_refresh_token_expires')
@field_validator('authjwt_refresh_token_expires')
@classmethod
def validate_refresh_token_expires(cls, v):
if v is True:
raise ValueError("The 'authjwt_refresh_token_expires' only accept value False (bool)")
return v

@validator('authjwt_denylist_token_checks', each_item=True)
@field_validator('authjwt_denylist_token_checks')
def validate_denylist_token_checks(cls, v):
if v not in ['access','refresh']:
raise ValueError("The 'authjwt_denylist_token_checks' must be between 'access' or 'refresh'")
for value in v:
if value not in ['access','refresh']:
raise ValueError("The 'authjwt_denylist_token_checks' must be between 'access' or 'refresh'")
return v

@validator('authjwt_token_location', each_item=True)
@field_validator('authjwt_token_location')
def validate_token_location(cls, v):
if v not in ['headers','cookies']:
raise ValueError("The 'authjwt_token_location' must be between 'headers' or 'cookies'")
for value in v:
if value not in ['headers','cookies']:
raise ValueError("The 'authjwt_token_location' must be between 'headers' or 'cookies'")
return v

@validator('authjwt_cookie_samesite')
@field_validator('authjwt_cookie_samesite')
@classmethod
def validate_cookie_samesite(cls, v):
if v not in ['strict','lax','none']:
raise ValueError("The 'authjwt_cookie_samesite' must be between 'strict', 'lax', 'none'")
return v

@validator('authjwt_csrf_methods', each_item=True)
@field_validator('authjwt_csrf_methods')
def validate_csrf_methods(cls, v):
if v.upper() not in {"GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"}:
raise ValueError("The 'authjwt_csrf_methods' must be between http request methods")
return v.upper()
for value in v:
if value.upper() not in {"GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"}:
raise ValueError("The 'authjwt_csrf_methods' must be between http request methods")
return [value.upper() for value in v]

class Config:
min_anystr_length = 1
anystr_strip_whitespace = True
model_config = ConfigDict(str_min_length=1, str_strip_whitespace=True)
Loading