@myfield/sdk 0.9.0 → 0.9.2
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/CHANGELOG.md +25 -0
- package/README.md +34 -54
- package/dist/index.js +1964 -674
- package/docs/API.md +145 -57
- package/docs/BUCKET-SPEC.md +33 -13
- package/package.json +4 -2
- package/src/auth/authorization.ts +254 -0
- package/src/auth/flow.ts +93 -0
- package/src/auth/identifiers.ts +46 -0
- package/src/auth/instance.ts +33 -514
- package/src/auth/keys.ts +73 -0
- package/src/auth/proof.ts +26 -0
- package/src/auth/session.ts +16 -0
- package/src/auth/store.ts +90 -0
- package/src/auth/types.ts +47 -0
- package/src/bucket/client.ts +79 -36
- package/src/bucket/format.ts +67 -18
- package/src/bucket/store.ts +31 -0
- package/src/bucket/transport.ts +73 -11
- package/src/errors.ts +31 -0
- package/src/facade.ts +337 -330
- package/src/index.ts +6 -0
- package/src/remote/bucket.ts +180 -0
- package/src/remote/client.ts +38 -0
- package/src/remote/connection.ts +529 -0
- package/src/remote/feed.ts +80 -0
- package/src/remote/http.ts +78 -0
- package/src/remote/session.ts +31 -0
- package/types/index.d.ts +85 -1
package/src/auth/flow.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { InstanceAuthClient } from "./instance.ts";
|
|
2
|
+
import type { StorageAdapter } from "../bucket/store.ts";
|
|
3
|
+
import { ClientError } from "../errors.ts";
|
|
4
|
+
import { authRecordKeys } from "./store.ts";
|
|
5
|
+
import { bucketResourceId } from "./identifiers.ts";
|
|
6
|
+
|
|
7
|
+
const notAuthorized = (message: string, cause?: unknown) => new ClientError("NOT_AUTHORIZED", message, { cause });
|
|
8
|
+
|
|
9
|
+
export function createAuthorizationFlow(deps: { getAppId(): string; getBucketId?(): string; persistent?: boolean }) {
|
|
10
|
+
const getBucketId = deps.getBucketId ?? deps.getAppId;
|
|
11
|
+
let closeAuthorization = () => {};
|
|
12
|
+
async function ownCallback(url: URL, storage: StorageAdapter): Promise<boolean> {
|
|
13
|
+
const resourceId = bucketResourceId(getBucketId());
|
|
14
|
+
const requestId = url.searchParams.get("mf_request_id");
|
|
15
|
+
if (!requestId) return false;
|
|
16
|
+
const keys = authRecordKeys(deps.getAppId());
|
|
17
|
+
const pointer = await storage.get<{ resourceId: string; pendingKey: string; requestId: string }>(keys.callback(requestId));
|
|
18
|
+
if (pointer?.resourceId !== resourceId || pointer.requestId !== requestId) return false;
|
|
19
|
+
const pending = await storage.get<{ resourceId: string }>(pointer.pendingKey);
|
|
20
|
+
if (pending && pending.resourceId !== resourceId) return false;
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function clearCallbackUrl(url: URL) {
|
|
25
|
+
const current = new URL(window.location.href);
|
|
26
|
+
if (current.searchParams.get("mf_request_id") !== url.searchParams.get("mf_request_id")) return;
|
|
27
|
+
for (const key of ["state", "error", "error_description", "error_uri", "mf_request_id", "mf_result"]) current.searchParams.delete(key);
|
|
28
|
+
const hash = new URLSearchParams(current.hash.slice(1));
|
|
29
|
+
if (hash.has("mf_auth_result")) { hash.delete("mf_auth_result"); current.hash = hash.toString(); }
|
|
30
|
+
window.history.replaceState(null, "", current.href);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function handleCallback(auth: InstanceAuthClient, url: URL, signal: AbortSignal, addressBar: boolean) {
|
|
34
|
+
try {
|
|
35
|
+
const result = await auth.handleCallback(url.href);
|
|
36
|
+
signal.throwIfAborted();
|
|
37
|
+
if (result?.result !== "approved") throw notAuthorized(result?.errorDescription || result?.result || "Invalid authorization callback");
|
|
38
|
+
} catch (error) {
|
|
39
|
+
signal.throwIfAborted();
|
|
40
|
+
if (error instanceof ClientError && error.code === "NOT_AUTHORIZED") throw error;
|
|
41
|
+
throw notAuthorized("Invalid authorization callback", error);
|
|
42
|
+
} finally {
|
|
43
|
+
if (addressBar) clearCallbackUrl(url);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function requestAuthorization(auth: InstanceAuthClient, storage: StorageAdapter, url: string, signal: AbortSignal, setPopup: (popup: Window | null) => void): Promise<void> {
|
|
48
|
+
signal.throwIfAborted();
|
|
49
|
+
const ios = typeof navigator !== "undefined" && (/iPad|iPhone|iPod/.test(navigator.userAgent)
|
|
50
|
+
|| (navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1));
|
|
51
|
+
const popup = ios ? null : window.open("about:blank", `myfield-sdk-auth:${getBucketId()}`, "popup=yes,width=520,height=720");
|
|
52
|
+
if (!popup && deps.persistent === false) throw new ClientError("INVALID_STATE", "Redirect authorization requires persistent storage");
|
|
53
|
+
setPopup(popup);
|
|
54
|
+
return new Promise<void>((resolve, reject) => {
|
|
55
|
+
let processing = false;
|
|
56
|
+
let settled = false;
|
|
57
|
+
let timer: ReturnType<typeof setInterval> | undefined;
|
|
58
|
+
const finish = (error?: unknown) => {
|
|
59
|
+
if (settled) return;
|
|
60
|
+
settled = true;
|
|
61
|
+
if (timer) clearInterval(timer);
|
|
62
|
+
window.removeEventListener("message", onMessage);
|
|
63
|
+
signal.removeEventListener("abort", onAbort);
|
|
64
|
+
popup?.close();
|
|
65
|
+
closeAuthorization = () => {};
|
|
66
|
+
if (error) reject(error); else resolve();
|
|
67
|
+
};
|
|
68
|
+
const onAbort = () => finish(signal.reason);
|
|
69
|
+
const onMessage = (event: MessageEvent) => {
|
|
70
|
+
if (!popup || event.source !== popup || ![new URL(url).origin, window.location.origin].includes(event.origin)) return;
|
|
71
|
+
const message = event.data as { type?: string; callbackUrl?: string } | null;
|
|
72
|
+
if (!message || message.type !== "myfield.auth.callback" || typeof message.callbackUrl !== "string") return;
|
|
73
|
+
void (async () => {
|
|
74
|
+
let callbackUrl: URL;
|
|
75
|
+
try { callbackUrl = new URL(message.callbackUrl!); } catch { return; }
|
|
76
|
+
if (!await ownCallback(callbackUrl, storage) || processing || settled) return;
|
|
77
|
+
processing = true;
|
|
78
|
+
await handleCallback(auth, callbackUrl, signal, false);
|
|
79
|
+
finish();
|
|
80
|
+
})().catch(finish);
|
|
81
|
+
};
|
|
82
|
+
window.addEventListener("message", onMessage);
|
|
83
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
84
|
+
closeAuthorization = onAbort;
|
|
85
|
+
if (popup) timer = setInterval(() => {
|
|
86
|
+
if (popup.closed && !processing) finish(notAuthorized("Authorization window closed"));
|
|
87
|
+
}, 250);
|
|
88
|
+
void auth.authorize({ bucketId: getBucketId() }).catch(finish);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return { ownCallback, handleCallback, requestAuthorization, close: () => closeAuthorization() };
|
|
93
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ClientError } from "../errors.ts";
|
|
2
|
+
|
|
3
|
+
export function isValidBucketId(value: unknown): value is string {
|
|
4
|
+
const safe = typeof value === "string" ? value.trim() : "";
|
|
5
|
+
return /^[a-z0-9](?:[a-z0-9.-]{0,61}[a-z0-9])?$/.test(safe) && !safe.includes("..");
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function requireValidBucketId(value: unknown): string {
|
|
9
|
+
if (!isValidBucketId(value)) throw new Error("invalid bucketId");
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function isValidInstanceId(value: unknown): value is string {
|
|
14
|
+
return typeof value === "string" && /^inst_[a-f0-9]{32}$/.test(value);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function isValidMfid(value: unknown): value is string {
|
|
18
|
+
return typeof value === "string" && /^[a-f0-9]{32}$/.test(value);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function isPublicP256EcJwk(value: unknown): value is JsonWebKey {
|
|
22
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
23
|
+
const jwk = value as JsonWebKey;
|
|
24
|
+
return jwk.kty === "EC" && jwk.crv === "P-256" && typeof jwk.x === "string" && jwk.x.length > 0 && typeof jwk.y === "string" && jwk.y.length > 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function bucketResourceId(bucketId: string): string {
|
|
28
|
+
return `urn:myfield:bucket:${bucketId}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function parseBucketResourceId(resourceId: unknown): { resourceId: string; bucketId: string } | null {
|
|
32
|
+
const safe = typeof resourceId === "string" ? resourceId.trim() : "";
|
|
33
|
+
const prefix = "urn:myfield:bucket:";
|
|
34
|
+
if (!safe.startsWith(prefix)) return null;
|
|
35
|
+
const bucketId = safe.slice(prefix.length);
|
|
36
|
+
return isValidBucketId(bucketId) ? { resourceId: safe, bucketId } : null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function resolveAppId(value?: string): string {
|
|
40
|
+
const appId = value === undefined ? (typeof window === "undefined" ? "" : window.location.hostname) : value;
|
|
41
|
+
if (typeof appId !== "string" || appId !== appId.trim() || !isValidBucketId(appId)
|
|
42
|
+
|| !appId.split(".").every(label => /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(label))) {
|
|
43
|
+
throw new ClientError("INVALID_INPUT", "appId must be a lowercase domain-form identifier of 1–63 characters");
|
|
44
|
+
}
|
|
45
|
+
return appId;
|
|
46
|
+
}
|