Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ foo@bar:~$ pip install -r requirements.txt

4) Запускайте приложение!
```console
foo@bar:~$ python -m auth_backend
foo@bar:~$ python -m auth_backend start
```

---
Expand Down
3 changes: 1 addition & 2 deletions auth_backend/routes/user_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from auth_backend.auth_plugins.email import Email
from auth_backend.base import StatusResponseModel
from auth_backend.exceptions import ObjectNotFound, SessionExpired
from auth_backend.models.db import AuthMethod, UserSession, session_expires_date
from auth_backend.models.db import AuthMethod, UserSession
from auth_backend.schemas.models import (
Session,
SessionPatch,
Expand Down Expand Up @@ -53,7 +53,6 @@ async def me(
),
) -> dict[str, str | int]:
auth_params = Email.get_auth_method_params(session.user_id, session=db.session)
session.expires = session_expires_date() # Автопродление сессии при активности пользователя
result: dict[str, str | int] = {}
result = (
result
Expand Down
14 changes: 8 additions & 6 deletions auth_backend/utils/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from starlette.requests import Request
from starlette.status import HTTP_403_FORBIDDEN

from auth_backend.models.db import UserSession
from auth_backend.models.db import UserSession, session_expires_date


class UnionAuth(SecurityBase):
Expand All @@ -21,6 +21,7 @@ class UnionAuth(SecurityBase):
auto_error: bool
allow_none: bool
_scopes: list[str] = []
_SESSION_UPDATE_SCOPE = 'auth.session.update'

def __init__(self, scopes: list[str] = None, allow_none=False, auto_error=False) -> None:
super().__init__()
Expand Down Expand Up @@ -49,12 +50,13 @@ async def __call__(
if not user_session:
self._except()
user_session.last_activity = datetime.datetime.utcnow()
db.session.commit()

if user_session.expired:
self._except()
if len(
set([_scope.lower() for _scope in self._scopes])
& set([scope.name.lower() for scope in user_session.scopes])
) != len(set(self._scopes)):
session_scopes = set([scope.name.lower() for scope in user_session.scopes])
if self._SESSION_UPDATE_SCOPE in session_scopes:
user_session.expires = session_expires_date()
db.session.commit()
if len(set([_scope.lower() for _scope in self._scopes]) & session_scopes) != len(set(self._scopes)):
self._except()
return user_session