forked from mtusi2026/mtusi2026lab2
Не оживлять отозванный токен после недоступности Valkey
This commit is contained in:
+3
-6
@@ -7,10 +7,10 @@ import secrets
|
||||
from argon2 import PasswordHasher
|
||||
from argon2.exceptions import InvalidHashError, VerificationError
|
||||
|
||||
from cache import REVOKED, CacheUnavailable, cache
|
||||
from cache import REVOKED, cache
|
||||
from config import settings
|
||||
from db import iso
|
||||
from errors import BASIC_CHALLENGE, BEARER_CHALLENGE, ApiError, unauthorized
|
||||
from errors import BASIC_CHALLENGE, BEARER_CHALLENGE, unauthorized
|
||||
import store
|
||||
|
||||
USERNAME = re.compile(r'^[a-z0-9_]{3,32}$')
|
||||
@@ -90,8 +90,5 @@ def authenticate(header):
|
||||
|
||||
|
||||
def logout(session):
|
||||
try:
|
||||
cache.revoke(session['token_hash'])
|
||||
except CacheUnavailable:
|
||||
raise ApiError(503, 'Отзыв сессии временно недоступен') from None
|
||||
store.delete_session(session['token_hash'])
|
||||
cache.revoke(session['token_hash'])
|
||||
|
||||
+22
-13
@@ -11,10 +11,7 @@ log = logging.getLogger('messenger.cache')
|
||||
|
||||
BYPASS_SECONDS = 5
|
||||
REVOKED = 'revoked'
|
||||
|
||||
|
||||
class CacheUnavailable(Exception):
|
||||
pass
|
||||
FAILED = object()
|
||||
|
||||
|
||||
class SessionCache:
|
||||
@@ -23,29 +20,40 @@ class SessionCache:
|
||||
decode_responses=True) if url else None
|
||||
self.max_ttl = max_ttl
|
||||
self.bypass_until = 0.0
|
||||
self.pending = set()
|
||||
|
||||
def _run(self, action, required=False):
|
||||
def _run(self, action, force=False):
|
||||
if self.client is None:
|
||||
return None
|
||||
if not required and time.monotonic() < self.bypass_until:
|
||||
return None
|
||||
if not force and time.monotonic() < self.bypass_until:
|
||||
return FAILED
|
||||
try:
|
||||
return action(self.client)
|
||||
except redis.RedisError as error:
|
||||
log.warning('session cache unavailable (%s), using PostgreSQL for %ds', type(error).__name__, BYPASS_SECONDS)
|
||||
self.bypass_until = time.monotonic() + BYPASS_SECONDS
|
||||
if required:
|
||||
raise CacheUnavailable from None
|
||||
return None
|
||||
return FAILED
|
||||
|
||||
@staticmethod
|
||||
def key(token_hash):
|
||||
return 'session:' + token_hash.hex()
|
||||
|
||||
def _tombstone(self, token_hash, force=False):
|
||||
return self._run(lambda c: c.set(self.key(token_hash), REVOKED, ex=self.max_ttl), force) is not FAILED
|
||||
|
||||
def _apply_pending(self):
|
||||
for token_hash in list(self.pending):
|
||||
if not self._tombstone(token_hash):
|
||||
return False
|
||||
self.pending.discard(token_hash)
|
||||
return True
|
||||
|
||||
def get(self, token_hash):
|
||||
if self.pending and not self._apply_pending():
|
||||
return REVOKED if token_hash in self.pending else None
|
||||
raw = self._run(lambda c: c.get(self.key(token_hash)))
|
||||
if not raw or raw == REVOKED:
|
||||
return raw or None
|
||||
if raw is FAILED or not raw or raw == REVOKED:
|
||||
return REVOKED if raw == REVOKED else None
|
||||
entry = json.loads(raw)
|
||||
if entry['expires_at'] <= time.time():
|
||||
return None
|
||||
@@ -60,7 +68,8 @@ class SessionCache:
|
||||
self._run(lambda c: c.set(self.key(token_hash), value, ex=ttl, nx=True))
|
||||
|
||||
def revoke(self, token_hash):
|
||||
self._run(lambda c: c.set(self.key(token_hash), REVOKED, ex=self.max_ttl), required=True)
|
||||
if self.client is not None and not self._tombstone(token_hash, force=True):
|
||||
self.pending.add(token_hash)
|
||||
|
||||
|
||||
cache = SessionCache(settings.cache_url, settings.session_cache_ttl)
|
||||
|
||||
Reference in New Issue
Block a user