|
| 1 | +import { test } from "tap"; |
| 2 | +import { randomUUID } from "node:crypto"; |
| 3 | +import { getTestInstance } from "../helpers/test-instance.js"; |
| 4 | +import { createApp } from "../../src/app/app.js"; |
| 5 | +import { |
| 6 | + authorizeParams, |
| 7 | + authorizeAndGetCode, |
| 8 | + tokenBody, |
| 9 | +} from "../helpers/oauth-flow.js"; |
| 10 | + |
| 11 | +function decodeJwtPayload(idToken) { |
| 12 | + return JSON.parse(Buffer.from(idToken.split(".")[1], "base64url").toString()); |
| 13 | +} |
| 14 | + |
| 15 | +test("id_token includes github_id for users with a linked GitHub account", async (t) => { |
| 16 | + const testInstance = await getTestInstance(t); |
| 17 | + const app = createApp(testInstance.auth, testInstance.db); |
| 18 | + const { getAuthHeaders } = testInstance; |
| 19 | + const email = "github-linked@example.com"; |
| 20 | + |
| 21 | + const authHeaders = await getAuthHeaders(email); |
| 22 | + |
| 23 | + const userResult = await testInstance.db.query( |
| 24 | + 'SELECT id FROM "user" WHERE email = $1', |
| 25 | + [email], |
| 26 | + ); |
| 27 | + const userId = userResult.rows[0]?.id; |
| 28 | + t.ok(userId, "user record found"); |
| 29 | + |
| 30 | + await testInstance.db.query( |
| 31 | + 'INSERT INTO "account" ("id", "accountId", "providerId", "userId", "createdAt", "updatedAt") VALUES ($1, $2, $3, $4, NOW(), NOW())', |
| 32 | + [randomUUID(), "987654321", "github", userId], |
| 33 | + ); |
| 34 | + |
| 35 | + const params = authorizeParams({ state: "jwt-github-state" }); |
| 36 | + const { code } = await authorizeAndGetCode(app, authHeaders, params); |
| 37 | + |
| 38 | + const tokenRes = await app.request("/api/auth/oauth2/token", { |
| 39 | + method: "POST", |
| 40 | + headers: { "Content-Type": "application/x-www-form-urlencoded" }, |
| 41 | + body: tokenBody(code), |
| 42 | + }); |
| 43 | + |
| 44 | + t.equal(tokenRes.status, 200, "token endpoint returns 200"); |
| 45 | + const body = await tokenRes.json(); |
| 46 | + t.ok(body.id_token, "response contains id_token"); |
| 47 | + |
| 48 | + const payload = decodeJwtPayload(body.id_token); |
| 49 | + t.equal(payload.github_id, "987654321", "payload includes linked github_id"); |
| 50 | +}); |
| 51 | + |
| 52 | +test("id_token omits github_id for users without a linked GitHub account", async (t) => { |
| 53 | + const testInstance = await getTestInstance(t); |
| 54 | + const app = createApp(testInstance.auth, testInstance.db); |
| 55 | + const { getAuthHeaders } = testInstance; |
| 56 | + const email = "magic-link-only@example.com"; |
| 57 | + |
| 58 | + const authHeaders = await getAuthHeaders(email); |
| 59 | + |
| 60 | + const params = authorizeParams({ state: "jwt-no-github-state" }); |
| 61 | + const { code } = await authorizeAndGetCode(app, authHeaders, params); |
| 62 | + |
| 63 | + const tokenRes = await app.request("/api/auth/oauth2/token", { |
| 64 | + method: "POST", |
| 65 | + headers: { "Content-Type": "application/x-www-form-urlencoded" }, |
| 66 | + body: tokenBody(code), |
| 67 | + }); |
| 68 | + |
| 69 | + t.equal(tokenRes.status, 200, "token endpoint returns 200"); |
| 70 | + const body = await tokenRes.json(); |
| 71 | + t.ok(body.id_token, "response contains id_token"); |
| 72 | + |
| 73 | + const payload = decodeJwtPayload(body.id_token); |
| 74 | + t.notOk( |
| 75 | + Object.prototype.hasOwnProperty.call(payload, "github_id"), |
| 76 | + "payload does not include github_id", |
| 77 | + ); |
| 78 | +}); |
0 commit comments