-
Notifications
You must be signed in to change notification settings - Fork 0
feat(auth): include linked GitHub account id in OAuth id_token #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mroderick
wants to merge
1
commit into
main
Choose a base branch
from
feature/github-id-id-token
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /** | ||
| * Resolve the linked GitHub account id for a user. | ||
| * | ||
| * The planner uses this stable id to match returning members whose GitHub | ||
| * email differs from their stored planner email. | ||
| * | ||
| * Failures are allowed to propagate: a transient database error here would | ||
| * otherwise silently omit `github_id` and cause the planner to fall back to | ||
| * email matching, re-creating the duplicate-member bug this claim is meant | ||
| * to prevent. | ||
| */ | ||
| export async function getGithubAccountId(db, userId) { | ||
| const result = await db.query( | ||
| 'SELECT "accountId" FROM "account" WHERE "userId" = $1 AND "providerId" = $2 LIMIT 1', | ||
| [userId, "github"], | ||
| ); | ||
|
|
||
| return result.rows[0]?.accountId ?? null; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { test } from "tap"; | ||
| import { randomUUID } from "node:crypto"; | ||
| import { getTestInstance } from "../helpers/test-instance.js"; | ||
| import { createApp } from "../../src/app/app.js"; | ||
| import { | ||
| authorizeParams, | ||
| authorizeAndGetCode, | ||
| tokenBody, | ||
| } from "../helpers/oauth-flow.js"; | ||
|
|
||
| function decodeJwtPayload(idToken) { | ||
| return JSON.parse(Buffer.from(idToken.split(".")[1], "base64url").toString()); | ||
| } | ||
|
|
||
| test("id_token includes github_id for users with a linked GitHub account", async (t) => { | ||
| const testInstance = await getTestInstance(t); | ||
| const app = createApp(testInstance.auth, testInstance.db); | ||
| const { getAuthHeaders } = testInstance; | ||
| const email = "github-linked@example.com"; | ||
|
|
||
| const authHeaders = await getAuthHeaders(email); | ||
|
|
||
| const userResult = await testInstance.db.query( | ||
| 'SELECT id FROM "user" WHERE email = $1', | ||
| [email], | ||
| ); | ||
| const userId = userResult.rows[0]?.id; | ||
| t.ok(userId, "user record found"); | ||
|
|
||
| await testInstance.db.query( | ||
| 'INSERT INTO "account" ("id", "accountId", "providerId", "userId", "createdAt", "updatedAt") VALUES ($1, $2, $3, $4, NOW(), NOW())', | ||
| [randomUUID(), "987654321", "github", userId], | ||
| ); | ||
|
|
||
| const params = authorizeParams({ state: "jwt-github-state" }); | ||
| const { code } = await authorizeAndGetCode(app, authHeaders, params); | ||
|
|
||
| const tokenRes = await app.request("/api/auth/oauth2/token", { | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/x-www-form-urlencoded" }, | ||
| body: tokenBody(code), | ||
| }); | ||
|
|
||
| t.equal(tokenRes.status, 200, "token endpoint returns 200"); | ||
| const body = await tokenRes.json(); | ||
| t.ok(body.id_token, "response contains id_token"); | ||
|
|
||
| const payload = decodeJwtPayload(body.id_token); | ||
| t.equal(payload.github_id, "987654321", "payload includes linked github_id"); | ||
| }); | ||
|
|
||
| test("id_token omits github_id for users without a linked GitHub account", async (t) => { | ||
| const testInstance = await getTestInstance(t); | ||
| const app = createApp(testInstance.auth, testInstance.db); | ||
| const { getAuthHeaders } = testInstance; | ||
| const email = "magic-link-only@example.com"; | ||
|
|
||
| const authHeaders = await getAuthHeaders(email); | ||
|
|
||
| const params = authorizeParams({ state: "jwt-no-github-state" }); | ||
| const { code } = await authorizeAndGetCode(app, authHeaders, params); | ||
|
|
||
| const tokenRes = await app.request("/api/auth/oauth2/token", { | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/x-www-form-urlencoded" }, | ||
| body: tokenBody(code), | ||
| }); | ||
|
|
||
| t.equal(tokenRes.status, 200, "token endpoint returns 200"); | ||
| const body = await tokenRes.json(); | ||
| t.ok(body.id_token, "response contains id_token"); | ||
|
|
||
| const payload = decodeJwtPayload(body.id_token); | ||
| t.notOk( | ||
| Object.prototype.hasOwnProperty.call(payload, "github_id"), | ||
| "payload does not include github_id", | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { test } from "tap"; | ||
| import { getGithubAccountId } from "../../src/auth/id-token-claims.js"; | ||
|
|
||
| function makeDb(rows) { | ||
| return { | ||
| query: async () => { | ||
| return { rows }; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| test("returns the linked GitHub account id", async (t) => { | ||
| const db = makeDb([{ accountId: "12345" }]); | ||
| const id = await getGithubAccountId(db, "user-1"); | ||
| t.equal(id, "12345"); | ||
| }); | ||
|
|
||
| test("returns null when the user has no linked GitHub account", async (t) => { | ||
| const db = makeDb([]); | ||
| const id = await getGithubAccountId(db, "user-1"); | ||
| t.equal(id, null); | ||
| }); | ||
|
|
||
| test("propagates database errors instead of swallowing them", async (t) => { | ||
| const db = { | ||
| query: async () => { | ||
| throw new Error("connection lost"); | ||
| }, | ||
| }; | ||
|
|
||
| await t.rejects( | ||
| () => getGithubAccountId(db, "user-1"), | ||
| { message: "connection lost" }, | ||
| "database error is propagated", | ||
| ); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would a fixed ‘github’ value inside the query be… clearer? As opposed to being a second arg?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, since this method is only for github