cursedauth 0.1.0 → 0.2.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.
@@ -0,0 +1,68 @@
1
+ /**
2
+ * The Hono test harness every SSO-CONSUMER satellite (notes, vault, hobbies, …) had
3
+ * written by hand: a per-run signing keypair standing in for the accounts satellite,
4
+ * a stand-in user registry, and the `grant → callback → session cookie` dance that
5
+ * signs a seeded user in exactly the way production does.
6
+ *
7
+ * Each app still builds its OWN route graph (its gate, its app-specific routes) and
8
+ * passes the resulting `Hono` app in — the security property under test is that graph's
9
+ * registration ORDER, which stays in the app. This module only supplies the identity
10
+ * mechanics that were byte-identical across the fleet.
11
+ *
12
+ * The identity PROVIDER (apps/auth) does not use this — it verifies real passwords, not
13
+ * grants — but it can still share {@link testRequest}, the `app.request` wrapper every
14
+ * app had copied.
15
+ */
16
+ import type { Hono } from "hono";
17
+ import { type SsoKeyPair } from "./sso";
18
+ /** A user as the accounts satellite would know them — the shape grants are minted from. */
19
+ export interface SeededTestUser {
20
+ id: string;
21
+ email: string;
22
+ name: string;
23
+ admin: boolean;
24
+ }
25
+ export type TestRequest = (path: string, init?: RequestInit & {
26
+ cookie?: string;
27
+ }) => Promise<Response>;
28
+ /**
29
+ * The `app.request` wrapper every satellite test file had re-declared: fold a `cookie`
30
+ * option into the header set and default a JSON content-type when there is a body.
31
+ * Returns a promise so callers can `await` uniformly even though `app.request` is sync.
32
+ */
33
+ export declare function testRequest(app: Hono): TestRequest;
34
+ export interface SsoConsumerHarness {
35
+ /** The stand-in registry's signing keypair — for suites that mint grants by hand. */
36
+ keys: SsoKeyPair;
37
+ /** Register a user in the stand-in registry; returns the stored record. Admin
38
+ * defaults to false — a consumer that wants an admin default wraps this. */
39
+ seed(user: {
40
+ email: string;
41
+ name?: string;
42
+ admin?: boolean;
43
+ }): SeededTestUser;
44
+ /** The seeded record for an email (case-insensitive), or undefined. */
45
+ seededUser(email: string): SeededTestUser | undefined;
46
+ /** Mint a grant for a seeded user (apps/auth's half, inlined). */
47
+ grantFor(email: string): string;
48
+ /** Drive a grant through the consumer's SSO callback. */
49
+ callback(grant: string): Promise<Response>;
50
+ /** The full dance: mint → callback → the `<cookie>=value` session header. */
51
+ login(email: string): Promise<string>;
52
+ request: TestRequest;
53
+ }
54
+ /**
55
+ * Build the SSO-consumer identity harness for a test app. Generate the keypair FIRST
56
+ * (the app needs its `publicKeyPem` to build the consumer) and pass it back in here so
57
+ * the grants this mints verify against the app's own consumer.
58
+ */
59
+ export declare function createSsoConsumerHarness(options: {
60
+ app: Hono;
61
+ /** The grant audience — this satellite's app key (e.g. "notes"). */
62
+ aud: string;
63
+ /** The session cookie name to read back off the callback response. */
64
+ cookieName: string;
65
+ /** The keypair the app's consumer was built with. Generated if omitted. */
66
+ keys?: SsoKeyPair;
67
+ }): SsoConsumerHarness;
68
+ //# sourceMappingURL=ssoTestHarness.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ssoTestHarness.d.ts","sourceRoot":"","sources":["../src/ssoTestHarness.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AACjC,OAAO,EAAiC,KAAK,UAAU,EAAE,MAAM,OAAO,CAAC;AAGvE,2FAA2F;AAC3F,MAAM,WAAW,cAAc;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,OAAO,CAAC;CACf;AAED,MAAM,MAAM,WAAW,GAAG,CACzB,IAAI,EAAE,MAAM,EACZ,IAAI,CAAC,EAAE,WAAW,GAAG;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,KACpC,OAAO,CAAC,QAAQ,CAAC,CAAC;AAEvB;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,IAAI,GAAG,WAAW,CASlD;AAED,MAAM,WAAW,kBAAkB;IAClC,qFAAqF;IACrF,IAAI,EAAE,UAAU,CAAC;IACjB;iFAC6E;IAC7E,IAAI,CAAC,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,cAAc,CAAC;IAC9E,uEAAuE;IACvE,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS,CAAC;IACtD,kEAAkE;IAClE,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;IAChC,yDAAyD;IACzD,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC3C,6EAA6E;IAC7E,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACtC,OAAO,EAAE,WAAW,CAAC;CACrB;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE;IACjD,GAAG,EAAE,IAAI,CAAC;IACV,oEAAoE;IACpE,GAAG,EAAE,MAAM,CAAC;IACZ,sEAAsE;IACtE,UAAU,EAAE,MAAM,CAAC;IACnB,2EAA2E;IAC3E,IAAI,CAAC,EAAE,UAAU,CAAC;CAClB,GAAG,kBAAkB,CAyCrB"}
@@ -0,0 +1,61 @@
1
+ import { generateSsoKeyPair, mintGrant } from "./sso";
2
+ import { SSO_CALLBACK_PATH } from "./ssoConsumer.server";
3
+ /**
4
+ * The `app.request` wrapper every satellite test file had re-declared: fold a `cookie`
5
+ * option into the header set and default a JSON content-type when there is a body.
6
+ * Returns a promise so callers can `await` uniformly even though `app.request` is sync.
7
+ */
8
+ export function testRequest(app) {
9
+ return (path, init) => {
10
+ const headers = new Headers(init?.headers);
11
+ if (init?.cookie)
12
+ headers.set("cookie", init.cookie);
13
+ if (init?.body && !headers.has("content-type")) {
14
+ headers.set("content-type", "application/json");
15
+ }
16
+ return Promise.resolve(app.request(path, { ...init, headers }));
17
+ };
18
+ }
19
+ /**
20
+ * Build the SSO-consumer identity harness for a test app. Generate the keypair FIRST
21
+ * (the app needs its `publicKeyPem` to build the consumer) and pass it back in here so
22
+ * the grants this mints verify against the app's own consumer.
23
+ */
24
+ export function createSsoConsumerHarness(options) {
25
+ const keys = options.keys ?? generateSsoKeyPair();
26
+ const seeded = new Map();
27
+ const request = testRequest(options.app);
28
+ const seededUser = (email) => seeded.get(email.toLowerCase());
29
+ const grantFor = (email) => {
30
+ const user = seededUser(email);
31
+ if (!user)
32
+ throw new Error(`no seeded user for ${email}`);
33
+ return mintGrant({
34
+ privateKeyPem: keys.privateKeyPem,
35
+ kid: keys.kid,
36
+ aud: options.aud,
37
+ user: { id: user.id, email: user.email, name: user.name, admin: user.admin },
38
+ });
39
+ };
40
+ const callback = (grant) => request(`${SSO_CALLBACK_PATH}?grant=${encodeURIComponent(grant)}`);
41
+ const login = async (email) => {
42
+ const res = await callback(grantFor(email));
43
+ const setCookie = res.headers.get("set-cookie") ?? "";
44
+ const match = new RegExp(`${options.cookieName}=([^;]+)`).exec(setCookie);
45
+ if (!match)
46
+ throw new Error("SSO callback returned no session cookie");
47
+ return `${options.cookieName}=${match[1]}`;
48
+ };
49
+ const seed = (user) => {
50
+ const record = {
51
+ id: crypto.randomUUID(),
52
+ email: user.email,
53
+ name: user.name ?? user.email.split("@")[0] ?? user.email,
54
+ admin: user.admin ?? false,
55
+ };
56
+ seeded.set(user.email.toLowerCase(), record);
57
+ return record;
58
+ };
59
+ return { keys, seed, seededUser, grantFor, callback, login, request };
60
+ }
61
+ //# sourceMappingURL=ssoTestHarness.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ssoTestHarness.js","sourceRoot":"","sources":["../src/ssoTestHarness.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAmB,MAAM,OAAO,CAAC;AACvE,OAAO,EAAE,iBAAiB,EAAE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AAC,KAAK,IAAI,KAAK;SAC1B,CAAC;QACF,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,WAAW,EAAE,EAAE,MAAM,CAAC,CAAC;QAC7C,OAAO,MAAM,CAAC;IACf,CAAC,CAAC;IAEF,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;AACvE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cursedauth",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "The app side of fleet SSO \u2014 sign in through the accounts service, keep a local snapshot of who signed in, and gate the app on it. Bun + Hono + React, one SQLite file per app.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -59,6 +59,18 @@
59
59
  "source": "./src/accountSlot.client.ts",
