fastapi-factory-utilities 0.3.4__py3-none-any.whl → 0.3.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of fastapi-factory-utilities might be problematic. Click here for more details.
- fastapi_factory_utilities/core/exceptions.py +2 -1
- fastapi_factory_utilities/core/plugins/odm_plugin/repositories.py +10 -22
- fastapi_factory_utilities/core/security/kratos.py +41 -21
- {fastapi_factory_utilities-0.3.4.dist-info → fastapi_factory_utilities-0.3.5.dist-info}/METADATA +1 -1
- {fastapi_factory_utilities-0.3.4.dist-info → fastapi_factory_utilities-0.3.5.dist-info}/RECORD +8 -8
- {fastapi_factory_utilities-0.3.4.dist-info → fastapi_factory_utilities-0.3.5.dist-info}/LICENSE +0 -0
- {fastapi_factory_utilities-0.3.4.dist-info → fastapi_factory_utilities-0.3.5.dist-info}/WHEEL +0 -0
- {fastapi_factory_utilities-0.3.4.dist-info → fastapi_factory_utilities-0.3.5.dist-info}/entry_points.txt +0 -0
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"""FastAPI Factory Utilities exceptions."""
|
|
2
2
|
|
|
3
3
|
import logging
|
|
4
|
-
from
|
|
4
|
+
from collections.abc import Sequence
|
|
5
|
+
from typing import Any
|
|
5
6
|
|
|
6
7
|
from opentelemetry.trace import Span, get_current_span
|
|
7
8
|
from opentelemetry.util.types import AttributeValue
|
|
@@ -2,24 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
import datetime
|
|
4
4
|
from abc import ABC
|
|
5
|
-
from collections.abc import AsyncGenerator, Callable
|
|
5
|
+
from collections.abc import AsyncGenerator, Callable, Mapping
|
|
6
6
|
from contextlib import asynccontextmanager
|
|
7
|
-
from typing import
|
|
8
|
-
Any,
|
|
9
|
-
Dict,
|
|
10
|
-
Generic,
|
|
11
|
-
List,
|
|
12
|
-
Mapping,
|
|
13
|
-
Optional,
|
|
14
|
-
Tuple,
|
|
15
|
-
TypeVar,
|
|
16
|
-
Union,
|
|
17
|
-
get_args,
|
|
18
|
-
)
|
|
7
|
+
from typing import Any, Generic, TypeVar, get_args
|
|
19
8
|
from uuid import UUID
|
|
20
9
|
|
|
21
10
|
from beanie import SortDirection
|
|
22
|
-
from beanie.odm.queries.find import FindMany
|
|
23
11
|
from motor.motor_asyncio import AsyncIOMotorClientSession, AsyncIOMotorDatabase
|
|
24
12
|
from pydantic import BaseModel
|
|
25
13
|
from pymongo.errors import DuplicateKeyError, PyMongoError
|
|
@@ -229,19 +217,19 @@ class AbstractRepository(ABC, Generic[DocumentGenericType, EntityGenericType]):
|
|
|
229
217
|
raise OperationError("Failed to delete document.")
|
|
230
218
|
|
|
231
219
|
@managed_session()
|
|
232
|
-
async def find(
|
|
220
|
+
async def find( # noqa: PLR0913
|
|
233
221
|
self,
|
|
234
|
-
*args:
|
|
222
|
+
*args: Mapping[str, Any] | bool,
|
|
235
223
|
projection_model: None = None,
|
|
236
|
-
skip:
|
|
237
|
-
limit:
|
|
238
|
-
sort:
|
|
239
|
-
session:
|
|
224
|
+
skip: int | None = None,
|
|
225
|
+
limit: int | None = None,
|
|
226
|
+
sort: None | str | list[tuple[str, SortDirection]] = None,
|
|
227
|
+
session: AsyncIOMotorClientSession | None = None,
|
|
240
228
|
ignore_cache: bool = False,
|
|
241
229
|
fetch_links: bool = False,
|
|
242
230
|
lazy_parse: bool = False,
|
|
243
|
-
nesting_depth:
|
|
244
|
-
nesting_depths_per_field:
|
|
231
|
+
nesting_depth: int | None = None,
|
|
232
|
+
nesting_depths_per_field: dict[str, int] | None = None,
|
|
245
233
|
**pymongo_kwargs: Any,
|
|
246
234
|
) -> list[EntityGenericType]:
|
|
247
235
|
"""Find documents in the database.
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"""Provide Kratos Session and Identity classes."""
|
|
2
2
|
|
|
3
|
+
from enum import StrEnum
|
|
3
4
|
from typing import Annotated
|
|
4
5
|
|
|
5
6
|
from fastapi import Depends, HTTPException, Request
|
|
@@ -13,20 +14,30 @@ from fastapi_factory_utilities.core.services.kratos import (
|
|
|
13
14
|
)
|
|
14
15
|
|
|
15
16
|
|
|
17
|
+
class KratosSessionAuthenticationErrors(StrEnum):
|
|
18
|
+
"""Kratos Session Authentication Errors."""
|
|
19
|
+
|
|
20
|
+
MISSING_CREDENTIALS = "Missing Credentials"
|
|
21
|
+
INVALID_CREDENTIALS = "Invalid Credentials"
|
|
22
|
+
INTERNAL_SERVER_ERROR = "Internal Server Error"
|
|
23
|
+
|
|
24
|
+
|
|
16
25
|
class KratosSessionAuthentication:
|
|
17
26
|
"""Kratos Session class."""
|
|
18
27
|
|
|
19
28
|
DEFAULT_COOKIE_NAME: str = "ory_kratos_session"
|
|
20
29
|
|
|
21
|
-
def __init__(self, cookie_name: str = DEFAULT_COOKIE_NAME) -> None:
|
|
30
|
+
def __init__(self, cookie_name: str = DEFAULT_COOKIE_NAME, raise_exception: bool = True) -> None:
|
|
22
31
|
"""Initialize the KratosSessionAuthentication class.
|
|
23
32
|
|
|
24
33
|
Args:
|
|
25
34
|
cookie_name (str): Name of the cookie to extract the session
|
|
35
|
+
raise_exception (bool): Whether to raise an exception or return None
|
|
26
36
|
"""
|
|
27
37
|
self._cookie_name: str = cookie_name
|
|
38
|
+
self._raise_exception: bool = raise_exception
|
|
28
39
|
|
|
29
|
-
def _extract_cookie(self, request: Request) -> str:
|
|
40
|
+
def _extract_cookie(self, request: Request) -> str | None:
|
|
30
41
|
"""Extract the cookie from the request.
|
|
31
42
|
|
|
32
43
|
Args:
|
|
@@ -38,17 +49,11 @@ class KratosSessionAuthentication:
|
|
|
38
49
|
Raises:
|
|
39
50
|
HTTPException: If the cookie is missing.
|
|
40
51
|
"""
|
|
41
|
-
|
|
42
|
-
if not cookie:
|
|
43
|
-
raise HTTPException(
|
|
44
|
-
status_code=401,
|
|
45
|
-
detail="Missing Credentials",
|
|
46
|
-
)
|
|
47
|
-
return cookie
|
|
52
|
+
return request.cookies.get(self._cookie_name, None)
|
|
48
53
|
|
|
49
54
|
async def __call__(
|
|
50
55
|
self, request: Request, kratos_service: Annotated[KratosService, Depends(depends_kratos_service)]
|
|
51
|
-
) -> KratosSessionObject:
|
|
56
|
+
) -> KratosSessionObject | KratosSessionAuthenticationErrors:
|
|
52
57
|
"""Extract the Kratos session from the request.
|
|
53
58
|
|
|
54
59
|
Args:
|
|
@@ -56,23 +61,38 @@ class KratosSessionAuthentication:
|
|
|
56
61
|
kratos_service (KratosService): Kratos service object.
|
|
57
62
|
|
|
58
63
|
Returns:
|
|
59
|
-
KratosSessionObject: Kratos session object.
|
|
64
|
+
KratosSessionObject | KratosSessionAuthenticationErrors: Kratos session object or error.
|
|
60
65
|
|
|
61
66
|
Raises:
|
|
62
|
-
HTTPException: If the session is invalid.
|
|
67
|
+
HTTPException: If the session is invalid and raise_exception is True.
|
|
63
68
|
"""
|
|
64
|
-
cookie: str = self._extract_cookie(request)
|
|
69
|
+
cookie: str | None = self._extract_cookie(request)
|
|
70
|
+
if not cookie:
|
|
71
|
+
if self._raise_exception:
|
|
72
|
+
raise HTTPException(
|
|
73
|
+
status_code=401,
|
|
74
|
+
detail=KratosSessionAuthenticationErrors.MISSING_CREDENTIALS,
|
|
75
|
+
)
|
|
76
|
+
else:
|
|
77
|
+
return KratosSessionAuthenticationErrors.MISSING_CREDENTIALS
|
|
78
|
+
|
|
65
79
|
try:
|
|
66
80
|
session: KratosSessionObject = await kratos_service.whoami(cookie_value=cookie)
|
|
67
81
|
except KratosSessionInvalidError as e:
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
82
|
+
if self._raise_exception:
|
|
83
|
+
raise HTTPException(
|
|
84
|
+
status_code=401,
|
|
85
|
+
detail="Invalid Credentials",
|
|
86
|
+
) from e
|
|
87
|
+
else:
|
|
88
|
+
return KratosSessionAuthenticationErrors.INVALID_CREDENTIALS
|
|
72
89
|
except KratosOperationError as e:
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
90
|
+
if self._raise_exception:
|
|
91
|
+
raise HTTPException(
|
|
92
|
+
status_code=500,
|
|
93
|
+
detail="Internal Server Error",
|
|
94
|
+
) from e
|
|
95
|
+
else:
|
|
96
|
+
return KratosSessionAuthenticationErrors.INTERNAL_SERVER_ERROR
|
|
77
97
|
|
|
78
98
|
return session
|
{fastapi_factory_utilities-0.3.4.dist-info → fastapi_factory_utilities-0.3.5.dist-info}/METADATA
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: fastapi_factory_utilities
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.5
|
|
4
4
|
Summary: Consolidate libraries and utilities to create microservices in Python with FastAPI, Beanie, Httpx, AioPika and OpenTelemetry.
|
|
5
5
|
License: MIT
|
|
6
6
|
Keywords: python,fastapi,beanie,httpx,opentelemetry,microservices
|
{fastapi_factory_utilities-0.3.4.dist-info → fastapi_factory_utilities-0.3.5.dist-info}/RECORD
RENAMED
|
@@ -15,7 +15,7 @@ fastapi_factory_utilities/core/app/fastapi_builder.py,sha256=DgIqiCnJK6cqsG-sg4H
|
|
|
15
15
|
fastapi_factory_utilities/core/app/plugin_manager/__init__.py,sha256=eMfxCsk41Caw_juAawmDZHhytNI_ubXmqfRDug2AzvQ,319
|
|
16
16
|
fastapi_factory_utilities/core/app/plugin_manager/exceptions.py,sha256=CFrZvROT7mLzNpXWwDra1j08lA_7ZrSrOHN94sEEfnQ,1026
|
|
17
17
|
fastapi_factory_utilities/core/app/plugin_manager/plugin_manager.py,sha256=5E_Qp535xNJHNujZ_QRiMfIkDUy9F_3Rbjlclny5P08,6682
|
|
18
|
-
fastapi_factory_utilities/core/exceptions.py,sha256=
|
|
18
|
+
fastapi_factory_utilities/core/exceptions.py,sha256=HAd0RDJNBVc1NOcwfHQo_Xya8SCYuD9edynx3VKMYVs,1784
|
|
19
19
|
fastapi_factory_utilities/core/plugins/__init__.py,sha256=W-BCkqP0xG980980z3mc8T6Vrp1Akv4szA0PRzkUbiU,756
|
|
20
20
|
fastapi_factory_utilities/core/plugins/example/__init__.py,sha256=GF69IygLXxzrCh7VryekEWun663kKBhWtRS3w-1tzBc,1030
|
|
21
21
|
fastapi_factory_utilities/core/plugins/httpx_plugin/__init__.py,sha256=P5FUyv7mQr8RZWQ8ifkoK8GXvqSI71q2b2dm-ag2JhQ,1028
|
|
@@ -25,7 +25,7 @@ fastapi_factory_utilities/core/plugins/odm_plugin/configs.py,sha256=zQoJC1wLNyq2
|
|
|
25
25
|
fastapi_factory_utilities/core/plugins/odm_plugin/depends.py,sha256=OcLsfTLzMBk_xFV6qsMy_-qFkiphEbbEuaHUooagxg8,730
|
|
26
26
|
fastapi_factory_utilities/core/plugins/odm_plugin/documents.py,sha256=BFQYHxHBmTacJRfhZi2OffvT_RAFvAAiDVQAa_d6Y7w,1141
|
|
27
27
|
fastapi_factory_utilities/core/plugins/odm_plugin/exceptions.py,sha256=acnKJB0lGAzDs-7-LjBap8shjP3iV1a7dw7ouPVF27o,551
|
|
28
|
-
fastapi_factory_utilities/core/plugins/odm_plugin/repositories.py,sha256
|
|
28
|
+
fastapi_factory_utilities/core/plugins/odm_plugin/repositories.py,sha256=-SqrdgXyfsqTjFNjri9h9nMG62IP57JrDHPC43S8bqo,11277
|
|
29
29
|
fastapi_factory_utilities/core/plugins/opentelemetry_plugin/__init__.py,sha256=UsXPjiAASn5GIHW8vrF32mklxGNq8ajILV-ty4K1Tbs,4371
|
|
30
30
|
fastapi_factory_utilities/core/plugins/opentelemetry_plugin/builder.py,sha256=9npQImifYAbEg0lFG7KwZ8V78SNrPoaINgd8vKitdMw,12509
|
|
31
31
|
fastapi_factory_utilities/core/plugins/opentelemetry_plugin/configs.py,sha256=pMG9leMB7rtdkdGFLIxXflV7bf9epGrrYPt2N97KZcM,3750
|
|
@@ -33,7 +33,7 @@ fastapi_factory_utilities/core/plugins/opentelemetry_plugin/exceptions.py,sha256
|
|
|
33
33
|
fastapi_factory_utilities/core/plugins/opentelemetry_plugin/helpers.py,sha256=qpTIzX67orJz7vy6SBIwRs24omMBoToJkhpurZRjPuk,1533
|
|
34
34
|
fastapi_factory_utilities/core/protocols.py,sha256=TzZKr_KfmTphk2LL-TD2XzxNlLbihbGM2DxWMhc5lEQ,2428
|
|
35
35
|
fastapi_factory_utilities/core/security/jwt.py,sha256=LuNVmTlONrmoKl7ghNv5JHV4qzMNOwuxJQlWgGSvBoo,5631
|
|
36
|
-
fastapi_factory_utilities/core/security/kratos.py,sha256=
|
|
36
|
+
fastapi_factory_utilities/core/security/kratos.py,sha256=yP9-TkELeXRPRYE9aQRlOPlwvaUJ7VQpyAea8ucWUfg,3364
|
|
37
37
|
fastapi_factory_utilities/core/services/kratos/__init__.py,sha256=DaC29-Ol0WR5vX56IHLGDXP9UrhISq0Juhg_sJTasw4,368
|
|
38
38
|
fastapi_factory_utilities/core/services/kratos/enums.py,sha256=ULJppowlZbOjdnUIXQyI4_nHmHZoNnv7-M1CYQBYXFY,220
|
|
39
39
|
fastapi_factory_utilities/core/services/kratos/exceptions.py,sha256=xAX01-lQvPpADgcwhB5YWSy1UqAxG38s2rlU9AJBJd8,472
|
|
@@ -71,8 +71,8 @@ fastapi_factory_utilities/example/models/books/repository.py,sha256=7K63uAsSEGZ2
|
|
|
71
71
|
fastapi_factory_utilities/example/services/books/__init__.py,sha256=Z06yNRoA7Zg3TGN-Q9rrvJg6Bbx-qJw661MVwukV6vQ,148
|
|
72
72
|
fastapi_factory_utilities/example/services/books/services.py,sha256=-x7d4hotUWLzWo5uImMjFmtNcSTHwWv2bfttIbYYKbA,5380
|
|
73
73
|
fastapi_factory_utilities/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
74
|
-
fastapi_factory_utilities-0.3.
|
|
75
|
-
fastapi_factory_utilities-0.3.
|
|
76
|
-
fastapi_factory_utilities-0.3.
|
|
77
|
-
fastapi_factory_utilities-0.3.
|
|
78
|
-
fastapi_factory_utilities-0.3.
|
|
74
|
+
fastapi_factory_utilities-0.3.5.dist-info/LICENSE,sha256=iO1nLzMMst6vEiqgSUrfrbetM7b0bvdzXhbed5tqG8o,1074
|
|
75
|
+
fastapi_factory_utilities-0.3.5.dist-info/METADATA,sha256=EUeNEhLRsQjQzv216cRp6peFD3U2JCKIZiwFxTSec98,3477
|
|
76
|
+
fastapi_factory_utilities-0.3.5.dist-info/WHEEL,sha256=fGIA9gx4Qxk2KDKeNJCbOEwSrmLtjWCwzBz351GyrPQ,88
|
|
77
|
+
fastapi_factory_utilities-0.3.5.dist-info/entry_points.txt,sha256=IK0VcBexXo4uXQmTrbfhhnnfq4GmXPRn0GBB8hzlsq4,101
|
|
78
|
+
fastapi_factory_utilities-0.3.5.dist-info/RECORD,,
|
{fastapi_factory_utilities-0.3.4.dist-info → fastapi_factory_utilities-0.3.5.dist-info}/LICENSE
RENAMED
|
File without changes
|
{fastapi_factory_utilities-0.3.4.dist-info → fastapi_factory_utilities-0.3.5.dist-info}/WHEEL
RENAMED
|
File without changes
|
|
File without changes
|