CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 | /**
* Developer Apps UI (Block B6).
*
* Lets authenticated users register + manage their OAuth 2.0 apps:
* GET /settings/applications list + new button
* GET /settings/applications/new form
* POST /settings/applications/new create (returns client_secret once)
* GET /settings/applications/:id edit / rotate secret / delete
* POST /settings/applications/:id update
* POST /settings/applications/:id/rotate generate a new client secret
* POST /settings/applications/:id/delete remove app + all tokens
*
* All writes audit()' the action. Read-only responses are HTML (SSR JSX).
*/
import { Hono } from "hono";
import { and, eq } from "drizzle-orm";
import { db } from "../db";
import { oauthApps } from "../db/schema";
import { Layout } from "../views/layout";
import { requireAuth } from "../middleware/auth";
import type { AuthEnv } from "../middleware/auth";
import {
generateClientId,
generateClientSecret,
sha256Hex,
isValidRedirectUri,
parseRedirectUris,
} from "../lib/oauth";
import { audit } from "../lib/notify";
const apps = new Hono<AuthEnv>();
apps.use("/settings/applications", requireAuth);
apps.use("/settings/applications/*", requireAuth);
function normaliseRedirectUris(raw: string): {
ok: boolean;
value?: string;
error?: string;
} {
const lines = raw
.split(/\r?\n/)
.map((s) => s.trim())
.filter(Boolean);
if (lines.length === 0) {
return { ok: false, error: "At least one redirect URI is required" };
}
if (lines.length > 10) {
return { ok: false, error: "At most 10 redirect URIs allowed" };
}
for (const u of lines) {
if (!isValidRedirectUri(u)) {
return { ok: false, error: `Invalid redirect URI: ${u}` };
}
}
return { ok: true, value: lines.join("\n") };
}
apps.get("/settings/applications", async (c) => {
const user = c.get("user")!;
const error = c.req.query("error");
const success = c.req.query("success");
let rows: (typeof oauthApps.$inferSelect)[] = [];
try {
rows = await db
.select()
.from(oauthApps)
.where(eq(oauthApps.ownerId, user.id));
} catch (err) {
console.error("[oauth-apps] list:", err);
}
return c.html(
<Layout title="OAuth applications" user={user}>
<div class="settings-container">
<div class="breadcrumb">
<a href="/settings">settings</a>
<span>/</span>
<span>applications</span>
</div>
<h2>OAuth applications</h2>
{error && <div class="auth-error">{decodeURIComponent(error)}</div>}
{success && (
<div class="auth-success">{decodeURIComponent(success)}</div>
)}
<p style="color: var(--text-muted); font-size: 13px">
Register third-party apps that can request access to gluecron on
behalf of users via the OAuth 2.0 authorization code flow.
</p>
<div style="margin: 16px 0">
<a href="/settings/applications/new" class="btn btn-primary">
New OAuth app
</a>
</div>
<div
style="border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden"
>
{rows.length === 0 ? (
<div
style="padding: 16px; color: var(--text-muted); font-size: 13px; background: var(--bg-secondary)"
>
No OAuth apps registered yet.
</div>
) : (
rows.map((app) => (
<div
style="padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--bg-secondary)"
>
<div style="display: flex; justify-content: space-between; align-items: center">
<div>
<strong>
<a href={`/settings/applications/${app.id}`}>{app.name}</a>
</strong>
{app.revokedAt && (
<span style="color: var(--red); font-size: 12px; margin-left: 8px">
revoked
</span>
)}
<div
style="color: var(--text-muted); font-size: 12px; margin-top: 2px"
>
<code>{app.clientId}</code>
{" · "}added {new Date(app.createdAt).toLocaleDateString()}
</div>
</div>
<a
href={`/settings/applications/${app.id}`}
class="btn btn-sm"
>
manage
</a>
</div>
</div>
))
)}
</div>
</div>
</Layout>
);
});
apps.get("/settings/applications/new", async (c) => {
const user = c.get("user")!;
const error = c.req.query("error");
return c.html(
<Layout title="New OAuth app" user={user}>
<div class="settings-container">
<div class="breadcrumb">
<a href="/settings/applications">applications</a>
<span>/</span>
<span>new</span>
</div>
<h2>Register a new OAuth app</h2>
{error && <div class="auth-error">{decodeURIComponent(error)}</div>}
<form method="POST" action="/settings/applications/new">
<div class="form-group">
<label for="name">Application name</label>
<input
type="text"
id="name"
name="name"
required
maxLength={80}
placeholder="My Awesome Integration"
/>
</div>
<div class="form-group">
<label for="homepage_url">Homepage URL</label>
<input
type="url"
id="homepage_url"
name="homepage_url"
placeholder="https://example.com"
/>
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea
id="description"
name="description"
rows={3}
maxLength={500}
/>
</div>
<div class="form-group">
<label for="redirect_uris">Authorization callback URLs</label>
<textarea
id="redirect_uris"
name="redirect_uris"
rows={4}
required
placeholder="https://example.com/oauth/callback"
/>
<small style="color: var(--text-muted)">
One URL per line. HTTPS required (HTTP allowed for localhost).
Exact match; no wildcards.
</small>
</div>
<div class="form-group">
<label>
<input
type="checkbox"
name="confidential"
value="on"
checked
/>
{" "}Confidential client (server-side app)
</label>
<br />
<small style="color: var(--text-muted)">
Uncheck for public SPA / mobile apps — they must use PKCE
instead of a client secret.
</small>
</div>
<button type="submit" class="btn btn-primary">
Register app
</button>
<a
href="/settings/applications"
class="btn"
style="margin-left: 8px"
>
Cancel
</a>
</form>
</div>
</Layout>
);
});
apps.post("/settings/applications/new", async (c) => {
const user = c.get("user")!;
const body = await c.req.parseBody();
const name = String(body.name || "").trim().slice(0, 80);
const homepageUrl = String(body.homepage_url || "").trim().slice(0, 200);
const description = String(body.description || "").trim().slice(0, 500);
const confidential = String(body.confidential || "") === "on";
const redirectRaw = String(body.redirect_uris || "");
if (!name) {
return c.redirect("/settings/applications/new?error=Name+is+required");
}
const parsed = normaliseRedirectUris(redirectRaw);
if (!parsed.ok) {
return c.redirect(
`/settings/applications/new?error=${encodeURIComponent(parsed.error || "Invalid redirect URIs")}`
);
}
const clientId = generateClientId();
const clientSecret = generateClientSecret();
const clientSecretHash = await sha256Hex(clientSecret);
try {
const [row] = await db
.insert(oauthApps)
.values({
ownerId: user.id,
name,
clientId,
clientSecretHash,
clientSecretPrefix: clientSecret.slice(0, 8),
redirectUris: parsed.value!,
homepageUrl: homepageUrl || null,
description: description || null,
confidential,
})
.returning();
await audit({
userId: user.id,
action: "oauth_app.create",
targetType: "oauth_app",
targetId: row.id,
metadata: { clientId },
});
// Redirect to the manage page with the plaintext secret appended once.
return c.redirect(
`/settings/applications/${row.id}?secret=${encodeURIComponent(clientSecret)}&success=App+created`
);
} catch (err) {
console.error("[oauth-apps] create:", err);
return c.redirect(
"/settings/applications/new?error=Service+unavailable"
);
}
});
apps.get("/settings/applications/:id", async (c) => {
const user = c.get("user")!;
const id = c.req.param("id");
const error = c.req.query("error");
const success = c.req.query("success");
const secret = c.req.query("secret");
let app: typeof oauthApps.$inferSelect | undefined;
try {
const [row] = await db
.select()
.from(oauthApps)
.where(and(eq(oauthApps.id, id), eq(oauthApps.ownerId, user.id)))
.limit(1);
app = row;
} catch (err) {
console.error("[oauth-apps] get:", err);
}
if (!app) {
return c.redirect("/settings/applications?error=Not+found");
}
return c.html(
<Layout title={app.name} user={user}>
<div class="settings-container">
<div class="breadcrumb">
<a href="/settings/applications">applications</a>
<span>/</span>
<span>{app.name}</span>
</div>
<h2>{app.name}</h2>
{error && <div class="auth-error">{decodeURIComponent(error)}</div>}
{success && (
<div class="auth-success">{decodeURIComponent(success)}</div>
)}
{secret && (
<div
style="padding: 12px; border: 1px solid var(--yellow); background: rgba(255,193,7,0.1); border-radius: var(--radius); margin-bottom: 16px"
>
<strong>Save this client secret — it will not be shown again:</strong>
<pre
style="margin-top: 8px; padding: 8px; background: var(--bg); border-radius: 4px; overflow-x: auto; user-select: all"
>
{secret}
</pre>
</div>
)}
<dl style="display: grid; grid-template-columns: 200px 1fr; gap: 8px 16px; margin-bottom: 16px">
<dt style="color: var(--text-muted)">Client ID</dt>
<dd>
<code style="user-select: all">{app.clientId}</code>
</dd>
<dt style="color: var(--text-muted)">Client secret prefix</dt>
<dd>
<code>{app.clientSecretPrefix}…</code>
</dd>
<dt style="color: var(--text-muted)">Type</dt>
<dd>{app.confidential ? "Confidential" : "Public (PKCE)"}</dd>
<dt style="color: var(--text-muted)">Created</dt>
<dd>{new Date(app.createdAt).toLocaleString()}</dd>
</dl>
<form method="POST" action={`/settings/applications/${app.id}`}>
<div class="form-group">
<label for="name">Application name</label>
<input
type="text"
id="name"
name="name"
required
maxLength={80}
defaultValue={app.name}
/>
</div>
<div class="form-group">
<label for="homepage_url">Homepage URL</label>
<input
type="url"
id="homepage_url"
name="homepage_url"
defaultValue={app.homepageUrl || ""}
/>
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea
id="description"
name="description"
rows={3}
maxLength={500}
>
{app.description || ""}
</textarea>
</div>
<div class="form-group">
<label for="redirect_uris">Authorization callback URLs</label>
<textarea
id="redirect_uris"
name="redirect_uris"
rows={4}
required
>
{app.redirectUris}
</textarea>
</div>
<button type="submit" class="btn btn-primary">
Save changes
</button>
</form>
<hr style="margin: 24px 0; border-color: var(--border)" />
<h3>Rotate client secret</h3>
<p style="color: var(--text-muted); font-size: 13px">
Generate a new secret. The old one is invalidated immediately —
existing access tokens keep working, but token exchange with the
old secret will fail.
</p>
<form
method="POST"
action={`/settings/applications/${app.id}/rotate`}
onsubmit="return confirm('Rotate the client secret? The old one will stop working immediately.')"
>
<button type="submit" class="btn">
Rotate secret
</button>
</form>
<hr style="margin: 24px 0; border-color: var(--border)" />
<h3 style="color: var(--red)">Danger zone</h3>
<form
method="POST"
action={`/settings/applications/${app.id}/delete`}
onsubmit="return confirm('Delete this OAuth app? All issued access tokens will be revoked.')"
>
<button type="submit" class="btn btn-danger">
Delete app
</button>
</form>
</div>
</Layout>
);
});
apps.post("/settings/applications/:id", async (c) => {
const user = c.get("user")!;
const id = c.req.param("id");
const body = await c.req.parseBody();
const name = String(body.name || "").trim().slice(0, 80);
const homepageUrl = String(body.homepage_url || "").trim().slice(0, 200);
const description = String(body.description || "").trim().slice(0, 500);
const redirectRaw = String(body.redirect_uris || "");
if (!name) {
return c.redirect(
`/settings/applications/${id}?error=Name+is+required`
);
}
const parsed = normaliseRedirectUris(redirectRaw);
if (!parsed.ok) {
return c.redirect(
`/settings/applications/${id}?error=${encodeURIComponent(parsed.error || "Invalid redirect URIs")}`
);
}
try {
const [existing] = await db
.select({ id: oauthApps.id, ownerId: oauthApps.ownerId })
.from(oauthApps)
.where(eq(oauthApps.id, id))
.limit(1);
if (!existing || existing.ownerId !== user.id) {
return c.redirect("/settings/applications?error=Not+found");
}
await db
.update(oauthApps)
.set({
name,
homepageUrl: homepageUrl || null,
description: description || null,
redirectUris: parsed.value!,
updatedAt: new Date(),
})
.where(eq(oauthApps.id, id));
await audit({
userId: user.id,
action: "oauth_app.update",
targetType: "oauth_app",
targetId: id,
});
return c.redirect(`/settings/applications/${id}?success=Saved`);
} catch (err) {
console.error("[oauth-apps] update:", err);
return c.redirect(
`/settings/applications/${id}?error=Service+unavailable`
);
}
});
apps.post("/settings/applications/:id/rotate", async (c) => {
const user = c.get("user")!;
const id = c.req.param("id");
try {
const [existing] = await db
.select({ id: oauthApps.id, ownerId: oauthApps.ownerId })
.from(oauthApps)
.where(eq(oauthApps.id, id))
.limit(1);
if (!existing || existing.ownerId !== user.id) {
return c.redirect("/settings/applications?error=Not+found");
}
const newSecret = generateClientSecret();
const newHash = await sha256Hex(newSecret);
await db
.update(oauthApps)
.set({
clientSecretHash: newHash,
clientSecretPrefix: newSecret.slice(0, 8),
updatedAt: new Date(),
})
.where(eq(oauthApps.id, id));
await audit({
userId: user.id,
action: "oauth_app.rotate_secret",
targetType: "oauth_app",
targetId: id,
});
return c.redirect(
`/settings/applications/${id}?secret=${encodeURIComponent(newSecret)}&success=Secret+rotated`
);
} catch (err) {
console.error("[oauth-apps] rotate:", err);
return c.redirect(
`/settings/applications/${id}?error=Service+unavailable`
);
}
});
apps.post("/settings/applications/:id/delete", async (c) => {
const user = c.get("user")!;
const id = c.req.param("id");
try {
const [existing] = await db
.select({ id: oauthApps.id, ownerId: oauthApps.ownerId })
.from(oauthApps)
.where(eq(oauthApps.id, id))
.limit(1);
if (!existing || existing.ownerId !== user.id) {
return c.redirect("/settings/applications?error=Not+found");
}
await db.delete(oauthApps).where(eq(oauthApps.id, id));
await audit({
userId: user.id,
action: "oauth_app.delete",
targetType: "oauth_app",
targetId: id,
});
return c.redirect("/settings/applications?success=App+deleted");
} catch (err) {
console.error("[oauth-apps] delete:", err);
return c.redirect(
`/settings/applications/${id}?error=Service+unavailable`
);
}
});
export default apps;
|