xn-auth 0.2.41.dev1__py3-none-any.whl → 0.2.43__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 CHANGED
@@ -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,27 +35,27 @@ class Auth:
34
35
  exclude=["/schema", "/auth", "/public"] + (exc_paths or []),
35
36
  )
36
37
 
37
- async def user_proc(user: WebAppUser) -> Response[WebAppUser]:
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[WebAppUser]: # widget
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[WebAppUser]:
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
61
  except ValueError as e:
x_auth/models.py CHANGED
@@ -18,16 +18,16 @@ from tortoise.fields import (
18
18
  OneToOneRelation,
19
19
  OneToOneNullableRelation,
20
20
  OneToOneField,
21
- SmallIntField,
22
21
  BackwardOneToOneRelation,
23
22
  BackwardFKRelation,
24
23
  ForeignKeyNullableRelation,
25
24
  JSONField,
25
+ CASCADE,
26
26
  )
27
27
  from tortoise.fields.data import CharEnumFieldInstance
28
28
 
29
29
  from x_auth import types
30
- from x_model.field import DatetimeSecField
30
+ from x_model.field import DatetimeSecField, UInt8Field, UniqBinaryField, UInt1Field, UInt2Field
31
31
  from x_model.models import Model, TsTrait
32
32
  from tortoise import Model as TortModel
33
33
  from x_model.types import BaseUpd
@@ -36,9 +36,9 @@ from x_auth.enums import Lang, Role, PeerType
36
36
 
37
37
 
38
38
  class Username(TortModel):
39
- id: int = BigIntField(True, description="tg_id")
39
+ id: int = UInt8Field(True, description="tg_id")
40
40
  username: str = CharField(127, null=True)
41
- phone = BigIntField(null=True)
41
+ phone = UInt8Field(null=True)
42
42
 
43
43
  user: BackwardOneToOneRelation["User"]
44
44
  peers: BackwardFKRelation["Peer"]
@@ -46,7 +46,7 @@ class Username(TortModel):
46
46
 
47
47
 
48
48
  class User(Model):
49
- username: OneToOneRelation[Username] = OneToOneField("models.Username", "user")
49
+ username: OneToOneRelation[Username] = OneToOneField("models.Username", "user", on_update=CASCADE)
50
50
  username_id: int
51
51
  first_name: str | None = CharField(63)
52
52
  pic: str | None = CharField(127, null=True)
@@ -54,6 +54,7 @@ class User(Model):
54
54
  blocked: bool = BooleanField(default=False)
55
55
  lang: Lang | None = IntEnumField(Lang, default=Lang.ru, null=True)
56
56
  role: Role = IntEnumField(Role, default=Role.READER)
57
+ pub = UniqBinaryField(unique=True, null=True) # len=32
57
58
 
58
59
  app: BackwardOneToOneRelation["App"]
59
60
 
@@ -88,8 +89,8 @@ class User(Model):
88
89
 
89
90
 
90
91
  class Country(Model):
91
- id = SmallIntField(True)
92
- code: int | None = IntField(null=True)
92
+ id = UInt1Field(True)
93
+ code: int | None = UInt2Field(null=True)
93
94
  short: str | None = CharField(3, null=True)
94
95
  name: str | None = CharField(63, unique=True, null=True)
95
96
 
@@ -99,11 +100,13 @@ class Country(Model):
99
100
  class Proxy(Model, TsTrait):
100
101
  id: int = CharField(63, primary_key=True)
101
102
  host: str = CharField(63)
102
- port: str = IntField()
103
+ port: str = UInt2Field()
103
104
  username: str = CharField(63)
104
105
  password: str = CharField(63)
105
106
  valid: bool = BooleanField(null=True)
106
- country: ForeignKeyRelation[Country] = ForeignKeyField("models.Country", "proxies", null=True) # todo rm nullable
107
+ country: ForeignKeyRelation[Country] = ForeignKeyField(
108
+ "models.Country", "proxies", on_update=CASCADE, null=True
109
+ ) # todo rm nullable
107
110
 
108
111
  class Meta:
109
112
  unique_together = (("host", "port"),)
@@ -168,7 +171,7 @@ class Proxy(Model, TsTrait):
168
171
 
169
172
 
170
173
  class Dc(TortModel):
171
- id: int = SmallIntField(True)
174
+ id: int = UInt1Field(True)
172
175
  ip = CharField(15, unique=True)
173
176
  pub = CharField(495, null=True)
174
177
 
@@ -177,41 +180,42 @@ class Dc(TortModel):
177
180
 
178
181
 
179
182
  class Fcm(TortModel):
180
- id: int = SmallIntField(True)
183
+ id: int = UInt1Field(True)
181
184
  json: dict = JSONField(default={})
182
185
 
183
186
  apps: BackwardFKRelation["App"]
184
187
 
185
188
 
186
189
  class App(Model):
190
+ id: int = IntField(True)
187
191
  hsh = CharField(32, unique=True)
188
- dc: ForeignKeyRelation[Dc] = ForeignKeyField("models.Dc", "apps", default=2)
192
+ dc: ForeignKeyRelation[Dc] = ForeignKeyField("models.Dc", "apps", on_update=CASCADE, default=2)
189
193
  dc_id: int
190
194
  title = CharField(127)
191
195
  short = CharField(76)
192
196
  ver = CharField(18, default="0.0.1")
193
- fcm: ForeignKeyNullableRelation[Fcm] = ForeignKeyField("models.Fcm", "apps", null=True)
197
+ fcm: ForeignKeyNullableRelation[Fcm] = ForeignKeyField("models.Fcm", "apps", on_update=CASCADE, null=True)
194
198
  fcm_id: int
195
199
  platform: ClientPlatform = CharEnumFieldInstance(ClientPlatform)
