Skip to content

Commit

Permalink
🐛 Bug: app auth cache keys missing app_id (#95)
Browse files Browse the repository at this point in the history
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
  • Loading branch information
zegl and yanyongyu committed Mar 20, 2024
1 parent 57ead1b commit e577197
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions githubkit/auth/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ 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:{app_id}:jwt"
INSTALLATION_CACHE_KEY = (
"githubkit:auth:app:installation:"
"githubkit:auth:app:{app_id}:installation:"
"{installation_id}:{permissions}:{repositories}:{repository_ids}"
)

Expand Down Expand Up @@ -73,16 +73,21 @@ 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:
if not (token := self.cache.get(self.JWT_CACHE_KEY)):
cache_key = self._get_jwt_cache_key()
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._get_jwt_cache_key()
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:
Expand Down Expand Up @@ -154,6 +159,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}!"
Expand Down

0 comments on commit e577197

Please sign in to comment.