Sub topic 4
Scopes, expiration, and revoking tokens
This chapter assumes you can already clone and push over HTTPS and have created a personal access token (PAT), as covered in the previous chapter. Here you will limit what a token can do, make it expire, and remove it when it is no longer safe.
Why token limits matter
A token is a password substitute. Anyone who holds the string can act as you, within the permissions the token carries. Authentication (proving who you are) succeeded when you created the token. Authorization (what the token may do) is set by its scopes or permissions. A narrow token limits the damage if it leaks.
Two kinds of tokens
GitHub offers two kinds of personal access token.
| Kind | How access is limited | Best for |
|---|---|---|
| Fine-grained token | Chosen repositories plus specific permissions, such as "Contents: Read and write" | Most Git work |
| Classic token | Broad scopes such as `repo` that cover every repository you can access | Tools that do not yet support fine-grained tokens |
Prefer a fine-grained token. It can be limited to one repository, and it must have an expiration date.
Choosing minimal permissions
For git clone, git pull, and git push over HTTPS, a fine-grained token needs the Contents repository permission. Use "Read-only" if you only fetch and "Read and write" if you push. Start there and add permissions only when a command fails because of a missing one.
With a classic token, the repo scope is the usual choice for private repositories. It grants full control of every repository you can access, so use it only when a fine-grained token will not work.
Setting an expiration
Choose an expiration date when you create the token, such as 30 or 90 days. An expired token stops working on its own, so a forgotten copy in an old script or backup becomes harmless. When it expires, Git fails with an authentication error, and you must generate a new token and update your stored credential.
Revoking a token
Revoke a token at once if it appears in a commit, a log, a screenshot, or a lost laptop.
- Open GitHub and go to Settings, then Developer settings, then Personal access tokens.
- Select the token.
- Choose Delete (fine-grained) or Delete on the classic token list.
- Create a replacement with narrower scope and update your credential helper.
Deleting takes effect immediately. Any machine still using the old token will then see an authentication failure.
Checking which token is in use
If Git works but you are unsure which token it is using, review your stored credential. First, see which helper holds it:
git config --global credential.helperThen compare the token's name and last-used date in the GitHub token list. Tokens with no recent use are good candidates for deletion.
Exercise
A teammate pastes a classic token with the repo scope into a public issue by mistake. Name the first action to take and one way to reduce the damage of a future leak.
Show answer
Delete (revoke) the token in Settings, Developer settings, Personal access tokens, so it stops working immediately. To reduce future damage, replace it with a fine-grained token limited to the needed repository, with only the Contents permission and a short expiration date.