zitejs 0.10.0-start.21 → 0.10.0-start.23

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.
Files changed (128) hide show
  1. package/dist/cjs/auth/client.d.ts +2 -2
  2. package/dist/cjs/check/start/sourceRules.js +3 -0
  3. package/dist/cjs/cli.js +6 -0
  4. package/dist/cjs/dev/index.js +15 -0
  5. package/dist/cjs/dev/signin.d.ts +16 -0
  6. package/dist/cjs/dev/signin.js +76 -0
  7. package/dist/cjs/internal/appMode.d.ts +11 -0
  8. package/dist/cjs/internal/appMode.js +4 -1
  9. package/dist/cjs/internal/appToken.d.ts +17 -0
  10. package/dist/cjs/internal/appToken.js +26 -0
  11. package/dist/cjs/internal/platformFetch.d.ts +2 -0
  12. package/dist/cjs/internal/platformFetch.js +13 -0
  13. package/dist/cjs/internal/serviceUrls.d.ts +7 -0
  14. package/dist/cjs/internal/serviceUrls.js +16 -0
  15. package/dist/cjs/runtime/index.d.ts +1 -1
  16. package/dist/cjs/start/aiUsage.d.ts +23 -0
  17. package/dist/cjs/start/aiUsage.js +151 -0
  18. package/dist/cjs/start/devApp.d.ts +1 -0
  19. package/dist/cjs/start/devApp.js +6 -0
  20. package/dist/cjs/start/entries.js +4 -0
  21. package/dist/cjs/start/provider.d.ts +24 -0
  22. package/dist/cjs/start/provider.js +159 -0
  23. package/dist/cjs/start/types.d.ts +302 -0
  24. package/dist/cjs/start/types.js +2 -0
  25. package/dist/cjs/sync/lib.js +23 -50
  26. package/dist/cjs/sync/sdkNames.d.ts +65 -7
  27. package/dist/cjs/sync/sdkNames.js +76 -12
  28. package/dist/cjs/sync/sdkNames.test.js +38 -0
  29. package/dist/cjs/upload/index.js +73 -22
  30. package/dist/cjs/upload/index.test.js +35 -2
  31. package/dist/esm/auth/client.d.ts +2 -2
  32. package/dist/esm/check/start/sourceRules.js +3 -0
  33. package/dist/esm/cli.js +6 -0
  34. package/dist/esm/dev/index.js +15 -0
  35. package/dist/esm/dev/signin.d.ts +16 -0
  36. package/dist/esm/dev/signin.js +72 -0
  37. package/dist/esm/internal/appMode.d.ts +11 -0
  38. package/dist/esm/internal/appMode.js +2 -0
  39. package/dist/esm/internal/appToken.d.ts +17 -0
  40. package/dist/esm/internal/appToken.js +22 -0
  41. package/dist/esm/internal/platformFetch.d.ts +2 -0
  42. package/dist/esm/internal/platformFetch.js +8 -0
  43. package/dist/esm/internal/serviceUrls.d.ts +7 -0
  44. package/dist/esm/internal/serviceUrls.js +12 -0
  45. package/dist/esm/runtime/index.d.ts +1 -1
  46. package/dist/esm/signin/conformance.d.ts +100 -0
  47. package/dist/esm/signin/conformance.js +471 -0
  48. package/dist/esm/signin/cookies.d.ts +28 -0
  49. package/dist/esm/signin/cookies.js +59 -0
  50. package/dist/esm/signin/encoding.d.ts +9 -0
  51. package/dist/esm/signin/encoding.js +39 -0
  52. package/dist/esm/signin/endpoints.d.ts +12 -0
  53. package/dist/esm/signin/endpoints.js +29 -0
  54. package/dist/esm/signin/http.d.ts +25 -0
  55. package/dist/esm/signin/http.js +69 -0
  56. package/dist/esm/signin/index.d.ts +8 -0
  57. package/dist/esm/signin/index.js +9 -0
  58. package/dist/esm/signin/keys.d.ts +39 -0
  59. package/dist/esm/signin/keys.js +80 -0
  60. package/dist/esm/signin/paths.d.ts +2 -0
  61. package/dist/esm/signin/paths.js +2 -0
  62. package/dist/esm/signin/seal.d.ts +8 -0
  63. package/dist/esm/signin/seal.js +35 -0
  64. package/dist/esm/signin/signin.d.ts +60 -0
  65. package/dist/esm/signin/signin.js +340 -0
  66. package/dist/esm/signin/transport.d.ts +111 -0
  67. package/dist/esm/signin/transport.js +1 -0
  68. package/dist/esm/signin/verify.d.ts +77 -0
  69. package/dist/esm/signin/verify.js +155 -0
  70. package/dist/esm/start/airtable/built.d.ts +2 -0
  71. package/dist/esm/start/airtable/built.js +3 -0
  72. package/dist/esm/start/airtable/index.d.ts +3 -5
  73. package/dist/esm/start/airtable/index.js +4 -25
  74. package/dist/esm/start/airtable/settings.d.ts +31 -0
  75. package/dist/esm/start/airtable/settings.js +110 -0
  76. package/dist/esm/start/analytics.d.ts +5 -0
  77. package/dist/esm/start/analytics.js +77 -0
  78. package/dist/esm/start/data/index.d.ts +21 -9
  79. package/dist/esm/start/data/index.js +26 -20
  80. package/dist/esm/start/data/names.d.ts +7 -0
  81. package/dist/esm/start/data/names.js +15 -0
  82. package/dist/esm/start/data/types.d.ts +4 -1
  83. package/dist/esm/start/devApp.d.ts +1 -0
  84. package/dist/esm/start/devApp.js +3 -0
  85. package/dist/esm/start/devSignIn.d.ts +1 -0
  86. package/dist/esm/start/devSignIn.js +3 -0
  87. package/dist/esm/start/direct.d.ts +44 -0
  88. package/dist/esm/start/direct.js +442 -0
  89. package/dist/esm/start/entries.js +4 -0
  90. package/dist/esm/start/fetch.d.ts +1 -0
  91. package/dist/esm/start/fetch.js +4 -0
  92. package/dist/esm/start/instance.js +6 -2
  93. package/dist/esm/start/localSignIn.d.ts +18 -0
  94. package/dist/esm/start/localSignIn.js +73 -0
  95. package/dist/esm/start/outbound.d.ts +9 -1
  96. package/dist/esm/start/outbound.js +130 -31
  97. package/dist/esm/start/pageHead.d.ts +36 -0
  98. package/dist/esm/start/pageHead.js +178 -0
  99. package/dist/esm/start/provider.d.ts +24 -0
  100. package/dist/esm/start/provider.js +150 -0
  101. package/dist/esm/start/router.d.ts +6 -2
  102. package/dist/esm/start/router.js +41 -2
  103. package/dist/esm/start/server.d.ts +23 -12
  104. package/dist/esm/start/server.js +185 -56
  105. package/dist/esm/start/telemetry.d.ts +13 -0
  106. package/dist/esm/start/telemetry.js +71 -0
  107. package/dist/esm/start/types.d.ts +141 -44
  108. package/dist/esm/start/uploads.d.ts +2 -0
  109. package/dist/esm/start/uploads.js +52 -0
  110. package/dist/esm/start/version.d.ts +1 -1
  111. package/dist/esm/start/version.js +1 -1
  112. package/dist/esm/start/vite.d.ts +18 -1
  113. package/dist/esm/start/vite.js +163 -4
  114. package/dist/esm/start/workspaceSchema.d.ts +2 -0
  115. package/dist/esm/start/workspaceSchema.js +3 -0
  116. package/dist/esm/sync/lib.js +23 -50
  117. package/dist/esm/sync/sdkNames.d.ts +65 -7
  118. package/dist/esm/sync/sdkNames.js +73 -11
  119. package/dist/esm/sync/sdkNames.test.js +38 -0
  120. package/dist/esm/upload/index.js +74 -23
  121. package/dist/esm/upload/index.test.js +35 -2
  122. package/package.json +22 -12
  123. package/dist/cjs/auth-server/index.d.ts +0 -369
  124. package/dist/cjs/auth-server/index.js +0 -119
  125. package/dist/esm/auth-server/index.d.ts +0 -369
  126. package/dist/esm/auth-server/index.js +0 -116
  127. package/dist/esm/start/problems.d.ts +0 -19
  128. package/dist/esm/start/problems.js +0 -8
