Sub topic 13
Choosing the right credential for the job
This chapter assumes you can create a fine-grained personal access token (PAT) and know the basic GitHub App flow: sign a JWT, then exchange it for an installation access token. You should also be comfortable sending REST requests with an Authorization header. Here you will compare the options and pick one for a given task.
The options at a glance
GitHub offers five ways to authenticate an API call. Two are legacy or narrow in purpose, and three are the main choices.
- Classic PAT. It is tied to a user and uses broad OAuth scopes such as
repo. It cannot be limited to one repository. - Fine-grained PAT. It is tied to a user, has one resource owner, and is limited to selected repositories and specific permissions. It always expires.
- GitHub App installation access token. It belongs to the app, not to a person. It is limited to the installation's repositories and the app's permissions, and it expires after one hour.
- GitHub App user access token. It lets the app act on behalf of a signed-in user. Its reach is the overlap of what the app may do and what the user may do.
- OAuth app token. It acts as a user and is limited by classic scopes. GitHub recommends GitHub Apps over OAuth apps for new work.
Inside GitHub Actions there is also the built-in GITHUB_TOKEN. It is created for each workflow run, limited to the repository running the workflow, and expires when the job ends. Use it first for anything that happens inside a single repository's workflow.
What actually differs
The credentials differ on four questions: who is the actor, how narrow is the access, how long does it live, and who owns it.
| Question | Fine-grained PAT | App installation token | App user token | OAuth app token |
|---|---|---|---|---|
| Actor in audit logs | The user | The app (bot) | The user, via the app | The user, via the app |
| Repository limit | Selected repos, one owner | Installed repos, can be narrowed per token | Overlap of install and user access | Everything the user can reach, within scopes |
| Permission model | Fine-grained | Fine-grained | Fine-grained | Classic scopes |
| Lifetime | You choose an expiry | 1 hour | 8 hours by default, with a refresh token | Until revoked |
| Survives a person leaving | No | Yes | No | No |
| Receives webhooks | No | Yes | Yes, through the app | Not by default |
Two rows matter most in practice. The first is the actor. A PAT makes every action look like it came from you, so when you leave a company or lose repository access, the automation stops working. An installation token is attributed to the app, so the automation keeps running.
The second is lifetime. A PAT you create today may be valid for months, so a leak stays dangerous for that long. An installation token is useless an hour after it is minted, and a leaked JWT is useless after ten minutes. The private key is the long-lived secret in the App model, so protect it the way you would protect a PAT.
A decision flow
Work through the questions in order. Stop at the first match.
Worked scenarios
A personal script that archives your issues. Only you use it, it runs on your laptop, and it touches two repositories. Use a fine-grained PAT. Set the resource owner to your account, select the two repositories, grant only Issues: Read, and set a 30-day expiry. An App would add setup work (key management, installation, token exchange) for no real gain.
A bot that labels pull requests across your organization. It runs on a server, reacts to pull_request events, and must keep working when staff change. Use a GitHub App. Subscribe to the webhook event, grant Pull requests: Write and Issues: Write, and install it on the organization. Mint installation tokens on demand.
A SaaS product where customers connect their GitHub accounts. Each customer installs your App on their own account or organization. You hold one private key and get a separate installation token for each customer. A PAT cannot do this, because it has only one resource owner and each customer would have to create one and hand it over.
A web app that shows "my pull requests" to a signed-in user. The result depends on who is asking. Use a user access token from the App's web flow, so the app sees only what that user can see. An installation token would show everything the app was installed on, which may expose more than the user is allowed to see.
A CI job that pushes a release tag in the same repository. Use GITHUB_TOKEN with contents: write set in the workflow's permissions block. No stored secret is needed.
Accessing the container or package registry. Check the documentation first. Not every endpoint and feature accepts every token type. Package registry authentication in particular has historically relied on classic PATs, so confirm support before you design around a fine-grained token.
Narrowing an installation token further
An installation token does not have to carry everything the installation allows. When you create it, you can ask for fewer repositories and fewer permissions. This is useful when one service has several jobs and each job needs only part of the access.
curl -L -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $APP_JWT" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/app/installations/$INSTALLATION_ID/access_tokens \
-d '{"repositories":["docs-site"],"permissions":{"issues":"write"}}'The response token can touch only docs-site and can write only issues. You can request only a subset of what the installation already has. Asking for a permission the app was never granted returns an error.
Encoding the decision in code
Writing the criteria down as code is a good way to test your own understanding, and it can serve as a review checklist for a team. The following Python function is runnable as shown.
def choose_credential(
in_actions_single_repo: bool,
acts_for_other_users: bool,
unattended_or_multi_account: bool,
needs_webhooks: bool,
) -> str:
if in_actions_single_repo:
return "GITHUB_TOKEN"
if acts_for_other_users:
return "GitHub App user access token"
if unattended_or_multi_account or needs_webhooks:
return "GitHub App installation access token"
return "Fine-grained personal access token"
if __name__ == "__main__":
print(choose_credential(False, False, False, False))
print(choose_credential(False, False, True, True))
print(choose_credential(False, True, False, False))The first call prints the fine-grained PAT, the second the installation token, and the third the user access token.
Common mistakes
- Choosing a PAT for a team service. The service then depends on one person's account. When that person is offboarded, the service breaks, often at a bad moment.
- Choosing an App for a ten-minute script. The setup cost is not worth it, and a private key file is one more secret to guard.
- Using an installation token where a user token is needed. The app then acts with its own broad reach, so a user can trigger actions or see data they could not reach themselves. Use a user token whenever the result should depend on the person.
- Treating a user token as more powerful than the user. It cannot be. It is capped by the user's access and by the app's permissions, whichever is smaller.
- Falling back to a classic PAT because "it just works". A classic
reposcope covers every repository you can reach, including private ones in organizations. Treat that as a last resort and give it a short life. - Ignoring rate limits. A PAT shares one user's limit across all scripts that use it. Installation tokens get their own limit, which grows with the size of the installation, so a busy service is easier to run with an App.
Migrating off a classic token
If you inherit a script that uses a classic PAT, follow this order.
- List the API calls the script makes.
- Look up the permission each endpoint needs in the REST API reference.
- Create a fine-grained PAT, or an App if the script is a shared service, with only those permissions and repositories.
- Run the script against the new credential and fix any
403or404responses by adding the missing permission. A404on a private repository often means the token cannot see it, not that it does not exist. - Revoke the classic token once nothing uses it.
Exercise
A nightly job on a company server must comment on new issues in 40 repositories owned by one organization, and it must keep running after its original author leaves. Which credential fits, and why is a fine-grained PAT a poor choice?
Show answer
Use a GitHub App installation access token. The App is installed on the organization, so the job acts as the app and does not depend on one person's account. A fine-grained PAT belongs to a user, so the job would stop when that person loses access. A PAT also lasts as long as its expiry date, so a leak stays dangerous longer than an installation token, which lasts one hour.
Exercise
Your web app has a dashboard of "pull requests assigned to me". Should it call the API with an installation token or a user access token, and what is the risk of the other choice?
Show answer
Use a user access token from the App's user authorization flow. Its reach is limited to what that signed-in user can see. An installation token reflects everything the app was installed on, so the dashboard could show pull requests the user is not allowed to view.