A powerful CLI tool for generating, managing, and analyzing GitHub App installation tokens. This tool simplifies the process of working with GitHub Apps by providing easy token generation, validation, and comprehensive app analysis.
- Features
- Installation
- Quick Start
- Usage Examples
- Command Reference
- Environment Variables
- Sample Outputs
- Common Use Cases
- π Token Generation: Generate GitHub App installation tokens for organizations or specific installations
- β Token Validation: Validate existing tokens and check their permissions
- ποΈ Token Revocation: Safely revoke tokens with confirmation prompts
- π App Analysis: Comprehensive analysis of GitHub App permissions, installations, and repositories
- π§ Flexible Authentication: Support for both private key files and direct key content
- π Environment Variables: Full support for environment-based configuration
- π¨ Rich Output: Colorized, well-formatted output with emojis and clear sections
- π Debug Mode: Detailed logging for troubleshooting
- A GitHub App with appropriate permissions
- GitHub App private key
pip install git+https://github.com/opencpk/cpk-lib-python-github.git@maingithub-app-token-generator --helpexport APP_ID=${{YOUR_APP_ID}}
export PRIVATE_KEY_PATH=bot.pemgithub-app-token-generator --org orgincgithub-app-token-generator --list-installationsgithub-app-token-generator --org orginc --app-id ${{YOUR_APP_ID}} --private-key-path bot.pemgithub-app-token-generator --installation-id ${{YOUR_INST_ID}} --app-id ${{YOUR_APP_ID}} --private-key-path bot.pemgithub-app-token-generator --org orginc --app-id ${{YOUR_APP_ID}} --private-key "$(cat /path/to/bot.pem)"github-app-token-generator --list-installations --app-id ${{YOUR_APP_ID}} --private-key-path bot.pemgithub-app-token-generator --app-id ${{YOUR_APP_ID}} --private-key-path bot.pemgithub-app-token-generator --analyze-app --app-id ${{YOUR_APP_ID}} --private-key-path bot.pemgithub-app-token-generator --validate-token $ghs_TOKENgithub-app-token-generator --revoke-token $ghs_TOKENgithub-app-token-generator --revoke-token $ghs_TOKEN --forcegithub-app-token-generator --debug --org orginc --app-id ${{YOUR_APP_ID}} --private-key-path bot.pemgithub-app-token-generator --help| Command | Description | Required Arguments |
|---|---|---|
--org <name> |
Generate token for organization | --app-id, --private-key-path or --private-key |
--installation-id <id> |
Generate token for installation ID | --app-id, --private-key-path or --private-key |
--list-installations |
List all app installations | --app-id, --private-key-path or --private-key |
--analyze-app |
Comprehensive app analysis | --app-id, --private-key-path or --private-key |
--validate-token <token> |
Validate existing token | None |
--revoke-token <token> |
Revoke existing token | None |
--force |
Skip confirmation prompts | Used with --revoke-token |
--debug |
Enable debug logging | None |
--help |
Show help message | None |
| Variable | Description | Example |
|---|---|---|
APP_ID |
GitHub App ID | ${{YOUR_APP_ID}} |
PRIVATE_KEY_PATH |
Path to private key file | bot.pem |
PRIVATE_KEY |
Private key content directly | "$(cat /path/to/bot.pem)" |
# Using file path (recommended)
export APP_ID=${{YOUR_APP_ID}}
export PRIVATE_KEY_PATH=bot.pem
# Using key content from file
export APP_ID=${{YOUR_APP_ID}}
export PRIVATE_KEY="$(cat /path/to/bot.pem)"
# Then use shorter commands
github-app-token-generator --org orginc
github-app-token-generator --list-installations
github-app-token-generator --analyze-app$ github-app-token-generator --list-installations --app-id ${{YOUR_APP_ID}} --private-key-path bot.pemOutput:
=== Available GitHub App Installations ===
Installation ID | Account | Target Type
------------------------------------------------------------
${{YOUR_INST_ID}} | orginc | Organization
87654321 | mycompany | Organization
12345678 | individual-user | User
βΉοΈ Found 3 installation(s)
π‘ Use --org <org-name> or --installation-id <id> to get a token
$ github-app-token-generator --org orginc --app-id ${{YOUR_APP_ID}} --private-key-path bot.pemOutput:
$ghs_TOKEN
π β
Token generated for organization 'orginc'
$ github-app-token-generator --validate-token $ghs_TOKENOutput:
β
Token is valid
Type: GitHub App Installation Token
Repositories: 25
Scopes: GitHub App permissions
Rate limit: 4847/5000
$ github-app-token-generator --validate-token ghs_InvalidToken123456789Output:
β Token is invalid or expired
Reason: Invalid or expired token
$ github-app-token-generator --analyze-app --app-id ${{YOUR_APP_ID}} --private-key-path bot.pemOutput:
=== GitHub App Analysis ===
π€ App Information
ID: ${{YOUR_APP_ID}}
Name: Your org GitHub Bot
Slug: org-github-bot
Description: Automated GitHub operations for Your org
Owner: orginc
Owner Type: Organization
URL: https://github.com/apps/org-github-bot
Created: 2024-01-15
π Installation Summary
Total Installations: 2
Total Repositories: 47
Installed On:
β
orginc (Organization) - 2024-01-15
β
org-dev (Organization) - 2024-02-01
π App Permissions
βοΈ contents: write
ποΈ metadata: read
βοΈ pull_requests: write
ποΈ issues: read
π§ actions: write
π‘ Subscribed Events
π¨ issues
π¨ pull_request
π¨ push
π¨ release
π Accessible Repositories (47 total)
orginc:
β’ orginc/main-website
β’ orginc/api-backend
β’ orginc/mobile-app
β’ orginc/infrastructure
org-dev:
β’ org-dev/test-repo
β’ org-dev/experimental-features
... and 41 more repositories
$ github-app-token-generator --revoke-token $ghs_TOKENOutput:
β οΈ Are you sure you want to revoke this token? (y/N): y
β
Token revoked successfully
$ github-app-token-generator --org nonexistent-org --app-id ${{YOUR_APP_ID}} --private-key-path bot.pemOutput:
β No installation found for organization: nonexistent-org
$ github-app-token-generator --debug --org orginc --app-id ${{YOUR_APP_ID}} --private-key-path bot.pemOutput:
2024-06-10 14:30:15,123 - __main__ - DEBUG - Debug logging enabled
2024-06-10 14:30:15,124 - __main__ - INFO - Starting GitHub App token generation
2024-06-10 14:30:15,124 - __main__ - INFO - App ID: ${{YOUR_APP_ID}}
2024-06-10 14:30:15,124 - __main__ - INFO - Private key path: bot.pem
2024-06-10 14:30:15,125 - __main__ - DEBUG - Reading private key from file: bot.pem
2024-06-10 14:30:15,126 - __main__ - DEBUG - Successfully read private key from: bot.pem
2024-06-10 14:30:15,127 - __main__ - DEBUG - Successfully generated JWT for app ID: ${{YOUR_APP_ID}}
2024-06-10 14:30:15,128 - __main__ - DEBUG - Fetching GitHub App installations
2024-06-10 14:30:15,456 - __main__ - INFO - Found 2 installations
2024-06-10 14:30:15,457 - __main__ - INFO - Looking for installation for organization: orginc
2024-06-10 14:30:15,458 - __main__ - INFO - Found installation ID ${{YOUR_INST_ID}} for organization: orginc
2024-06-10 14:30:15,459 - __main__ - DEBUG - Requesting access token for installation ID: ${{YOUR_INST_ID}}
2024-06-10 14:30:15,678 - __main__ - INFO - Successfully obtained access token for installation: ${{YOUR_INST_ID}}
ghs_TOKEN
π β
Token generated for organization 'orginc'
# In your CI/CD script
export APP_ID=${{YOUR_APP_ID}}
export PRIVATE_KEY="$GITHUB_APP_PRIVATE_KEY" # From secrets
TOKEN=$(github-app-token-generator --org orginc)
# Use $TOKEN for GitHub API calls
curl -H "Authorization: token $TOKEN" https://api.github.com/repos/orginc/myrepo# Set up environment
export APP_ID=${{YOUR_APP_ID}}
export PRIVATE_KEY_PATH=bot.pem
# Generate token for development
TOKEN=$(github-app-token-generator --org orginc)
echo "Your token: $TOKEN"# Generate token
TOKEN=$(github-app-token-generator --org orginc --app-id ${{YOUR_APP_ID}} --private-key-path bot.pem)
# Validate token before using
github-app-token-generator --validate-token $TOKEN
# Use token for operations
# ... your work ...
# Clean up - revoke token
github-app-token-generator --revoke-token $TOKEN --force# Check app installations and permissions
github-app-token-generator --analyze-app --app-id ${{YOUR_APP_ID}} --private-key-path bot.pem
# List all installations
github-app-token-generator --list-installations --app-id ${{YOUR_APP_ID}} --private-key-path bot.pem
# Validate existing tokens
github-app-token-generator --validate-token $ghs_TOKEN# If you know the installation ID
github-app-token-generator --installation-id ${{YOUR_INST_ID}} --app-id ${{YOUR_APP_ID}} --private-key-path bot.pemEnable debug logging for detailed troubleshooting:
github-app-token-generator --debug --org orginc --app-id ${{YOUR_APP_ID}} --private-key-path bot.pemThis will show:
- π Detailed API requests and responses
- π JWT generation details
- π Installation lookup process
β οΈ Warning messages and errors
The tool automatically creates log files:
- Location:
github_app_token.log(in current directory) - Content: All operations, errors, and debug information
- Rotation: Append mode (consider rotating large files)
If you prefer to use this tool as a Python library in your scripts, you can import and use it directly:
python3 -c "from cpk_lib_python_github import GitHubAPIClient, TokenManager, OutputFormatter, Config
# Configure your GitHub App
config = Config(
app_id='YOUR_APP_ID',
private_key_path='YOUR_PATH/bot.pem',
timeout=60,
debug=False
)
# Initialize components
api_client = GitHubAPIClient(timeout=60)
formatter = OutputFormatter(use_colors=False)
token_manager = TokenManager(api_client, formatter)
print('π Testing generate_org_token for YOUR_ORG_NAME...')
# Generate token for organization
token_manager.generate_org_token(config, 'YOUR_ORG_NAME')
print('β
README example completed successfully!')
"This project is licensed under the GPLv3 License.
Contributions are welcome! Please feel free to submit issues and pull requests.