From 3eac802eaafb1ab77645169e82adde95233ff5f7 Mon Sep 17 00:00:00 2001 From: Min RK Date: Fri, 14 Jun 2024 13:21:42 +0200 Subject: [PATCH] apply token via auth adapter requests doesn't respect Authorization header reliably --- github_activity/auth.py | 17 +++++++++++++++++ github_activity/github_activity.py | 8 +++++--- github_activity/graphql.py | 10 ++++++---- 3 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 github_activity/auth.py diff --git a/github_activity/auth.py b/github_activity/auth.py new file mode 100644 index 0000000..29efcfd --- /dev/null +++ b/github_activity/auth.py @@ -0,0 +1,17 @@ +from requests.auth import AuthBase + + +class TokenAuth(AuthBase): + """Apply Bearer token auth to request + + Requests doesn't respect Authorization header reliably + unless applied via `auth` adapter. + """ + + def __init__(self, token): + self.token = token + + def __call__(self, r): + # add token to auth + r.headers["Authorization"] = f"Bearer {self.token}" + return r diff --git a/github_activity/github_activity.py b/github_activity/github_activity.py index 54c43d9..374fa17 100644 --- a/github_activity/github_activity.py +++ b/github_activity/github_activity.py @@ -18,6 +18,7 @@ import pytz import requests +from .auth import TokenAuth from .cache import _cache_data from .graphql import GitHubGraphQlQuery @@ -781,11 +782,12 @@ def _get_datetime_and_type(org, repo, datetime_or_git_ref, auth): ) -def _get_datetime_from_git_ref(org, repo, ref, auth): +def _get_datetime_from_git_ref(org, repo, ref, token): """Return a datetime from a git reference.""" - headers = {"Authorization": "Bearer %s" % auth} + auth = TokenAuth(token) url = f"https://api.github.com/repos/{org}/{repo}/commits/{ref}" - response = requests.get(url, headers=headers) + # prevent requests from using netrc + response = requests.get(url, auth=auth) response.raise_for_status() return dateutil.parser.parse(response.json()["commit"]["committer"]["date"]) diff --git a/github_activity/graphql.py b/github_activity/graphql.py index 57eea9a..b26d382 100644 --- a/github_activity/graphql.py +++ b/github_activity/graphql.py @@ -6,6 +6,8 @@ import requests from tqdm.auto import tqdm +from .auth import TokenAuth + comments_query = """\ comments(last: 100) { edges { @@ -136,8 +138,8 @@ def __init__(self, query, display_progress=True, auth=None): self.query = query # Authentication - auth = auth or os.environ.get("GITHUB_ACCESS_TOKEN") - if not auth: + token = auth or os.environ.get("GITHUB_ACCESS_TOKEN") + if not token: raise ValueError( "Either the environment variable GITHUB_ACCESS_TOKEN or the " "--auth flag or must be used to pass a Personal Access Token " @@ -146,7 +148,7 @@ def __init__(self, query, display_progress=True, auth=None): "working with a public repository, you don’t need to set any " "scopes on the token you create." ) - self.headers = {"Authorization": "Bearer %s" % auth} + self.auth = TokenAuth(token) self.gql_template = gql_template self.display_progress = display_progress @@ -183,7 +185,7 @@ def request(self, n_pages=100, n_per_page=50): ii_request = requests.post( "https://api.github.com/graphql", json={"query": ii_gql_query}, - headers=self.headers, + auth=self.auth, ) if ii_request.status_code != 200: raise Exception(