|
1 | 1 | import { afterAll, beforeAll, describe, expect, it } from "bun:test"; |
2 | 2 | import type { TenantEnv } from "@intx/hub-api"; |
3 | | -import { credential } from "@intx/db/schema"; |
| 3 | +import { credential, grant } from "@intx/db/schema"; |
4 | 4 | import { eq } from "drizzle-orm"; |
5 | 5 | import { Hono } from "hono"; |
6 | 6 |
|
@@ -62,7 +62,7 @@ beforeAll(async () => { |
62 | 62 |
|
63 | 63 | afterAll(() => t.close()); |
64 | 64 |
|
65 | | -function app(principalId: string) { |
| 65 | +function app(principalId: string, owner?: "tenant") { |
66 | 66 | const host = new Hono<TenantEnv>(); |
67 | 67 | host.use(async (c, next) => { |
68 | 68 | const at = new Date(0); |
@@ -94,6 +94,7 @@ function app(principalId: string) { |
94 | 94 | requireGrant: async (_c, next) => { |
95 | 95 | await next(); |
96 | 96 | }, |
| 97 | + ...(owner !== undefined ? { owner } : {}), |
97 | 98 | }); |
98 | 99 | return host; |
99 | 100 | } |
@@ -167,4 +168,128 @@ describe("the OAuth login target", () => { |
167 | 168 | const first = await login("first"); |
168 | 169 | expect(await login("second")).toBe(first); |
169 | 170 | }); |
| 171 | + |
| 172 | + // Interchange resolves only an organizational credential (principalId |
| 173 | + // null) for a catalog model provider, so a tenant-owned login must land |
| 174 | + // with no principal, and must not be replaceable by a personal one. |
| 175 | + describe("owned by the tenant", () => { |
| 176 | + const persistAs = ( |
| 177 | + name: string, |
| 178 | + principalId: string, |
| 179 | + access: string, |
| 180 | + owner?: "tenant", |
| 181 | + ) => |
| 182 | + persistOAuthCredential({ |
| 183 | + db: t.db, |
| 184 | + cipher: plainCipher, |
| 185 | + tenantId: "ta", |
| 186 | + principalId, |
| 187 | + providerId: "prov_inherited", |
| 188 | + provider: "acme", |
| 189 | + name, |
| 190 | + scopes: ["openid"], |
| 191 | + tokens: { access, refresh: "refresh" }, |
| 192 | + metadata: {}, |
| 193 | + ...(owner !== undefined ? { owner } : {}), |
| 194 | + }); |
| 195 | + |
| 196 | + it("stores no principal and no creator grant", async () => { |
| 197 | + const id = await persistAs("shared", "p_a", "first", "tenant"); |
| 198 | + const row = await t.db.query.credential.findFirst({ |
| 199 | + where: eq(credential.id, id), |
| 200 | + }); |
| 201 | + expect(row?.principalId).toBeNull(); |
| 202 | + const grants = await t.db.query.grant.findMany({ |
| 203 | + where: eq(grant.resource, `credential:${id}`), |
| 204 | + }); |
| 205 | + expect(grants).toEqual([]); |
| 206 | + }); |
| 207 | + |
| 208 | + it("never replaces an existing name, so a login cannot take over an org credential", async () => { |
| 209 | + const id = await persistAs("org", "p_a", "first", "tenant"); |
| 210 | + await expect(persistAs("org", "p_b", "second", "tenant")).rejects.toThrow( |
| 211 | + "already exists", |
| 212 | + ); |
| 213 | + await expect(persistAs("org", "p_a", "third")).rejects.toThrow( |
| 214 | + "already exists", |
| 215 | + ); |
| 216 | + const row = await t.db.query.credential.findFirst({ |
| 217 | + where: eq(credential.id, id), |
| 218 | + }); |
| 219 | + expect(row?.secret).toEndWith("|first"); |
| 220 | + }); |
| 221 | + |
| 222 | + it("stores a mount's completed login with no principal", async () => { |
| 223 | + const host = new Hono<TenantEnv>(); |
| 224 | + host.use(async (c, next) => { |
| 225 | + const at = new Date(0); |
| 226 | + c.set("tenant", { |
| 227 | + id: "ta", |
| 228 | + name: "ta", |
| 229 | + slug: "ta", |
| 230 | + domain: "ta.example", |
| 231 | + parentId: "tparent", |
| 232 | + config: null, |
| 233 | + createdAt: at, |
| 234 | + updatedAt: at, |
| 235 | + }); |
| 236 | + c.set("principal", { |
| 237 | + id: "p_a", |
| 238 | + tenantId: "ta", |
| 239 | + kind: "user", |
| 240 | + refId: "p_a", |
| 241 | + status: "active", |
| 242 | + createdAt: at, |
| 243 | + updatedAt: at, |
| 244 | + }); |
| 245 | + await next(); |
| 246 | + }); |
| 247 | + mountOAuthLogin(host, { |
| 248 | + db: t.db, |
| 249 | + cipher: plainCipher, |
| 250 | + providers: { |
| 251 | + granting: { |
| 252 | + oauthConfig: { |
| 253 | + ...providers.acme.oauthConfig, |
| 254 | + redirectUri: "http://127.0.0.1:1457/callback", |
| 255 | + }, |
| 256 | + exchange: () => Promise.resolve({ access: "minted", refresh: "r" }), |
| 257 | + }, |
| 258 | + }, |
| 259 | + requireGrant: async (_c, next) => { |
| 260 | + await next(); |
| 261 | + }, |
| 262 | + owner: "tenant", |
| 263 | + }); |
| 264 | + const started = await host.request("/oauth-logins", { |
| 265 | + method: "POST", |
| 266 | + headers: { "content-type": "application/json" }, |
| 267 | + body: JSON.stringify({ |
| 268 | + provider: "granting", |
| 269 | + providerId: "prov_inherited", |
| 270 | + credentialName: "mounted", |
| 271 | + }), |
| 272 | + }); |
| 273 | + expect(started.status).toBe(201); |
| 274 | + const { loginId, authorizeUrl } = (await started.json()) as { |
| 275 | + loginId: string; |
| 276 | + authorizeUrl: string; |
| 277 | + }; |
| 278 | + const state = new URL(authorizeUrl).searchParams.get("state"); |
| 279 | + await fetch(`http://127.0.0.1:1457/callback?code=c&state=${state}`); |
| 280 | + let polled: { status?: string; credentialId?: string } = {}; |
| 281 | + for (let i = 0; i < 50 && polled.status !== "completed"; i++) { |
| 282 | + polled = (await ( |
| 283 | + await host.request(`/oauth-logins/${loginId}`) |
| 284 | + ).json()) as typeof polled; |
| 285 | + if (polled.status !== "completed") await Bun.sleep(20); |
| 286 | + } |
| 287 | + expect(polled.status).toBe("completed"); |
| 288 | + const row = await t.db.query.credential.findFirst({ |
| 289 | + where: eq(credential.id, polled.credentialId ?? ""), |
| 290 | + }); |
| 291 | + expect(row?.principalId).toBeNull(); |
| 292 | + expect(row?.secret).toEndWith("|minted"); |
| 293 | + }); |
| 294 | + }); |
170 | 295 | }); |
0 commit comments