Sub topic 6
Expiration, approval and organization policies
This chapter covers how a fine-grained personal access token (PAT) ends, who must approve it, and how an organization can restrict it. It assumes you can already create a fine-grained token, choose a resource owner and repositories, and set permissions. It also assumes you can call the REST API with an Authorization header.
Why tokens should expire
A token that never expires stays a risk until someone remembers to delete it. Fine-grained PATs require an expiration date when you create them. This differs from classic tokens, which can be created with no expiration.
The date you pick is a trade-off. A short lifetime limits the damage from a leaked token, but you must replace the token more often. A long lifetime is more convenient, but a leak stays useful to an attacker for longer.
- Use a few days to 30 days for one-off scripts and experiments.
- Use up to 90 days for tools you check on regularly.
- Avoid very long lifetimes unless the owning organization allows them and you have a rotation process.
An organization can set a maximum lifetime for tokens that access its resources. If the maximum is shorter than the date you chose, the token cannot reach that organization's resources.
What happens at expiry
Once a token expires, GitHub rejects it. The API returns 401 Bad credentials. The token is not renewed automatically. You must generate a new one, or use the "regenerate" option on the token's settings page, and then update every place that stores the old value.
GitHub sends an email before a token expires. Treat that email as a reminder to rotate. Do not wait for a failed job to tell you.
You can detect an expired or revoked token from a script by checking the response status:
status=$(curl -s -o /dev/null -w "%{http_code}" \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/user)
if [ "$status" = "401" ]; then
echo "Token is expired, revoked or invalid. Rotate it." >&2
exit 1
fi
echo "Token accepted (HTTP $status)"Organization approval
When the resource owner is an organization, the organization can require that an owner approves each fine-grained token before it works on private resources.
The flow is:
- A member creates a token with the organization as resource owner.
- If approval is required, the token starts in a pending state.
- An organization owner reviews the repositories and permissions requested, then approves or denies it.
- An approved token works. A denied token does not.
While the request is pending, the token can still read public resources. It cannot touch private data in that organization.
If the organization does not require approval, the token is active as soon as it is created. It is still limited by the permissions you set and by the organization's other policies.
Organization policies
Organization owners control fine-grained PATs through settings. The main choices are:
| Policy | Options | Effect |
|---|---|---|
| Token access | Allow, or restrict | Controls whether fine-grained PATs can access organization resources at all. |
| Approval | Required, or not required | Decides whether an owner must approve each token first. |
| Maximum lifetime | A set number of days | Rejects tokens that outlive the limit. |
| Classic tokens | Allow, or restrict | Can block classic PATs from reaching organization resources. |
Owners can also list the active tokens that can access the organization and see which repositories and permissions each has. They can revoke any of them without contacting the token's creator.
Restricting classic tokens pushes people toward fine-grained ones. This is a common reason to review these settings.
Common errors
- Getting
401after a long period of working. The token expired. Create a replacement and update the stored secret. - Getting
403or404on an organization repository. The token may still be pending approval, or the organization may restrict fine-grained tokens. Check the token's status in your settings. - Choosing a lifetime longer than the organization maximum. The token will not reach that organization's resources, even though creation succeeds.
Exercise
Your fine-grained token targets an organization that requires approval. A teammate says the token is created, but a private repository request returns 404. Give two checks that explain this.
Show answer
First, check whether the token is still pending approval. Pending tokens only read public resources, so a private repository appears not to exist. Second, check whether the token's lifetime exceeds the organization's maximum, or whether the token was not granted that repository in its repository selection. An owner can review the pending request and approve it.
Exercise
What response status signals an expired token, and what should you do next?
Show answer
The API returns 401 Bad credentials. Generate or regenerate a token with a new expiration date, then update every place that stores the old value.