xn-auth 0.2.38.dev1__py3-none-any.whl → 0.2.40__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.
- x_auth/controller.py +24 -11
- x_auth/models.py +8 -14
- x_auth/types.py +26 -2
- {xn_auth-0.2.38.dev1.dist-info → xn_auth-0.2.40.dist-info}/METADATA +2 -1
- xn_auth-0.2.40.dist-info/RECORD +10 -0
- xn_auth-0.2.38.dev1.dist-info/RECORD +0 -10
- {xn_auth-0.2.38.dev1.dist-info → xn_auth-0.2.40.dist-info}/WHEEL +0 -0
- {xn_auth-0.2.38.dev1.dist-info → xn_auth-0.2.40.dist-info}/top_level.txt +0 -0
x_auth/controller.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from datetime import timedelta
|
|
2
2
|
|
|
3
|
-
from aiogram.utils.
|
|
3
|
+
from aiogram.utils.auth_widget import check_signature
|
|
4
|
+
from aiogram.utils.web_app import WebAppInitData, safe_parse_webapp_init_data, WebAppUser
|
|
4
5
|
from litestar import Response, post
|
|
5
6
|
from litestar.connection import ASGIConnection
|
|
6
7
|
from litestar.exceptions import NotAuthorizedException
|
|
@@ -8,7 +9,7 @@ from litestar.security.jwt import JWTCookieAuth
|
|
|
8
9
|
|
|
9
10
|
from x_auth.middleware import JWTAuthMiddleware, Tok
|
|
10
11
|
from x_auth.models import User
|
|
11
|
-
from x_auth.types import AuthUser
|
|
12
|
+
from x_auth.types import AuthUser, TgUser
|
|
12
13
|
|
|
13
14
|
|
|
14
15
|
async def retrieve_user_handler(token: Tok, _cn: ASGIConnection) -> AuthUser:
|
|
@@ -33,20 +34,32 @@ class Auth:
|
|
|
33
34
|
exclude=["/schema", "/auth", "/public"] + (exc_paths or []),
|
|
34
35
|
)
|
|
35
36
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
try:
|
|
39
|
-
twaid: WebAppInitData = safe_parse_webapp_init_data(self.jwt.token_secret, tid)
|
|
40
|
-
except ValueError:
|
|
41
|
-
raise NotAuthorizedException(detail="Tg Initdata invalid")
|
|
42
|
-
user_in = await user_model.tg2in(twaid.user)
|
|
37
|
+
async def user_proc(user: WebAppUser) -> Response[WebAppUser]:
|
|
38
|
+
user_in = await user_model.tg2in(user)
|
|
43
39
|
db_user, cr = await user_model.update_or_create(**user_in.df_unq()) # on login: update user in db from tg
|
|
44
40
|
res = self.jwt.login(
|
|
45
41
|
identifier=str(db_user.id),
|
|
46
42
|
token_extras={"role": db_user.role, "blocked": db_user.blocked},
|
|
47
|
-
response_body=
|
|
43
|
+
response_body=user,
|
|
48
44
|
)
|
|
49
45
|
res.cookies[0].httponly = False
|
|
50
46
|
return res
|
|
51
47
|
|
|
52
|
-
|
|
48
|
+
# login for api endpoint
|
|
49
|
+
@post("/auth/twa", tags=["Auth"], description="Gen JWToken from tg login widget")
|
|
50
|
+
async def twa(data: TgUser) -> Response[WebAppUser]: # widget
|
|
51
|
+
dct = data.dump()
|
|
52
|
+
if not check_signature(self.jwt.token_secret, dct.pop("hash"), **dct):
|
|
53
|
+
raise NotAuthorizedException("Tg login widget data invalid")
|
|
54
|
+
return await user_proc(WebAppUser(**dct))
|
|
55
|
+
|
|
56
|
+
@post("/auth/tma", tags=["Auth"], description="Gen JWToken from tg initData")
|
|
57
|
+
async def tma(tid: str) -> Response[WebAppUser]:
|
|
58
|
+
try:
|
|
59
|
+
twaid: WebAppInitData = safe_parse_webapp_init_data(self.jwt.token_secret, tid)
|
|
60
|
+
except ValueError:
|
|
61
|
+
raise NotAuthorizedException(detail="Tg Initdata invalid")
|
|
62
|
+
return await user_proc(twaid.user)
|
|
63
|
+
|
|
64
|
+
self.tma_handler = tma
|
|
65
|
+
self.twa_handler = twa
|
x_auth/models.py
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
from datetime import datetime
|
|
2
2
|
|
|
3
|
+
from aiogram.utils.web_app import WebAppUser
|
|
3
4
|
from aiohttp import ClientSession
|
|
4
5
|
from msgspec import convert
|
|
5
6
|
from pyrogram.enums.client_platform import ClientPlatform
|
|
6
|
-
from pyrogram.types import User as
|
|
7
|
+
from pyrogram.types import User as PyroUser
|
|
8
|
+
from aiogram.types import User as AioUser
|
|
7
9
|
from tortoise.fields import (
|
|
8
10
|
BigIntField,
|
|
9
11
|
BooleanField,
|
|
@@ -31,7 +33,6 @@ from tortoise import Model as TortModel
|
|
|
31
33
|
from x_model.types import BaseUpd
|
|
32
34
|
|
|
33
35
|
from x_auth.enums import Lang, Role, PeerType
|
|
34
|
-
from x_auth.types import AuthUser
|
|
35
36
|
|
|
36
37
|
|
|
37
38
|
class Username(TortModel):
|
|
@@ -48,6 +49,7 @@ class User(Model):
|
|
|
48
49
|
username: OneToOneRelation[Username] = OneToOneField("models.Username", "user")
|
|
49
50
|
username_id: int
|
|
50
51
|
first_name: str | None = CharField(63)
|
|
52
|
+
pic: str | None = CharField(127, null=True)
|
|
51
53
|
last_name: str | None = CharField(31, null=True)
|
|
52
54
|
blocked: bool = BooleanField(default=False)
|
|
53
55
|
lang: Lang | None = IntEnumField(Lang, default=Lang.ru, null=True)
|
|
@@ -55,11 +57,8 @@ class User(Model):
|
|
|
55
57
|
|
|
56
58
|
app: BackwardOneToOneRelation["App"]
|
|
57
59
|
|
|
58
|
-
def get_auth(self) -> AuthUser:
|
|
59
|
-
return AuthUser.model_validate(self, from_attributes=True)
|
|
60
|
-
|
|
61
60
|
@classmethod
|
|
62
|
-
async def tg2in(cls, u:
|
|
61
|
+
async def tg2in(cls, u: PyroUser | AioUser | WebAppUser, blocked: bool = None) -> BaseUpd:
|
|
63
62
|
un, _ = await cls._meta.fields_map["username"].related_model.update_or_create({"username": u.username}, id=u.id)
|
|
64
63
|
user = cls.validate(
|
|
65
64
|
{
|
|
@@ -67,6 +66,7 @@ class User(Model):
|
|
|
67
66
|
"last_name": u.last_name,
|
|
68
67
|
"username_id": un.id,
|
|
69
68
|
"lang": u.language_code and Lang[u.language_code],
|
|
69
|
+
"pic": u.photo_url and u.photo_url.replace("https://t.me/i/userpic/320/", "")[:-4],
|
|
70
70
|
}
|
|
71
71
|
)
|
|
72
72
|
if blocked is not None:
|
|
@@ -78,7 +78,7 @@ class User(Model):
|
|
|
78
78
|
return (await cls[int(sid)]).blocked
|
|
79
79
|
|
|
80
80
|
@classmethod
|
|
81
|
-
async def
|
|
81
|
+
async def tg_upsert(cls, u: PyroUser | AioUser, blocked: bool = None) -> tuple["User", bool]:
|
|
82
82
|
user_in: cls.in_type() = await cls.tg2in(u, blocked)
|
|
83
83
|
prms = user_in.df_unq()
|
|
84
84
|
return await cls.update_or_create(**prms)
|
|
@@ -87,13 +87,6 @@ class User(Model):
|
|
|
87
87
|
# abstract = True
|
|
88
88
|
|
|
89
89
|
|
|
90
|
-
# @pre_save(User)
|
|
91
|
-
# async def username(_meta, user: User, _db, _updated: dict) -> None:
|
|
92
|
-
# if user.username_id:
|
|
93
|
-
# return
|
|
94
|
-
# user.username = await Username.create(name=user.username)
|
|
95
|
-
|
|
96
|
-
|
|
97
90
|
class Country(Model):
|
|
98
91
|
id = SmallIntField(True)
|
|
99
92
|
code: int | None = IntField(null=True)
|
|
@@ -170,6 +163,7 @@ class Proxy(Model, TsTrait):
|
|
|
170
163
|
return dict(scheme="socks5", hostname=self.host, port=self.port, username=self.username, password=self.password)
|
|
171
164
|
|
|
172
165
|
def str(self):
|
|
166
|
+
# noinspection HttpUrlsUsage
|
|
173
167
|
return f"http://{self.username}:{self.password}@{self.host}:{self.port}"
|
|
174
168
|
|
|
175
169
|
|
x_auth/types.py
CHANGED
|
@@ -1,11 +1,25 @@
|
|
|
1
1
|
from datetime import datetime
|
|
2
|
-
from
|
|
2
|
+
from json import dumps
|
|
3
|
+
from typing import Literal, Self
|
|
3
4
|
|
|
4
|
-
from msgspec import Struct
|
|
5
|
+
from msgspec import Struct, to_builtins, convert
|
|
5
6
|
|
|
6
7
|
from x_auth.enums import Role
|
|
7
8
|
|
|
8
9
|
|
|
10
|
+
class Xs(Struct):
|
|
11
|
+
def dump(self, nones: bool = False) -> dict:
|
|
12
|
+
return {k: v for k, v in to_builtins(self).items() if nones or v is not None}
|
|
13
|
+
|
|
14
|
+
def json(self, nones: bool = False) -> str:
|
|
15
|
+
return dumps(self.dump())
|
|
16
|
+
|
|
17
|
+
@classmethod
|
|
18
|
+
def load(cls, obj, **kwargs) -> Self:
|
|
19
|
+
dct = dict(obj)
|
|
20
|
+
return convert({**dct, **kwargs}, cls) # , strict=False
|
|
21
|
+
|
|
22
|
+
|
|
9
23
|
class AuthUser(Struct):
|
|
10
24
|
id: int
|
|
11
25
|
blocked: bool
|
|
@@ -35,3 +49,13 @@ class Replacement(Struct):
|
|
|
35
49
|
proxy_port: int
|
|
36
50
|
proxy_country_code: str
|
|
37
51
|
created_at: datetime
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class TgUser(Xs):
|
|
55
|
+
id: int
|
|
56
|
+
first_name: str
|
|
57
|
+
auth_date: int
|
|
58
|
+
hash: str
|
|
59
|
+
username: str | None = None
|
|
60
|
+
photo_url: str | None = None
|
|
61
|
+
last_name: str | None = None
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: xn-auth
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.40
|
|
4
4
|
Summary: Auth adapter for XN-Api framework
|
|
5
5
|
Author-email: Artemiev <mixartemev@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -10,6 +10,7 @@ Keywords: litestar,jwt,auth
|
|
|
10
10
|
Requires-Python: >=3.11
|
|
11
11
|
Description-Content-Type: text/markdown
|
|
12
12
|
Requires-Dist: aiogram
|
|
13
|
+
Requires-Dist: kurigram
|
|
13
14
|
Requires-Dist: msgspec
|
|
14
15
|
Requires-Dist: pyjwt
|
|
15
16
|
Requires-Dist: xn-model
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
x_auth/controller.py,sha256=8OVENJa5rUHui-8qswKBs2wYnU_PzQ74cDneszehcxo,2857
|
|
2
|
+
x_auth/enums.py,sha256=l4NTYsA-h0gyOp4PUe40Lb8LKoA94zL6EDkCmoGmBL0,732
|
|
3
|
+
x_auth/exceptions.py,sha256=2B4okJxhPyNqTJXlSTfblJUQJ60bLGXdgJIu6ue7S6w,162
|
|
4
|
+
x_auth/middleware.py,sha256=JfssQomDv0J_69GfS2a_2_uyRzs26zSY6IW1Vk7m8K0,2900
|
|
5
|
+
x_auth/models.py,sha256=m_7ANZMcuxRAW56dgGt86r843Wdib436xV-_e4CBLZ0,8832
|
|
6
|
+
x_auth/types.py,sha256=zojizO58op_BUFvmxSaOtNoPB_liBCY3x3Ff80bgBMo,1310
|
|
7
|
+
xn_auth-0.2.40.dist-info/METADATA,sha256=fKBWmd-GVj257JtxRFmPlvdRrRDtA20P3vyq6ZpdIIw,823
|
|
8
|
+
xn_auth-0.2.40.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
9
|
+
xn_auth-0.2.40.dist-info/top_level.txt,sha256=ydMDkzxgQPtW-E_MNDfUAroAFZvWSqU-x_kZSA7NSFo,7
|
|
10
|
+
xn_auth-0.2.40.dist-info/RECORD,,
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
x_auth/controller.py,sha256=0QhqmqXOqFaWIUTWHlJKITQNmJQ5kXZOkLg7wSUTh60,2240
|
|
2
|
-
x_auth/enums.py,sha256=l4NTYsA-h0gyOp4PUe40Lb8LKoA94zL6EDkCmoGmBL0,732
|
|
3
|
-
x_auth/exceptions.py,sha256=2B4okJxhPyNqTJXlSTfblJUQJ60bLGXdgJIu6ue7S6w,162
|
|
4
|
-
x_auth/middleware.py,sha256=JfssQomDv0J_69GfS2a_2_uyRzs26zSY6IW1Vk7m8K0,2900
|
|
5
|
-
x_auth/models.py,sha256=bTSehdkdsF4YzhGnAmpsPXiLFyq8lG0l30kpgfXA8ZE,8859
|
|
6
|
-
x_auth/types.py,sha256=j3WGcyH24DmFEdTT6U7xzb_fEm1tFcBZsANKMy7bydo,689
|
|
7
|
-
xn_auth-0.2.38.dev1.dist-info/METADATA,sha256=awMEXeoPJJTXmdPdVcYnpe_6c2oPgOhWbfd1qWaUpgM,804
|
|
8
|
-
xn_auth-0.2.38.dev1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
9
|
-
xn_auth-0.2.38.dev1.dist-info/top_level.txt,sha256=ydMDkzxgQPtW-E_MNDfUAroAFZvWSqU-x_kZSA7NSFo,7
|
|
10
|
-
xn_auth-0.2.38.dev1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|