Sub topic 10
Authenticating as the app with a JWT
This chapter assumes you know what a bearer token is, can call a REST API with curl, and have registered a GitHub App. You also need the App ID and a private key (a .pem file) from the app's settings page.
Two identities, one app
A GitHub App can authenticate in two ways:
- As the app itself. It proves it owns the private key by sending a signed JWT. This identity can only call a small set of app-level endpoints, such as listing installations.
- As an installation. It exchanges the JWT for an installation access token, which can act on the repositories the app was installed on. The next chapter covers this step.
The JWT is the first step in the second path. It is also the only credential that uses your private key directly.
What a JWT is
A JSON Web Token (JWT) is a signed string made of three parts: a header, a payload of claims, and a signature. GitHub verifies the signature with the public half of your key pair. Because only you hold the private key, a valid signature proves the request comes from your app.
GitHub requires these claims in the payload:
| Claim | Meaning | Value |
|---|---|---|
| `iss` | Issuer, the app's identity | Your App ID (or Client ID) |
| `iat` | Issued at, in Unix seconds | Now minus 60 seconds |
| `exp` | Expiration, in Unix seconds | At most 10 minutes after now |
The token must be signed with the RS256 algorithm. Backdating iat by 60 seconds protects you from small clock differences between your machine and GitHub. If your clock runs ahead, GitHub would otherwise reject the token as issued in the future.
Generating the JWT
This script reads the private key from a file path in an environment variable and prints a token valid for 10 minutes. Install the library first with npm install jsonwebtoken, and set "type": "module" in your package.json so the import syntax works.
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);Keep the key in a file or secret store and pass only its path through the environment. Never paste the key into source code.
Using the JWT
Send the JWT as a bearer token. A simple test is GET /app, which returns details about your app.
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $APP_JWT" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/appIf the response shows your app's name and ID, the key and claims are correct. The same token works for GET /app/installations, which lists where the app is installed.
Common errors
- 401 "Issued at claim ('iat') must be an Integer". The value was a string or a millisecond timestamp. Use whole seconds.
- 401 "'Expiration time' claim ('exp') is too far in the future".
expis more than 10 minutes ahead ofnow. Keep it at or belownow + 600. - 401 "A JSON web token could not be decoded". The key, algorithm, or token format is wrong. Confirm you used RS256 and the full PEM contents, including the header and footer lines.
- Using a JWT for repository calls. A JWT cannot read repository contents. Those calls need an installation access token.
Generate a fresh JWT whenever you need one rather than caching it. It is cheap to sign, and short lifetimes limit the damage if one is exposed.
Exercise
A script signs a JWT with exp set to now + 3600 and gets a 401 error when calling GET /app. What is wrong, and what should exp be?
Show answer
GitHub allows a JWT lifetime of at most 10 minutes, and one hour is too long. Set exp to now + 600 or less. Keep iat at now - 60 to allow for clock drift.
Exercise
Why can you call GET /app/installations with a JWT but not GET /repos/OWNER/REPO/contents/README.md?
Show answer
The JWT authenticates you as the app, which is enough for app-level endpoints. Repository content belongs to an installation, so you must exchange the JWT for an installation access token first.