xn-auth 0.2.40__tar.gz → 0.2.41__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.
- {xn_auth-0.2.40/xn_auth.egg-info → xn_auth-0.2.41}/PKG-INFO +1 -1
- {xn_auth-0.2.40 → xn_auth-0.2.41}/x_auth/controller.py +8 -7
- {xn_auth-0.2.40 → xn_auth-0.2.41}/x_auth/models.py +6 -0
- {xn_auth-0.2.40 → xn_auth-0.2.41}/x_auth/types.py +5 -0
- {xn_auth-0.2.40 → xn_auth-0.2.41/xn_auth.egg-info}/PKG-INFO +1 -1
- {xn_auth-0.2.40 → xn_auth-0.2.41}/.env.dist +0 -0
- {xn_auth-0.2.40 → xn_auth-0.2.41}/.gitignore +0 -0
- {xn_auth-0.2.40 → xn_auth-0.2.41}/.pre-commit-config.yaml +0 -0
- {xn_auth-0.2.40 → xn_auth-0.2.41}/README.md +0 -0
- {xn_auth-0.2.40 → xn_auth-0.2.41}/makefile +0 -0
- {xn_auth-0.2.40 → xn_auth-0.2.41}/pyproject.toml +0 -0
- {xn_auth-0.2.40 → xn_auth-0.2.41}/setup.cfg +0 -0
- {xn_auth-0.2.40 → xn_auth-0.2.41}/x_auth/enums.py +0 -0
- {xn_auth-0.2.40 → xn_auth-0.2.41}/x_auth/exceptions.py +0 -0
- {xn_auth-0.2.40 → xn_auth-0.2.41}/x_auth/middleware.py +0 -0
- {xn_auth-0.2.40 → xn_auth-0.2.41}/xn_auth.egg-info/SOURCES.txt +0 -0
- {xn_auth-0.2.40 → xn_auth-0.2.41}/xn_auth.egg-info/dependency_links.txt +0 -0
- {xn_auth-0.2.40 → xn_auth-0.2.41}/xn_auth.egg-info/requires.txt +0 -0
- {xn_auth-0.2.40 → xn_auth-0.2.41}/xn_auth.egg-info/top_level.txt +0 -0
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
from base64 import b64encode
|
|
1
2
|
from datetime import timedelta
|
|
2
3
|
|
|
3
4
|
from aiogram.utils.auth_widget import check_signature
|
|
@@ -9,7 +10,7 @@ from litestar.security.jwt import JWTCookieAuth
|
|
|
9
10
|
|
|
10
11
|
from x_auth.middleware import JWTAuthMiddleware, Tok
|
|
11
12
|
from x_auth.models import User
|
|
12
|
-
from x_auth.types import AuthUser, TgUser
|
|
13
|
+
from x_auth.types import AuthUser, TgUser, XyncUser
|
|
13
14
|
|
|
14
15
|
|
|
15
16
|
async def retrieve_user_handler(token: Tok, _cn: ASGIConnection) -> AuthUser:
|
|
@@ -34,31 +35,31 @@ class Auth:
|
|
|
34
35
|
exclude=["/schema", "/auth", "/public"] + (exc_paths or []),
|
|
35
36
|
)
|
|
36
37
|
|
|
37
|
-
async def user_proc(user: WebAppUser) -> Response[
|
|
38
|
+
async def user_proc(user: WebAppUser) -> Response[XyncUser]:
|
|
38
39
|
user_in = await user_model.tg2in(user)
|
|
39
40
|
db_user, cr = await user_model.update_or_create(**user_in.df_unq()) # on login: update user in db from tg
|
|
40
41
|
res = self.jwt.login(
|
|
41
42
|
identifier=str(db_user.id),
|
|
42
43
|
token_extras={"role": db_user.role, "blocked": db_user.blocked},
|
|
43
|
-
response_body=user,
|
|
44
|
+
response_body=XyncUser.model_validate({**user.model_dump(), "pub": b64encode(db_user.pub)}),
|
|
44
45
|
)
|
|
45
46
|
res.cookies[0].httponly = False
|
|
46
47
|
return res
|
|
47
48
|
|
|
48
49
|
# login for api endpoint
|
|
49
50
|
@post("/auth/twa", tags=["Auth"], description="Gen JWToken from tg login widget")
|
|
50
|
-
async def twa(data: TgUser) -> Response[
|
|
51
|
+
async def twa(data: TgUser) -> Response[XyncUser]: # widget
|
|
51
52
|
dct = data.dump()
|
|
52
53
|
if not check_signature(self.jwt.token_secret, dct.pop("hash"), **dct):
|
|
53
54
|
raise NotAuthorizedException("Tg login widget data invalid")
|
|
54
55
|
return await user_proc(WebAppUser(**dct))
|
|
55
56
|
|
|
56
57
|
@post("/auth/tma", tags=["Auth"], description="Gen JWToken from tg initData")
|
|
57
|
-
async def tma(tid: str) -> Response[
|
|
58
|
+
async def tma(tid: str) -> Response[XyncUser]:
|
|
58
59
|
try:
|
|
59
60
|
twaid: WebAppInitData = safe_parse_webapp_init_data(self.jwt.token_secret, tid)
|
|
60
|
-
except ValueError:
|
|
61
|
-
raise NotAuthorizedException(detail="Tg Initdata invalid")
|
|
61
|
+
except ValueError as e:
|
|
62
|
+
raise NotAuthorizedException(detail=f"Tg Initdata invalid {e}")
|
|
62
63
|
return await user_proc(twaid.user)
|
|
63
64
|
|
|
64
65
|
self.tma_handler = tma
|
|
@@ -35,6 +35,10 @@ from x_model.types import BaseUpd
|
|
|
35
35
|
from x_auth.enums import Lang, Role, PeerType
|
|
36
36
|
|
|
37
37
|
|
|
38
|
+
class UniqBinaryField(BinaryField):
|
|
39
|
+
indexable = True
|
|
40
|
+
|
|
41
|
+
|
|
38
42
|
class Username(TortModel):
|
|
39
43
|
id: int = BigIntField(True, description="tg_id")
|
|
40
44
|
username: str = CharField(127, null=True)
|
|
@@ -54,6 +58,8 @@ class User(Model):
|
|
|
54
58
|
blocked: bool = BooleanField(default=False)
|
|
55
59
|
lang: Lang | None = IntEnumField(Lang, default=Lang.ru, null=True)
|
|
56
60
|
role: Role = IntEnumField(Role, default=Role.READER)
|
|
61
|
+
# prv = BinaryField(null=True) # len=32
|
|
62
|
+
pub = UniqBinaryField(unique=True, null=True) # len=32
|
|
57
63
|
|
|
58
64
|
app: BackwardOneToOneRelation["App"]
|
|
59
65
|
|
|
@@ -2,6 +2,7 @@ from datetime import datetime
|
|
|
2
2
|
from json import dumps
|
|
3
3
|
from typing import Literal, Self
|
|
4
4
|
|
|
5
|
+
from aiogram.utils.web_app import WebAppUser
|
|
5
6
|
from msgspec import Struct, to_builtins, convert
|
|
6
7
|
|
|
7
8
|
from x_auth.enums import Role
|
|
@@ -59,3 +60,7 @@ class TgUser(Xs):
|
|
|
59
60
|
username: str | None = None
|
|
60
61
|
photo_url: str | None = None
|
|
61
62
|
last_name: str | None = None
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class XyncUser(WebAppUser):
|
|
66
|
+
pub: bytes
|
|
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
|