@@ -0,0 +1,25 @@
1
+ import type { Sealer } from "./seal.js";
2
+ import type { Transport } from "./transport.js";
3
+ /** `none` for `<app>:dev` (PKCE and a loopback redirect); a secret, or later a private key JWT, for a hosted client. */
4
+ export type ClientAuth = {
5
+ kind: "none";
6
+ } | {
7
+ kind: "secret";
8
+ secret: string;
9
+ };
10
+ export type HttpTransportOptions = {
11
+ issuer: string;
12
+ clientId: string;
13
+ clientAuth?: ClientAuth;
14
+ /** Seals the refresh token into the module's session cookie. */
15
+ sealer: Sealer;
16
+ /** Overrides for endpoints other than Zite's layout. */
17
+ endpoints?: Partial<{
18
+ token: string;
19
+ endSession: string;
20
+ revocation: string;
21
+ }>;
22
+ fetch?: (request: Request) => Promise<Response>;
23
+ timeoutMs?: number;
24
+ };
25
+ export declare const httpTransport: (options: HttpTransportOptions) => Transport;
@@ -0,0 +1,69 @@
1
+ // Standard OIDC to the identity server's public endpoints, for the module off Zite (T5 design §3.1, §5.9).
2
+ import { isRecord } from "./encoding.js";
3
+ import { ziteEndpoints } from "./endpoints.js";
4
+ const DEFAULT_TIMEOUT_MS = 10_000;
5
+ export const httpTransport = (options) => {
6
+ const endpoints = { ...ziteEndpoints(options.issuer), ...options.endpoints };
7
+ const send = options.fetch ?? ((request) => fetch(request));
8
+ const auth = options.clientAuth ?? { kind: "none" };
9
+ // Undefined when the server couldn't be reached or didn't answer JSON.
10
+ const post = async (url, form) => {
11
+ const headers = new Headers({ "content-type": "application/x-www-form-urlencoded", accept: "application/json" });
12
+ if (auth.kind === "secret") {
13
+ headers.set("authorization", `Basic ${btoa(`${encodeURIComponent(options.clientId)}:${encodeURIComponent(auth.secret)}`)}`);
14
+ }
15
+ const body = new URLSearchParams({ ...form, client_id: options.clientId });
16
+ try {
17
+ const response = await send(new Request(url, { method: "POST", headers, body, signal: AbortSignal.timeout(options.timeoutMs ?? DEFAULT_TIMEOUT_MS) }));
18
+ const parsed = await response.json().catch(() => ({}));
19
+ return { status: response.status, body: isRecord(parsed) ? parsed : {} };
20
+ }
21
+ catch (error) {
22
+ console.error(`[zite] the identity server at ${url} didn't answer`, error);
23
+ return undefined;
24
+ }
25
+ };
26
+ const text = (value) => (typeof value === "string" && value ? value : undefined);
27
+ const errorOf = (answer) => text(answer.body.error) ?? `http_${answer.status}`;
28
+ return {
29
+ longLived: { kind: "refresh-token", sealer: options.sealer },
30
+ async exchange({ code, verifier, redirectUri }) {
31
+ const answer = await post(endpoints.token, {
32
+ grant_type: "authorization_code",
33
+ code,
34
+ code_verifier: verifier,
35
+ redirect_uri: redirectUri,
36
+ });
37
+ if (!answer)
38
+ return { kind: "failed", error: "unavailable" };
39
+ const idToken = text(answer.body.id_token);
40
+ if (answer.status !== 200 || !idToken)
41
+ return { kind: "failed", error: errorOf(answer) };
42
+ const refreshToken = text(answer.body.refresh_token);
43
+ return { kind: "issued", idToken, ...(refreshToken && { refreshToken }) };
44
+ },
45
+ async renew({ longLived }) {
46
+ const answer = await post(endpoints.token, { grant_type: "refresh_token", refresh_token: longLived });
47
+ // Only the token endpoint's own refusals (RFC 6749 §5.2) end the session; a busy or broken server doesn't.
48
+ if (!answer || ![200, 400, 401].includes(answer.status))
49
+ return { kind: "unavailable" };
50
+ const idToken = text(answer.body.id_token);
51
+ if (answer.status !== 200 || !idToken)
52
+ return { kind: "signed-out", code: errorOf(answer) };
53
+ const refreshToken = text(answer.body.refresh_token);
54
+ return { kind: "renewed", idToken, ...(refreshToken && { refreshToken }) };
55
+ },
56
+ // This device only: the refresh token is revoked, and the identity server's session ends when it can match the
57
+ // hint. A refresh token elsewhere is the identity server's to revoke.
58
+ async endSession({ longLived, idToken }) {
59
+ await Promise.all([
60
+ longLived ? post(endpoints.revocation, { token: longLived, token_type_hint: "refresh_token" }) : undefined,
61
+ idToken ? post(endpoints.endSession, { id_token_hint: idToken }) : undefined,
62
+ ]);
63
+ return {};
64
+ },
65
+ async updateProfile() {
66
+ return { kind: "unsupported" };
67
+ },
68
+ };
69
+ };
@@ -0,0 +1,8 @@
1
+ export { createSignIn, isSignInPath, PREVIEW_IDENTITY_HEADER, PREVIEW_IDENTITY_PARAM, type Resolution, type SignIn, type SignInConfig } from "./signin.js";
2
+ export { createIdentityVerifier, DEFAULT_MAX_LIFETIME_S, IDENTITY_TOKEN_TYPE, type IdentityClaims, type IdentityMode, type IdentityVerifier, type Refusal, type RefusalReport, type Verdict, type VerifyExpectations, } from "./verify.js";
3
+ export { IDENTITY_KEY_KIND, identityKeys, remoteIdentityKeys, staticIdentityKeys, type IdentityKeys, type IdentityKeySource, } from "./keys.js";
4
+ export { clearCookie, cookieName, isModuleCookie, MODULE_COOKIES, type CookieContext, type ModuleCookie } from "./cookies.js";
5
+ export { aesGcmSealer, type Sealer } from "./seal.js";
6
+ export { httpTransport, type ClientAuth, type HttpTransportOptions } from "./http.js";
7
+ export { ziteEndpoints } from "./endpoints.js";
8
+ export type { ExchangeOutcome, IdentityBinding, LongLived, PlatformTransportContext, PlatformTransportFactory, ProfileChanges, ProfileOutcome, RenewOutcome, RenewReason, Transport, } from "./transport.js";
@@ -0,0 +1,9 @@
1
+ // `zitejs/signin`: Start's sign-in client. No TanStack, no `node:*`, Web Crypto only, so it runs in the dispatcher's
2
+ // bundle, in an app's Worker and on Node alike.
3
+ export { createSignIn, isSignInPath, PREVIEW_IDENTITY_HEADER, PREVIEW_IDENTITY_PARAM } from "./signin.js";
4
+ export { createIdentityVerifier, DEFAULT_MAX_LIFETIME_S, IDENTITY_TOKEN_TYPE, } from "./verify.js";
5
+ export { IDENTITY_KEY_KIND, identityKeys, remoteIdentityKeys, staticIdentityKeys, } from "./keys.js";
6
+ export { clearCookie, cookieName, isModuleCookie, MODULE_COOKIES } from "./cookies.js";
7
+ export { aesGcmSealer } from "./seal.js";
8
+ export { httpTransport } from "./http.js";
9
+ export { ziteEndpoints } from "./endpoints.js";
@@ -0,0 +1,39 @@
1
+ /** The only kind of key an identity token may be signed with. App-token keys share the map under other kinds. */
2
+ export declare const IDENTITY_KEY_KIND = "zite-identity";
3
+ /**
4
+ * The platform's public-key map as `ZITE_APP_TOKEN_PUBLIC_KEYS` holds it (`kid → { iss, key }`, where `iss` is the
5
+ * key's kind and `key` its raw Ed25519 public key, base64url). Keys of every kind are kept so a token signed with
6
+ * another kind's key is refused by name.
7
+ */
8
+ export type IdentityKeys = Readonly<Record<string, {
9
+ kind: string;
10
+ key: string;
11
+ }>>;
12
+ /** Where a verifier finds keys: what it holds now, and a refetch for a `kid` it doesn't know. */
13
+ export type IdentityKeySource = {
14
+ current(): IdentityKeys;
15
+ /** The keys after a refetch, or undefined when none happened (none configured, or one ran under a minute ago). */
16
+ refresh(): Promise<IdentityKeys | undefined>;
17
+ };
18
+ /**
19
+ * Parses the key map with `parseAppTokenPublicKeys`'s rules: a malformed entry throws, so a bad deploy fails at once,
20
+ * and off a local stack local dev's key is refused. Keys of other kinds are kept as they are and never used, so a
21
+ * kind added later doesn't break an older zitejs.
22
+ */
23
+ export declare const identityKeys: (json: string, options?: {
24
+ localDev?: boolean;
25
+ }) => IdentityKeys;
26
+ /** Keys that never change: the dispatcher's configured map. */
27
+ export declare const staticIdentityKeys: (keys: IdentityKeys) => IdentityKeySource;
28
+ /** For tests: forget every fetched key map. */
29
+ export declare const resetFetchedIdentityKeys: () => void;
30
+ /**
31
+ * Keys from `url` (the backend's static map), refetched on an unknown `kid` at most once a minute per process. Make
32
+ * one per request; concurrent refreshes on it share one fetch.
33
+ */
34
+ export declare const remoteIdentityKeys: (url: string, options?: {
35
+ initial?: IdentityKeys;
36
+ localDev?: boolean;
37
+ fetch?: (request: Request) => Promise<Response>;
38
+ now?: () => number;
39
+ }) => IdentityKeySource;
@@ -0,0 +1,80 @@
1
+ import { fromBase64Url, isRecord } from "./encoding.js";
2
+ /** The only kind of key an identity token may be signed with. App-token keys share the map under other kinds. */
3
+ export const IDENTITY_KEY_KIND = "zite-identity";
4
+ /** Accepted only by a local stack: its private half is in restly's repo. */
5
+ const LOCAL_DEV_PUBLIC_KEY = "SQn0d_i6VvM5J81X8IP4qtis0x-DI2pJVvdAbbG7las";
6
+ const KEY_MAP_NAME = "the public-key map";
7
+ /**
8
+ * Parses the key map with `parseAppTokenPublicKeys`'s rules: a malformed entry throws, so a bad deploy fails at once,
9
+ * and off a local stack local dev's key is refused. Keys of other kinds are kept as they are and never used, so a
10
+ * kind added later doesn't break an older zitejs.
11
+ */
12
+ export const identityKeys = (json, options = {}) => {
13
+ let parsed;
14
+ try {
15
+ parsed = JSON.parse(json);
16
+ }
17
+ catch {
18
+ parsed = undefined;
19
+ }
20
+ if (!isRecord(parsed))
21
+ throw new Error(`${KEY_MAP_NAME} must be a JSON object`);
22
+ const keys = {};
23
+ for (const [kid, entry] of Object.entries(parsed)) {
24
+ if (!/^[A-Za-z0-9_-]{1,64}$/.test(kid) || !isRecord(entry) || typeof entry.iss !== "string" || typeof entry.key !== "string") {
25
+ throw new Error(`${KEY_MAP_NAME}: bad entry ${kid.slice(0, 64)}`);
26
+ }
27
+ const identity = entry.iss === IDENTITY_KEY_KIND;
28
+ if (identity && fromBase64Url(entry.key)?.length !== 32)
29
+ throw new Error(`${KEY_MAP_NAME}: ${kid} isn't a raw Ed25519 key`);
30
+ if (identity && !options.localDev && entry.key === LOCAL_DEV_PUBLIC_KEY) {
31
+ throw new Error(`${KEY_MAP_NAME}: ${kid} is local dev's key, which only a local stack takes`);
32
+ }
33
+ keys[kid] = { kind: entry.iss, key: entry.key };
34
+ }
35
+ return keys;
36
+ };
37
+ /** Keys that never change: the dispatcher's configured map. */
38
+ export const staticIdentityKeys = (keys) => ({
39
+ current: () => keys,
40
+ refresh: async () => undefined,
41
+ });
42
+ const REFETCH_EVERY_MS = 60_000;
43
+ const FETCH_TIMEOUT_MS = 5_000;
44
+ // Per process, only fetched keys and when a fetch last started: Workers ties a fetch to the request that made it, so a
45
+ // pending one is never shared across requests. Another request inside the minute keeps the keys it has.
46
+ const fetched = new Map();
47
+ /** For tests: forget every fetched key map. */
48
+ export const resetFetchedIdentityKeys = () => fetched.clear();
49
+ /**
50
+ * Keys from `url` (the backend's static map), refetched on an unknown `kid` at most once a minute per process. Make
51
+ * one per request; concurrent refreshes on it share one fetch.
52
+ */
53
+ export const remoteIdentityKeys = (url, options = {}) => {
54
+ const now = options.now ?? Date.now;
55
+ let refreshing;
56
+ const load = async () => {
57
+ const held = fetched.get(url);
58
+ const startedAt = now();
59
+ if (held && startedAt - held.startedAt < REFETCH_EVERY_MS)
60
+ return undefined;
61
+ fetched.set(url, { keys: held?.keys, startedAt });
62
+ try {
63
+ const request = new Request(url, { headers: { accept: "application/json" }, signal: AbortSignal.timeout(FETCH_TIMEOUT_MS) });
64
+ const response = await (options.fetch ?? fetch)(request);
65
+ if (!response.ok)
66
+ throw new Error(`${response.status}`);
67
+ const keys = identityKeys(await response.text(), { localDev: options.localDev });
68
+ fetched.set(url, { keys, startedAt });
69
+ return keys;
70
+ }
71
+ catch (error) {
72
+ console.error(`[zite] couldn't fetch the identity keys from ${url}`, error);
73
+ return undefined;
74
+ }
75
+ };
76
+ return {
77
+ current: () => fetched.get(url)?.keys ?? options.initial ?? {},
78
+ refresh: () => (refreshing ??= load()),
79
+ };
80
+ };
@@ -0,0 +1,2 @@
1
+ /** The two reserved prefixes: an app never answers anything under them (T5 design §2). */
2
+ export declare const isSignInPath: (pathname: string) => boolean;
@@ -0,0 +1,2 @@
1
+ /** The two reserved prefixes: an app never answers anything under them (T5 design §2). */
2
+ export const isSignInPath = (pathname) => pathname === "/auth" || pathname.startsWith("/auth/") || pathname === "/api/auth" || pathname.startsWith("/api/auth/");
@@ -0,0 +1,8 @@
1
+ /** Encrypts what only the server may read back: off Zite, the refresh token in its cookie. */
2
+ export type Sealer = {
3
+ seal(plain: string): Promise<string>;
4
+ /** The plain text, or undefined for anything this sealer didn't seal. */
5
+ open(sealed: string): Promise<string | undefined>;
6
+ };
7
+ /** AES-256-GCM under a key derived from `secret` (`ZITE_AUTH_SECRET`). */
8
+ export declare const aesGcmSealer: (secret: string, label?: string) => Sealer;
@@ -0,0 +1,35 @@
1
+ import { fromBase64Url, fromUtf8, toBase64Url, utf8 } from "./encoding.js";
2
+ const MIN_SECRET_LENGTH = 32;
3
+ const IV_BYTES = 12;
4
+ /** AES-256-GCM under a key derived from `secret` (`ZITE_AUTH_SECRET`). */
5
+ export const aesGcmSealer = (secret, label = "zitejs signin session") => {
6
+ if (secret.length < MIN_SECRET_LENGTH)
7
+ throw new Error(`The sign-in secret must be at least ${MIN_SECRET_LENGTH} characters`);
8
+ let derived;
9
+ // Derived on first use, inside a request: Workers runs no crypto at module scope.
10
+ const key = () => (derived ??= crypto.subtle
11
+ .importKey("raw", utf8(secret), "HKDF", false, ["deriveKey"])
12
+ .then((base) => crypto.subtle.deriveKey({ name: "HKDF", hash: "SHA-256", salt: new Uint8Array(), info: utf8(label) }, base, { name: "AES-GCM", length: 256 }, false, ["encrypt", "decrypt"])));
13
+ return {
14
+ async seal(plain) {
15
+ const iv = crypto.getRandomValues(new Uint8Array(IV_BYTES));
16
+ const sealed = new Uint8Array(await crypto.subtle.encrypt({ name: "AES-GCM", iv }, await key(), utf8(plain)));
17
+ const out = new Uint8Array(IV_BYTES + sealed.length);
18
+ out.set(iv);
19
+ out.set(sealed, IV_BYTES);
20
+ return toBase64Url(out);
21
+ },
22
+ async open(value) {
23
+ const bytes = fromBase64Url(value);
24
+ if (!bytes || bytes.length <= IV_BYTES)
25
+ return undefined;
26
+ try {
27
+ const plain = await crypto.subtle.decrypt({ name: "AES-GCM", iv: bytes.slice(0, IV_BYTES) }, await key(), bytes.slice(IV_BYTES));
28
+ return fromUtf8(new Uint8Array(plain));
29
+ }
30
+ catch {
31
+ return undefined;
32
+ }
33
+ },
34
+ };
35
+ };
@@ -0,0 +1,60 @@
1
+ import { type CookieContext } from "./cookies.js";
2
+ import { type IdentityKeys, type IdentityKeySource } from "./keys.js";
3
+ import type { Transport } from "./transport.js";
4
+ import { type IdentityClaims, type IdentityMode, type Refusal, type RefusalReport, type Verdict } from "./verify.js";
5
+ export type SignInConfig = {
6
+ /** `https://<host>/auth`: on Zite the host the request was served on; off Zite the app's live host. */
7
+ issuer: string;
8
+ /** `<app>:<mode>`. */
9
+ clientId: string;
10
+ app: string;
11
+ mode: IdentityMode;
12
+ /** The org settings version an identity must carry, or `any` off Zite, where the module can't know it. */
13
+ ver: number | null | "any";
14
+ /** Keys never change on the dispatcher; off Zite they're fetched on an unknown `kid`. */
15
+ keys: IdentityKeys | IdentityKeySource;
16
+ /** `secure` on https. `framed` only for previews and demos in the editor's frame: never Partitioned on a live host. */
17
+ cookies: CookieContext;
18
+ transport: Transport;
19
+ /** Default `<request origin>/auth/callback`. */
20
+ redirectUri?: string;
21
+ /** A snapshot host's pinned build, which a preview identity must name. */
22
+ previewBuild?: number;
23
+ /** The longest identity the verifier accepts, in seconds. Issued for 60; default 300. */
24
+ identityLifetimeS?: number;
25
+ /** Overrides for an identity server other than Zite's layout. */
26
+ endpoints?: Partial<{
27
+ authorize: string;
28
+ signInPage: string;
29
+ }>;
30
+ /** Milliseconds since the epoch. */
31
+ now?: () => number;
32
+ onRefusal?: (report: RefusalReport) => void;
33
+ };
34
+ export type Resolution = {
35
+ /** Verified, or renewed and verified; null when nobody is signed in. */
36
+ user: IdentityClaims | null;
37
+ state: "valid" | "renewed" | "anonymous" | "refused";
38
+ /** The verifier's refusal, or the identity server's code when it wouldn't renew. */
39
+ reason?: Refusal | string;
40
+ /** For the response: a renewed identity, or clears. */
41
+ setCookies: string[];
42
+ /** The request's Cookie header with the module's cookies and the long-lived credential removed. */
43
+ cookie: string | null;
44
+ /** On previews, the identity in force, for the frame's URL and `x-zite-preview`. */
45
+ previewToken?: string;
46
+ };
47
+ export type SignIn = {
48
+ /** Answers the module's own paths (T5 design §2), else undefined. */
49
+ handle(request: Request): Promise<Response | undefined>;
50
+ /** Who is calling: verified, renewed on expiry or a version change, or nobody. */
51
+ resolve(request: Request): Promise<Resolution>;
52
+ verify(token: string, expect?: {
53
+ nonce?: string;
54
+ }): Promise<Verdict>;
55
+ };
56
+ export { isSignInPath } from "./paths.js";
57
+ /** The preview identity's query parameter and header, as old builds carry them. */
58
+ export declare const PREVIEW_IDENTITY_PARAM = "zite_preview";
59
+ export declare const PREVIEW_IDENTITY_HEADER = "x-zite-preview";
60
+ export declare const createSignIn: (config: SignInConfig) => SignIn;