xn-auth 0.2.2.dev1__tar.gz → 0.2.2.dev3__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.
- {xn_auth-0.2.2.dev1/xn_auth.egg-info → xn_auth-0.2.2.dev3}/PKG-INFO +1 -1
- {xn_auth-0.2.2.dev1 → xn_auth-0.2.2.dev3}/x_auth/__init__.py +4 -18
- {xn_auth-0.2.2.dev1 → xn_auth-0.2.2.dev3}/x_auth/enums.py +0 -8
- xn_auth-0.2.2.dev1/x_auth/model.py → xn_auth-0.2.2.dev3/x_auth/models.py +5 -1
- {xn_auth-0.2.2.dev1 → xn_auth-0.2.2.dev3}/x_auth/router.py +6 -5
- {xn_auth-0.2.2.dev1 → xn_auth-0.2.2.dev3/xn_auth.egg-info}/PKG-INFO +1 -1
- {xn_auth-0.2.2.dev1 → xn_auth-0.2.2.dev3}/xn_auth.egg-info/SOURCES.txt +1 -1
- {xn_auth-0.2.2.dev1 → xn_auth-0.2.2.dev3}/.env.dist +0 -0
- {xn_auth-0.2.2.dev1 → xn_auth-0.2.2.dev3}/.gitignore +0 -0
- {xn_auth-0.2.2.dev1 → xn_auth-0.2.2.dev3}/.pre-commit-config.yaml +0 -0
- {xn_auth-0.2.2.dev1 → xn_auth-0.2.2.dev3}/README.md +0 -0
- {xn_auth-0.2.2.dev1 → xn_auth-0.2.2.dev3}/makefile +0 -0
- {xn_auth-0.2.2.dev1 → xn_auth-0.2.2.dev3}/pyproject.toml +0 -0
- {xn_auth-0.2.2.dev1 → xn_auth-0.2.2.dev3}/setup.cfg +0 -0
- {xn_auth-0.2.2.dev1 → xn_auth-0.2.2.dev3}/x_auth/backend.py +0 -0
- {xn_auth-0.2.2.dev1 → xn_auth-0.2.2.dev3}/x_auth/depend.py +0 -0
- {xn_auth-0.2.2.dev1 → xn_auth-0.2.2.dev3}/x_auth/pydantic.py +0 -0
- {xn_auth-0.2.2.dev1 → xn_auth-0.2.2.dev3}/xn_auth.egg-info/dependency_links.txt +0 -0
- {xn_auth-0.2.2.dev1 → xn_auth-0.2.2.dev3}/xn_auth.egg-info/requires.txt +0 -0
- {xn_auth-0.2.2.dev1 → xn_auth-0.2.2.dev3}/xn_auth.egg-info/top_level.txt +0 -0
|
@@ -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
|
|
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,
|
|
@@ -54,8 +40,8 @@ class BearerModel(HTTPBase):
|
|
|
54
40
|
class BearerSecurity(SecurityBase):
|
|
55
41
|
"""HTTP Bearer token authentication"""
|
|
56
42
|
|
|
57
|
-
def __init__(self,
|
|
58
|
-
self.model =
|
|
43
|
+
def __init__(self, model_: BearerModel = BearerModel(), auto_error: bool = False, scheme_name: str = None):
|
|
44
|
+
self.model = model_
|
|
59
45
|
self.scheme_name = scheme_name or self.__class__.__name__
|
|
60
46
|
self.auto_error = auto_error
|
|
61
47
|
|
|
@@ -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,5 +1,6 @@
|
|
|
1
1
|
from tortoise import fields
|
|
2
|
-
from
|
|
2
|
+
from x_auth.pydantic import AuthUser
|
|
3
|
+
from x_model.models import Model as BaseModel, TsTrait
|
|
3
4
|
|
|
4
5
|
from x_auth.enums import UserStatus, Role, Scope
|
|
5
6
|
|
|
@@ -29,5 +30,8 @@ class User(Model, TsTrait):
|
|
|
29
30
|
def _can(self, scope: Scope) -> bool:
|
|
30
31
|
return bool(self.role.value & scope)
|
|
31
32
|
|
|
33
|
+
def get_auth(self) -> AuthUser:
|
|
34
|
+
return AuthUser.model_validate(self, from_attributes=True)
|
|
35
|
+
|
|
32
36
|
class Meta:
|
|
33
37
|
table_description = "Users"
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
from datetime import timedelta
|
|
2
2
|
from tortoise.exceptions import IntegrityError, ConfigurationError
|
|
3
|
+
from x_model import FailReason
|
|
4
|
+
|
|
3
5
|
from x_auth.backend import AuthBackend
|
|
4
6
|
|
|
5
7
|
from x_auth.depend import Depend
|
|
6
|
-
from x_auth.enums import
|
|
8
|
+
from x_auth.enums import AuthFailReason
|
|
7
9
|
from x_auth import jwt_encode, HTTPException, AuthException, BearerSecurity
|
|
8
|
-
from x_auth.
|
|
10
|
+
from x_auth.models import User
|
|
9
11
|
from x_auth.pydantic import AuthUser, UserReg, Token
|
|
10
12
|
|
|
11
13
|
|
|
@@ -21,7 +23,7 @@ class AuthRouter:
|
|
|
21
23
|
async def refresh(auth_user: AuthUser = self.depend.AUTHENTICATED) -> Token:
|
|
22
24
|
try:
|
|
23
25
|
db_user: User = await self.db_user_model[auth_user.id]
|
|
24
|
-
auth_user =
|
|
26
|
+
auth_user: AuthUser = db_user.get_auth()
|
|
25
27
|
except ConfigurationError:
|
|
26
28
|
raise AuthException(AuthFailReason.username, f"Not inicialized user model: {User})", 500)
|
|
27
29
|
except Exception:
|
|
@@ -46,5 +48,4 @@ class AuthRouter:
|
|
|
46
48
|
db_user: User = await self.db_user_model.create(**data)
|
|
47
49
|
except IntegrityError as e:
|
|
48
50
|
raise HTTPException(FailReason.body, e)
|
|
49
|
-
|
|
50
|
-
return self._user2tok(user)
|
|
51
|
+
return self._user2tok(db_user.get_auth())
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|