jamlib 3.0.0b12.dev1__tar.gz → 3.0.0b12.dev2__tar.gz
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.
- {jamlib-3.0.0b12.dev1/src/jamlib.egg-info → jamlib-3.0.0b12.dev2}/PKG-INFO +1 -1
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/pyproject.toml +1 -1
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/__init__.py +1 -1
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/exceptions/__init__.py +2 -1
- jamlib-3.0.0b12.dev2/src/jam/exceptions/plugins.py +17 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/ext/litestar/__init__.py +2 -1
- jamlib-3.0.0b12.dev2/src/jam/ext/litestar/middleware.py +81 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/ext/litestar/plugins.py +31 -11
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2/src/jamlib.egg-info}/PKG-INFO +1 -1
- jamlib-3.0.0b12.dev1/src/jam/exceptions/plugins.py +0 -10
- jamlib-3.0.0b12.dev1/src/jam/ext/litestar/middleware.py +0 -50
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/LICENSE.md +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/README.md +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/setup.cfg +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/__base__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/__base_encoder__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/__deprecated__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/aio/__base__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/aio/__init__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/aio/instance.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/aio/jwt/__init__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/aio/oauth2/__base__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/aio/oauth2/__init__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/aio/oauth2/builtin/__init__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/aio/oauth2/builtin/github.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/aio/oauth2/builtin/gitlab.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/aio/oauth2/builtin/google.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/aio/oauth2/builtin/yandex.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/aio/oauth2/client.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/aio/sessions/__base__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/aio/sessions/__init__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/aio/sessions/json.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/aio/sessions/redis.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/encoders.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/exceptions/base.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/exceptions/jwt.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/exceptions/oauth2.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/exceptions/paseto.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/exceptions/sessions.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/ext/__init__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/ext/fastapi/__init__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/ext/flask/__init__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/ext/flask/extensions.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/ext/litestar/objects.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/ext/starlette/__init__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/ext/starlette/auth_backends.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/ext/starlette/value.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/instance.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/jwt/__algorithms__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/jwt/__base__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/jwt/__init__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/jwt/__types__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/jwt/lists/__abc_list_repo__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/jwt/lists/__init__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/jwt/lists/json.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/jwt/lists/redis.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/jwt/module.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/jwt/utils.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/logger.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/oauth2/__base__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/oauth2/__init__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/oauth2/builtin/__init__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/oauth2/builtin/github.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/oauth2/builtin/gitlab.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/oauth2/builtin/google.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/oauth2/builtin/yandex.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/oauth2/client.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/otp/__base__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/otp/__init__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/otp/hotp.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/otp/totp.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/paseto/__base__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/paseto/__init__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/paseto/utils.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/paseto/v1.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/paseto/v2.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/paseto/v3.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/paseto/v4.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/py.typed +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/sessions/__base__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/sessions/__init__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/sessions/json.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/sessions/redis.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/tests/__init__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/tests/clients.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/tests/fakers.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/utils/__init__.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/utils/aes.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/utils/await_maybe.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/utils/basic_auth.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/utils/config_maker.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/utils/ed.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/utils/otp_keys.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/utils/rsa.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/utils/salt_hash.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/utils/symmetric.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/utils/xchacha20poly1305.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jam/utils/xor.py +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jamlib.egg-info/SOURCES.txt +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jamlib.egg-info/dependency_links.txt +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jamlib.egg-info/requires.txt +0 -0
- {jamlib-3.0.0b12.dev1 → jamlib-3.0.0b12.dev2}/src/jamlib.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: jamlib
|
|
3
|
-
Version: 3.0.0b12.
|
|
3
|
+
Version: 3.0.0b12.dev2
|
|
4
4
|
Summary: Simple and universal library for authorization.
|
|
5
5
|
Author-email: Makridenko Adrian <adrianmakridenko@duck.com>, Ksenia Travnikova <kseniatravnikova@duck.com>
|
|
6
6
|
License: Apache-2.0
|
|
@@ -36,7 +36,7 @@ from .paseto import (
|
|
|
36
36
|
JamPASETOInvalidTokenFormat
|
|
37
37
|
)
|
|
38
38
|
|
|
39
|
-
from .plugins import JamLitestarPluginConfigError
|
|
39
|
+
from .plugins import JamLitestarPluginConfigError, JamLitestarPluginError
|
|
40
40
|
|
|
41
41
|
from .sessions import (
|
|
42
42
|
JamSessionNotFound,
|
|
@@ -66,6 +66,7 @@ __all__ = [
|
|
|
66
66
|
"JamPASETOInvalidTokenFormat",
|
|
67
67
|
"JamPASTOKeyVerificationError",
|
|
68
68
|
"JamLitestarPluginConfigError",
|
|
69
|
+
"JamLitestarPluginError",
|
|
69
70
|
"JamSessionNotFound",
|
|
70
71
|
"JamSessionEmptyAESKey",
|
|
71
72
|
]
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from jam.exceptions.base import JamConfigurationError, JamError
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class JamLitestarPluginConfigError(JamConfigurationError):
|
|
7
|
+
"""Exception raised when a configuration error occurs for a litestar plugin."""
|
|
8
|
+
|
|
9
|
+
default_message = "Configuration error occurred for a litestar plugin."
|
|
10
|
+
default_code = "jam.configuration.plugin.litestar"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class JamLitestarPluginError(JamError):
|
|
14
|
+
"""Exception raised when an error occurs for a litestar plugin."""
|
|
15
|
+
|
|
16
|
+
default_message = "An error occurred for a litestar plugin."
|
|
17
|
+
default_code = "jam.plugin.litestar"
|
|
@@ -12,7 +12,7 @@ from .objects import (
|
|
|
12
12
|
Token
|
|
13
13
|
)
|
|
14
14
|
|
|
15
|
-
from .plugins import BasePlugin, JamJWTPlugin
|
|
15
|
+
from .plugins import BasePlugin, JamJWTPlugin, JamSessionPlugin
|
|
16
16
|
|
|
17
17
|
__all__ = [
|
|
18
18
|
"BaseUser",
|
|
@@ -20,4 +20,5 @@ __all__ = [
|
|
|
20
20
|
"Token",
|
|
21
21
|
"BasePlugin",
|
|
22
22
|
"JamJWTPlugin",
|
|
23
|
+
"JamSessionPlugin",
|
|
23
24
|
]
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from collections.abc import Callable
|
|
4
|
+
|
|
5
|
+
from litestar.connection import ASGIConnection
|
|
6
|
+
from litestar.middleware import (
|
|
7
|
+
AbstractAuthenticationMiddleware,
|
|
8
|
+
AuthenticationResult,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
from jam.aio.sessions.__base__ import BaseAsyncSessionModule
|
|
12
|
+
from jam.exceptions import JamLitestarPluginConfigError, JamLitestarPluginError
|
|
13
|
+
from jam.ext.litestar.objects import BaseUser, Token
|
|
14
|
+
from jam.jwt import JWT
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class BaseMiddleware(AbstractAuthenticationMiddleware):
|
|
18
|
+
"""Base Jam middleware for litestar."""
|
|
19
|
+
|
|
20
|
+
AUTH_MODULE: Callable
|
|
21
|
+
HEADER_NAME: str | None
|
|
22
|
+
COOKIE_NAME: str | None
|
|
23
|
+
USER: type[BaseUser]
|
|
24
|
+
|
|
25
|
+
def _get_auth_token(self, connection: ASGIConnection) -> str | None:
|
|
26
|
+
if self.HEADER_NAME:
|
|
27
|
+
token = connection.headers.get(self.HEADER_NAME, None)
|
|
28
|
+
elif self.COOKIE_NAME:
|
|
29
|
+
token = connection.cookies.get(self.COOKIE_NAME, None)
|
|
30
|
+
else:
|
|
31
|
+
raise JamLitestarPluginConfigError(
|
|
32
|
+
message="No header or cookie name provided for JWT authentication.",
|
|
33
|
+
details={
|
|
34
|
+
"header_name": self.HEADER_NAME,
|
|
35
|
+
"cookie_name": self.COOKIE_NAME,
|
|
36
|
+
},
|
|
37
|
+
)
|
|
38
|
+
return token
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class JWTMiddleware(BaseMiddleware):
|
|
42
|
+
"""JWT Middleware for litestar."""
|
|
43
|
+
|
|
44
|
+
AUTH_MODULE: JWT
|
|
45
|
+
|
|
46
|
+
async def authenticate_request( # noqa
|
|
47
|
+
self, connection: ASGIConnection
|
|
48
|
+
) -> AuthenticationResult:
|
|
49
|
+
token = self._get_auth_token(connection)
|
|
50
|
+
token_model = Token(token=token)
|
|
51
|
+
if not token:
|
|
52
|
+
return AuthenticationResult(user=None, auth=token_model)
|
|
53
|
+
try:
|
|
54
|
+
data = self.AUTH_MODULE.decode(token)
|
|
55
|
+
user = self.USER.from_payload(data)
|
|
56
|
+
|
|
57
|
+
return AuthenticationResult(user=user, auth=token_model)
|
|
58
|
+
except Exception as e:
|
|
59
|
+
raise JamLitestarPluginError(message=str(e))
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class SessionMiddleware(BaseMiddleware):
|
|
63
|
+
"""Session middleware for litestar."""
|
|
64
|
+
|
|
65
|
+
AUTH_MODULE: BaseAsyncSessionModule
|
|
66
|
+
|
|
67
|
+
async def authenticate_request( # noqa
|
|
68
|
+
self, connection: ASGIConnection
|
|
69
|
+
) -> AuthenticationResult:
|
|
70
|
+
token = self._get_auth_token(connection)
|
|
71
|
+
token_model = Token(token=token)
|
|
72
|
+
if not token:
|
|
73
|
+
return AuthenticationResult(None, auth=token_model)
|
|
74
|
+
try:
|
|
75
|
+
data = await self.AUTH_MODULE.get(token)
|
|
76
|
+
if not data:
|
|
77
|
+
return AuthenticationResult(None, auth=token_model)
|
|
78
|
+
user = self.USER.from_payload(data)
|
|
79
|
+
return AuthenticationResult(user, token)
|
|
80
|
+
except Exception as e:
|
|
81
|
+
raise JamLitestarPluginError(message=str(e))
|
|
@@ -7,7 +7,13 @@ from litestar.config.app import AppConfig
|
|
|
7
7
|
from litestar.di import Provide
|
|
8
8
|
from litestar.plugins import InitPlugin
|
|
9
9
|
|
|
10
|
+
from jam.aio.sessions import create_instance
|
|
10
11
|
from jam.exceptions import JamLitestarPluginConfigError
|
|
12
|
+
from jam.ext.litestar.middleware import (
|
|
13
|
+
BaseMiddleware,
|
|
14
|
+
JWTMiddleware,
|
|
15
|
+
SessionMiddleware,
|
|
16
|
+
)
|
|
11
17
|
from jam.ext.litestar.objects import BaseUser
|
|
12
18
|
from jam.jwt import JWT
|
|
13
19
|
from jam.utils.config_maker import GENERIC_POINTER, __config_maker__
|
|
@@ -17,16 +23,13 @@ class BasePlugin(InitPlugin):
|
|
|
17
23
|
"""Base Litestar plugin."""
|
|
18
24
|
|
|
19
25
|
MODULE: Callable
|
|
26
|
+
_MIDDLEWARE: type[BaseMiddleware]
|
|
27
|
+
_DI_KEY: str
|
|
28
|
+
_CONFIG_KEY: str
|
|
20
29
|
|
|
21
30
|
def _setup_config(self, config: dict[str, Any]) -> None:
|
|
22
31
|
self._auth = self.MODULE(**config)
|
|
23
32
|
|
|
24
|
-
|
|
25
|
-
class JamJWTPlugin(BasePlugin):
|
|
26
|
-
"""JWT plugin for litestar."""
|
|
27
|
-
|
|
28
|
-
MODULE = JWT
|
|
29
|
-
|
|
30
33
|
def __init__( # noqa
|
|
31
34
|
self,
|
|
32
35
|
config: dict[str, Any] | str | None = None,
|
|
@@ -57,13 +60,12 @@ class JamJWTPlugin(BasePlugin):
|
|
|
57
60
|
__config_maker__(config, pointer) if config else None
|
|
58
61
|
)
|
|
59
62
|
|
|
60
|
-
|
|
63
|
+
params = _config.pop(self._CONFIG_KEY) if _config else kwargs
|
|
64
|
+
self._setup_config(params)
|
|
61
65
|
self._middleware = None
|
|
62
66
|
|
|
63
67
|
if middleware:
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
_middleware = JWTMiddleware
|
|
68
|
+
_middleware = self._MIDDLEWARE
|
|
67
69
|
_middleware.COOKIE_NAME = cookie_name
|
|
68
70
|
_middleware.HEADER_NAME = header_name
|
|
69
71
|
_middleware.AUTH_MODULE = self._auth
|
|
@@ -71,10 +73,28 @@ class JamJWTPlugin(BasePlugin):
|
|
|
71
73
|
self._middleware = _middleware
|
|
72
74
|
|
|
73
75
|
def on_app_init(self, app_config: AppConfig) -> AppConfig: # noqa
|
|
74
|
-
app_config.dependencies[
|
|
76
|
+
app_config.dependencies[self._DI_KEY] = Provide(
|
|
75
77
|
lambda: self._auth, sync_to_thread=True
|
|
76
78
|
)
|
|
77
79
|
app_config.middleware.append(
|
|
78
80
|
self._middleware
|
|
79
81
|
) if self._middleware else None
|
|
80
82
|
return app_config
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class JamJWTPlugin(BasePlugin):
|
|
86
|
+
"""JWT plugin for litestar."""
|
|
87
|
+
|
|
88
|
+
MODULE = JWT
|
|
89
|
+
_MIDDLEWARE = JWTMiddleware
|
|
90
|
+
_DI_KEY = "jwt"
|
|
91
|
+
_CONFIG_KEY = "jwt"
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class JamSessionPlugin(BasePlugin):
|
|
95
|
+
"""Redis sessions plugin for litestar."""
|
|
96
|
+
|
|
97
|
+
MODULE = staticmethod(create_instance)
|
|
98
|
+
_MIDDLEWARE = SessionMiddleware
|
|
99
|
+
_DI_KEY = "session"
|
|
100
|
+
_CONFIG_KEY = "sessions"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: jamlib
|
|
3
|
-
Version: 3.0.0b12.
|
|
3
|
+
Version: 3.0.0b12.dev2
|
|
4
4
|
Summary: Simple and universal library for authorization.
|
|
5
5
|
Author-email: Makridenko Adrian <adrianmakridenko@duck.com>, Ksenia Travnikova <kseniatravnikova@duck.com>
|
|
6
6
|
License: Apache-2.0
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
|
|
3
|
-
from jam.exceptions.base import JamConfigurationError
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
class JamLitestarPluginConfigError(JamConfigurationError):
|
|
7
|
-
"""Exception raised when a configuration error occurs for a litestar plugin."""
|
|
8
|
-
|
|
9
|
-
default_message = "Configuration error occurred for a litestar plugin."
|
|
10
|
-
default_code = "jam.configuration.plugin"
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
|
|
3
|
-
from litestar.connection import ASGIConnection
|
|
4
|
-
from litestar.middleware import (
|
|
5
|
-
AbstractAuthenticationMiddleware,
|
|
6
|
-
AuthenticationResult,
|
|
7
|
-
)
|
|
8
|
-
|
|
9
|
-
from jam.exceptions import JamLitestarPluginConfigError
|
|
10
|
-
from jam.ext.litestar.objects import BaseUser, Token
|
|
11
|
-
from jam.jwt import JWT
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
class JWTMiddleware(AbstractAuthenticationMiddleware):
|
|
15
|
-
"""Middleware created by jam plugin."""
|
|
16
|
-
|
|
17
|
-
HEADER_NAME: str | None
|
|
18
|
-
COOKIE_NAME: str | None
|
|
19
|
-
AUTH_MODULE: JWT
|
|
20
|
-
USER: type[BaseUser]
|
|
21
|
-
|
|
22
|
-
def __get_auth_token(self, connection: ASGIConnection) -> str | None:
|
|
23
|
-
if self.HEADER_NAME:
|
|
24
|
-
token = connection.headers.get(self.HEADER_NAME, None)
|
|
25
|
-
elif self.COOKIE_NAME:
|
|
26
|
-
token = connection.cookies.get(self.COOKIE_NAME, None)
|
|
27
|
-
else:
|
|
28
|
-
raise JamLitestarPluginConfigError(
|
|
29
|
-
message="No header or cookie name provided for JWT authentication.",
|
|
30
|
-
details={
|
|
31
|
-
"header_name": self.HEADER_NAME,
|
|
32
|
-
"cookie_name": self.COOKIE_NAME,
|
|
33
|
-
},
|
|
34
|
-
)
|
|
35
|
-
return token
|
|
36
|
-
|
|
37
|
-
async def authenticate_request( # noqa
|
|
38
|
-
self, connection: ASGIConnection
|
|
39
|
-
) -> AuthenticationResult:
|
|
40
|
-
token = self.__get_auth_token(connection)
|
|
41
|
-
token_model = Token(token=token)
|
|
42
|
-
if not token:
|
|
43
|
-
return AuthenticationResult(None, auth=token_model)
|
|
44
|
-
try:
|
|
45
|
-
data = self.AUTH_MODULE.decode(token)
|
|
46
|
-
user = self.USER.from_payload(data)
|
|
47
|
-
|
|
48
|
-
return AuthenticationResult(user, token_model)
|
|
49
|
-
except Exception:
|
|
50
|
-
raise Exception # TODO: нормальная ошибка
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|