Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: app auth cache keys missing app_id #95

Merged
merged 4 commits into from
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
algorithm="RS256",
)

def _get_jwt_cache_key(self) -> str:
return self.JWT_CACHE_KEY.format(app_id=self.app_id)

Check warning on line 77 in githubkit/auth/app.py

View check run for this annotation

Codecov / codecov/patch

githubkit/auth/app.py#L77

Added line #L77 was not covered by tests

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)):

Check warning on line 81 in githubkit/auth/app.py

View check run for this annotation

Codecov / codecov/patch

githubkit/auth/app.py#L80-L81

Added lines #L80 - L81 were not covered by tests
token = self._create_jwt()
self.cache.set(self.JWT_CACHE_KEY, token, timedelta(minutes=8))
self.cache.set(cache_key, token, timedelta(minutes=8))

Check warning on line 83 in githubkit/auth/app.py

View check run for this annotation

Codecov / codecov/patch

githubkit/auth/app.py#L83

Added line #L83 was not covered by tests
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)):

Check warning on line 88 in githubkit/auth/app.py

View check run for this annotation

Codecov / codecov/patch

githubkit/auth/app.py#L87-L88

Added lines #L87 - L88 were not covered by tests
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))

Check warning on line 90 in githubkit/auth/app.py

View check run for this annotation

Codecov / codecov/patch

githubkit/auth/app.py#L90

Added line #L90 was not covered by tests
return token

def _build_installation_auth_request(self) -> httpx.Request:
Expand Down Expand Up @@ -154,6 +159,7 @@
[] 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
Loading