Sub topic 13
Choosing the right credential for the job
This chapter assumes you know how fine-grained personal access tokens (PATs) work. It also assumes you know what a GitHub App is, how installations work, and how an app gets an installation access token. Here you will use that knowledge to pick a credential for a given task.
The three candidates
- Fine-grained PAT. A token you create for your own account or for an organization you belong to. It acts as you, limited to the repositories and permissions you select.
- GitHub App. An identity that is installed on accounts and repositories. It acts as itself through installation access tokens, or as a user through user access tokens.
- OAuth app. An older integration type that asks a user to authorize it and then receives a token limited by broad scopes.
Questions that decide the choice
Ask these in order.
- Who should the action be attributed to? A person's script can act as that person. A service that runs for a team should act as its own identity, so activity does not disappear when someone leaves.
- Does it run unattended? A PAT is tied to one person and expires. An installation access token is minted on demand from the app's private key and lasts one hour, so it suits automation.
- How many accounts or organizations will use it? A PAT has one resource owner. An app can be installed by many owners, each choosing its own repositories.
- Does it need webhook events? Only an app can subscribe to events and receive them.
- Must it act as a user? Use an app's user access token. It is limited by both the app's permissions and the user's own access.
Comparison
| Need | Fine-grained PAT | GitHub App | OAuth app |
|---|---|---|---|
| Personal script or one-off task | Best fit | More setup than needed | Not needed |
| Unattended automation | Weak: tied to a person | Best fit | Weak |
| Used by many organizations | Not possible | Best fit | Possible, but with broad scopes |
| Receives webhook events | No | Yes | No |
| Permission granularity | Per permission and repository | Per permission and repository | Coarse scopes |
| Token lifetime | Up to the expiration you set | Installation token: 1 hour | Until revoked |
A decision flow
Worked examples
A script that lists your own open pull requests. Use a fine-grained PAT. Select the repositories you work in, grant read access to pull requests, and set a short expiration.
A bot that comments on every new issue in a team's repositories. Use a GitHub App. It needs the issues webhook event and write access to issues. The comments should come from the bot, not from a person. The code requests an installation token each time it needs one:
import { App } from "octokit";
import fs from "node:fs";
const app = new App({
appId: process.env.GITHUB_APP_ID,
privateKey: fs.readFileSync(process.env.GITHUB_APP_PRIVATE_KEY_PATH, "utf8"),
});
const octokit = await app.getInstallationOctokit(Number(process.env.INSTALLATION_ID));
const { data } = await octokit.request("GET /installation/repositories");
console.log(data.repositories.map((r) => r.full_name));A web tool that shows each signed-in user the repositories they can see. Use a GitHub App user access token. The result then reflects each user's own access.
Common mistakes
- Using a PAT for a shared service. When its owner leaves or loses access, the service breaks.
- Reaching for a classic token because it is quicker to set up. It grants access to every repository the user can reach.
- Building an app when a PAT would do. An app adds a private key, JWT signing, and installation management.
- Giving an app more permissions than its code uses. Request only what you need.
Exercise
A company wants a service that runs nightly across 40 repositories in its organization and posts a report as a bot account. Which credential fits, and why not a fine-grained PAT?
Show answer
A GitHub App with an installation access token. The job is unattended and shared, and the report should come from the app's identity. A PAT is tied to one person, expires, and would break or lose access if that person left the organization.
Exercise
A tool must show each user only the repositories that user can access. Which token type fits?
Show answer
A GitHub App user access token. It is limited by both the app's permissions and the signed-in user's own access.