xn-auth 0.2.2__tar.gz → 0.2.2.dev1__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xn-auth
3
- Version: 0.2.2
3
+ Version: 0.2.2.dev1
4
4
  Summary: Auth adapter for XN-Api framework
5
5
  Author-email: Artemiev <mixartemev@gmail.com>
6
6
  License: MIT
@@ -16,6 +16,7 @@ Requires-Dist: xn-model
16
16
  Provides-Extra: dev
17
17
  Requires-Dist: build; extra == "dev"
18
18
  Requires-Dist: python-dotenv; extra == "dev"
19
+ Requires-Dist: setuptools_scm; extra == "dev"
19
20
  Requires-Dist: twine; extra == "dev"
20
21
 
21
22
  # X-Auth
@@ -14,14 +14,15 @@ dependencies = [
14
14
  "fastapi",
15
15
  'pwdlib[argon2]',
16
16
  "python-jose[cryptography]",
17
- "xn-model"
17
+ "xn-model",
18
18
  ]
19
19
 
20
20
  [project.optional-dependencies]
21
21
  dev = [
22
22
  "build",
23
23
  "python-dotenv",
24
- "twine"
24
+ "setuptools_scm",
25
+ "twine",
25
26
  ]
26
27
 
27
28
  [project.urls]
@@ -1,5 +1,7 @@
1
+ import logging
1
2
  from datetime import timedelta
2
3
 
4
+ from fastapi import HTTPException as BaseHTTPException
3
5
  from fastapi.openapi.models import HTTPBase, SecuritySchemeType
4
6
  from fastapi.security.base import SecurityBase
5
7
  from fastapi.security.utils import get_authorization_scheme_param
@@ -11,14 +13,26 @@ from starlette.authentication import AuthenticationError
11
13
  from starlette.requests import HTTPConnection
12
14
  from starlette.responses import Response
13
15
  from tortoise.timezone import now
14
- from x_model import HTTPException
15
16
 
16
- from x_auth.enums import AuthFailReason
17
+ from x_auth.enums import FailReason, AuthFailReason
17
18
  from x_auth.pydantic import AuthUser
18
19
 
19
20
  cookie_name = "access_token"
20
21
 
21
22
 
23
+ class HTTPException(BaseHTTPException):
24
+ def __init__(
25
+ self,
26
+ reason: FailReason | AuthFailReason,
27
+ parent: Exception | str = None,
28
+ status_: status = status.HTTP_400_BAD_REQUEST,
29
+ hdrs: dict = None,
30
+ ) -> None:
31
+ detail = f"{reason.name}{f': {parent}' if parent else ''}"
32
+ logging.error(detail)
33
+ super().__init__(status_, detail, hdrs)
34
+
35
+
22
36
  class AuthException(HTTPException, AuthenticationError):
23
37
  def __init__(
24
38
  self,
@@ -40,8 +54,8 @@ class BearerModel(HTTPBase):
40
54
  class BearerSecurity(SecurityBase):
41
55
  """HTTP Bearer token authentication"""
42
56
 
43
- def __init__(self, model_: BearerModel = BearerModel(), auto_error: bool = False, scheme_name: str = None):
44
- self.model = model_
57
+ def __init__(self, model: BearerModel = BearerModel(), auto_error: bool = True, scheme_name: str = None):
58
+ self.model = model
45
59
  self.scheme_name = scheme_name or self.__class__.__name__
46
60
  self.auto_error = auto_error
47
61
 
@@ -25,6 +25,14 @@ class Role(IntEnum):
25
25
  return [scope.name for scope in Scope if self.value & scope.value]
26
26
 
27
27
 
28
+ class FailReason(IntEnum):
29
+ path = 8
30
+ query = 9
31
+ body = 10
32
+ method = 11
33
+ protocol = 12
34
+
35
+
28
36
  class AuthFailReason(IntEnum):
29
37
  no_token = 0
30
38
  username = 1
@@ -1,6 +1,5 @@
1
1
  from tortoise import fields
2
- from x_auth.pydantic import AuthUser
3
- from x_model.models import Model as BaseModel, TsTrait
2
+ from x_model.model import Model as BaseModel, TsTrait
4
3
 
5
4
  from x_auth.enums import UserStatus, Role, Scope
6
5
 
@@ -30,8 +29,5 @@ class User(Model, TsTrait):
30
29
  def _can(self, scope: Scope) -> bool:
31
30
  return bool(self.role.value & scope)
32
31
 
33
- def get_auth(self) -> AuthUser:
34
- return AuthUser.model_validate(self, from_attributes=True)
35
-
36
32
  class Meta:
37
33
  table_description = "Users"
@@ -1,13 +1,11 @@
1
1
  from datetime import timedelta
2
2
  from tortoise.exceptions import IntegrityError, ConfigurationError
3
- from x_model import FailReason
4
-
5
3
  from x_auth.backend import AuthBackend
6
4
 
7
5
  from x_auth.depend import Depend
8
- from x_auth.enums import AuthFailReason
6
+ from x_auth.enums import FailReason, AuthFailReason
9
7
  from x_auth import jwt_encode, HTTPException, AuthException, BearerSecurity
10
- from x_auth.models import User
8
+ from x_auth.model import User
11
9
  from x_auth.pydantic import AuthUser, UserReg, Token
12
10
 
13
11
 
@@ -23,7 +21,7 @@ class AuthRouter:
23
21
  async def refresh(auth_user: AuthUser = self.depend.AUTHENTICATED) -> Token:
24
22
  try:
25
23
  db_user: User = await self.db_user_model[auth_user.id]
26
- auth_user: AuthUser = db_user.get_auth()
24
+ auth_user = AuthUser.model_validate(db_user, from_attributes=True)
27
25
  except ConfigurationError:
28
26
  raise AuthException(AuthFailReason.username, f"Not inicialized user model: {User})", 500)
29
27
  except Exception:
@@ -48,4 +46,5 @@ class AuthRouter:
48
46
  db_user: User = await self.db_user_model.create(**data)
49
47
  except IntegrityError as e:
50
48
  raise HTTPException(FailReason.body, e)
51
- return self._user2tok(db_user.get_auth())
49
+ user: AuthUser = AuthUser.model_validate(db_user, from_attributes=True)
50
+ return self._user2tok(user)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xn-auth
3
- Version: 0.2.2
3
+ Version: 0.2.2.dev1
4
4
  Summary: Auth adapter for XN-Api framework
5
5
  Author-email: Artemiev <mixartemev@gmail.com>
6
6
  License: MIT
@@ -16,6 +16,7 @@ Requires-Dist: xn-model
16
16
  Provides-Extra: dev
17
17
  Requires-Dist: build; extra == "dev"
18
18
  Requires-Dist: python-dotenv; extra == "dev"
19
+ Requires-Dist: setuptools_scm; extra == "dev"
19
20
  Requires-Dist: twine; extra == "dev"
20
21
 
21
22
  # X-Auth
@@ -8,7 +8,7 @@ x_auth/__init__.py
8
8
  x_auth/backend.py
9
9
  x_auth/depend.py
10
10
  x_auth/enums.py
11
- x_auth/models.py
11
+ x_auth/model.py
12
12
  x_auth/pydantic.py
13
13
  x_auth/router.py
14
14
  xn_auth.egg-info/PKG-INFO
@@ -6,4 +6,5 @@ xn-model
6
6
  [dev]
7
7
  build
8
8
  python-dotenv
9
+ setuptools_scm
9
10
  twine
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes