Skip to content

Checking github.Auth.AppInstallationAuth expiry raises timezone-related error #2560

@antoineKorbit

Description

@antoineKorbit

I'm currently running the master branch in order to test the PR#2540 locally, and I'm getting an exception due to expired token when it's definitely not.

Here is the context and what I do to reproduce:

  1. I have a Github application bot that I connect to using the following code:
    gh_app_auth = github.Auth.AppAuth(
        app_id=get_github_app_id(), private_key=get_private_key(), jwt_expiry=600
    )
    gh_app = github.Auth.AppInstallationAuth(
        app_auth=gh_app_auth,
        installation_id=int(installation_id),
    )
    
    rest_github = github.Github(
        auth=gh_app,
    )
  2. When I try to get a repository with rest_github.get_repo(repository_name) it works the first time.
  3. But if I run the same function again, I will get the following exception:
    Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "/opt/miniconda3/envs/my_env/lib/python3.11/site-packages/github/MainClass.py", line 360, in get_repo
        headers, data = self.__requester.requestJsonAndCheck("GET", url)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/miniconda3/envs/my_env/lib/python3.11/site-packages/github/Requester.py", line 431, in requestJsonAndCheck
        *self.requestJson(
        ^^^^^^^^^^^^^^^^^
    File "/opt/miniconda3/envs/my_env/lib/python3.11/site-packages/github/Requester.py", line 568, in requestJson
        return self.__requestEncode(cnx, verb, url, parameters, headers, input, encode)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/miniconda3/envs/my_env/lib/python3.11/site-packages/github/Requester.py", line 661, in __requestEncode
        ] = f"{self.__auth.token_type} {self.__auth.token}"
                                        ^^^^^^^^^^^^^^^^^
    File "/opt/miniconda3/envs/my_env/lib/python3.11/site-packages/github/Auth.py", line 299, in token
        if self.__installation_authorization is None or self._is_expired:
                                                        ^^^^^^^^^^^^^^^^
    File "/opt/miniconda3/envs/my_env/lib/python3.11/site-packages/github/Auth.py", line 310, in _is_expired
        return token_expires_at < datetime.now(timezone.utc)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    TypeError: can't compare offset-naive and offset-aware datetimes
    

I have set the jwt_expiry value to 600s, but it doesn't take it into consideration.
Would anyone have a suggestion ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions