Sub topic 3
Authenticating as a GitHub App with a JWT
Assume you know basic HTTP, REST APIs, environment variables, and bearer tokens. This chapter focuses on how a GitHub App proves its identity and obtains credentials for API requests.
Authenticating as the app with a JWT
A JSON Web Token (JWT) is a signed, short-lived credential. A GitHub App uses a JWT to authenticate as the app itself, not as a user or an installation.
The JWT contains:
iss: the GitHub App's IDiat: the issue time, usually backdated slightly for clock differencesexp: the expiration time, no more than 10 minutes after issue
The app signs these claims with its private key using the RS256 algorithm.
import fs from "node:fs";
import jwt from "jsonwebtoken";
const privateKey = fs.readFileSync(
process.env.GITHUB_APP_PRIVATE_KEY_PATH,
"utf8"
);
const now = Math.floor(Date.now() / 1000);
const appJwt = jwt.sign(
{
iat: now - 60,
exp: now + 600,
iss: process.env.GITHUB_APP_ID
},
privateKey,
{ algorithm: "RS256" }
);
console.log(appJwt);Creating and using installation access tokens
An installation is a GitHub App installed on an account with access to selected repositories. The app exchanges its JWT for an installation access token:
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"The returned token is short-lived and limited by the installation's repositories and app permissions. Use it as a bearer token in later API requests.
flowchart TD
A[App private key] --> B[Signed app JWT]
B --> C[Installation access token]
C --> D[GitHub API request]User access tokens for acting on behalf of a user
A user access token represents a person who authorized the app. Use it when the operation must reflect that user's identity, permissions, or consent. An installation token represents the app's installation instead. The two credentials can expose different repositories and API behavior.
> callout note > Never treat an app JWT as a general API token. It authenticates only the app and is mainly used to obtain installation-level credentials.
Choosing the right credential
| Credential | Identity | Best fit |
|---|---|---|
| Fine-grained token | A user | Personal scripts with selected repositories |
| Installation token | A GitHub App installation | Automation serving repositories or organizations |
| User access token | A user through an app | Actions requiring user authorization |
Storing, rotating, and revoking credentials safely
Store private keys and tokens in a secret manager or protected environment variables. Do not commit them or print them in logs. Give each credential the smallest required access. Rotate keys and tokens on a schedule and immediately after suspected exposure. Revoke unused credentials and remove app installations that are no longer needed.
Exercise
Which credential should a repository automation service normally use, and why?
Show answer
It should normally use an installation access token because the token represents the app installation and is limited by its repositories and granted permissions.