xn-auth 0.2.55__py3-none-any.whl → 0.2.56__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/exceptions.py CHANGED
@@ -2,4 +2,4 @@ from jwt import ExpiredSignatureError
2
2
 
3
3
 
4
4
  class ExpiredSignature(Exception):
5
- def __init__(self, _uid: int, _encoded_token: str, _e: ExpiredSignatureError): ...
5
+ def __init__(self, uid: int, encoded_token: str, secret: str, _e: ExpiredSignatureError): ...
x_auth/middleware.py CHANGED
@@ -38,8 +38,8 @@ class Tok(Token):
38
38
  }
39
39
  )
40
40
  tok = convert(payload, cls, strict=False)
41
- encoded_token = tok.encode(secret, algorithms[0]) # check where from getting algorithms
42
- raise ExpiredSignature(int(payload["sub"]), encoded_token, e)
41
+ encoded_token = tok.encode(secret, algorithms[0])
42
+ raise ExpiredSignature(int(payload["sub"]), encoded_token, secret, e)
43
43
 
44
44
 
45
45
  class JWTAuthMiddleware(JWTCookieAuthenticationMiddleware):
@@ -47,10 +47,17 @@ class JWTAuthMiddleware(JWTCookieAuthenticationMiddleware):
47
47
  try:
48
48
  await super().__call__(scope, receive, send)
49
49
  except ExpiredSignature as e:
50
- uid, uet, _e = e.args # uid, updated encoded token
51
- if await scope["app"].state.get("user_model").is_blocked(uid):
50
+ uid, uet, secret, _e = e.args # uid, updated encoded token
51
+ blocked, role = await scope["app"].state.get("user_model").permissions(uid)
52
+ if blocked:
52
53
  logging.error(f"User#{uid} can't refresh. Blocked!")
53
54
  raise _e
55
+ payload = Tok.decode_payload(uet, secret, ["HS256"])
56
+ if role.value != payload["extras"]["role"]:
57
+ # update user role in jwtoken
58
+ payload["extras"]["role"] = role.value
59
+ tok = convert(payload, Tok, strict=False)
60
+ uet = tok.encode(secret, "HS256")
54
61
 
55
62
  async def send_wrapper(msg: Message) -> None:
56
63
  if msg["type"] == "http.response.start":
x_auth/models.py CHANGED
@@ -78,8 +78,9 @@ class User(Model):
78
78
  return user_dict
79
79
 
80
80
  @classmethod
81
- async def is_blocked(cls, sid: str) -> bool:
82
- return (await cls[int(sid)]).blocked
81
+ async def permissions(cls, self_id: str) -> tuple[bool, Role]:
82
+ user = await cls[self_id]
83
+ return user.blocked, user.role
83
84
 
84
85
  @classmethod
85
86
  async def tg_upsert(cls, u: PyroUser | AioUser | WebAppUser, blocked: bool = None) -> tuple["User", bool]:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xn-auth
3
- Version: 0.2.55
3
+ Version: 0.2.56
4
4
  Summary: Auth adapter for XN-Api framework
5
5
  Author-email: Artemiev <mixartemev@gmail.com>
6
6
  License: MIT
@@ -22,6 +22,10 @@ Requires-Dist: twine; extra == "dev"
22
22
  # X-Auth
23
23
  ###### JWT authentication for x-api
24
24
 
25
+ JWT cookie based Auth Middleware for ASGI framework.
26
+ Stores in user_id, issued and expired dates, user role and blocked state.
27
+ When token expired, it is automatically fetching user from db, and if he is not blocked now, then updates issue/expire dates, and user role if it was changed after the last user fetch.
28
+
25
29
  #### Requirements
26
30
  - Python >= 3.12
27
31
 
@@ -0,0 +1,10 @@
1
+ x_auth/controller.py,sha256=6AMvrhfW_2UEZhx4f9xFT0EC9YVNsS2tY4Lf8867VVU,3862
2
+ x_auth/enums.py,sha256=pciVrb92S9YQFMhHe6pKNYwcvtevwfMXIiG1WxAEa-Q,761
3
+ x_auth/exceptions.py,sha256=kQeaxTzlJMZo7md6maBVrcOppMk2oRU5leAQmxGKcXQ,173
4
+ x_auth/middleware.py,sha256=10iz4fPPUuLyrHUBrlWwESnGlsurCtuPCI746Vc9xtA,3328
5
+ x_auth/models.py,sha256=uGQOqGl-3tGLFOKPQ7dk4N1orOLfugGdfHfoQ_-rNLM,9257
6
+ x_auth/types.py,sha256=MD7wDLZ2Lf_G2LWeyZEPaY1zSaz3atn4JYGk4cFjZ74,1529
7
+ xn_auth-0.2.56.dist-info/METADATA,sha256=IAZygy74Tox5UIYqkg2B8eGqyIm-CtAlq2PwIjgm7EQ,1136
8
+ xn_auth-0.2.56.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
9
+ xn_auth-0.2.56.dist-info/top_level.txt,sha256=ydMDkzxgQPtW-E_MNDfUAroAFZvWSqU-x_kZSA7NSFo,7
10
+ xn_auth-0.2.56.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- x_auth/controller.py,sha256=6AMvrhfW_2UEZhx4f9xFT0EC9YVNsS2tY4Lf8867VVU,3862
2
- x_auth/enums.py,sha256=pciVrb92S9YQFMhHe6pKNYwcvtevwfMXIiG1WxAEa-Q,761
3
- x_auth/exceptions.py,sha256=2B4okJxhPyNqTJXlSTfblJUQJ60bLGXdgJIu6ue7S6w,162
4
- x_auth/middleware.py,sha256=KLRv60Ih1a4yobn-jcjysV-m0i5tu0bKKlpcaC3Ghbs,2984
5
- x_auth/models.py,sha256=V3lvrUVtS5viyqO_-Y7u2zQep5bRjPOpWmc5kweMrN0,9211
6
- x_auth/types.py,sha256=MD7wDLZ2Lf_G2LWeyZEPaY1zSaz3atn4JYGk4cFjZ74,1529
7
- xn_auth-0.2.55.dist-info/METADATA,sha256=xgbgLpgaZM_43TyDet3XrXgzE1jEk9rCONEfR2MxvG8,823
8
- xn_auth-0.2.55.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
9
- xn_auth-0.2.55.dist-info/top_level.txt,sha256=ydMDkzxgQPtW-E_MNDfUAroAFZvWSqU-x_kZSA7NSFo,7
10
- xn_auth-0.2.55.dist-info/RECORD,,