@tribe-nest/forge 1.15.0 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tribe-nest/forge",
3
- "version": "1.15.0",
3
+ "version": "1.16.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -0,0 +1,107 @@
1
+ // Authenticate the CALLER of one of your app's OWN server routes (TanStack
2
+ // server handlers, e.g. a "run this job now" or "save settings" endpoint).
3
+ // SERVER-ONLY.
4
+ //
5
+ // Why this exists: `useAppAuth` / `useAppAdminGuard` are React hooks — they run
6
+ // in the browser and CANNOT gate a server route. Without a server-side check, a
7
+ // privileged route is either wide open or bolted shut behind a shared secret.
8
+ // Neither is right. Instead: the client already holds the caller's token (the
9
+ // app-user token, or the admin's TribeNest account token); forward it to your
10
+ // server route in the `Authorization` header, and verify it HERE. The app Worker
11
+ // is only a CLIENT — TribeNest is the sole authority on identity + access, so
12
+ // these helpers forward the token to the platform and trust its answer.
13
+ //
14
+ // NEVER gate a privileged app route with a hardcoded/shared secret. Verify the
15
+ // actual person making the request:
16
+ // - APP ADMIN → `verifyAppAdmin`: the owner or a team member with a Work grant
17
+ // on the app (operator actions: trigger a sync, edit config, rebuild data).
18
+ // - APP USER → `verifyAppUser`: an app-scoped account (`useAppAuth`); scope
19
+ // end-user actions to that user's own data.
20
+
21
+ export interface AppAuthConfig {
22
+ /** The app id (VITE_APP_ID). `jobsConfig` from src/lib/jobs.ts satisfies this. */
23
+ appId: string;
24
+ /** The TribeNest public API base (VITE_API_URL). */
25
+ apiUrl: string;
26
+ }
27
+
28
+ export interface AppAdminIdentity {
29
+ isAdmin: true;
30
+ /** Highest permission the account holds on the app (view/comment/edit/manage). */
31
+ permission: string | null;
32
+ appName: string | null;
33
+ }
34
+
35
+ export interface AppUserIdentity {
36
+ id: string;
37
+ associationId: string;
38
+ email: string;
39
+ firstName: string | null;
40
+ lastName: string | null;
41
+ status: string;
42
+ appId: string | null;
43
+ }
44
+
45
+ // The bearer credential the client forwarded, or null. We pass it through as-is
46
+ // (the platform verifies it) — never parse or trust its contents here.
47
+ function bearer(request: Request): string | null {
48
+ const h = request.headers.get("authorization");
49
+ return h && /^Bearer\s+\S/i.test(h) ? h : null;
50
+ }
51
+
52
+ /**
53
+ * Verify the request comes from an APP ADMIN — the owner or a team member with a
54
+ * Work grant on the app. Forwards the caller's TribeNest account token to
55
+ * `GET /public/apps/:appId/admin-access`. Returns the admin identity, or `null`
56
+ * when there's no token / it's invalid / the account isn't an admin.
57
+ *
58
+ * export const Route = createFileRoute("/api/tn-jobs/trigger")({
59
+ * server: { handlers: { POST: async ({ request }) => {
60
+ * const admin = await verifyAppAdmin(request, jobsConfig);
61
+ * if (!admin) return new Response("forbidden", { status: 403 });
62
+ * const { job, payload } = await request.json();
63
+ * const { env } = await import("cloudflare:workers");
64
+ * await enqueueAppJob(env, jobsConfig, job, payload);
65
+ * return Response.json({ ok: true });
66
+ * } } },
67
+ * });
68
+ *
69
+ * The dashboard/admin UI calls this route with the admin's token in the
70
+ * `Authorization` header (the same token `usePublicAuth` holds) — so the button
71
+ * is authenticated as the person clicking it, no shared secret.
72
+ */
73
+ export async function verifyAppAdmin(request: Request, cfg: AppAuthConfig): Promise<AppAdminIdentity | null> {
74
+ const auth = bearer(request);
75
+ if (!auth) return null;
76
+ try {
77
+ const res = await fetch(`${cfg.apiUrl}/public/apps/${cfg.appId}/admin-access`, {
78
+ headers: { authorization: auth },
79
+ });
80
+ if (!res.ok) return null;
81
+ const data = (await res.json()) as { isAdmin?: boolean; permission?: string | null; appName?: string | null };
82
+ return data.isAdmin ? { isAdmin: true, permission: data.permission ?? null, appName: data.appName ?? null } : null;
83
+ } catch {
84
+ return null;
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Verify the request comes from a logged-in APP USER (`useAppAuth` account).
90
+ * Forwards the app-user token to `GET /public/app-sessions/me`. Returns the
91
+ * user, or `null` when unauthenticated. Use to scope an end-user server action
92
+ * to that user (e.g. read/write only their own rows) — the returned
93
+ * `associationId` is the owner id the platform enforces on `mine`-scoped data.
94
+ */
95
+ export async function verifyAppUser(request: Request, cfg: AppAuthConfig): Promise<AppUserIdentity | null> {
96
+ const auth = bearer(request);
97
+ if (!auth) return null;
98
+ try {
99
+ const res = await fetch(`${cfg.apiUrl}/public/app-sessions/me`, {
100
+ headers: { authorization: auth },
101
+ });
102
+ if (!res.ok) return null;
103
+ return (await res.json()) as AppUserIdentity;
104
+ } catch {
105
+ return null;
106
+ }
107
+ }
@@ -15,6 +15,11 @@
15
15
  // App background-job runtime (no-config wrapper over the JobsDO). Server-only.
16
16
  export * from "./jobs";
17
17
 
18
+ // Authenticate the caller of an app's own server routes (admin / app-user).
19
+ // Server-only — the sanctioned alternative to gating a privileged route with a
20
+ // shared secret. See appAuth.ts.
21
+ export * from "./appAuth";
22
+
18
23
  import { emptyContentDocument, type ContentDocument } from "../content/types";
19
24
  import type {
20
25
  IEvent,