Skip to content
Draft
Show file tree
Hide file tree
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
19 changes: 15 additions & 4 deletions src/github_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
from __future__ import annotations

import contextlib
import logging
import time
from typing import Generator

import jwt
import requests

logger = logging.getLogger(__name__)


class GithubAppToken:
def __init__(self, private_key, app_id) -> None:
Expand All @@ -29,10 +32,18 @@ def get_token(self, installation_id: int) -> Generator[str, None, None]:
# This token expires in an hour
yield resp["token"]
finally:
requests.delete(
"https://api.github.com/installation/token",
headers={"Authorization": f"token {resp['token']}"},
)
try:
revoke_response = requests.delete(
"https://api.github.com/installation/token",
headers={"Authorization": f"token {resp['token']}"},
timeout=10,
)
revoke_response.raise_for_status()
except requests.RequestException:
logger.warning(
"Failed to revoke GitHub installation token; it will expire automatically.",
exc_info=True,
)

def get_jwt_token(self, private_key, app_id):
payload = {
Expand Down
29 changes: 29 additions & 0 deletions tests/test_github_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import annotations

import requests
import responses

from src.github_app import GithubAppToken


@responses.activate
def test_token_revocation_failure_does_not_mask_successful_work():
token = "synthetic-token"
installation_id = 1234
client = object.__new__(GithubAppToken)
client.headers = {"Authorization": "Bearer synthetic-jwt"}

responses.post(
f"https://api.github.com/app/installations/{installation_id}/access_tokens",
json={"token": token},
status=201,
)
responses.delete(
"https://api.github.com/installation/token",
body=requests.ConnectionError("connection closed during cleanup"),
)

with client.get_token(installation_id) as installation_token:
assert installation_token == token

assert len(responses.calls) == 2
Loading