Skip to content

Instantly share code, notes, and snippets.

@khamidou
Created May 24, 2018 16:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khamidou/9fc0b881d4bee7475ddec615e81c8ca6 to your computer and use it in GitHub Desktop.
Save khamidou/9fc0b881d4bee7475ddec615e81c8ca6 to your computer and use it in GitHub Desktop.
How to generate Github API JWT tokens in Python
#!/usr/bin/env python3
#
# This is an example script that generates JWT tokens for the Github API.
# Usage: genjwt.py your_github_private_key.pem
#
# After getting a token, you can make curl requests to the API like this:
# curl -i -H "Authorization: Bearer JWT_TOKEN" -H "Accept: application/vnd.github.machine-man-preview+json" "https://api.github.com/app"
import sys
import jwt
import time
from cryptography.hazmat.primitives.serialization import load_pem_private_key
from cryptography.hazmat.backends import default_backend
current_time = int(time.time())
payload = {
# issued at time
'iat': current_time,
# JWT expiration time (10 minute maximum)
'exp': current_time + (10 * 60),
# GitHub App's identifier – you can get it from the github application dashboard
'iss': YOUR_APP_IDENTIFIER,
}
private_key_file = sys.argv[1]
with open(private_key_file) as fd:
private_key_contents = fd.read().encode()
cert_obj = load_pem_private_key(private_key_contents, password=None, backend=default_backend())
encoded = jwt.encode(payload, private_key_contents, algorithm='RS256')
print(encoded)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment