From 89a4a7f74150bbd58d55d1aabc6d972bb64ccb72 Mon Sep 17 00:00:00 2001 From: Gustav Westling Date: Tue, 19 Mar 2024 17:44:08 +0100 Subject: [PATCH 1/4] Add app_id to JWT app cache keys This makes the cache multi-app aware, and so that it can handle JWT tokens from multiple apps --- githubkit/auth/app.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/githubkit/auth/app.py b/githubkit/auth/app.py index fab01073a..d67f34bf3 100644 --- a/githubkit/auth/app.py +++ b/githubkit/auth/app.py @@ -39,7 +39,7 @@ class AppAuth(httpx.Auth): permissions: Union[Unset, "AppPermissionsType"] = UNSET cache: "BaseCache" = DEFAULT_CACHE - JWT_CACHE_KEY = "githubkit:auth:app:jwt" + JWT_CACHE_KEY = "githubkit:auth:app:jwt:{app_id}" INSTALLATION_CACHE_KEY = ( "githubkit:auth:app:installation:" "{installation_id}:{permissions}:{repositories}:{repository_ids}" @@ -74,15 +74,17 @@ def _create_jwt(self) -> str: ) def get_jwt(self) -> str: - if not (token := self.cache.get(self.JWT_CACHE_KEY)): + cache_key = self.JWT_CACHE_KEY.format("app_id", self.app_id) + if not (token := self.cache.get(cache_key)): token = self._create_jwt() - self.cache.set(self.JWT_CACHE_KEY, token, timedelta(minutes=8)) + self.cache.set(cache_key, token, timedelta(minutes=8)) return token async def aget_jwt(self) -> str: - if not (token := await self.cache.aget(self.JWT_CACHE_KEY)): + cache_key = self.JWT_CACHE_KEY.format("app_id", self.app_id) + if not (token := await self.cache.aget(cache_key)): token = self._create_jwt() - await self.cache.aset(self.JWT_CACHE_KEY, token, timedelta(minutes=8)) + await self.cache.aset(cache_key, token, timedelta(minutes=8)) return token def _build_installation_auth_request(self) -> httpx.Request: From 2364ca548b6156e37a918cb855d4b640ade4c89a Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Wed, 20 Mar 2024 01:46:34 +0800 Subject: [PATCH 2/4] Apply suggestions from code review --- githubkit/auth/app.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/githubkit/auth/app.py b/githubkit/auth/app.py index d67f34bf3..b3c238cd4 100644 --- a/githubkit/auth/app.py +++ b/githubkit/auth/app.py @@ -39,7 +39,7 @@ class AppAuth(httpx.Auth): permissions: Union[Unset, "AppPermissionsType"] = UNSET cache: "BaseCache" = DEFAULT_CACHE - JWT_CACHE_KEY = "githubkit:auth:app:jwt:{app_id}" + JWT_CACHE_KEY = "githubkit:auth:app:{app_id}:jwt" INSTALLATION_CACHE_KEY = ( "githubkit:auth:app:installation:" "{installation_id}:{permissions}:{repositories}:{repository_ids}" @@ -74,14 +74,14 @@ def _create_jwt(self) -> str: ) def get_jwt(self) -> str: - cache_key = self.JWT_CACHE_KEY.format("app_id", self.app_id) + cache_key = self.JWT_CACHE_KEY.format(app_id=self.app_id) if not (token := self.cache.get(cache_key)): token = self._create_jwt() self.cache.set(cache_key, token, timedelta(minutes=8)) return token async def aget_jwt(self) -> str: - cache_key = self.JWT_CACHE_KEY.format("app_id", self.app_id) + cache_key = self.JWT_CACHE_KEY.format(app_id=self.app_id) if not (token := await self.cache.aget(cache_key)): token = self._create_jwt() await self.cache.aset(cache_key, token, timedelta(minutes=8)) From fdb641297ab62041d2907c5b15648125c9bf9c2d Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Wed, 20 Mar 2024 16:31:15 +0800 Subject: [PATCH 3/4] :bug: add app_id to installation cache key --- githubkit/auth/app.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/githubkit/auth/app.py b/githubkit/auth/app.py index b3c238cd4..9a8b5e102 100644 --- a/githubkit/auth/app.py +++ b/githubkit/auth/app.py @@ -41,7 +41,7 @@ class AppAuth(httpx.Auth): JWT_CACHE_KEY = "githubkit:auth:app:{app_id}:jwt" INSTALLATION_CACHE_KEY = ( - "githubkit:auth:app:installation:" + "githubkit:auth:app:{app_id}:installation:" "{installation_id}:{permissions}:{repositories}:{repository_ids}" ) @@ -156,6 +156,7 @@ def _get_installation_cache_key(self) -> str: [] if isinstance(self.repository_ids, Unset) else self.repository_ids ) return self.INSTALLATION_CACHE_KEY.format( + app_id=self.app_id, installation_id=self.installation_id, permissions=",".join( name if value == "read" else f"{name}!" From 55c22ba40acf08d73def2c48dfd8e96f7e1f5ef8 Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Wed, 20 Mar 2024 16:33:24 +0800 Subject: [PATCH 4/4] Update app.py --- githubkit/auth/app.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/githubkit/auth/app.py b/githubkit/auth/app.py index 9a8b5e102..ca5e3ddd0 100644 --- a/githubkit/auth/app.py +++ b/githubkit/auth/app.py @@ -73,15 +73,18 @@ def _create_jwt(self) -> str: algorithm="RS256", ) + def _get_jwt_cache_key(self) -> str: + return self.JWT_CACHE_KEY.format(app_id=self.app_id) + def get_jwt(self) -> str: - cache_key = self.JWT_CACHE_KEY.format(app_id=self.app_id) + cache_key = self._get_jwt_cache_key() if not (token := self.cache.get(cache_key)): token = self._create_jwt() self.cache.set(cache_key, token, timedelta(minutes=8)) return token async def aget_jwt(self) -> str: - cache_key = self.JWT_CACHE_KEY.format(app_id=self.app_id) + cache_key = self._get_jwt_cache_key() if not (token := await self.cache.aget(cache_key)): token = self._create_jwt() await self.cache.aset(cache_key, token, timedelta(minutes=8))