xn-auth 0.2.2.dev2__tar.gz → 0.2.3__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.dev2
3
+ Version: 0.2.3
4
4
  Summary: Auth adapter for XN-Api framework
5
5
  Author-email: Artemiev <mixartemev@gmail.com>
6
6
  License: MIT
@@ -16,7 +16,6 @@ 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"
20
19
  Requires-Dist: twine; extra == "dev"
21
20
 
22
21
  # X-Auth
@@ -14,15 +14,14 @@ 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
- "setuptools_scm",
25
- "twine",
24
+ "twine"
26
25
  ]
27
26
 
28
27
  [project.urls]
@@ -1,7 +1,5 @@
1
- import logging
2
1
  from datetime import timedelta
3
2
 
4
- from fastapi import HTTPException as BaseHTTPException
5
3
  from fastapi.openapi.models import HTTPBase, SecuritySchemeType
6
4
  from fastapi.security.base import SecurityBase
7
5
  from fastapi.security.utils import get_authorization_scheme_param
@@ -13,26 +11,14 @@ from starlette.authentication import AuthenticationError
13
11
  from starlette.requests import HTTPConnection
14
12
  from starlette.responses import Response
15
13
  from tortoise.timezone import now
14
+ from x_model import HTTPException
16
15
 
17
- from x_auth.enums import FailReason, AuthFailReason
16
+ from x_auth.enums import AuthFailReason
18
17
  from x_auth.pydantic import AuthUser
19
18
 
20
19
  cookie_name = "access_token"
21
20
 
22
21
 
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
-
36
22
  class AuthException(HTTPException, AuthenticationError):
37
23
  def __init__(
38
24
  self,
@@ -69,7 +55,7 @@ class BearerSecurity(SecurityBase):
69
55
  return None
70
56
  if scheme.lower() != self.model.scheme:
71
57
  if self.auto_error:
72
- raise AuthException(reason=AuthFailReason.scheme, parent="Not Bearer scheme")
58
+ raise AuthException(reason=AuthFailReason.scheme, parent="Invaid scheme")
73
59
  else:
74
60
  return None
75
61
  return credentials
@@ -77,9 +63,10 @@ class BearerSecurity(SecurityBase):
77
63
 
78
64
  def on_error(_: HTTPConnection, exc: AuthException) -> Response:
79
65
  hdr = {}
80
- if exc.status_code == 303 and "/login" in (r.path for r in _.app.routes):
66
+ status = getattr(exc, "status_code", 401)
67
+ if status == 303 and "/login" in (r.path for r in _.app.routes):
81
68
  hdr = {"Location": "/login"}
82
- resp = Response(str(exc), status_code=exc.status_code, headers=hdr)
69
+ resp = Response(exc.__repr__(), status_code=status, headers=hdr)
83
70
  resp.delete_cookie(cookie_name)
84
71
  return resp
85
72
 
@@ -25,14 +25,6 @@ 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
-
36
28
  class AuthFailReason(IntEnum):
37
29
  no_token = 0
38
30
  username = 1
@@ -1,6 +1,6 @@
1
1
  from tortoise import fields
2
2
  from x_auth.pydantic import AuthUser
3
- from x_model.model import Model as BaseModel, TsTrait
3
+ from x_model.models import Model as BaseModel, TsTrait
4
4
 
5
5
  from x_auth.enums import UserStatus, Role, Scope
6
6
 
@@ -6,18 +6,10 @@ from x_auth.enums import UserStatus, Role
6
6
 
7
7
  class UserReg(BaseModel):
8
8
  username: str
9
+ status: UserStatus = UserStatus.WAIT
10
+ role: Role = Role.READER
9
11
  email: str | None = None
10
12
  phone: int | None = None
11
- role: Role = Role.READER
12
- status: UserStatus = UserStatus.WAIT
13
-
14
-
15
- class UserUpdate(BaseModel):
16
- username: str
17
- status: UserStatus
18
- email: str | None
19
- phone: int | None
20
- role: Role
21
13
 
22
14
 
23
15
  class AuthUser(BaseModel, BaseUser):
@@ -29,7 +21,7 @@ class AuthUser(BaseModel, BaseUser):
29
21
  @computed_field
30
22
  @property
31
23
  def is_authenticated(self) -> bool:
32
- return self.status > UserStatus.BANNED
24
+ return bool(self.status)
33
25
 
34
26
  @computed_field
35
27
  @property
@@ -1,18 +1,26 @@
1
1
  from datetime import timedelta
2
2
  from tortoise.exceptions import IntegrityError, ConfigurationError
3
+ from starlette.authentication import AuthenticationBackend
4
+ from fastapi.routing import APIRoute
5
+ from x_model import FailReason
3
6
  from x_auth.backend import AuthBackend
4
-
5
7
  from x_auth.depend import Depend
6
- from x_auth.enums import FailReason, AuthFailReason
8
+ from x_auth.enums import AuthFailReason
7
9
  from x_auth import jwt_encode, HTTPException, AuthException, BearerSecurity
8
- from x_auth.model import User
10
+ from x_auth.models import User
9
11
  from x_auth.pydantic import AuthUser, UserReg, Token
10
12
 
11
13
 
12
14
  class AuthRouter:
13
15
  expires = timedelta(minutes=15)
14
16
 
15
- def __init__(self, secret: str, db_user_model: type(User) = User, scheme: BearerSecurity = BearerSecurity()):
17
+ def __init__(
18
+ self,
19
+ secret: str,
20
+ db_user_model: type(User) = User,
21
+ backend: AuthenticationBackend = None,
22
+ scheme: BearerSecurity = BearerSecurity(),
23
+ ):
16
24
  self.depend = Depend(scheme)
17
25
  self.secret = secret
18
26
  self.db_user_model = db_user_model
@@ -33,7 +41,7 @@ class AuthRouter:
33
41
  "reg": (self.reg, "POST"),
34
42
  "refresh": (refresh, "GET"),
35
43
  }
36
- self.backend = AuthBackend(secret, scheme)
44
+ self.backend = backend or AuthBackend(secret, scheme)
37
45
 
38
46
  # API ENDOINTS
39
47
  def _user2tok(self, user: AuthUser) -> Token:
@@ -47,3 +55,16 @@ class AuthRouter:
47
55
  except IntegrityError as e:
48
56
  raise HTTPException(FailReason.body, e)
49
57
  return self._user2tok(db_user.get_auth())
58
+
59
+ def get_routes(self) -> list[APIRoute]:
60
+ return [
61
+ APIRoute(
62
+ "/" + path,
63
+ func,
64
+ tags=["Auth"],
65
+ methods=[method],
66
+ name=path.title(),
67
+ operation_id=path,
68
+ )
69
+ for path, (func, method) in self.routes.items()
70
+ ]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xn-auth
3
- Version: 0.2.2.dev2
3
+ Version: 0.2.3
4
4
  Summary: Auth adapter for XN-Api framework
5
5
  Author-email: Artemiev <mixartemev@gmail.com>
6
6
  License: MIT
@@ -16,7 +16,6 @@ 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"
20
19
  Requires-Dist: twine; extra == "dev"
21
20
 
22
21
  # 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/model.py
11
+ x_auth/models.py
12
12
  x_auth/pydantic.py
13
13
  x_auth/router.py
14
14
  xn_auth.egg-info/PKG-INFO
@@ -6,5 +6,4 @@ xn-model
6
6
  [dev]
7
7
  build
8
8
  python-dotenv
9
- setuptools_scm
10
9
  twine
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes