@flipstream/sdk 0.0.0-stage → 0.4.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.
Files changed (78) hide show
  1. package/CHANGELOG.md +85 -0
  2. package/LICENSE +202 -0
  3. package/README.md +280 -2
  4. package/dist/api/index.d.ts +1 -0
  5. package/dist/api/index.js +2 -0
  6. package/dist/api/index.js.map +1 -0
  7. package/dist/auth/discovery.d.ts +21 -0
  8. package/dist/auth/discovery.js +68 -0
  9. package/dist/auth/discovery.js.map +1 -0
  10. package/dist/auth/index.d.ts +5 -0
  11. package/dist/auth/index.js +5 -0
  12. package/dist/auth/index.js.map +1 -0
  13. package/dist/auth/pkce.d.ts +10 -0
  14. package/dist/auth/pkce.js +25 -0
  15. package/dist/auth/pkce.js.map +1 -0
  16. package/dist/auth/signIn.d.ts +97 -0
  17. package/dist/auth/signIn.js +133 -0
  18. package/dist/auth/signIn.js.map +1 -0
  19. package/dist/auth/transport.d.ts +18 -0
  20. package/dist/auth/transport.js +26 -0
  21. package/dist/auth/transport.js.map +1 -0
  22. package/dist/core/body.d.ts +14 -0
  23. package/dist/core/body.js +69 -0
  24. package/dist/core/body.js.map +1 -0
  25. package/dist/core/call.d.ts +17 -0
  26. package/dist/core/call.js +116 -0
  27. package/dist/core/call.js.map +1 -0
  28. package/dist/core/fetch.d.ts +35 -0
  29. package/dist/core/fetch.js +105 -0
  30. package/dist/core/fetch.js.map +1 -0
  31. package/dist/core/read.d.ts +17 -0
  32. package/dist/core/read.js +92 -0
  33. package/dist/core/read.js.map +1 -0
  34. package/dist/core/result.d.ts +94 -0
  35. package/dist/core/result.js +40 -0
  36. package/dist/core/result.js.map +1 -0
  37. package/dist/core/retry.d.ts +40 -0
  38. package/dist/core/retry.js +41 -0
  39. package/dist/core/retry.js.map +1 -0
  40. package/dist/core/transport.d.ts +62 -0
  41. package/dist/core/transport.js +9 -0
  42. package/dist/core/transport.js.map +1 -0
  43. package/dist/flipstream.d.ts +45 -0
  44. package/dist/flipstream.js +26 -0
  45. package/dist/flipstream.js.map +1 -0
  46. package/dist/index.d.ts +11 -0
  47. package/dist/index.js +8 -0
  48. package/dist/index.js.map +1 -0
  49. package/dist/node/headless.d.ts +20 -0
  50. package/dist/node/headless.js +28 -0
  51. package/dist/node/headless.js.map +1 -0
  52. package/dist/node/index.d.ts +4 -0
  53. package/dist/node/index.js +5 -0
  54. package/dist/node/index.js.map +1 -0
  55. package/dist/node/loopback.d.ts +58 -0
  56. package/dist/node/loopback.js +126 -0
  57. package/dist/node/loopback.js.map +1 -0
  58. package/dist/query/catalog.d.ts +15 -0
  59. package/dist/query/catalog.js +41 -0
  60. package/dist/query/catalog.js.map +1 -0
  61. package/dist/query/errors.d.ts +8 -0
  62. package/dist/query/errors.js +16 -0
  63. package/dist/query/errors.js.map +1 -0
  64. package/dist/query/query.d.ts +27 -0
  65. package/dist/query/query.js +35 -0
  66. package/dist/query/query.js.map +1 -0
  67. package/dist/recorded/query-planner/catalog-google_search_console.json +140 -0
  68. package/dist/recorded/query-planner/catalog.json +1234 -0
  69. package/dist/spec/query-planner/index.d.ts +1 -0
  70. package/dist/spec/query-planner/index.js +3 -0
  71. package/dist/spec/query-planner/index.js.map +1 -0
  72. package/dist/spec/query-planner/types.gen.d.ts +300 -0
  73. package/dist/spec/query-planner/types.gen.js +3 -0
  74. package/dist/spec/query-planner/types.gen.js.map +1 -0
  75. package/dist/spec/query-planner/zod.gen.d.ts +325 -0
  76. package/dist/spec/query-planner/zod.gen.js +137 -0
  77. package/dist/spec/query-planner/zod.gen.js.map +1 -0
  78. package/package.json +90 -4