60
60
  "import": "./dist/accountSlot.client.js"
61
61
  },
62
+ "./sso": {
63
+ "types": "./dist/sso.d.ts",
64
+ "bun": "./src/sso.ts",
65
+ "source": "./src/sso.ts",
66
+ "import": "./dist/sso.js"
67
+ },
68
+ "./testing": {
69
+ "types": "./dist/ssoTestHarness.d.ts",
70
+ "bun": "./src/ssoTestHarness.ts",
71
+ "source": "./src/ssoTestHarness.ts",
72
+ "import": "./dist/ssoTestHarness.js"
73
+ },
62
74
  "./package.json": "./package.json"
63
75
  },
64
76
  "files": [
@@ -0,0 +1,120 @@
1
+ /**
2
+ * The Hono test harness every SSO-CONSUMER satellite (notes, vault, hobbies, …) had
3
+ * written by hand: a per-run signing keypair standing in for the accounts satellite,
4
+ * a stand-in user registry, and the `grant → callback → session cookie` dance that
5
+ * signs a seeded user in exactly the way production does.
6
+ *
7
+ * Each app still builds its OWN route graph (its gate, its app-specific routes) and
8
+ * passes the resulting `Hono` app in — the security property under test is that graph's
9
+ * registration ORDER, which stays in the app. This module only supplies the identity
10
+ * mechanics that were byte-identical across the fleet.
11
+ *
12
+ * The identity PROVIDER (apps/auth) does not use this — it verifies real passwords, not
13
+ * grants — but it can still share {@link testRequest}, the `app.request` wrapper every
14
+ * app had copied.
15
+ */
16
+ import type { Hono } from "hono";
17
+ import { generateSsoKeyPair, mintGrant, type SsoKeyPair } from "./sso";
18
+ import { SSO_CALLBACK_PATH } from "./ssoConsumer.server";
19
+
20
+ /** A user as the accounts satellite would know them — the shape grants are minted from. */
21
+ export interface SeededTestUser {
22
+ id: string;
23
+ email: string;
24
+ name: string;
25
+ admin: boolean;
26
+ }
27
+
28
+ export type TestRequest = (
29
+ path: string,
30
+ init?: RequestInit & { cookie?: string },
31
+ ) => Promise<Response>;
32
+
33
+ /**
34
+ * The `app.request` wrapper every satellite test file had re-declared: fold a `cookie`
35
+ * option into the header set and default a JSON content-type when there is a body.
36
+ * Returns a promise so callers can `await` uniformly even though `app.request` is sync.
37
+ */
38
+ export function testRequest(app: Hono): TestRequest {
39
+ return (path, init) => {
40
+ const headers = new Headers(init?.headers);
41
+ if (init?.cookie) headers.set("cookie", init.cookie);
42
+ if (init?.body && !headers.has("content-type")) {
43
+ headers.set("content-type", "application/json");
44
+ }
45
+ return Promise.resolve(app.request(path, { ...init, headers }));
46
+ };
47
+ }
48
+
49
+ export interface SsoConsumerHarness {
50
+ /** The stand-in registry's signing keypair — for suites that mint grants by hand. */
51
+ keys: SsoKeyPair;
52
+ /** Register a user in the stand-in registry; returns the stored record. Admin
53
+ * defaults to false — a consumer that wants an admin default wraps this. */
54
+ seed(user: { email: string; name?: string; admin?: boolean }): SeededTestUser;
55
+ /** The seeded record for an email (case-insensitive), or undefined. */
56
+ seededUser(email: string): SeededTestUser | undefined;
57
+ /** Mint a grant for a seeded user (apps/auth's half, inlined). */
58
+ grantFor(email: string): string;
59
+ /** Drive a grant through the consumer's SSO callback. */
60
+ callback(grant: string): Promise<Response>;
61
+ /** The full dance: mint → callback → the `<cookie>=value` session header. */
62
+ login(email: string): Promise<string>;
63
+ request: TestRequest;
64
+ }
65
+
66
+ /**
67
+ * Build the SSO-consumer identity harness for a test app. Generate the keypair FIRST
68
+ * (the app needs its `publicKeyPem` to build the consumer) and pass it back in here so
69
+ * the grants this mints verify against the app's own consumer.
70
+ */
71
+ export function createSsoConsumerHarness(options: {
72
+ app: Hono;
73
+ /** The grant audience — this satellite's app key (e.g. "notes"). */
74
+ aud: string;
75
+ /** The session cookie name to read back off the callback response. */
76
+ cookieName: string;
77
+ /** The keypair the app's consumer was built with. Generated if omitted. */
78
+ keys?: SsoKeyPair;
79
+ }): SsoConsumerHarness {
80
+ const keys = options.keys ?? generateSsoKeyPair();
81
+ const seeded = new Map<string, SeededTestUser>();
82
+ const request = testRequest(options.app);
83
+
84
+ const seededUser = (email: string): SeededTestUser | undefined => seeded.get(email.toLowerCase());
85
+
86
+ const grantFor = (email: string): string => {
87
+ const user = seededUser(email);
88
+ if (!user) throw new Error(`no seeded user for ${email}`);
89
+ return mintGrant({
90
+ privateKeyPem: keys.privateKeyPem,
91
+ kid: keys.kid,
92
+ aud: options.aud,
93
+ user: { id: user.id, email: user.email, name: user.name, admin: user.admin },
94
+ });
95
+ };
96
+
97
+ const callback = (grant: string): Promise<Response> =>
98
+ request(`${SSO_CALLBACK_PATH}?grant=${encodeURIComponent(grant)}`);
99
+
100
+ const login = async (email: string): Promise<string> => {
101
+ const res = await callback(grantFor(email));
102
+ const setCookie = res.headers.get("set-cookie") ?? "";
103
+ const match = new RegExp(`${options.cookieName}=([^;]+)`).exec(setCookie);
104
+ if (!match) throw new Error("SSO callback returned no session cookie");
105
+ return `${options.cookieName}=${match[1]}`;
106
+ };
107
+
108
+ const seed = (user: { email: string; name?: string; admin?: boolean }): SeededTestUser => {
109
+ const record: SeededTestUser = {
110
+ id: crypto.randomUUID(),
111
+ email: user.email,
112
+ name: user.name ?? user.email.split("@")[0] ?? user.email,
113
+ admin: user.admin ?? false,
114
+ };
115
+ seeded.set(user.email.toLowerCase(), record);
116
+ return record;
117
+ };
118
+
119
+ return { keys, seed, seededUser, grantFor, callback, login, request };
120
+ }