Skip to content

Commit dd76851

Browse files
Phlogistiqueclaude
andauthored
Read the GitHub App private key from the environment (#37)
Reverts #34. Reading GH_APP_PRIVATE_KEY from the environment instead of a .gh-app-private-key.pem file lets the token generator run in Claude Code web, where the key is provided as an environment variable rather than a file. Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent a168db2 commit dd76851

1 file changed

Lines changed: 6 additions & 13 deletions

File tree

tests/get_github_app_token.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010
"""
1111
GitHub App Token Generator with caching
1212
13-
Generates an installation access token from GitHub App credentials.
13+
Generates an installation access token from GitHub App credentials in environment.
1414
Caches tokens and reuses them until they expire (with 5-minute buffer).
1515
1616
Environment variables required:
1717
- GH_APP_ID: GitHub App ID
18-
19-
Files required:
20-
- .gh-app-private-key.pem: PEM private key
18+
- GH_APP_PRIVATE_KEY: PEM private key
2119
2220
Usage:
2321
# Run with uv (automatically installs dependencies)
@@ -42,7 +40,6 @@
4240

4341
# Cache file location
4442
CACHE_FILE = Path("/tmp/gh_app_token_cache.json")
45-
PRIVATE_KEY_FILE = Path(__file__).resolve().parents[1] / ".gh-app-private-key.pem"
4643
# Buffer time before expiration (5 minutes)
4744
EXPIRATION_BUFFER_SECONDS = 300
4845

@@ -99,18 +96,14 @@ def save_token_to_cache(token, expires_at):
9996

10097
def generate_installation_token():
10198
"""Generate a new installation access token for the GitHub App."""
99+
# Get credentials from environment
102100
app_id = os.getenv("GH_APP_ID")
101+
private_key = os.getenv("GH_APP_PRIVATE_KEY")
103102

104-
if not app_id:
105-
print("Error: Missing GH_APP_ID", file=sys.stderr)
106-
sys.exit(1)
107-
108-
if not PRIVATE_KEY_FILE.exists():
109-
print(f"Error: Missing private key file: {PRIVATE_KEY_FILE}", file=sys.stderr)
103+
if not all([app_id, private_key]):
104+
print("Error: Missing GH_APP_ID or GH_APP_PRIVATE_KEY", file=sys.stderr)
110105
sys.exit(1)
111106

112-
private_key = PRIVATE_KEY_FILE.read_text()
113-
114107
# Generate JWT
115108
now = int(time.time())
116109
payload = {

0 commit comments

Comments
 (0)