Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
oauth.test.ts12.0 KB · 335 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
 * Tests for the OAuth 2.0 provider (Block B6).
 *
 * Covers:
 *   - pure helper functions in src/lib/oauth.ts
 *   - unauthed redirect behaviour for authed OAuth + developer-apps routes
 *   - /oauth/token and /oauth/revoke endpoint surface behaviour
 *
 * These tests intentionally avoid the DB wherever possible so they run fast
 * and don't depend on a live Postgres. Routes that touch the DB accept either
 * the expected auth/validation error or 503 (DB unreachable) since the CI
 * runner may not have a Neon connection.
 */

import { describe, it, expect } from "bun:test";
import app from "../app";
import {
  generateClientId,
  generateClientSecret,
  generateAuthCode,
  generateAccessToken,
  generateRefreshToken,
  sha256Hex,
  b64urlFromBytes,
  verifyPkce,
  timingSafeEqual,
  parseScopes,
  serializeScopes,
  parseRedirectUris,
  isValidRedirectUri,
  redirectUriAllowed,
  SUPPORTED_SCOPES,
  ACCESS_TOKEN_TTL_MS,
  REFRESH_TOKEN_TTL_MS,
  AUTH_CODE_TTL_MS,
} from "../lib/oauth";

describe("oauth helpers (B6)", () => {
  it("generateClientId returns unique values prefixed with 'glc_app_'", () => {
    const a = generateClientId();
    const b = generateClientId();
    expect(a.startsWith("glc_app_")).toBe(true);
    expect(b.startsWith("glc_app_")).toBe(true);
    expect(a).not.toBe(b);
    // The suffix is randomHex(12) → 24 hex chars; prefix is 8 chars → total 32.
    expect(a.length).toBe("glc_app_".length + 24);
  });

  it("generateClientSecret returns unique values prefixed with 'glcs_'", () => {
    const a = generateClientSecret();
    const b = generateClientSecret();
    expect(a.startsWith("glcs_")).toBe(true);
    expect(b.startsWith("glcs_")).toBe(true);
    expect(a).not.toBe(b);
    // The suffix is randomHex(32) → 64 hex chars; prefix is 5 chars → total 69.
    expect(a.length).toBe("glcs_".length + 64);
  });

  it("generateAuthCode returns unique values prefixed with 'glca_'", () => {
    const a = generateAuthCode();
    const b = generateAuthCode();
    expect(a.startsWith("glca_")).toBe(true);
    expect(b.startsWith("glca_")).toBe(true);
    expect(a).not.toBe(b);
  });

  it("generateAccessToken and generateRefreshToken produce distinct prefixes", () => {
    const at = generateAccessToken();
    const rt = generateRefreshToken();
    expect(at.startsWith("glct_")).toBe(true);
    expect(rt.startsWith("glcr_")).toBe(true);
    expect(at).not.toBe(rt);
  });

  it("sha256Hex('hello') returns the known SHA-256 hex of 'hello'", async () => {
    const h = await sha256Hex("hello");
    expect(h).toBe(
      "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
    );
  });

  it("timingSafeEqual handles equal, different, and mismatched lengths", () => {
    expect(timingSafeEqual("abc", "abc")).toBe(true);
    expect(timingSafeEqual("abc", "abd")).toBe(false);
    expect(timingSafeEqual("abc", "abcd")).toBe(false);
    expect(timingSafeEqual("", "")).toBe(true);
  });

  it("parseScopes strips unknown scopes, deduplicates, handles separators + empty", () => {
    expect(parseScopes(null)).toEqual([]);
    expect(parseScopes(undefined)).toEqual([]);
    expect(parseScopes("")).toEqual([]);
    expect(parseScopes("read:user read:user write:repo")).toEqual([
      "read:user",
      "write:repo",
    ]);
    expect(parseScopes("read:user,write:repo")).toEqual([
      "read:user",
      "write:repo",
    ]);
    expect(parseScopes("nonsense,read:user")).toEqual(["read:user"]);
    expect(parseScopes("   ")).toEqual([]);
  });

  it("serializeScopes round-trips through parseScopes", () => {
    const s = serializeScopes(["read:user", "write:repo"]);
    expect(s).toBe("read:user write:repo");
    expect(parseScopes(s)).toEqual(["read:user", "write:repo"]);
  });

  it("parseRedirectUris splits on newlines, trims, drops empty lines", () => {
    const parsed = parseRedirectUris(
      "https://a.com/cb\n  https://b.com/cb  \n\n\nhttps://c.com/cb\n"
    );
    expect(parsed).toEqual([
      "https://a.com/cb",
      "https://b.com/cb",
      "https://c.com/cb",
    ]);
    expect(parseRedirectUris("")).toEqual([]);
    expect(parseRedirectUris("   \n  \n")).toEqual([]);
  });

  it("isValidRedirectUri accepts valid https and localhost-http URIs", () => {
    expect(isValidRedirectUri("https://example.com/callback")).toBe(true);
    expect(isValidRedirectUri("http://localhost:3000/cb")).toBe(true);
    expect(isValidRedirectUri("http://127.0.0.1/cb")).toBe(true);
  });

  it("isValidRedirectUri rejects invalid URIs", () => {
    expect(isValidRedirectUri("http://example.com/cb")).toBe(false);
    expect(isValidRedirectUri("ftp://example.com/cb")).toBe(false);
    expect(isValidRedirectUri("https://example.com/cb#frag")).toBe(false);
    expect(isValidRedirectUri("https://*.example.com/cb")).toBe(false);
    expect(isValidRedirectUri("not a url")).toBe(false);
  });

  it("redirectUriAllowed requires exact match against the registered list", () => {
    expect(
      redirectUriAllowed("https://a.com/cb", [
        "https://a.com/cb",
        "https://b.com/cb",
      ])
    ).toBe(true);
    // Trailing slash matters — exact match only.
    expect(redirectUriAllowed("https://a.com/cb/", ["https://a.com/cb"])).toBe(
      false
    );
    expect(redirectUriAllowed("", ["https://a.com/cb"])).toBe(false);
    expect(redirectUriAllowed("https://a.com/cb", [])).toBe(false);
  });

  it("verifyPkce S256 accepts the RFC 7636 §B example", async () => {
    const ok = await verifyPkce({
      method: "S256",
      challenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
      verifier: "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk",
    });
    expect(ok).toBe(true);
  });

  it("verifyPkce S256 rejects a mismatched verifier", async () => {
    const ok = await verifyPkce({
      method: "S256",
      challenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
      verifier: "not-the-right-verifier",
    });
    expect(ok).toBe(false);
  });

  it("verifyPkce plain matches only when verifier === challenge", async () => {
    expect(
      await verifyPkce({ method: "plain", challenge: "abc", verifier: "abc" })
    ).toBe(true);
    expect(
      await verifyPkce({ method: "plain", challenge: "abc", verifier: "abd" })
    ).toBe(false);
  });

  it("verifyPkce returns false when challenge is missing", async () => {
    expect(
      await verifyPkce({ method: "S256", challenge: "", verifier: "x" })
    ).toBe(false);
    expect(
      await verifyPkce({ method: "S256", challenge: null, verifier: "x" })
    ).toBe(false);
    expect(
      await verifyPkce({ method: "plain", challenge: undefined, verifier: "x" })
    ).toBe(false);
  });

  it("b64urlFromBytes produces URL-safe unpadded base64", () => {
    // RFC 4648 test vector: "fooba" → "Zm9vYmE"
    const bytes = new TextEncoder().encode("fooba");
    const out = b64urlFromBytes(bytes);
    expect(out).toBe("Zm9vYmE");
    expect(out).not.toContain("=");
    expect(out).not.toContain("+");
    expect(out).not.toContain("/");
  });

  it("SUPPORTED_SCOPES includes at least the core three", () => {
    expect(SUPPORTED_SCOPES).toContain("read:user");
    expect(SUPPORTED_SCOPES).toContain("read:repo");
    expect(SUPPORTED_SCOPES).toContain("write:repo");
  });

  it("TTL constants have sensible magnitudes", () => {
    expect(ACCESS_TOKEN_TTL_MS).toBe(60 * 60 * 1000);
    expect(REFRESH_TOKEN_TTL_MS).toBe(30 * 24 * 60 * 60 * 1000);
    expect(AUTH_CODE_TTL_MS).toBe(10 * 60 * 1000);
    expect(ACCESS_TOKEN_TTL_MS).toBeLessThan(REFRESH_TOKEN_TTL_MS);
    expect(AUTH_CODE_TTL_MS).toBeLessThan(ACCESS_TOKEN_TTL_MS);
  });
});

