Managing and Rotating Your Personal Access Tokens
Personal access tokens should be treated with the same level of care as passwords. In this final step, we will discuss best practices for managing and rotating your tokens to maintain security.
Listing Your Personal Access Tokens
To view your existing personal access tokens on GitHub:
-
Using the GitHub CLI:
gh auth status
This command displays information about your current authentication status, including the token being used.
-
Alternatively, you can view all your tokens through the GitHub web interface:
- Go to GitHub.com and log in
- Click on your profile picture in the top right
- Select "Settings"
- Navigate to "Developer settings" > "Personal access tokens" > "Tokens (classic)"
Token Security Best Practices
To maintain the security of your personal access tokens, follow these best practices:
-
Limit token scopes: Only grant the minimum permissions necessary for the token's intended use.
-
Use descriptive names: Give your tokens meaningful names that indicate their purpose and usage.
-
Set expiration dates: For non-critical tokens, set an expiration date to enforce regular rotation.
To create a token with an expiration date using the GitHub CLI:
gh auth refresh -s repo,read:org --expiry 30d
gh auth token
This creates a token that expires in 30 days.
-
Regularly audit tokens: Periodically review your tokens and revoke any that are no longer needed.
-
Store tokens securely: Never store tokens in public repositories, unencrypted files, or share them with others.
-
Use environment variables: When using tokens in scripts or applications, store them as environment variables rather than hardcoding them.
For example:
export GITHUB_TOKEN=your_token_here
Revoking a Personal Access Token
If you suspect a token has been compromised or is no longer needed, revoke it immediately:
-
Using the GitHub CLI (replace TOKEN with your token):
gh auth logout
This logs you out and invalidates the current token.
-
Through the GitHub web interface:
- Go to GitHub.com and log in
- Navigate to "Settings" > "Developer settings" > "Personal access tokens" > "Tokens (classic)"
- Find the token you want to revoke
- Click "Delete" and confirm the action
Rotating Your Tokens
Regular token rotation is a good security practice. Here's how to rotate your tokens:
- Generate a new token with the same permissions as the old one.
- Update your applications and configurations to use the new token.
- Test that everything works with the new token.
- Revoke the old token.
For automated processes, consider using environment variables or secure vaults to store tokens, making rotation easier.
## Generate a new token
gh auth refresh -s repo,read:org
NEW_TOKEN=$(gh auth token)
## Update your Git remote URL with the new token
git remote set-url origin https://${NEW_TOKEN}@github.com/yourusername/your-repo.git
## Verify it works
git fetch
## Revoke the old token through GitHub website
echo "Remember to revoke your old token in GitHub settings"
By following these best practices, you can maintain secure access to your Git repositories while minimizing the risk of unauthorized access.