Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
9 changes: 6 additions & 3 deletions docs/apps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ Example on how you would obtain the access token for authenticating as a GitHub
`Authenticating as an installation <https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/about-authentication-with-a-github-app#authentication-as-an-app-installation>`_ API endpoint.


.. function:: get_jwt(*, app_id, private_key)
.. function:: get_jwt(*, app_id, private_key, expiration = 10 * 60)

Construct the JWT (JSON Web Token), that can be used to access endpoints
that require it.
that require it. Default expiration period is 10 minutes.

Example::

Expand All @@ -58,7 +58,10 @@ Example on how you would obtain the access token for authenticating as a GitHub
-----END RSA PRIVATE KEY-----
"""

token = get_jwt(app_id=123, private_key=private_key)
# Generate a token that expires 30 minutes from now
token = get_jwt(
app_id=123, private_key=private_key, expiration = 30 * 60
)
data = gh.getitem(
"/app/installations",
jwt=token,
Expand Down
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Changelog
Unreleased
----------

- :meth:`gidgethub.apps.get_jwt` now accepts an ``expiration`` parameter to
configure JWT token expiration time
(`PR #215 <https://github.com/gidgethub/gidgethub/pull/215>`_)

- Add support for Python 3.12-3.13 and drop EOL Python 3.7
(`PR #209 <https://github.com/brettcannon/gidgethub/pull/209>`_)

Expand Down
4 changes: 2 additions & 2 deletions gidgethub/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
from gidgethub.abc import GitHubAPI


def get_jwt(*, app_id: str, private_key: str) -> str:
def get_jwt(*, app_id: str, private_key: str, expiration: int = 10 * 60) -> str:
"""Construct the JWT (JSON Web Token), used for GitHub App authentication."""
time_int = int(time.time())
payload = {"iat": time_int, "exp": time_int + (10 * 60), "iss": app_id}
payload = {"iat": time_int, "exp": time_int + expiration, "iss": app_id}
bearer_token = jwt.encode(payload, private_key, algorithm="RS256")

return bearer_token
Expand Down
24 changes: 24 additions & 0 deletions tests/test_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ def test_get_jwt(self, time_mock):

assert result == jwt.encode(expected_payload, private_key, algorithm="RS256")

@mock.patch("time.time")
def test_get_jwt_with_custom_expiry(self, time_mock):
app_id = 12345

time_mock.return_value = 1587069751.5588422

# test file copied from https://github.com/jpadilla/pyjwt/blob/master/tests/keys/testkey_rsa
private_key = (
importlib_resources.files(rsa_key_samples) / "test_rsa_key"
).read_bytes()

# Custom expiration
expiration = 30 * 60
result = apps.get_jwt(
app_id=app_id, private_key=private_key, expiration=expiration
)
expected_payload = {
"iat": 1587069751,
"exp": 1587069751 + expiration,
"iss": app_id,
}

assert result == jwt.encode(expected_payload, private_key, algorithm="RS256")

@pytest.mark.asyncio
async def test_get_installation_access_token(self):
gh = MockGitHubAPI()
Expand Down