describe("oauth routes (B6) — unauthed redirects", () => {
  it("GET /oauth/authorize without session redirects to /login", async () => {
    const res = await app.request("/oauth/authorize");
    expect([301, 302, 303, 307]).toContain(res.status);
    const loc = res.headers.get("location") || "";
    expect(loc.startsWith("/login")).toBe(true);
  });

  it("POST /oauth/authorize/decision without session redirects to /login", async () => {
    const res = await app.request("/oauth/authorize/decision", {
      method: "POST",
      headers: { "content-type": "application/x-www-form-urlencoded" },
      body: "decision=approve",
    });
    expect([301, 302, 303, 307]).toContain(res.status);
    const loc = res.headers.get("location") || "";
    expect(loc.startsWith("/login")).toBe(true);
  });

  it("GET /settings/authorizations without session redirects to /login", async () => {
    const res = await app.request("/settings/authorizations");
    expect([301, 302, 303, 307]).toContain(res.status);
    const loc = res.headers.get("location") || "";
    expect(loc.startsWith("/login")).toBe(true);
  });

  it("POST /settings/authorizations/:id/revoke without session redirects to /login", async () => {
    const res = await app.request("/settings/authorizations/some-id/revoke", {
      method: "POST",
      headers: { "content-type": "application/x-www-form-urlencoded" },
      body: "",
    });
    expect([301, 302, 303, 307]).toContain(res.status);
    const loc = res.headers.get("location") || "";
    expect(loc.startsWith("/login")).toBe(true);
  });

  it("GET /settings/applications without session redirects to /login", async () => {
    const res = await app.request("/settings/applications");
    expect([301, 302, 303, 307]).toContain(res.status);
    const loc = res.headers.get("location") || "";
    expect(loc.startsWith("/login")).toBe(true);
  });

  it("POST /settings/applications/new without session redirects to /login", async () => {
    const res = await app.request("/settings/applications/new", {
      method: "POST",
      headers: { "content-type": "application/x-www-form-urlencoded" },
      body: "name=Test",
    });
    expect([301, 302, 303, 307]).toContain(res.status);
    const loc = res.headers.get("location") || "";
    expect(loc.startsWith("/login")).toBe(true);
  });
});

