From ed2d306dee78b3be6dd81b7bb05da95f62e1d80d Mon Sep 17 00:00:00 2001 From: Hay1tsme Date: Thu, 30 Nov 2023 18:22:01 -0500 Subject: [PATCH] adb: add configurable auth id lifetime --- core/aimedb.py | 4 ++-- core/config.py | 6 ++++++ core/utils.py | 4 ++-- example_config/core.yaml | 1 + 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/core/aimedb.py b/core/aimedb.py index 1945b20..e65c2c7 100644 --- a/core/aimedb.py +++ b/core/aimedb.py @@ -182,7 +182,7 @@ class AimedbProtocol(Protocol): ) if user_id and user_id > 0 and self.config.aimedb.id_secret: - auth_key = create_sega_auth_key(user_id, req.head.game_id, req.head.store_id, req.head.keychip_id, self.config.aimedb.id_secret) + auth_key = create_sega_auth_key(user_id, req.head.game_id, req.head.store_id, req.head.keychip_id, self.config.aimedb.id_secret, self.config.aimedb.id_lifetime_seconds) if auth_key is not None: auth_key_extra_len = 256 - len(auth_key) auth_key_full = auth_key.encode() + (b"\0" * auth_key_extra_len) @@ -254,7 +254,7 @@ class AimedbProtocol(Protocol): resp = ADBFelicaLookup2Response.from_req(req.head, user_id, access_code) if user_id and user_id > 0 and self.config.aimedb.id_secret: - auth_key = create_sega_auth_key(user_id, req.head.game_id, req.head.store_id, req.head.keychip_id, self.config.aimedb.id_secret) + auth_key = create_sega_auth_key(user_id, req.head.game_id, req.head.store_id, req.head.keychip_id, self.config.aimedb.id_secret, self.config.aimedb.id_lifetime_seconds) if auth_key is not None: auth_key_extra_len = 256 - len(auth_key) auth_key_full = auth_key.encode() + (b"\0" * auth_key_extra_len) diff --git a/core/config.py b/core/config.py index 44d83a8..68db052 100644 --- a/core/config.py +++ b/core/config.py @@ -320,6 +320,12 @@ class AimedbConfig: self.__config, "core", "aimedb", "id_secret", default="" ) + @property + def id_lifetime_seconds(self) -> int: + return CoreConfig.get_config_field( + self.__config, "core", "aimedb", "id_lifetime_seconds", default=86400 + ) + class MuchaConfig: def __init__(self, parent_config: "CoreConfig") -> None: diff --git a/core/utils.py b/core/utils.py index bdde5f4..8264213 100644 --- a/core/utils.py +++ b/core/utils.py @@ -64,10 +64,10 @@ class Utils: return cls.real_title_port_ssl -def create_sega_auth_key(aime_id: int, game: str, place_id: int, keychip_id: str, b64_secret: str, err_logger: str = 'aimedb') -> Optional[str]: +def create_sega_auth_key(aime_id: int, game: str, place_id: int, keychip_id: str, b64_secret: str, exp_seconds: int = 86400, err_logger: str = 'aimedb') -> Optional[str]: logger = logging.getLogger(err_logger) try: - return jwt.encode({ "aime_id": aime_id, "game": game, "place_id": place_id, "keychip_id": keychip_id, "exp": int(datetime.now(tz=timezone.utc).timestamp()) + 86400 }, b64decode(b64_secret), algorithm="HS256") + return jwt.encode({ "aime_id": aime_id, "game": game, "place_id": place_id, "keychip_id": keychip_id, "exp": int(datetime.now(tz=timezone.utc).timestamp()) + exp_seconds }, b64decode(b64_secret), algorithm="HS256") except jwt.InvalidKeyError: logger.error("Failed to encode Sega Auth Key because the secret is invalid!") return None diff --git a/example_config/core.yaml b/example_config/core.yaml index 69234a5..21b1a9d 100644 --- a/example_config/core.yaml +++ b/example_config/core.yaml @@ -57,6 +57,7 @@ aimedb: port: 22345 key: "" id_secret: "" + id_lifetime_seconds: 86400 mucha: enable: False