Sub topic 12
User Access Tokens for Acting on Behalf of a User
This chapter assumes you know how a GitHub App authenticates with a JWT and how it exchanges that JWT for an installation access token. It also assumes you know what an app installation is and what permissions an app declares.
Two identities, two tokens
An installation access token makes the app act as itself. Commits, comments, and issues it creates show the app as the author, usually with a [bot] suffix. That fits automation that runs without a person present.
Some work needs to be done as a person. Examples are showing a user their own private repositories, opening a pull request in their name, or deciding what a signed-in user may see. For this, the app uses a user access token. GitHub issues it after the user authorizes the app. Requests made with it are attributed to that user and to the app together.
Limits on a user access token
A user access token cannot do more than the user can do. It also cannot do more than the app is allowed to do. The effective access is the intersection of three things:
- What the user can access on GitHub.
- The permissions the app has declared.
- The installation's repository access. The app must be installed on the repositories involved.
If the user loses access to a repository, the token loses it too. If the app does not declare a permission, no user can grant it. This is why a user access token is safer than a classic token with broad scopes. Its reach is bounded on both sides.
Do not request user authorization for work that does not need a user. A background job that labels issues should use an installation token.
The web application flow
The web application flow is the standard way to obtain a user access token.
The sequence below shows the steps.
In words:
- Your app redirects the user to GitHub's authorization page, including a random
statevalue that you store. - The user approves. GitHub redirects back to your callback URL with a temporary
codeand yourstate. - Your server checks that
statematches, then exchanges thecodefor a token using the app's client ID and client secret. - Your server calls the API with the token.
The following Node.js function performs step 3 and then asks GitHub who the user is.
async function exchangeCode(code) {
const res = await fetch("https://github.com/login/oauth/access_token", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
client_id: process.env.GITHUB_APP_CLIENT_ID,
client_secret: process.env.GITHUB_APP_CLIENT_SECRET,
code,
}),
});
const body = await res.json();
if (body.error) throw new Error(body.error_description);
const me = await fetch("https://api.github.com/user", {
headers: {
Accept: "application/vnd.github+json",
Authorization: `Bearer ${body.access_token}`,
"X-GitHub-Api-Version": "2022-11-28",
},
});
return me.json();
}The code is single use and expires quickly. Run the exchange on your server only, because the client secret must never reach a browser.
Expiration and refresh
You can configure a GitHub App so that user access tokens expire. Expiring tokens are the recommended setting. The response then includes an expires_in value and a refresh_token. Refresh tokens last longer, and each refresh returns a new pair. Store the new refresh token, because the old one stops working.
Common errors
bad_verification_code: the code was already used, expired, or does not belong to this client ID.- 404 on a repository the user can see: the app is not installed on that repository, or it lacks the needed permission.
- 403 with a valid token: the user may lack access, or the organization may restrict the app.
- Token works for one user, not another: access follows each user's own rights.
Choosing between the two tokens
| Need | Use |
|---|---|
| Scheduled or event-driven automation | Installation access token |
| Action attributed to a specific person | User access token |
| Showing only what the signed-in user may see | User access token |
Exercise
A GitHub App lets a signed-in user create an issue in a private repository. The user has write access, but the app is not installed on that repository. Will the request succeed with the user access token, and why?
Show answer
No. The effective access is the intersection of the user's rights, the app's declared permissions, and the installation's repositories. The user can write to the repository, but the app is not installed there, so the request fails (typically with a 404 or 403).