From 6774716e06c6f301018a9794c425a380a607e265 Mon Sep 17 00:00:00 2001 From: Hay1tsme Date: Mon, 27 Nov 2023 16:39:50 -0500 Subject: [PATCH] add sega auth key encode/decode in Utils, requires pyjwt --- core/utils.py | 37 ++++++++++++++++++++++++++++++++++++- requirements.txt | 41 +++++++++++++++++++++-------------------- 2 files changed, 57 insertions(+), 21 deletions(-) diff --git a/core/utils.py b/core/utils.py index 325272e..1f5ccf2 100644 --- a/core/utils.py +++ b/core/utils.py @@ -1,9 +1,12 @@ -from typing import Dict, Any +from typing import Dict, Any, Optional from types import ModuleType from twisted.web.http import Request import logging import importlib from os import walk +import jwt +from base64 import b64decode +from datetime import datetime, timezone from .config import CoreConfig @@ -60,3 +63,35 @@ class Utils: cls.real_title_port_ssl = cfg.title.port_ssl return cls.real_title_port_ssl + +def create_sega_auth_key(aime_id: int, game: str, ver: float, place_id: str, b64_secret: str, err_logger: str = 'aimedb') -> Optional[str]: + logger = logging.getLogger(err_logger) + try: + return jwt.encode({ "aime_id": aime_id, "game": game, "ver": ver, "place_id": place_id, "exp": int(datetime.now(tz=timezone.utc).timestamp()) + 86400 }, b64decode(b64_secret), algorithm="HS256") + except jwt.InvalidKeyError: + logger.error("Failed to encode Sega Auth Key because the secret is invalid!") + return None + except Exception as e: + logger.error(f"Unknown exception occoured when encoding Sega Auth Key! {e}") + return None + +def decode_sega_auth_key(token: str, b64_secret: str, err_logger: str = 'aimedb') -> Optional[Dict]: + logger = logging.getLogger(err_logger) + try: + return jwt.decode(token, "secret", b64decode(b64_secret), algorithms=["HS256"], options={"verify_signature": True}) + except jwt.ExpiredSignatureError: + logger.error("Sega Auth Key failed to validate due to an expired signature!") + return None + except jwt.InvalidSignatureError: + logger.error("Sega Auth Key failed to validate due to an invalid signature!") + return None + except jwt.DecodeError as e: + logger.error(f"Sega Auth Key failed to decode! {e}") + return None + except jwt.InvalidTokenError as e: + logger.error(f"Sega Auth Key is invalid! {e}") + return None + except Exception as e: + logger.error(f"Unknown exception occoured when decoding Sega Auth Key! {e}") + return None + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 53d867a..c399e1f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,20 +1,21 @@ -mypy -wheel -twisted -pytz -pyyaml -sqlalchemy==1.4.46 -mysqlclient -pyopenssl -service_identity -PyCryptodome -inflection -coloredlogs -pylibmc; platform_system != "Windows" -wacky -Routes -bcrypt -jinja2 -protobuf -autobahn -pillow +mypy +wheel +twisted +pytz +pyyaml +sqlalchemy==1.4.46 +mysqlclient +pyopenssl +service_identity +PyCryptodome +inflection +coloredlogs +pylibmc; platform_system != "Windows" +wacky +Routes +bcrypt +jinja2 +protobuf +autobahn +pillow +pyjwt