@seed-hypermedia/client 0.0.68 → 0.0.70
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/LICENSE +201 -0
- package/dist/auth.d.ts +79 -0
- package/dist/auth.mjs +266 -0
- package/dist/auto-link.d.ts +3 -2
- package/dist/auto-link.mjs +21 -0
- package/dist/base64.mjs +9 -0
- package/dist/blobs.d.ts +170 -0
- package/dist/blobs.mjs +51 -0
- package/dist/block-diff.mjs +17 -0
- package/dist/blocks-to-markdown.mjs +22 -0
- package/dist/capability.d.ts +7 -2
- package/dist/capability.mjs +14 -0
- package/dist/cbor.d.ts +13 -0
- package/dist/cbor.mjs +13 -0
- package/dist/change.d.ts +8 -7
- package/dist/change.mjs +24 -0
- package/dist/chunk-2MD3FXKB.mjs +14 -0
- package/dist/chunk-37WRGUK4.mjs +125 -0
- package/dist/chunk-3KESF5XJ.mjs +297 -0
- package/dist/chunk-3TPCJCVO.mjs +24 -0
- package/dist/chunk-4Q3SAL5R.mjs +75 -0
- package/dist/chunk-52P3E6H6.mjs +459 -0
- package/dist/chunk-BUPMCRR4.mjs +48 -0
- package/dist/chunk-CBELM7KX.mjs +29 -0
- package/dist/{chunk-KCR3277A.mjs → chunk-DBHL3BYB.mjs} +9 -14
- package/dist/chunk-DR3HJ6BV.mjs +25 -0
- package/dist/chunk-EU36RFR3.mjs +159 -0
- package/dist/chunk-EY6RQVAP.mjs +138 -0
- package/dist/chunk-H7L4TI6X.mjs +705 -0
- package/dist/chunk-HBIVZA6C.mjs +102 -0
- package/dist/chunk-IT5LLTMP.mjs +209 -0
- package/dist/chunk-JT57MV7Y.mjs +136 -0
- package/dist/chunk-KO4XH5L2.mjs +475 -0
- package/dist/chunk-LVAHHQIK.mjs +41 -0
- package/dist/chunk-LYECQKZE.mjs +49 -0
- package/dist/chunk-N3P7S5YZ.mjs +118 -0
- package/dist/chunk-NPG76WUS.mjs +348 -0
- package/dist/chunk-NYWCMQTH.mjs +412 -0
- package/dist/chunk-OZKQOI2X.mjs +334 -0
- package/dist/chunk-PJ2RFT5A.mjs +113 -0
- package/dist/chunk-PPAY4WZX.mjs +40 -0
- package/dist/chunk-S2PVJK2P.mjs +517 -0
- package/dist/chunk-SHOAYNJY.mjs +289 -0
- package/dist/chunk-T5BTAJPW.mjs +725 -0
- package/dist/{chunk-EGCUJ6D6.mjs → chunk-UOL3F6XQ.mjs} +0 -12
- package/dist/chunk-VN7F33SS.mjs +108 -0
- package/dist/chunk-YNVFRCUH.mjs +171 -0
- package/dist/client.d.ts +3 -2
- package/dist/client.mjs +15 -0
- package/dist/comment.d.ts +5 -4
- package/dist/comment.mjs +19 -0
- package/dist/contact.d.ts +5 -4
- package/dist/contact.mjs +15 -0
- package/dist/document-state.mjs +8 -0
- package/dist/editor-types.mjs +0 -0
- package/dist/editorblock-to-hmblock.mjs +11 -0
- package/dist/encryption.mjs +13 -0
- package/dist/errors.mjs +11 -0
- package/dist/file-to-ipfs.mjs +13 -0
- package/dist/grobid.mjs +11 -0
- package/dist/hm-resolver.mjs +10 -0
- package/dist/hmauth.d.ts +169 -0
- package/dist/hmauth.mjs +53 -0
- package/dist/hmblock-to-editorblock.mjs +12 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +132 -5453
- package/dist/keyfile.mjs +19 -0
- package/dist/markdown-to-blocks.mjs +15 -0
- package/dist/pdf-to-blocks-embedded.mjs +7 -0
- package/dist/pdf-to-blocks.mjs +11 -0
- package/dist/ref.d.ts +5 -4
- package/dist/ref.mjs +13 -0
- package/dist/resource-read.mjs +15 -0
- package/dist/signer.d.ts +42 -0
- package/dist/signer.mjs +9 -0
- package/dist/signing.d.ts +10 -2
- package/dist/signing.mjs +16 -0
- package/dist/tei-to-blocks.mjs +8 -0
- package/dist/unicode.mjs +19 -0
- package/dist/vault-local.mjs +10 -6
- package/dist/vault.d.ts +14 -4
- package/dist/vault.mjs +6 -2
- package/package.json +18 -28
- package/src/index.ts +0 -144
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2024 Mintter Inc.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-side "Sign in with Seed" session management.
|
|
3
|
+
*
|
|
4
|
+
* Implements the client half of the Hypermedia Auth Protocol (see `./hmauth`):
|
|
5
|
+
* a site generates a non-extractable Ed25519 session key, redirects the user to
|
|
6
|
+
* their Vault to authorize it, and receives back a signed Capability delegating
|
|
7
|
+
* the `AGENT` role to that session key. The account's own key never leaves the
|
|
8
|
+
* Vault.
|
|
9
|
+
*
|
|
10
|
+
* Browser-only: requires WebCrypto Ed25519, IndexedDB (unless a custom store is
|
|
11
|
+
* supplied), and `CompressionStream`/`DecompressionStream`. A secure context
|
|
12
|
+
* (https, or localhost) is required for `crypto.subtle`.
|
|
13
|
+
*/
|
|
14
|
+
import type { HMSigner } from './hm-types';
|
|
15
|
+
import { type AuthResult, type HypermediaAuthConfig, type StoredSession } from './hmauth';
|
|
16
|
+
/** A session record as persisted between the redirect out and the callback back. */
|
|
17
|
+
export interface AuthSessionRecord {
|
|
18
|
+
keyPair: CryptoKeyPair;
|
|
19
|
+
publicKeyRaw: Uint8Array;
|
|
20
|
+
principal: string;
|
|
21
|
+
vaultUrl: string;
|
|
22
|
+
createTime: number;
|
|
23
|
+
/** Pending CSRF state, cleared once the callback is consumed. */
|
|
24
|
+
authState: string | null;
|
|
25
|
+
authStartTime: number | null;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Persistence for pending and active sessions, keyed by vault URL.
|
|
29
|
+
*
|
|
30
|
+
* The default implementation uses IndexedDB, which is required to persist a
|
|
31
|
+
* non-extractable `CryptoKey` across the redirect — `localStorage` cannot hold
|
|
32
|
+
* one, and exporting the key would defeat the point.
|
|
33
|
+
*/
|
|
34
|
+
export interface AuthSessionStore {
|
|
35
|
+
get(vaultUrl: string): Promise<AuthSessionRecord | null>;
|
|
36
|
+
put(vaultUrl: string, record: AuthSessionRecord): Promise<void>;
|
|
37
|
+
delete(vaultUrl: string): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
/** The default IndexedDB-backed session store. */
|
|
40
|
+
export declare function indexedDBSessionStore(): AuthSessionStore;
|
|
41
|
+
/** Options accepted by every entry point in this module. */
|
|
42
|
+
export interface AuthOptions {
|
|
43
|
+
/** Override where sessions are persisted. Defaults to IndexedDB. */
|
|
44
|
+
store?: AuthSessionStore;
|
|
45
|
+
}
|
|
46
|
+
/** Generate a non-extractable Ed25519 session key pair using WebCrypto. */
|
|
47
|
+
export declare function generateSessionKey(): Promise<{
|
|
48
|
+
keyPair: CryptoKeyPair;
|
|
49
|
+
publicKeyRaw: Uint8Array;
|
|
50
|
+
principal: string;
|
|
51
|
+
}>;
|
|
52
|
+
/**
|
|
53
|
+
* Begin the sign-in flow.
|
|
54
|
+
*
|
|
55
|
+
* Generates a session key, persists it against `vaultUrl`, and returns the URL
|
|
56
|
+
* to navigate the user to. Navigate with a full page load — the flow is
|
|
57
|
+
* redirect-based and does not work from an iframe.
|
|
58
|
+
*/
|
|
59
|
+
export declare function startAuth(config: HypermediaAuthConfig, options?: AuthOptions): Promise<string>;
|
|
60
|
+
/**
|
|
61
|
+
* Consume the vault's callback on your `redirect_uri` page.
|
|
62
|
+
*
|
|
63
|
+
* Returns null when the current URL carries no delegation result, so it is safe
|
|
64
|
+
* to call unconditionally on page load. Throws if the user denied the request or
|
|
65
|
+
* if any validation fails.
|
|
66
|
+
*/
|
|
67
|
+
export declare function handleCallback(config: Pick<HypermediaAuthConfig, 'vaultUrl'>, options?: AuthOptions): Promise<AuthResult | null>;
|
|
68
|
+
/** Retrieve the stored session for a vault URL, or null if not signed in. */
|
|
69
|
+
export declare function getSession(vaultUrl: string, options?: AuthOptions): Promise<StoredSession | null>;
|
|
70
|
+
/** Sign arbitrary bytes with the session's non-extractable private key. */
|
|
71
|
+
export declare function signWithSession(session: StoredSession, data: Uint8Array): Promise<Uint8Array>;
|
|
72
|
+
/**
|
|
73
|
+
* Adapt a session into an `HMSigner`, so it can be passed straight to
|
|
74
|
+
* `createComment`, `createContact`, `publishDocument`, and the other blob
|
|
75
|
+
* builders in this package.
|
|
76
|
+
*/
|
|
77
|
+
export declare function createSessionSigner(session: StoredSession): HMSigner;
|
|
78
|
+
/** Forget the stored session for a vault URL. */
|
|
79
|
+
export declare function clearSession(vaultUrl: string, options?: AuthOptions): Promise<void>;
|
package/dist/auth.mjs
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PARAM_CLIENT_ID,
|
|
3
|
+
PARAM_DATA,
|
|
4
|
+
PARAM_EMAIL,
|
|
5
|
+
PARAM_ERROR,
|
|
6
|
+
PARAM_PROOF,
|
|
7
|
+
PARAM_REDIRECT_URI,
|
|
8
|
+
PARAM_SESSION_KEY,
|
|
9
|
+
PARAM_SITE_NAME,
|
|
10
|
+
PARAM_STATE,
|
|
11
|
+
PARAM_TS,
|
|
12
|
+
validateClientId,
|
|
13
|
+
validateRedirectUri
|
|
14
|
+
} from "./chunk-SHOAYNJY.mjs";
|
|
15
|
+
import {
|
|
16
|
+
decode,
|
|
17
|
+
encode
|
|
18
|
+
} from "./chunk-2MD3FXKB.mjs";
|
|
19
|
+
import {
|
|
20
|
+
encode as encode2,
|
|
21
|
+
principalEqual,
|
|
22
|
+
principalFromEd25519,
|
|
23
|
+
principalFromString,
|
|
24
|
+
principalToString,
|
|
25
|
+
verify
|
|
26
|
+
} from "./chunk-IT5LLTMP.mjs";
|
|
27
|
+
import "./chunk-DR3HJ6BV.mjs";
|
|
28
|
+
import {
|
|
29
|
+
decode as decode2
|
|
30
|
+
} from "./chunk-3TPCJCVO.mjs";
|
|
31
|
+
import "./chunk-7P6ASYW6.mjs";
|
|
32
|
+
|
|
33
|
+
// src/auth.ts
|
|
34
|
+
var AUTH_STATE_BYTES = 16;
|
|
35
|
+
var DB_NAME = "seed-auth";
|
|
36
|
+
var DB_VERSION = 1;
|
|
37
|
+
var STORE_NAME = "sessions";
|
|
38
|
+
function openDatabase() {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
const request = indexedDB.open(DB_NAME, DB_VERSION);
|
|
41
|
+
request.onupgradeneeded = () => {
|
|
42
|
+
if (!request.result.objectStoreNames.contains(STORE_NAME)) {
|
|
43
|
+
request.result.createObjectStore(STORE_NAME);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
request.onsuccess = () => resolve(request.result);
|
|
47
|
+
request.onerror = () => reject(request.error);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
function promisifyRequest(request) {
|
|
51
|
+
return new Promise((resolve, reject) => {
|
|
52
|
+
request.onsuccess = () => resolve(request.result);
|
|
53
|
+
request.onerror = () => reject(request.error);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
async function withStore(mode, fn) {
|
|
57
|
+
const db = await openDatabase();
|
|
58
|
+
try {
|
|
59
|
+
const tx = db.transaction(STORE_NAME, mode);
|
|
60
|
+
const result = await promisifyRequest(fn(tx.objectStore(STORE_NAME)));
|
|
61
|
+
return result;
|
|
62
|
+
} finally {
|
|
63
|
+
db.close();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function indexedDBSessionStore() {
|
|
67
|
+
return {
|
|
68
|
+
async get(vaultUrl) {
|
|
69
|
+
const value = await withStore("readonly", (store) => store.get(vaultUrl));
|
|
70
|
+
return value ?? null;
|
|
71
|
+
},
|
|
72
|
+
async put(vaultUrl, record) {
|
|
73
|
+
await withStore("readwrite", (store) => store.put(record, vaultUrl));
|
|
74
|
+
},
|
|
75
|
+
async delete(vaultUrl) {
|
|
76
|
+
await withStore("readwrite", (store) => store.delete(vaultUrl));
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
var defaultStore = null;
|
|
81
|
+
function resolveStore(store) {
|
|
82
|
+
if (store) return store;
|
|
83
|
+
if (!defaultStore) defaultStore = indexedDBSessionStore();
|
|
84
|
+
return defaultStore;
|
|
85
|
+
}
|
|
86
|
+
function generateAuthState() {
|
|
87
|
+
return encode(crypto.getRandomValues(new Uint8Array(AUTH_STATE_BYTES)));
|
|
88
|
+
}
|
|
89
|
+
async function signWithKey(privateKey, payload) {
|
|
90
|
+
const signature = await crypto.subtle.sign(
|
|
91
|
+
"Ed25519",
|
|
92
|
+
privateKey,
|
|
93
|
+
payload
|
|
94
|
+
);
|
|
95
|
+
return new Uint8Array(signature);
|
|
96
|
+
}
|
|
97
|
+
async function collectStream(readable) {
|
|
98
|
+
const chunks = [];
|
|
99
|
+
const reader = readable.getReader();
|
|
100
|
+
for (; ; ) {
|
|
101
|
+
const { done, value } = await reader.read();
|
|
102
|
+
if (done) break;
|
|
103
|
+
chunks.push(value);
|
|
104
|
+
}
|
|
105
|
+
const total = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
|
|
106
|
+
const result = new Uint8Array(total);
|
|
107
|
+
let offset = 0;
|
|
108
|
+
for (const chunk of chunks) {
|
|
109
|
+
result.set(chunk, offset);
|
|
110
|
+
offset += chunk.length;
|
|
111
|
+
}
|
|
112
|
+
return result;
|
|
113
|
+
}
|
|
114
|
+
async function decompress(data) {
|
|
115
|
+
const stream = new DecompressionStream("gzip");
|
|
116
|
+
const writer = stream.writable.getWriter();
|
|
117
|
+
writer.write(data);
|
|
118
|
+
writer.close();
|
|
119
|
+
return collectStream(stream.readable);
|
|
120
|
+
}
|
|
121
|
+
async function generateSessionKey() {
|
|
122
|
+
const keyPair = await crypto.subtle.generateKey("Ed25519", false, [
|
|
123
|
+
"sign",
|
|
124
|
+
"verify"
|
|
125
|
+
]);
|
|
126
|
+
const publicKeyRaw = new Uint8Array(await crypto.subtle.exportKey("raw", keyPair.publicKey));
|
|
127
|
+
const principal = principalToString(principalFromEd25519(publicKeyRaw));
|
|
128
|
+
return { keyPair, publicKeyRaw, principal };
|
|
129
|
+
}
|
|
130
|
+
async function startAuth(config, options = {}) {
|
|
131
|
+
const clientId = config.clientId ?? window.location.origin;
|
|
132
|
+
const redirectUri = config.redirectUri ?? `${window.location.origin}${window.location.pathname}`;
|
|
133
|
+
validateClientId(clientId);
|
|
134
|
+
validateRedirectUri(redirectUri, clientId);
|
|
135
|
+
const session = await generateSessionKey();
|
|
136
|
+
const authState = generateAuthState();
|
|
137
|
+
const authStartTime = Date.now();
|
|
138
|
+
await resolveStore(options.store).put(config.vaultUrl, {
|
|
139
|
+
keyPair: session.keyPair,
|
|
140
|
+
publicKeyRaw: session.publicKeyRaw,
|
|
141
|
+
principal: session.principal,
|
|
142
|
+
vaultUrl: config.vaultUrl,
|
|
143
|
+
createTime: authStartTime,
|
|
144
|
+
authState,
|
|
145
|
+
authStartTime
|
|
146
|
+
});
|
|
147
|
+
const url = new URL(config.vaultUrl);
|
|
148
|
+
url.search = "";
|
|
149
|
+
url.hash = "";
|
|
150
|
+
url.searchParams.set(PARAM_CLIENT_ID, clientId);
|
|
151
|
+
url.searchParams.set(PARAM_REDIRECT_URI, redirectUri);
|
|
152
|
+
url.searchParams.set(PARAM_SESSION_KEY, session.principal);
|
|
153
|
+
url.searchParams.set(PARAM_STATE, authState);
|
|
154
|
+
url.searchParams.set(PARAM_TS, String(authStartTime));
|
|
155
|
+
if (config.email) url.searchParams.set(PARAM_EMAIL, config.email);
|
|
156
|
+
if (config.siteName) url.searchParams.set(PARAM_SITE_NAME, config.siteName);
|
|
157
|
+
const signedUrl = url.toString();
|
|
158
|
+
const proof = encode(await signWithKey(session.keyPair.privateKey, new TextEncoder().encode(signedUrl)));
|
|
159
|
+
const delimiter = signedUrl.includes("?") ? "&" : "?";
|
|
160
|
+
return `${signedUrl}${delimiter}${PARAM_PROOF}=${encodeURIComponent(proof)}`;
|
|
161
|
+
}
|
|
162
|
+
async function handleCallback(config, options = {}) {
|
|
163
|
+
const url = new URL(window.location.href);
|
|
164
|
+
const dataParam = url.searchParams.get(PARAM_DATA);
|
|
165
|
+
const stateParam = url.searchParams.get(PARAM_STATE);
|
|
166
|
+
const error = url.searchParams.get(PARAM_ERROR);
|
|
167
|
+
if (!dataParam && !error) return null;
|
|
168
|
+
const vaultUrl = config == null ? void 0 : config.vaultUrl;
|
|
169
|
+
if (!vaultUrl) throw new Error("vaultUrl is required to retrieve the stored session");
|
|
170
|
+
if (!stateParam) throw new Error("Missing callback state");
|
|
171
|
+
const store = resolveStore(options.store);
|
|
172
|
+
const record = await store.get(vaultUrl);
|
|
173
|
+
if (!record) throw new Error("No stored session found for this vault. Was startAuth() called first?");
|
|
174
|
+
if (!record.authState) throw new Error("No pending auth state found for this vault. Was startAuth() called first?");
|
|
175
|
+
if (record.authState !== stateParam) throw new Error("Invalid callback state");
|
|
176
|
+
if (error) {
|
|
177
|
+
await store.put(vaultUrl, { ...record, authState: null, authStartTime: null });
|
|
178
|
+
throw new Error(`Delegation error: ${error}`);
|
|
179
|
+
}
|
|
180
|
+
if (!dataParam) throw new Error("Missing callback data");
|
|
181
|
+
const session = {
|
|
182
|
+
keyPair: record.keyPair,
|
|
183
|
+
publicKeyRaw: record.publicKeyRaw,
|
|
184
|
+
principal: record.principal,
|
|
185
|
+
vaultUrl: record.vaultUrl,
|
|
186
|
+
createdAt: record.createTime
|
|
187
|
+
};
|
|
188
|
+
const callbackData = decode2(await decompress(decode(dataParam)));
|
|
189
|
+
let notifyServerUrl;
|
|
190
|
+
if (callbackData.notifyServerUrl && typeof callbackData.notifyServerUrl === "string") {
|
|
191
|
+
try {
|
|
192
|
+
new URL(callbackData.notifyServerUrl);
|
|
193
|
+
notifyServerUrl = callbackData.notifyServerUrl;
|
|
194
|
+
} catch {
|
|
195
|
+
console.warn(`Invalid notify server URL in callback data: ${callbackData.notifyServerUrl}`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const capability = encode2(callbackData.capability);
|
|
199
|
+
if (capability.cid.toString() !== callbackData.capabilityCid.toString()) {
|
|
200
|
+
throw new Error("Capability CID mismatch: data was corrupted in transit");
|
|
201
|
+
}
|
|
202
|
+
let profile;
|
|
203
|
+
if (callbackData.profile || callbackData.profileCid) {
|
|
204
|
+
if (!callbackData.profile || !callbackData.profileCid) {
|
|
205
|
+
throw new Error("Profile callback data is incomplete");
|
|
206
|
+
}
|
|
207
|
+
profile = encode2(callbackData.profile);
|
|
208
|
+
if (profile.cid.toString() !== callbackData.profileCid.toString()) {
|
|
209
|
+
throw new Error("Profile CID mismatch: data was corrupted in transit");
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (!verify(callbackData.capability)) throw new Error("Invalid capability signature");
|
|
213
|
+
if (callbackData.profile && !verify(callbackData.profile)) throw new Error("Invalid profile signature");
|
|
214
|
+
const expectedDelegate = principalFromString(session.principal);
|
|
215
|
+
if (!principalEqual(callbackData.capability.delegate, expectedDelegate)) {
|
|
216
|
+
throw new Error("Capability delegate does not match local session key");
|
|
217
|
+
}
|
|
218
|
+
if (!principalEqual(callbackData.account, callbackData.capability.signer)) {
|
|
219
|
+
throw new Error("Callback account does not match capability signer");
|
|
220
|
+
}
|
|
221
|
+
if (callbackData.profile && !principalEqual(callbackData.account, callbackData.profile.signer)) {
|
|
222
|
+
throw new Error("Callback account does not match profile signer");
|
|
223
|
+
}
|
|
224
|
+
await store.put(vaultUrl, { ...record, authState: null, authStartTime: null });
|
|
225
|
+
return {
|
|
226
|
+
accountPrincipal: principalToString(callbackData.account),
|
|
227
|
+
capability,
|
|
228
|
+
...profile ? { profile } : {},
|
|
229
|
+
session,
|
|
230
|
+
notifyServerUrl
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
async function getSession(vaultUrl, options = {}) {
|
|
234
|
+
const record = await resolveStore(options.store).get(vaultUrl);
|
|
235
|
+
if (!record) return null;
|
|
236
|
+
return {
|
|
237
|
+
keyPair: record.keyPair,
|
|
238
|
+
publicKeyRaw: record.publicKeyRaw,
|
|
239
|
+
principal: record.principal,
|
|
240
|
+
vaultUrl: record.vaultUrl,
|
|
241
|
+
createdAt: record.createTime
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
async function signWithSession(session, data) {
|
|
245
|
+
return signWithKey(session.keyPair.privateKey, data);
|
|
246
|
+
}
|
|
247
|
+
function createSessionSigner(session) {
|
|
248
|
+
const principal = principalFromString(session.principal);
|
|
249
|
+
return {
|
|
250
|
+
getPublicKey: async () => principal,
|
|
251
|
+
sign: async (data) => signWithSession(session, data)
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
async function clearSession(vaultUrl, options = {}) {
|
|
255
|
+
await resolveStore(options.store).delete(vaultUrl);
|
|
256
|
+
}
|
|
257
|
+
export {
|
|
258
|
+
clearSession,
|
|
259
|
+
createSessionSigner,
|
|
260
|
+
generateSessionKey,
|
|
261
|
+
getSession,
|
|
262
|
+
handleCallback,
|
|
263
|
+
indexedDBSessionStore,
|
|
264
|
+
signWithSession,
|
|
265
|
+
startAuth
|
|
266
|
+
};
|
package/dist/auto-link.d.ts
CHANGED
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
* - Operation builder: what DocumentOperations to emit
|
|
10
10
|
* - Full I/O convenience: fetch parent, check, build ops, sign, publish
|
|
11
11
|
*/
|
|
12
|
-
import type { HMDocument,
|
|
12
|
+
import type { HMDocument, UnpackedHypermediaId } from './hm-types';
|
|
13
|
+
import type { AnySigner } from './signer';
|
|
13
14
|
import type { SeedClient } from './client';
|
|
14
15
|
import type { DocumentOperation } from './change';
|
|
15
16
|
/**
|
|
@@ -52,7 +53,7 @@ export type AutoLinkChildToParentOptions = {
|
|
|
52
53
|
/** hm:// URL of the published child document (without version). */
|
|
53
54
|
childHmUrl: string;
|
|
54
55
|
/** Signer for the parent document change. */
|
|
55
|
-
signer:
|
|
56
|
+
signer: AnySigner;
|
|
56
57
|
};
|
|
57
58
|
/**
|
|
58
59
|
* Auto-link a newly published child document to its parent.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {
|
|
2
|
+
autoLinkChildToParent,
|
|
3
|
+
createAutoLinkOps,
|
|
4
|
+
documentContainsLinkToChild,
|
|
5
|
+
documentHasSelfQuery,
|
|
6
|
+
shouldAutoLinkParent
|
|
7
|
+
} from "./chunk-EU36RFR3.mjs";
|
|
8
|
+
import "./chunk-JT57MV7Y.mjs";
|
|
9
|
+
import "./chunk-PJ2RFT5A.mjs";
|
|
10
|
+
import "./chunk-HBIVZA6C.mjs";
|
|
11
|
+
import "./chunk-LVAHHQIK.mjs";
|
|
12
|
+
import "./chunk-DR3HJ6BV.mjs";
|
|
13
|
+
import "./chunk-SQEUO7FO.mjs";
|
|
14
|
+
import "./chunk-7P6ASYW6.mjs";
|
|
15
|
+
export {
|
|
16
|
+
autoLinkChildToParent,
|
|
17
|
+
createAutoLinkOps,
|
|
18
|
+
documentContainsLinkToChild,
|
|
19
|
+
documentHasSelfQuery,
|
|
20
|
+
shouldAutoLinkParent
|
|
21
|
+
};
|