describe("oauth /token endpoint (B6)", () => {
  it("returns 401 invalid_client when client_id is missing", async () => {
    const res = await app.request("/oauth/token", {
      method: "POST",
      headers: { "content-type": "application/x-www-form-urlencoded" },
      body: "grant_type=authorization_code",
    });
    expect(res.status).toBe(401);
    const body = await res.json();
    expect(body.error).toBe("invalid_client");
  });

  it("returns 401 or 503 when client_id is unknown", async () => {
    const res = await app.request("/oauth/token", {
      method: "POST",
      headers: { "content-type": "application/x-www-form-urlencoded" },
      body:
        "grant_type=authorization_code&client_id=glc_app_nonexistent000000000000",
    });
    // 401 if DB confirms unknown client, 503 if DB unreachable.
    expect([401, 503]).toContain(res.status);
    if (res.status === 401) {
      const body = await res.json();
      expect(body.error).toBe("invalid_client");
    }
  });

  it("returns 400 on malformed JSON body", async () => {
    const res = await app.request("/oauth/token", {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: "{this is not json",
    });
    expect(res.status).toBe(400);
    const body = await res.json();
    expect(body.error).toBeTruthy();
  });

  it("returns 401 or 503 for a bogus client_id + unsupported grant_type", async () => {
    const res = await app.request("/oauth/token", {
      method: "POST",
      headers: { "content-type": "application/x-www-form-urlencoded" },
      body:
        "grant_type=password&client_id=glc_app_000000000000000000000000",
    });
    // Client lookup runs before grant_type validation. Either the DB says
    // unknown client (401) or the DB is unreachable (503).
    expect([401, 503]).toContain(res.status);
  });
});

describe("oauth /revoke endpoint (B6)", () => {
  it("returns 401 when no client credentials are supplied", async () => {
    const res = await app.request("/oauth/revoke", {
      method: "POST",
      headers: { "content-type": "application/x-www-form-urlencoded" },
      body: "token=glct_something",
    });
    expect(res.status).toBe(401);
    const body = await res.json();
    expect(body.error).toBe("invalid_client");
  });
});