Skip to content

Instantly share code, notes, and snippets.

@guibranco
Created September 23, 2023 20:56
Show Gist options
  • Save guibranco/9de2f2534a348f1bcf70827114d841dc to your computer and use it in GitHub Desktop.
Save guibranco/9de2f2534a348f1bcf70827114d841dc to your computer and use it in GitHub Desktop.
Generates a JWT for a GitHub application based on PEM file and APP ID.
#!/usr/bin/env python3
import jwt
import time
import sys
# Get PEM file path
if len(sys.argv) > 1:
pem = sys.argv[1]
else:
pem = input("Enter path of private PEM file: ")
# Get the App ID
if len(sys.argv) > 2:
app_id = sys.argv[2]
else:
app_id = input("Enter your APP ID: ")
# Open PEM
with open(pem, 'rb') as pem_file:
signing_key = jwt.jwk_from_pem(pem_file.read())
payload = {
# Issued at time
'iat': int(time.time()),
# JWT expiration time (10 minutes maximum)
'exp': int(time.time()) + 600,
# GitHub App's identifier
'iss': app_id
}
# Create JWT
jwt_instance = jwt.JWT()
encoded_jwt = jwt_instance.encode(payload, signing_key, alg='RS256')
print(f"JWT: {encoded_jwt}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment