Sub topic 6
Expiration, approval and organization policies
This chapter covers the controls that limit how long a fine-grained personal access token (PAT) lives and who must sign off on it. It assumes you have read the earlier chapters in this tutorial. You should know how to create a fine-grained PAT, what a resource owner is, and how repository selection and permissions work. You should also be comfortable sending a request with an Authorization header.
Why lifecycle controls matter
Repository selection and permissions limit what a token can do. Lifecycle controls limit how long it can do it, and whether anyone besides its creator agreed to it. A leaked token with narrow permissions is still a problem if it works for three years. Likewise, a developer can create a token that reaches an organization's private repositories without the organization knowing.
GitHub addresses both problems with three mechanisms:
- Expiration. Every fine-grained PAT has an end date.
- Approval. An organization can require an owner to approve a token before it can reach private organization resources.
- Policies. Organization owners and enterprise owners can decide whether fine-grained PATs are allowed at all, and how long they may live.
Expiration
When you create a fine-grained PAT, you choose an expiration. The usual options are 7, 30, 60 or 90 days, or a custom date. A token's lifetime has an upper limit of 366 days, unless a no-expiration option is offered. GitHub shows a warning when you choose no expiration, and an organization policy can remove that option.
Once the expiration date passes, the token stops working. Requests made with it return 401 Unauthorized. The token cannot be extended. You can regenerate it from the token's settings page, which issues a new secret with the same name, owner, repositories and permissions. Anything that used the old secret must be updated. GitHub also sends an email before a token expires.
This gives you a practical rule. Choose the shortest lifetime that fits the job. Use 7 days for a one-off migration. Use 90 days for a scheduled script, and rotate it on a calendar. If a task needs a credential that never expires, that is a signal to consider a GitHub App instead. Installation access tokens last one hour and are minted on demand.
Checking expiry from the API
Every API response made with a token that expires includes a header named github-authentication-token-expiration. Its value is a UTC timestamp. You can use it to warn yourself before a token dies.
#!/usr/bin/env bash
set -euo pipefail
# Reads the expiry header from any authenticated response and prints days left.
expiry=$(curl -s -D - -o /dev/null \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/user \
| tr -d '\r' \
| grep -i '^github-authentication-token-expiration:' \
| cut -d' ' -f2-)
if [ -z "$expiry" ]; then
echo "No expiration header: the token does not expire, or the request failed."
exit 1
fi
now=$(date -u +%s)
end=$(date -u -d "$expiry" +%s) # GNU date; on macOS install coreutils and use gdate
echo "Token expires: $expiry ($(( (end - now) / 86400 )) days left)"Run this from a scheduled job and alert when the count drops below a threshold such as 14 days.
Approval
Approval applies when the resource owner is an organization. The organization's setting decides what happens when a member creates a token that targets it.
The key rules are these:
- A token that only reads public resources needs no approval. Public data is available to everyone anyway.
- If the organization requires approval, a token that requests private resources is created in a pending state. It can't reach those resources until an owner approves it.
- Tokens created by organization owners are approved automatically, because owners could approve them anyway.
- If the organization does not require approval, the token works for the selected repositories immediately. Owners can still see it and revoke it.
A pending token doesn't fail loudly. A request for a private repository typically returns 404 Not Found or 403 Forbidden, as if the repository didn't exist for that token. A developer who sees this should check the token's status in their settings before debugging permissions.
The token request lifecycle
The diagram shows the states a token passes through when its resource owner requires approval.
Owners handle requests under Organization settings, then Personal access tokens. There are two views. Pending requests lists tokens waiting for a decision, with the requested repositories and permissions. Active tokens lists approved tokens, and from there an owner can inspect or revoke each one.
A good review checks three things: whether the token needs write access, whether it needs all repositories, and whether its expiration is reasonable. If the request is too broad, deny it and ask the member to create a narrower one.
Automating review through the API
Organizations with many members can list requests through the REST API. The caller needs organization-level permission to manage token requests. In practice this means a GitHub App or fine-grained token with the matching organization permission.
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $ADMIN_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/orgs/ORG/personal-access-token-requestsThe response includes the requesting user, the permissions asked for, and the repository selection. A script can flag requests that ask for write access to many repositories and leave the rest for a human.
Organization policies
Organization owners set the policy for fine-grained PATs in the organization's Personal access tokens settings. Enterprise owners can set defaults or enforce choices across all organizations in the enterprise.
| Policy | Options | Effect |
|---|---|---|
| Fine-grained PAT access | Allow, or restrict | When restricted, fine-grained PATs cannot reach the organization's private resources, even if the member has access themselves. |
| Approval | Require approval, or do not require | Controls whether tokens targeting private resources start as pending. |
| Classic PAT access | Allow, or restrict | When restricted, classic tokens cannot reach the organization's resources. |
| Maximum lifetime | A number of days | Rejects tokens with a longer lifetime, including no-expiration tokens. |
A few points about how these interact:
- Policies apply to the resource owner, not the person. The same developer can have a token that works for their personal repositories and is blocked for an organization's.
- Restriction takes effect on existing tokens. If an owner restricts classic tokens, tokens already in use stop working for that organization. Tell your team before changing it.
- A maximum lifetime applies when the token is created. Tokens that already exist and exceed the limit may need to be replaced. Check the settings page for what applies to them.
- Fine-grained tokens and SAML SSO. If your organization enforces SAML single sign-on, fine-grained PATs don't need the separate "authorize for SSO" step that classic tokens need. Verify this against your own organization's behavior before relying on it in an audit.
The policy that usually gives the most security for the least friction is: restrict classic tokens, allow fine-grained tokens, require approval, and set a maximum lifetime of 90 days or less.
How this compares to GitHub Apps
Apps follow a similar idea through different mechanisms. An installation access token expires after one hour, so it needs no lifecycle policy. An organization owner approves the installation, which grants the app its permissions and repositories. If the app later asks for more permissions, the installation owner must approve the change. User access tokens from an app can expire after 8 hours, with refresh tokens, if token expiration is enabled for the app.
Audit and response
Approvals and revocations are recorded in the organization's audit log. Searching it is the quickest way to answer "who approved this token, and when?". If a token is exposed, don't wait for expiration. Revoke it from the owner's Active tokens view, or ask the member to delete it, and then review the log for activity under that token.
Common errors
- A new token returns 404 on a private organization repository. The token is probably pending approval, or the organization restricts fine-grained PATs.
- A token creation form won't accept "No expiration". The organization or enterprise sets a maximum lifetime.
- A script that worked yesterday now returns 401. Check the expiration date first, then check whether the token was revoked.
- A token works for public repositories but not private ones. Public access needs no approval. Private access does.
Exercise
Your organization requires approval for fine-grained PATs and has a maximum lifetime of 90 days. A teammate creates a token with a 30-day lifetime for three private repositories, and the first API call returns 404. What is the most likely cause, and what are the next two steps?
Show answer
The token is pending approval, so it can't yet reach private organization resources. The teammate should check the token's status in their settings and ask an organization owner to review it. The owner opens Organization settings, Personal access tokens, Pending requests. There they check the requested repositories and permissions, and then approve or deny.
Exercise
What does the github-authentication-token-expiration response header tell you, and how would you use it?
Show answer
It gives the UTC time when the token used for the request expires. A scheduled script can read it from any authenticated response, compute the days remaining, and alert before the token stops working with 401 errors.