xn-auth 0.2.2.dev3__tar.gz → 0.2.4__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.dev3
3
+ Version: 0.2.4
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]
@@ -55,7 +55,7 @@ class BearerSecurity(SecurityBase):
55
55
  return None
56
56
  if scheme.lower() != self.model.scheme:
57
57
  if self.auto_error:
58
- raise AuthException(reason=AuthFailReason.scheme, parent="Not Bearer scheme")
58
+ raise AuthException(reason=AuthFailReason.scheme, parent="Invaid scheme")
59
59
  else:
60
60
  return None
61
61
  return credentials
@@ -63,9 +63,10 @@ class BearerSecurity(SecurityBase):
63
63
 
64
64
  def on_error(_: HTTPConnection, exc: AuthException) -> Response:
65
65
  hdr = {}
66
- 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):
67
68
  hdr = {"Location": "/login"}
68
- resp = Response(str(exc), status_code=exc.status_code, headers=hdr)
69
+ resp = Response(exc.__repr__(), status_code=status, headers=hdr)
69
70
  resp.delete_cookie(cookie_name)
70
71
  return resp
71
72
 
@@ -4,7 +4,7 @@ from fastapi import Depends, Security
4
4
  from fastapi.security import SecurityScopes
5
5
  from starlette.requests import HTTPConnection
6
6
 
7
- from x_auth.enums import AuthFailReason, UserStatus, Scope
7
+ from x_auth.enums import AuthFailReason, Scope
8
8
  from x_auth import AuthUser, AuthException, BearerSecurity
9
9
 
10
10
 
@@ -19,7 +19,7 @@ class Depend:
19
19
  self.AUTHENTICATED = Depends(get_authenticated_user)
20
20
 
21
21
  def is_active(auth_user: AuthUser = self.AUTHENTICATED):
22
- if auth_user.status < UserStatus.TEST:
22
+ if auth_user.status < 2:
23
23
  raise AuthException(AuthFailReason.status, parent=f"{auth_user.status.name} status denied")
24
24
 
25
25
  self.ACTIVE = Depends(is_active)
@@ -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,9 +1,11 @@
1
1
  from datetime import timedelta
2
+
3
+ from starlette.responses import JSONResponse
2
4
  from tortoise.exceptions import IntegrityError, ConfigurationError
5
+ from starlette.authentication import AuthenticationBackend
6
+ from fastapi.routing import APIRoute
3
7
  from x_model import FailReason
4
-
5
8
  from x_auth.backend import AuthBackend
6
-
7
9
  from x_auth.depend import Depend
8
10
  from x_auth.enums import AuthFailReason
9
11
  from x_auth import jwt_encode, HTTPException, AuthException, BearerSecurity
@@ -14,9 +16,17 @@ from x_auth.pydantic import AuthUser, UserReg, Token
14
16
  class AuthRouter:
15
17
  expires = timedelta(minutes=15)
16
18
 
17
- def __init__(self, secret: str, db_user_model: type(User) = User, scheme: BearerSecurity = BearerSecurity()):
19
+ def __init__(
20
+ self,
21
+ secret: str,
22
+ domain: str,
23
+ db_user_model: type(User) = User,
24
+ backend: AuthenticationBackend = None,
25
+ scheme: BearerSecurity = BearerSecurity(),
26
+ ):
18
27
  self.depend = Depend(scheme)
19
28
  self.secret = secret
29
+ self.domain = domain
20
30
  self.db_user_model = db_user_model
21
31
 
22
32
  # api refresh token
@@ -35,11 +45,14 @@ class AuthRouter:
35
45
  "reg": (self.reg, "POST"),
36
46
  "refresh": (refresh, "GET"),
37
47
  }
38
- self.backend = AuthBackend(secret, scheme)
48
+ self.backend = backend or AuthBackend(secret, scheme)
39
49
 
40
50
  # API ENDOINTS
41
- def _user2tok(self, user: AuthUser) -> Token:
42
- return Token(access_token=jwt_encode(user, self.secret, self.expires), user=user)
51
+ def _user2tok(self, user: AuthUser) -> Token | JSONResponse:
52
+ token = Token(access_token=jwt_encode(user, self.secret, self.expires), user=user)
53
+ resp = JSONResponse(token.model_dump())
54
+ resp.set_cookie("access_token", token.access_token, domain=f".{self.domain}", samesite="none", secure=True)
55
+ return resp
43
56
 
44
57
  # api reg endpoint
45
58
  async def reg(self, user_reg_input: UserReg) -> Token:
@@ -49,3 +62,16 @@ class AuthRouter:
49
62
  except IntegrityError as e:
50
63
  raise HTTPException(FailReason.body, e)
51
64
  return self._user2tok(db_user.get_auth())
65
+
66
+ def get_routes(self) -> list[APIRoute]:
67
+ return [
68
+ APIRoute(
69
+ "/" + path,
70
+ func,
71
+ tags=["Auth"],
72
+ methods=[method],
73
+ name=path.title(),
74
+ operation_id=path,
75
+ )
76
+ for path, (func, method) in self.routes.items()
77
+ ]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xn-auth
3
- Version: 0.2.2.dev3
3
+ Version: 0.2.4
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
@@ -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
File without changes