Skip to content

Commit

Permalink
♻️ refactor BaseSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
yezz123 committed Oct 14, 2023
1 parent d7002c4 commit bad2db3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
41 changes: 41 additions & 0 deletions authx/_internal/_session.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion authx/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions authx/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand All @@ -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__)
Expand Down

0 comments on commit bad2db3

Please sign in to comment.