-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86b7892
commit c43d57f
Showing
31 changed files
with
330 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import json | ||
import os | ||
from typing import Any | ||
|
||
import redis | ||
|
||
from .constants import PASSWORD_RESET_TOKEN_EXPIRATION_TIME | ||
|
||
|
||
class Database: | ||
AUTH_PREFIX = "auth" | ||
|
||
def __init__(self, prefix: str, debug_fn: Any = print) -> None: | ||
self.prefix = prefix | ||
self.debug_fn = debug_fn | ||
host = os.environ.get("CACHE_HOST", "localhost") | ||
port = int(os.environ.get("CACHE_PORT", 6379)) | ||
self.redis = redis.Redis(host=host, port=port) | ||
|
||
def set(self, key: str, obj: Any, expire: bool = False) -> None: | ||
redisKey = self.getPrefixedKey(key) | ||
self.redis.hset(redisKey, redisKey, json.dumps(obj)) | ||
if expire: | ||
self.redis.expire(redisKey, PASSWORD_RESET_TOKEN_EXPIRATION_TIME) | ||
|
||
def get(self, key: str) -> Any: | ||
redisKey = self.getPrefixedKey(key) | ||
data = self.redis.hget(redisKey, redisKey) | ||
if data: | ||
return json.loads(data) | ||
|
||
def getPrefix(self) -> str: | ||
return f"{Database.AUTH_PREFIX}:{self.prefix}" | ||
|
||
def getPrefixedKey(self, key: str) -> str: | ||
return f"{self.getPrefix()}:{key}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import os | ||
from collections import defaultdict | ||
from time import time | ||
from typing import Any, Dict, Optional | ||
|
||
import redis | ||
|
||
|
||
class MessageBus: | ||
def __init__(self, debug_fn: Any = print) -> None: | ||
self.debug_fn = debug_fn | ||
host = os.environ.get("MESSAGE_BUS_HOST", "localhost") | ||
port = int(os.environ.get("MESSAGE_BUS_PORT", 6379)) | ||
self.redis = redis.Redis(host=host, port=port) | ||
self.last_ids: Dict[str, str] = defaultdict(lambda: "0-0") | ||
|
||
def xread(self, topic: str, max_age: Optional[int] = None) -> Any: | ||
last_id = self.last_ids[topic] | ||
last_timestamp = int(last_id.split("-")[0]) | ||
if max_age: | ||
min_timestamp = round((time() - max_age) * 1000) | ||
if min_timestamp > last_timestamp: | ||
last_id = str(min_timestamp) | ||
response = self.redis.xread({topic: last_id}) | ||
if response: | ||
entries = response[0][1] | ||
self.last_ids[topic] = entries[-1][0].decode() | ||
return entries | ||
else: | ||
self.last_ids[topic] = last_id | ||
return [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from time import time | ||
from typing import Any, Dict, Tuple | ||
|
||
from ordered_set import OrderedSet | ||
|
||
from .http_handler import HttpHandler | ||
from .message_bus import MessageBus | ||
|
||
|
||
class Session: | ||
id: str | ||
timestamp: int | ||
|
||
def __init__(self, id: str, timestamp: int = 0) -> None: | ||
self.id = id | ||
self.timestamp = timestamp | ||
|
||
def __eq__(self, other): | ||
return isinstance(other, Session) and self.id == other.id | ||
|
||
def __hash__(self): | ||
return hash(self.id) | ||
|
||
|
||
class SessionHandler: | ||
LOGOUT_TOPIC = "logout" | ||
PRUNE_TIME = int(600 * 1.1) | ||
|
||
invalid_sessions: OrderedSet[Session] | ||
|
||
def __init__(self, debug_fn: Any = print) -> None: | ||
self.debug_fn = debug_fn | ||
self.http_handler = HttpHandler(debug_fn) | ||
self.message_bus = MessageBus(debug_fn) | ||
self.invalid_sessions = OrderedSet() | ||
|
||
def update_invalid_sessions(self) -> None: | ||
if len(self.invalid_sessions): | ||
index = len(self.invalid_sessions) // 2 | ||
median = self.invalid_sessions[index] | ||
if median.timestamp / 1000 < time() - self.PRUNE_TIME: | ||
self.invalid_sessions = self.invalid_sessions[index + 1 :] | ||
new_logouts = self.message_bus.xread(self.LOGOUT_TOPIC, self.PRUNE_TIME) | ||
self.invalid_sessions.update( | ||
[self.create_session(logout) for logout in new_logouts] | ||
) | ||
|
||
def create_session(self, logout_data: Tuple[bytes, Dict[bytes, bytes]]) -> Session: | ||
timestamp = int(logout_data[0].decode().split("-")[0]) | ||
session_id = logout_data[1][b"sessionId"].decode() | ||
return Session(session_id, timestamp) | ||
|
||
def is_session_invalid(self, session_id: str) -> bool: | ||
self.update_invalid_sessions() | ||
return Session(session_id) in self.invalid_sessions | ||
|
||
def clear_all_sessions(self, token: str, cookie: str) -> None: | ||
self.http_handler.send_secure_request("clear-all-sessions", token, cookie) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
argon2-cffi==23.1.0 | ||
ordered-set==4.1.0 | ||
pyjwt==2.6.0 | ||
redis==5.0.1 | ||
requests==2.31.0 | ||
simplejson==3.18.0 | ||
argon2-cffi==23.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ isort==5.11.3 | |
mypy==0.991 | ||
pytest==7.2.0 | ||
|
||
types-redis==4.6.0.11 | ||
types-requests==2.28.1 |
Oops, something went wrong.