From bad2db3fa9466848c1744993d3730ad479c0b7ea Mon Sep 17 00:00:00 2001 From: Yasser Tahiri Date: Sun, 25 Jun 2023 00:21:28 +0400 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor=20`BaseSettings`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- authx/_internal/_session.py | 41 +++++++++++++++++++++++++++++++++++++ authx/config.py | 3 ++- authx/schema.py | 7 +++---- 3 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 authx/_internal/_session.py diff --git a/authx/_internal/_session.py b/authx/_internal/_session.py new file mode 100644 index 00000000..f2805033 --- /dev/null +++ b/authx/_internal/_session.py @@ -0,0 +1,41 @@ +from datetime import timedelta +from typing import Callable, Optional +from uuid import uuid4 + +from pydantic_settings import BaseSettings + + +def genSessionId() -> str: + return uuid4().hex + + +settings = {"sessionIdGenerator": genSessionId} + + +class Config(BaseSettings): + redisURL: str = "redis://localhost:6379/0" + settings: dict = settings + sessionIdName: str = "ssid" + expireTime: timedelta = timedelta(hours=6) + + def genSessionId(self) -> str: + return self.settings["sessionIdGenerator"]() + + +config = Config() + + +def basicConfig( + redisURL: Optional[str] = "", + sessionIdName: Optional[str] = "", + sessionIdGenerator: Optional[Callable[[], str]] = None, + expireTime: Optional[timedelta] = None, +): + if redisURL: + config.redisURL = redisURL + if sessionIdName: + config.sessionIdName = sessionIdName + if sessionIdGenerator: + config.settings["sessionIdGenerator"] = sessionIdGenerator + if expireTime: + config.expireTime = expireTime diff --git a/authx/config.py b/authx/config.py index 9f23b3ca..a302c8de 100644 --- a/authx/config.py +++ b/authx/config.py @@ -2,7 +2,8 @@ from typing import List, Optional, Sequence from jwt.algorithms import get_default_algorithms, requires_cryptography -from pydantic import BaseSettings, Field +from pydantic import Field +from pydantic_settings import BaseSettings from authx.exceptions import BadConfigurationError from authx.types import AlgorithmType, HTTPMethods, SameSitePolicy, StringOrSequence, TokenLocations diff --git a/authx/schema.py b/authx/schema.py index 862f1952..7a2b5b6b 100644 --- a/authx/schema.py +++ b/authx/schema.py @@ -2,7 +2,7 @@ from hmac import compare_digest from typing import Any, Dict, List, Optional, Sequence -from pydantic import BaseModel, Extra, Field, ValidationError, validator +from pydantic import BaseModel, ConfigDict, Field, ValidationError, validator from authx._internal._utils import get_now, get_now_ts, get_uuid from authx.exceptions import ( @@ -18,6 +18,8 @@ class TokenPayload(BaseModel): + model_config = ConfigDict(extra='allow') + jti: Optional[str] = Field(default_factory=get_uuid) iss: Optional[str] = None sub: Optional[str] = None @@ -30,9 +32,6 @@ class TokenPayload(BaseModel): scopes: Optional[List[str]] = None fresh: bool = False - class Config: - extra = Extra.allow - @property def _additional_fields(self): return set(self.__dict__) - set(self.__fields__)