@@ -0,0 +1,68 @@
1
+ import { z } from 'zod';
2
+ import { call } from '../core/call.js';
3
+ import { authTransport, isHttpsUrl } from './transport.js';
4
+ // Unknown fields are kept and never read: `first_party_client_id` among them, which the SDK never
5
+ // uses in place of the app's own client ID (D421).
6
+ const zMetadata = z.looseObject({
7
+ issuer: z.string(),
8
+ authorization_endpoint: z.string(),
9
+ token_endpoint: z.string(),
10
+ code_challenge_methods_supported: z.array(z.string()).exactOptional(),
11
+ authorization_response_iss_parameter_supported: z.boolean().exactOptional(),
12
+ });
13
+ /**
14
+ * The authorization server at `issuer`, from its RFC 8414 metadata. `refused`, with nothing sent,
15
+ * when `issuer` isn't an https URL. Refused as `invalid`: metadata that doesn't name `issuer`
16
+ * exactly (§3.3), an endpoint that is not an https URL, and a server that doesn't list PKCE's
17
+ * S256, since one that lists no methods supports no PKCE (§2), and a code it issues is bound to no
18
+ * verifier.
19
+ */
20
+ export async function discover(issuer, options = {}) {
21
+ if (typeof issuer !== 'string' || !isHttpsUrl(issuer))
22
+ return { ok: false, error: { kind: 'refused' } };
23
+ const read = await call(authTransport(options), {
24
+ effect: 'read',
25
+ service: 'auth',
26
+ endpoint: 'auth.discover',
27
+ makeRequest: () => new Request(metadataUrl(issuer), { headers: { accept: 'application/json' } }),
28
+ schema: zMetadata,
29
+ deadlineMs: 10_000,
30
+ retry: 'read',
31
+ }, options.signal);
32
+ if (!read.ok)
33
+ return read;
34
+ const doc = read.data;
35
+ const problem = problemWith(doc, issuer);
36
+ if (problem !== null)
37
+ return { ok: false, error: { kind: 'invalid', issues: problem, status: 200 } };
38
+ return {
39
+ ok: true,
40
+ data: {
41
+ issuer: doc.issuer,
42
+ authorizationEndpoint: doc.authorization_endpoint,
43
+ tokenEndpoint: doc.token_endpoint,
44
+ issuerOnRedirect: doc.authorization_response_iss_parameter_supported === true,
45
+ },
46
+ };
47
+ }
48
+ /**
49
+ * RFC 8414 §3.1: the well-known path goes between the host and the issuer's own path, so
50
+ * `https://a.example/tenant` is read at `https://a.example/.well-known/oauth-authorization-server/tenant`.
51
+ */
52
+ function metadataUrl(issuer) {
53
+ const url = new URL(issuer);
54
+ const path = url.pathname === '/' ? '' : url.pathname.replace(/\/+$/, '');
55
+ url.pathname = `/.well-known/oauth-authorization-server${path}`;
56
+ return url.toString();
57
+ }
58
+ function problemWith(doc, issuer) {
59
+ // Identical, as §3.3 says: a redirect's `iss` is later held to this very string (RFC 9207).
60
+ if (doc.issuer !== issuer)
61
+ return 'the metadata names another issuer';
62
+ if (!isHttpsUrl(doc.authorization_endpoint) || !isHttpsUrl(doc.token_endpoint))
63
+ return 'an endpoint is not an https URL';
64
+ if (doc.code_challenge_methods_supported?.includes('S256') !== true)
65
+ return 'the server does not list PKCE S256';
66
+ return null;
67
+ }
68
+ //# sourceMappingURL=discovery.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"discovery.js","sourceRoot":"","sources":["../../src/auth/discovery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAA;AAEtC,OAAO,EAAE,aAAa,EAAE,UAAU,EAAwB,MAAM,gBAAgB,CAAA;AAchF,kGAAkG;AAClG,mDAAmD;AACnD,MAAM,SAAS,GAAG,CAAC,CAAC,WAAW,CAAC;IAC9B,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE;IAClB,sBAAsB,EAAE,CAAC,CAAC,MAAM,EAAE;IAClC,cAAc,EAAE,CAAC,CAAC,MAAM,EAAE;IAC1B,gCAAgC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,aAAa,EAAE;IACrE,8CAA8C,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,aAAa,EAAE;CAC5E,CAAC,CAAA;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,MAAc,EAAE,UAA2B,EAAE;IAC1E,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC;QAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,CAAA;IACvG,MAAM,IAAI,GAAG,MAAM,IAAI,CACrB,aAAa,CAAC,OAAO,CAAC,EAC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,IAAI,EAAE,CAAA;IAC/D,OAAO,GAAG,CAAC,QAAQ,EAAE,CAAA;AACvB,CAAC;AAED,SAAS,WAAW,CAAC,GAA8B,EAAE,MAAc;IACjE,4FAA4F;IAC5F,IAAI,GAAG,CAAC,MAAM,KAAK,MAAM;QAAE,OAAO,mCAAmC,CAAA;IACrE,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,sBAAsB,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,cAAc,CAAC;QAAE,OAAO,iCAAiC,CAAA;IACxH,IAAI,GAAG,CAAC,gCAAgC,EAAE,QAAQ,CAAC,MAAM,CAAC,KAAK,IAAI;QAAE,OAAO,oCAAoC,CAAA;IAChH,OAAO,IAAI,CAAA;AACb,CAAC"}
@@ -0,0 +1,5 @@
1
+ export { discover } from './discovery.js';
2
+ export type { AuthServer } from './discovery.js';
3
+ export { finishSignIn, startSignIn } from './signIn.js';
4
+ export type { CallbackError, PendingSignIn, SignInError, SignInRequest, SignInResult, Tokens } from './signIn.js';
5
+ export type { AuthCallOptions } from './transport.js';
@@ -0,0 +1,5 @@
1
+ // Sign-in (#63–#66) exports from here, so it never edits src/index.ts. For Flipstream's own apps
2
+ // for now (D424): each brings its own client ID and scopes (D421).
3
+ export { discover } from './discovery.js';
4
+ export { finishSignIn, startSignIn } from './signIn.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"AAAA,iGAAiG;AACjG,mEAAmE;AACnE,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAEzC,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA"}
@@ -0,0 +1,10 @@
1
+ /** base64url without padding (RFC 7636 §3, RFC 4648 §5). */
2
+ export declare function base64url(bytes: Uint8Array): string;
3
+ /**
4
+ * `byteLength` bytes from the platform's CSPRNG, base64url-encoded: the verifier (64 bytes, 86
5
+ * characters, inside RFC 7636's 43 to 128) and the state. Never a non-cryptographic generator:
6
+ * the verifier is a public client's whole defence against a stolen code.
7
+ */
8
+ export declare function randomValue(byteLength: number): string;
9
+ /** The S256 challenge: base64url(SHA-256(the verifier's ASCII bytes)). */
10
+ export declare function challengeOf(verifier: string): Promise<string>;
@@ -0,0 +1,25 @@
1
+ /*
2
+ * PKCE (RFC 7636) and the sign-in's random values, from Web Crypto alone, so the same code runs
3
+ * in browsers and in Node 20+, with no node:crypto in a bundle. Ported from the CLI's pkce.ts.
4
+ */
5
+ /** base64url without padding (RFC 7636 §3, RFC 4648 §5). */
6
+ export function base64url(bytes) {
7
+ let binary = '';
8
+ for (const byte of bytes)
9
+ binary += String.fromCharCode(byte);
10
+ return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
11
+ }
12
+ /**
13
+ * `byteLength` bytes from the platform's CSPRNG, base64url-encoded: the verifier (64 bytes, 86
14
+ * characters, inside RFC 7636's 43 to 128) and the state. Never a non-cryptographic generator:
15
+ * the verifier is a public client's whole defence against a stolen code.
16
+ */
17
+ export function randomValue(byteLength) {
18
+ return base64url(crypto.getRandomValues(new Uint8Array(byteLength)));
19
+ }
20
+ /** The S256 challenge: base64url(SHA-256(the verifier's ASCII bytes)). */
21
+ export async function challengeOf(verifier) {
22
+ const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(verifier));
23
+ return base64url(new Uint8Array(digest));
24
+ }
25
+ //# sourceMappingURL=pkce.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pkce.js","sourceRoot":"","sources":["../../src/auth/pkce.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,4DAA4D;AAC5D,MAAM,UAAU,SAAS,CAAC,KAAiB;IACzC,IAAI,MAAM,GAAG,EAAE,CAAA;IACf,KAAK,MAAM,IAAI,IAAI,KAAK;QAAE,MAAM,IAAI,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,CAAA;IAC7D,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAA;AAChF,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,UAAkB;IAC5C,OAAO,SAAS,CAAC,MAAM,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;AACtE,CAAC;AAED,0EAA0E;AAC1E,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,QAAgB;IAChD,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAA;IACxF,OAAO,SAAS,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC,CAAA;AAC1C,CAAC"}
@@ -0,0 +1,97 @@
1
+ import type { ApiError } from '../core/result.js';
2
+ import type { AuthServer } from './discovery.js';
3
+ import { type AuthCallOptions } from './transport.js';
4
+ /** What `startSignIn` takes. */
5
+ export interface SignInRequest {
6
+ /** The server, as `discover` gave it. */
7
+ readonly server: AuthServer;
8
+ /** The app's own OAuth client ID, never one a server announces (D421). */
9
+ readonly clientId: string;
10
+ /** Where the server sends the person back, as registered for the client. */
11
+ readonly redirectUri: string;
12
+ /** What the app asks for. At least one: there are no default scopes (D421). */
13
+ readonly scopes: readonly [string, ...string[]];
14
+ }
15
+ /**
16
+ * What finishing needs, held by the app between the two halves: only strings and a flag, so it
17
+ * survives a page load in the app's own storage. The verifier is a secret: never log it.
18
+ */
19
+ export interface PendingSignIn {
20
+ /** What the redirect must carry back (CSRF). */
21
+ readonly state: string;
22
+ /** The PKCE code verifier: the secret the code is exchanged with. */
23
+ readonly verifier: string;
24
+ readonly clientId: string;
25
+ readonly redirectUri: string;
26
+ /** The scopes asked for, space-separated (RFC 6749 §3.3). */
27
+ readonly scope: string;
28
+ /** The server's issuer, its token endpoint, and whether it puts `iss` on every redirect. */
29
+ readonly issuer: string;
30
+ readonly tokenEndpoint: string;
31
+ readonly issuerOnRedirect: boolean;
32
+ }
33
+ /** What a sign-in gives. The app stores them; the SDK keeps no copy. */
34
+ export interface Tokens {
35
+ /** For the transport's token function: never log it. */
36
+ readonly accessToken: string;
37
+ /** Absent when the server issued none. */
38
+ readonly refreshToken?: string;
39
+ /** When the access token expires, in ms since the epoch; absent when the server didn't say. */
40
+ readonly expiresAt?: number;
41
+ /** The scopes granted, space-separated. */
42
+ readonly scope: string;
43
+ }
44
+ /** Why the redirect the person came back on was not taken. Nothing was sent for any of them. */
45
+ export type CallbackError =
46
+ /** Its `state` isn't this sign-in's: another sign-in's redirect, or a forged one. */
47
+ {
48
+ readonly kind: 'wrong_state';
49
+ }
50
+ /** Its `iss` names another server, or is missing though the server sends one (RFC 9207). */
51
+ | {
52
+ readonly kind: 'wrong_issuer';
53
+ }
54
+ /** The server ended the sign-in (RFC 6749 §4.1.2.1): `access_denied` when the person declined. */
55
+ | {
56
+ readonly kind: 'provider_error';
57
+ readonly code: string;
58
+ readonly description?: string;
59
+ }
60
+ /** No code and no error. */
61
+ | {
62
+ readonly kind: 'no_code';
63
+ };
64
+ /** Why a sign-in didn't finish: the redirect's (`CallbackError`), or the exchange's (`ApiError`). */
65
+ export type SignInError = ApiError | CallbackError;
66
+ /** What `finishSignIn` gives: the tokens, or one failure with one meaning (D412). */
67
+ export type SignInResult = {
68
+ readonly ok: true;
69
+ readonly data: Tokens;
70
+ } | {
71
+ readonly ok: false;
72
+ readonly error: SignInError;
73
+ };
74
+ /**
75
+ * The URL to send the person to, and what finishing will need. Rejects with a TypeError, before
76
+ * anything is made, when the client ID is missing or empty, the redirect URI isn't absolute, or
77
+ * the scopes are empty or one holds a space: from TypeScript, none of these compiles. Always S256;
78
+ * no `resource` is sent, since the hosts share one audience.
79
+ */
80
+ export declare function startSignIn(request: SignInRequest): Promise<{
81
+ readonly url: string;
82
+ readonly pending: PendingSignIn;
83
+ }>;
84
+ /**
85
+ * The redirect, read in this order: its state first, so a forged one learns nothing; then its
86
+ * issuer, then the server's error, then the code. Pure: nothing is sent.
87
+ */
88
+ export declare function readCallback(callbackUrl: string | URL, pending: PendingSignIn): {
89
+ readonly kind: 'code';
90
+ readonly code: string;
91
+ } | CallbackError;
92
+ /**
93
+ * Finishes a sign-in: the redirect is read (`readCallback`), and only then is its code exchanged
94
+ * for tokens, once, never retried, since a code is good once. A `pending` that isn't one (from
95
+ * storage the app couldn't read back) is `refused`, with nothing sent.
96
+ */
97
+ export declare function finishSignIn(callbackUrl: string | URL, pending: PendingSignIn, options?: AuthCallOptions): Promise<SignInResult>;
@@ -0,0 +1,133 @@
1
+ import { z } from 'zod';
2
+ import { call } from '../core/call.js';
3
+ import { challengeOf, randomValue } from './pkce.js';
4
+ import { authTransport, isHttpsUrl } from './transport.js';
5
+ const nonEmpty = (value) => typeof value === 'string' && value !== '';
6
+ /**
7
+ * The URL to send the person to, and what finishing will need. Rejects with a TypeError, before
8
+ * anything is made, when the client ID is missing or empty, the redirect URI isn't absolute, or
9
+ * the scopes are empty or one holds a space: from TypeScript, none of these compiles. Always S256;
10
+ * no `resource` is sent, since the hosts share one audience.
11
+ */
12
+ export async function startSignIn(request) {
13
+ const { server, clientId, redirectUri, scopes } = request;
14
+ if (!nonEmpty(clientId))
15
+ throw new TypeError("startSignIn: clientId is required: the app's own OAuth client ID");
16
+ if (!nonEmpty(redirectUri) || !URL.canParse(redirectUri))
17
+ throw new TypeError('startSignIn: redirectUri must be an absolute URL');
18
+ if (!Array.isArray(scopes) || scopes.length === 0 || !scopes.every((scope) => nonEmpty(scope) && !/\s/.test(scope))) {
19
+ throw new TypeError('startSignIn: scopes must be one or more scope names, none with a space');
20
+ }
21
+ const state = randomValue(32);
22
+ const verifier = randomValue(64);
23
+ const scope = scopes.join(' ');
24
+ const url = new URL(server.authorizationEndpoint);
25
+ url.searchParams.set('response_type', 'code');
26
+ url.searchParams.set('client_id', clientId);
27
+ url.searchParams.set('redirect_uri', redirectUri);
28
+ url.searchParams.set('scope', scope);
29
+ url.searchParams.set('state', state);
30
+ url.searchParams.set('code_challenge', await challengeOf(verifier));
31
+ url.searchParams.set('code_challenge_method', 'S256');
32
+ return {
33
+ url: url.toString(),
34
+ pending: {
35
+ state,
36
+ verifier,
37
+ clientId,
38
+ redirectUri,
39
+ scope,
40
+ issuer: server.issuer,
41
+ tokenEndpoint: server.tokenEndpoint,
42
+ issuerOnRedirect: server.issuerOnRedirect,
43
+ },
44
+ };
45
+ }
46
+ /**
47
+ * The redirect, read in this order: its state first, so a forged one learns nothing; then its
48
+ * issuer, then the server's error, then the code. Pure: nothing is sent.
49
+ */
50
+ export function readCallback(callbackUrl, pending) {
51
+ const params = new URL(callbackUrl).searchParams;
52
+ if (params.get('state') !== pending.state)
53
+ return { kind: 'wrong_state' };
54
+ const iss = params.get('iss');
55
+ // Compared as strings, as RFC 9207 §2.4 says: the issuer exactly as its metadata names it.
56
+ if (iss === null ? pending.issuerOnRedirect : iss !== pending.issuer)
57
+ return { kind: 'wrong_issuer' };
58
+ const error = params.get('error');
59
+ if (error !== null) {
60
+ const description = params.get('error_description');
61
+ return description === null ? { kind: 'provider_error', code: error } : { kind: 'provider_error', code: error, description };
62
+ }
63
+ const code = params.get('code');
64
+ return nonEmpty(code) ? { kind: 'code', code } : { kind: 'no_code' };
65
+ }
66
+ const zPending = z.object({
67
+ state: z.string().min(1),
68
+ verifier: z.string().min(43).max(128),
69
+ clientId: z.string().min(1),
70
+ redirectUri: z.string().min(1),
71
+ scope: z.string().min(1),
72
+ issuer: z.string().min(1),
73
+ tokenEndpoint: z.string().refine(isHttpsUrl),
74
+ issuerOnRedirect: z.boolean(),
75
+ });
76
+ // RFC 6749 §5.1. Kept loose: the server's own fields (`id_token`, `refresh_expires_in`) pass by.
77
+ const zTokenAnswer = z.looseObject({
78
+ access_token: z.string().min(1),
79
+ token_type: z.string().refine((type) => type.toLowerCase() === 'bearer', 'the token type is not bearer'),
80
+ expires_in: z.int().nonnegative().exactOptional(),
81
+ refresh_token: z.string().exactOptional(),
82
+ scope: z.string().exactOptional(),
83
+ });
84
+ /**
85
+ * Finishes a sign-in: the redirect is read (`readCallback`), and only then is its code exchanged
86
+ * for tokens, once, never retried, since a code is good once. A `pending` that isn't one (from
87
+ * storage the app couldn't read back) is `refused`, with nothing sent.
88
+ */
89
+ export async function finishSignIn(callbackUrl, pending, options = {}) {
90
+ if (!zPending.safeParse(pending).success || !URL.canParse(callbackUrl))
91
+ return { ok: false, error: { kind: 'refused' } };
92
+ const callback = readCallback(callbackUrl, pending);
93
+ if (callback.kind !== 'code')
94
+ return { ok: false, error: callback };
95
+ const form = new URLSearchParams({
96
+ grant_type: 'authorization_code',
97
+ code: callback.code,
98
+ redirect_uri: pending.redirectUri,
99
+ client_id: pending.clientId,
100
+ code_verifier: pending.verifier,
101
+ });
102
+ // Before the request, so the expiry is never later than the server's own.
103
+ const sentAt = Date.now();
104
+ const answer = await call(authTransport(options), {
105
+ effect: 'write',
106
+ service: 'auth',
107
+ endpoint: 'auth.token',
108
+ makeRequest: () => new Request(pending.tokenEndpoint, {
109
+ method: 'POST',
110
+ headers: { accept: 'application/json', 'content-type': 'application/x-www-form-urlencoded' },
111
+ body: form.toString(),
112
+ }),
113
+ schema: zTokenAnswer,
114
+ deadlineMs: 30_000,
115
+ retry: 'never',
116
+ }, options.signal);
117
+ if (!answer.ok)
118
+ return answer;
119
+ const { access_token, expires_in, refresh_token, scope } = answer.data;
120
+ return {
121
+ ok: true,
122
+ data: {
123
+ accessToken: access_token,
124
+ // pulse-auth sends "" for "no refresh token" (its model's default): that is none, not a token.
125
+ ...(nonEmpty(refresh_token) ? { refreshToken: refresh_token } : {}),
126
+ ...(expires_in === undefined ? {} : { expiresAt: sentAt + expires_in * 1000 }),
127
+ // RFC 6749 §5.1: a server leaves `scope` out when it granted what was asked. pulse-auth
128
+ // always leaves it out.
129
+ scope: scope ?? pending.scope,
130
+ },
131
+ };
132
+ }
133
+ //# sourceMappingURL=signIn.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signIn.js","sourceRoot":"","sources":["../../src/auth/signIn.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAA;AAGtC,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AACpD,OAAO,EAAE,aAAa,EAAE,UAAU,EAAwB,MAAM,gBAAgB,CAAA;AAoEhF,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAmB,EAAE,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,EAAE,CAAA;AAE/F;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,OAAsB;IACtD,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE,GAAG,OAAO,CAAA;IACzD,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC;QAAE,MAAM,IAAI,SAAS,CAAC,kEAAkE,CAAC,CAAA;IAChH,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,WAAW,CAAC;QAAE,MAAM,IAAI,SAAS,CAAC,kDAAkD,CAAC,CAAA;IACjI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;QACpH,MAAM,IAAI,SAAS,CAAC,wEAAwE,CAAC,CAAA;IAC/F,CAAC;IACD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AAE,YAAY,EAAE,aAAa,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YACnE,GAAG,CAAC,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,EAAE,CAAC;YAC9E,wFAAwF;YACxF,wBAAwB;YACxB,KAAK,EAAE,KAAK,IAAI,OAAO,CAAC,KAAK;SAC9B;KACF,CAAA;AACH,CAAC"}
@@ -0,0 +1,18 @@
1
+ import type { Transport } from '../core/transport.js';
2
+ /** What every sign-in call takes. */
3
+ export interface AuthCallOptions {
4
+ /** Ends the call at once: its answer is `aborted`. */
5
+ readonly signal?: AbortSignal | undefined;
6
+ /** The app's own fetch, called as a plain function; the global one when it is undefined. */
7
+ readonly fetch?: typeof fetch | undefined;
8
+ }
9
+ /**
10
+ * An absolute https URL a Request can be made for: one with a user or password in it is refused
11
+ * by fetch, so it is no endpoint either.
12
+ */
13
+ export declare function isHttpsUrl(value: string): boolean;
14
+ /**
15
+ * Sign-in's transport: https only, and no token, since these calls are how a token is had. It
16
+ * sends as the default transport does (`sendRequest`): no cookies, and no redirect followed.
17
+ */
18
+ export declare function authTransport(options: AuthCallOptions): Transport;
@@ -0,0 +1,26 @@
1
+ import { sendRequest } from '../core/fetch.js';
2
+ /**
3
+ * An absolute https URL a Request can be made for: one with a user or password in it is refused
4
+ * by fetch, so it is no endpoint either.
5
+ */
6
+ export function isHttpsUrl(value) {
7
+ if (!URL.canParse(value))
8
+ return false;
9
+ const url = new URL(value);
10
+ return url.protocol === 'https:' && url.username === '' && url.password === '';
11
+ }
12
+ /**
13
+ * Sign-in's transport: https only, and no token, since these calls are how a token is had. It
14
+ * sends as the default transport does (`sendRequest`): no cookies, and no redirect followed.
15
+ */
16
+ export function authTransport(options) {
17
+ const send = options.fetch ?? globalThis.fetch;
18
+ return async (call) => {
19
+ const built = call.makeRequest();
20
+ if (new URL(built.url).protocol !== 'https:')
21
+ return { kind: 'refused', reason: 'destination' };
22
+ const sent = await sendRequest(send, built, new Headers(built.headers), call.signal);
23
+ return sent.kind === 'sent' ? { kind: 'response', response: sent.response } : sent;
24
+ };
25
+ }
26
+ //# sourceMappingURL=transport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transport.js","sourceRoot":"","sources":["../../src/auth/transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAW9C;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,KAAa;IACtC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAA;IACtC,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAA;IAC1B,OAAO,GAAG,CAAC,QAAQ,KAAK,QAAQ,IAAI,GAAG,CAAC,QAAQ,KAAK,EAAE,IAAI,GAAG,CAAC,QAAQ,KAAK,EAAE,CAAA;AAChF,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,OAAwB;IACpD,MAAM,IAAI,GAAG,OAAO,CAAC,KAAK,IAAI,UAAU,CAAC,KAAK,CAAA;IAC9C,OAAO,KAAK,EAAE,IAAI,EAAE,EAAE;QACpB,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,EAAE,CAAA;QAChC,IAAI,IAAI,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,QAAQ,KAAK,QAAQ;YAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,aAAa,EAAE,CAAA;QAC/F,MAAM,IAAI,GAAG,MAAM,WAAW,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,CAAA;QACpF,OAAO,IAAI,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,IAAI,CAAA;IACpF,CAAC,CAAA;AACH,CAAC"}
@@ -0,0 +1,14 @@
1
+ import type { Said } from './result.js';
2
+ import type { ServiceName } from './transport.js';
3
+ export type Json = {
4
+ readonly ok: true;
5
+ readonly value: unknown;
6
+ } | {
7
+ readonly ok: false;
8
+ };
9
+ export declare function parseJson(text: string): Json;
10
+ /**
11
+ * What a failure status from `service` came with: the first of its envelopes that reads the body,
12
+ * or the body as it came.
13
+ */
14
+ export declare function said(text: string, service: ServiceName): Said;
@@ -0,0 +1,69 @@
1
+ import { zErrorBody } from '../spec/query-planner/zod.gen.js';
2
+ export function parseJson(text) {
3
+ try {
4
+ return { ok: true, value: JSON.parse(text) };
5
+ }
6
+ catch {
7
+ return { ok: false };
8
+ }
9
+ }
10
+ /**
11
+ * A code is SCREAMING_SNAKE, as the CLI checks it (its `planner/errors.ts`): anything else isn't
12
+ * a host's code, gets no trust, and leaves the body read as text. It keeps the field bounded, so it
13
+ * can't carry a token or terminal escapes to wherever a caller prints it.
14
+ */
15
+ const CODE_SHAPE = /^[A-Z][A-Z0-9_]{0,63}$/;
16
+ /** A message is capped, by code point so the cap never splits a character in two. */
17
+ const MESSAGE_MAX = 500;
18
+ const capped = (message) => (message.length <= MESSAGE_MAX ? message : Array.from(message).slice(0, MESSAGE_MAX).join(''));
19
+ /** The planner's `{ code, message }`, its spec's ErrorBody (D429), with a code of a code's shape. */
20
+ const planner = (body) => {
21
+ const read = zErrorBody.safeParse(body);
22
+ if (!read.success || !CODE_SHAPE.test(read.data.code))
23
+ return null;
24
+ return { code: read.data.code, message: capped(read.data.message) };
25
+ };
26
+ /**
27
+ * An OAuth code is lowercase (`invalid_grant`, RFC 6749 §5.2), so it has its own bound, kept as
28
+ * tight as a planner code's for the same reason.
29
+ */
30
+ const OAUTH_CODE_SHAPE = /^[a-z][a-z0-9_]{0,63}$/;
31
+ /**
32
+ * OAuth's `{ error, error_description? }` (RFC 6749 §5.2), as pulse-auth's sign-in and token
33
+ * endpoints send it (D442). Written from the RFC, because pulse-auth declares no error schema; a
34
+ * declared one would replace this. Its codes stay raw strings, with no typed list until a caller
35
+ * needs one. A missing description reads as an empty message.
36
+ */
37
+ const oauth = (body) => {
38
+ if (typeof body !== 'object' || body === null || Array.isArray(body))
39
+ return null;
40
+ const error = 'error' in body ? body.error : undefined;
41
+ const description = 'error_description' in body ? body.error_description : undefined;
42
+ if (typeof error !== 'string' || !OAUTH_CODE_SHAPE.test(error))
43
+ return null;
44
+ if (description !== undefined && typeof description !== 'string')
45
+ return null;
46
+ return { code: error, message: capped(description ?? '') };
47
+ };
48
+ /**
49
+ * Each service's envelopes (D442): a host's error body is read only by the envelope that host
50
+ * sends, so a proxy's `{"error":"not_found"}` on the query host is never taken for an OAuth code.
51
+ * #91's `{ error: { code, message, request_id } }` joins `api` from its own spec (SD37).
52
+ */
53
+ const ENVELOPES = { query: [planner], auth: [oauth], api: [] };
54
+ /**
55
+ * What a failure status from `service` came with: the first of its envelopes that reads the body,
56
+ * or the body as it came.
57
+ */
58
+ export function said(text, service) {
59
+ const json = parseJson(text);
60
+ if (json.ok) {
61
+ for (const read of ENVELOPES[service]) {
62
+ const error = read(json.value);
63
+ if (error !== null)
64
+ return error;
65
+ }
66
+ }
67
+ return { text };
68
+ }
69
+ //# sourceMappingURL=body.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"body.js","sourceRoot":"","sources":["../../src/core/body.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAA;AAQ7D,MAAM,UAAU,SAAS,CAAC,IAAY;IACpC,IAAI,CAAC;QACH,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAA;IAC9C,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,CAAA;IACtB,CA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AC,CAAC,CAAC,SAAS,CAAA;IAC7F,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,CAAA;IAC3E,IAAI,WAAW,KAAK,SAAS,IAAI,OAAO,WAAW,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAA;IAC7E,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,WAAW,IAAI,EAAE,CAAC,EAAE,CAAA;AAC5D,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,SAAS,GAAuD,EAAE,KAAK,EAAE,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,CAAC,KAAK,CAAC,EAAE,GAAG,EAAE,EAAE,EAAE,CAAA;AAElH;;;GAGG;AACH,MAAM,UAAU,IAAI,CAAC,IAAY,EAAE,OAAoB;IACrD,MAAM,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC,CAAA;IAC5B,IAAI,IAAI,CAAC,EAAE,EAAE,CAAC;QACZ,KAAK,MAAM,IAAI,IAAI,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC;YACtC,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;YAC9B,IAAI,KAAK,KAAK,IAAI;gBAAE,OAAO,KAAK,CAAA;QAClC,CAAC;IACH,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,CAAA;AACjB,CAAC"}
@@ -0,0 +1,17 @@
1
+ import type { z } from 'zod';
2
+ import { type ApiResult } from './result.js';
3
+ import { type RetryRule } from './retry.js';
4
+ import type { ApiRequest, Transport } from './transport.js';
5
+ /** One endpoint's call: what it sends, and the rules it is sent under, declared next to its schema. */
6
+ export interface CallSpec<T> extends Omit<ApiRequest, 'signal'> {
7
+ readonly schema: z.ZodType<T>;
8
+ /** How long the whole call may take: every attempt, every wait and the reading of the answer. */
9
+ readonly deadlineMs: number;
10
+ readonly retry: RetryRule;
11
+ }
12
+ /**
13
+ * One call: attempts under its rule, every attempt, wait and read under one signal that joins the
14
+ * caller's with the deadline. Whichever fires first decides the answer, `timeout` or `aborted`
15
+ * (#15), never mixed. The call ends when it fires, whatever the transport does with the signal.
16
+ */
17
+ export declare function call<T>(transport: Transport, spec: CallSpec<T>, signal?: AbortSignal): Promise<ApiResult<T>>;