Skip to content

Commit

Permalink
Merge pull request #28 from dudil/upgrade_pydantic
Browse files Browse the repository at this point in the history
Upgrade to pydantic version 2
  • Loading branch information
dudil authored Oct 7, 2023
2 parents ccf5215 + fe0f903 commit 0f2a4e9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
12 changes: 6 additions & 6 deletions fastapi_msal/clients/async_conf_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ async def __execute_async__(func: Callable[..., T], **kwargs: Any) -> T:
def decode_id_token(id_token: str) -> Optional[IDTokenClaims]:
decoded: OptStrsDict = json.loads(oidc.decode_part(id_token.split(".")[1]))
if decoded:
return IDTokenClaims.parse_obj(decoded)
return IDTokenClaims.model_validate(decoded)
return None

async def validate_id_token(self, id_token: str, nonce: OptStr = None) -> IDTokenClaims:
token_claims: OptStrsDict = await self.__execute_async__(
self._cca.client.decode_id_token, id_token=id_token, nonce=nonce
)
return IDTokenClaims.parse_obj(token_claims)
return IDTokenClaims.model_validate(token_claims)

async def get_application_token(self, claims_challenge: OptStrsDict = None) -> AuthToken:
token: StrsDict = await self.__execute_async__(
Expand Down Expand Up @@ -92,14 +92,14 @@ async def initiate_auth_flow(
async def finalize_auth_flow(self, auth_code_flow: AuthCode, auth_response: AuthResponse) -> AuthToken:
auth_token: StrsDict = await self.__execute_async__(
self._cca.acquire_token_by_auth_code_flow,
auth_code_flow=auth_code_flow.dict(exclude_none=True),
auth_response=auth_response.dict(exclude_none=True),
auth_code_flow=auth_code_flow.model_dump(exclude_none=True),
auth_response=auth_response.model_dump(exclude_none=True),
scopes=self.client_config.scopes,
)
return AuthToken.parse_obj_debug(to_parse=auth_token)

async def remove_account(self, account: LocalAccount) -> None:
await self.__execute_async__(self._cca.remove_account, account=account.dict(exclude_none=True))
await self.__execute_async__(self._cca.remove_account, account=account.model_dump(exclude_none=True))

async def get_accounts(self, username: OptStr = None) -> list[LocalAccount]:
accounts_objects: list[StrsDict] = await self.__execute_async__(self._cca.get_accounts, username=username)
Expand All @@ -117,7 +117,7 @@ async def acquire_token_silent(
token = await self.__execute_async__(
self._cca.acquire_token_silent,
scopes=self.client_config.scopes,
account=(account.dict(exclude_none=True) if account else None),
account=(account.model_dump(exclude_none=True) if account else None),
authority=authority,
force_refresh=force_refresh,
claims_challenge=claims_challenge,
Expand Down
13 changes: 7 additions & 6 deletions fastapi_msal/core/msal_client_config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from enum import Enum
from typing import ClassVar

from pydantic import BaseSettings
from pydantic_settings import BaseSettings

from .utils import OptStr, StrList
from .utils import OptStr


class MSALPolicies(str, Enum):
Expand All @@ -16,14 +17,14 @@ class MSALPolicies(str, Enum):
class MSALClientConfig(BaseSettings):
# The following params must be set according to the app registration data recieved from AAD
# https://docs.microsoft.com/azure/active-directory/develop/quickstart-v2-register-an-app
client_id: OptStr
client_credential: OptStr
tenant: OptStr
client_id: OptStr = None
client_credential: OptStr = None
tenant: OptStr = None

# Optional to set, see MSALPolicies for different options, default is single AAD (B2B)
policy: MSALPolicies = MSALPolicies.AAD_SINGLE
# Optional to set - If you are unsure don't set - it will be filled by MSAL as required
scopes: StrList = []
scopes: ClassVar[list[str]] = []
# Not in use - for future support
session_type: str = "filesystem"

Expand Down
4 changes: 2 additions & 2 deletions fastapi_msal/core/session_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ def save(self, model: M) -> None:
if session is None:
msg = "No session id, (Make sure you initialized the session by calling init_session)"
raise OSError(msg)
session.update({model.__repr_name__(): model.json(exclude_none=True, by_alias=True)})
session.update({model.__repr_name__(): model.model_dump_json(exclude_none=True, by_alias=True)}) # type: ignore
self._write_session(session=session)

def load(self, model_cls: type[M]) -> Optional[M]:
session: OptStrsDict = self._read_session()
if session:
raw_model: OptStr = session.get(model_cls.__name__, None)
if raw_model:
return model_cls.parse_raw(raw_model)
return model_cls.model_validate_json(raw_model)
return None

def clear(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion fastapi_msal/models/base_auth_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BaseAuthModel(BaseModel):

@classmethod
def parse_obj_debug(cls: type[AuthModel], to_parse: StrsDict) -> AuthModel:
debug_model: AuthModel = cls.parse_obj(obj=to_parse)
debug_model: AuthModel = cls.model_validate(obj=to_parse)
debug_model.__setattr__("_recieved", to_parse)
return debug_model

Expand Down
4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ classifiers = [
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
]
dependencies = ["pydantic==1.*", "starlette", "fastapi", "msal"]
dynamic = ["version"]
dependencies = ["pydantic>=2.4", "pydantic_settings>=2.0", "starlette", "fastapi", "msal"]

[project.urls]
Homepage = "https://github.com/dudil/fastapi_msal"
Expand Down Expand Up @@ -69,12 +69,10 @@ test = "pytest {args:tests}"
all = ["style", "typing"]

[tool.black]
target-version = ["py39"]
line-length = 122
skip-string-normalization = true

[tool.ruff]
target-version = "py39"
line-length = 122
select = [
"A",
Expand Down

0 comments on commit 0f2a4e9

Please sign in to comment.