196
- owner: OneToOneNullableRelation["User"] = OneToOneField("models.User", "app", null=True)
200
+ owner: OneToOneNullableRelation["User"] = OneToOneField("models.User", "app", on_update=CASCADE, null=True)
197
201
 
198
202
  sessions: BackwardFKRelation["Session"]
199
203
 
200
204
 
201
205
  class Session(TortModel):
202
206
  id = BigIntField(True)
203
- api: ForeignKeyRelation[App] = ForeignKeyField("models.App", "sessions")
207
+ api: ForeignKeyRelation[App] = ForeignKeyField("models.App", "sessions", on_update=CASCADE)
204
208
  api_id: int
205
- dc: ForeignKeyRelation[Dc] = ForeignKeyField("models.Dc", "sessions", default=2)
209
+ dc: ForeignKeyRelation[Dc] = ForeignKeyField("models.Dc", "sessions", on_update=CASCADE, default=2)
206
210
  dc_id: int
207
211
  test_mode = BooleanField(default=False)
208
212
  auth_key = BinaryField(null=True)
209
213
  date = IntField(default=0) # todo: refact to datetime?
210
- user: OneToOneNullableRelation[Username] = OneToOneField("models.Username", "session", null=True)
214
+ user: OneToOneNullableRelation[Username] = OneToOneField("models.Username", "session", on_update=CASCADE, null=True)
211
215
  user_id: int
212
216
  is_bot = CharField(42, null=True)
213
217
  state: dict = JSONField(default={})
214
- proxy: ForeignKeyNullableRelation[Proxy] = ForeignKeyField("models.Proxy", "sessions", null=True)
218
+ proxy: ForeignKeyNullableRelation[Proxy] = ForeignKeyField("models.Proxy", "sessions", on_update=CASCADE, null=True)
215
219
 
216
220
  peers: BackwardFKRelation["Peer"]
217
221
  update_states: BackwardFKRelation["UpdateState"]
@@ -222,12 +226,12 @@ class Session(TortModel):
222
226
 
223
227
  class Peer(TortModel):
224
228
  id: int = BigIntField(True, description="access_hash")
225
- username: ForeignKeyRelation[Username] = ForeignKeyField("models.Username", "peers")
229
+ username: ForeignKeyRelation[Username] = ForeignKeyField("models.Username", "peers", on_update=CASCADE)
226
230
  username_id: int
227
- session: ForeignKeyRelation[Session] = ForeignKeyField("models.Session", "peers")
231
+ session: ForeignKeyRelation[Session] = ForeignKeyField("models.Session", "peers", on_update=CASCADE)
228
232
  session_id: int
229
233
  type: PeerType = IntEnumField(PeerType)
230
- phone_number = BigIntField(null=True) # todo: rm (already moved to Username.phone)
234
+ phone_number = UInt8Field(null=True) # duplicated to Username.phone
231
235
  last_update_on: datetime | None = DatetimeSecField(auto_now=True)
232
236
 
233
237
  class Meta:
@@ -235,13 +239,13 @@ class Peer(TortModel):
235
239
 
236
240
 
237
241
  class UpdateState(TortModel):
238
- id = IntField(True)
239
- session: ForeignKeyRelation[Session] = ForeignKeyField("models.Session", "update_states")
240
- pts = IntField()
241
- qts = IntField()
242
- date = IntField()
243
- seq = IntField()
242
+ id = UInt2Field(True)
243
+ session: ForeignKeyRelation[Session] = ForeignKeyField("models.Session", "update_states", on_update=CASCADE)
244
+ pts = UInt2Field()
245
+ qts = UInt2Field()
246
+ date = UInt2Field()
247
+ seq = UInt2Field()
244
248
 
245
249
 
246
250
  class Version(TortModel):
247
- number = SmallIntField(True)
251
+ number = UInt2Field(True)
x_auth/types.py CHANGED
@@ -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
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xn-auth
3
- Version: 0.2.41.dev1
3
+ Version: 0.2.43
4
4
  Summary: Auth adapter for XN-Api framework
5
5
  Author-email: Artemiev <mixartemev@gmail.com>
6
6
  License: MIT
@@ -0,0 +1,10 @@
1
+ x_auth/controller.py,sha256=KGbXT-hX7Rp6eYZJGx_-ioAr3As73bbKjPhow41c1SE,2973
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=LZhDQkyXDyFj10nliT2k3PohpGkkgoyZr2A3Z56v8iA,9195
6
+ x_auth/types.py,sha256=GTjNOm7XkyYDCdIaqByP2LzHnwCJRkWGOwdc4tVWFy0,1400
7
+ xn_auth-0.2.43.dist-info/METADATA,sha256=fKd7B8wbaK6Gk8nI0p016r8L-yZuZC9mHEP9hqohEL0,823
8
+ xn_auth-0.2.43.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
9
+ xn_auth-0.2.43.dist-info/top_level.txt,sha256=ydMDkzxgQPtW-E_MNDfUAroAFZvWSqU-x_kZSA7NSFo,7
10
+ xn_auth-0.2.43.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- x_auth/controller.py,sha256=aW3iuzkDghxh6TTbPsg9YQcd-F5Yyek_KY2y9hRIKds,2867
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.41.dev1.dist-info/METADATA,sha256=GMaEX5wsydNjwcmqvVRF7Qxboc-FUleVni6XpawNyus,828
8
- xn_auth-0.2.41.dev1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
9
- xn_auth-0.2.41.dev1.dist-info/top_level.txt,sha256=ydMDkzxgQPtW-E_MNDfUAroAFZvWSqU-x_kZSA7NSFo,7
10
- xn_auth-0.2.41.dev1.dist-info/RECORD,,