@kedem/okdb 2.3.3 → 2.3.4
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 +59 -0
- package/algorithm-worker-thread.js +1 -1
- package/bin/okdb.js +1 -1
- package/docs/auth-and-sync.md +170 -463
- package/docs/auth-permissions.md +3 -1
- package/docs/deployment.md +8 -0
- package/docs/embeddings.md +1 -0
- package/docs/files.md +176 -51
- package/docs/functions.md +1 -1
- package/docs/getting-started.md +40 -13
- package/docs/http-api.md +12 -3
- package/docs/http-cluster.md +25 -0
- package/docs/resolved-fields.md +2 -0
- package/okdb-functions-sandbox-worker.js +1 -1
- package/okdb-http-worker-child.js +1 -1
- package/okdb-views-bootstrap-worker.js +1 -1
- package/okdb.js +1 -1
- package/package.json +1 -1
- package/public/sections/files/parts/files-file-list.ok.js +1 -1
- package/public/sections/files/parts/files-preview.ok.js +1 -0
- package/types/features/files.d.ts +38 -3
- package/types/index.d.ts +18 -3
package/docs/auth-and-sync.md
CHANGED
|
@@ -1,558 +1,265 @@
|
|
|
1
1
|
# Auth and Sync
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
How HTTP requests are authenticated, how OAuth fits in, and how sync peers authenticate to each
|
|
4
|
+
other.
|
|
4
5
|
|
|
5
|
-
For
|
|
6
|
+
For creating tokens and the bootstrap window see
|
|
7
|
+
**[Getting Started → Authentication](./getting-started.md#authentication)**. For permissions,
|
|
8
|
+
grants and token recipes see **[Auth Permissions](./auth-permissions.md)**.
|
|
6
9
|
|
|
7
10
|
---
|
|
8
11
|
|
|
9
12
|
## Part 1 — Authentication
|
|
10
13
|
|
|
11
|
-
### The
|
|
14
|
+
### The model
|
|
12
15
|
|
|
13
|
-
|
|
16
|
+
Credentials are **tokens** stored in the database — the `~tokens` type in `~system`, hashed. There
|
|
17
|
+
are no passwords, API keys or token maps in the constructor options or `.kdbconfig`. A token has
|
|
18
|
+
an optional `name` + `password` (for login), a raw bearer value, `permissions` and per-env
|
|
19
|
+
`grants`. Create them with `okdb token create`, the admin UI, or `POST /api/auth/tokens`.
|
|
14
20
|
|
|
15
|
-
|
|
16
|
-
HTTP request
|
|
17
|
-
↓
|
|
18
|
-
db.auth.authenticateRequest(req)
|
|
19
|
-
↓
|
|
20
|
-
token verification
|
|
21
|
-
↓
|
|
22
|
-
auth result { type, roles, isAuthenticated }
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
There is no user database. Authentication is entirely config-driven.
|
|
26
|
-
|
|
27
|
-
---
|
|
28
|
-
|
|
29
|
-
### Auth modes
|
|
21
|
+
Every request goes through `db.auth.authenticateRequest(req)`:
|
|
30
22
|
|
|
31
23
|
```
|
|
32
|
-
|
|
24
|
+
1. Bootstrap window ~tokens empty and not open mode → localhost gets full access, others refused
|
|
25
|
+
2. Open mode client IP matches auth.open → full access, no credentials
|
|
26
|
+
3. Bearer token Authorization: Bearer <token> → see "Bearer verification" below
|
|
27
|
+
4. Session cookie okdb_session cookie → login session or OAuth session
|
|
28
|
+
5. Otherwise anonymous → 401 on protected routes
|
|
33
29
|
```
|
|
34
30
|
|
|
35
|
-
|
|
36
|
-
| -------- | ---------------------------------------------------------------------------------------- |
|
|
37
|
-
| `open` | All requests pass. A system user with `admin` role is injected. No credentials required. |
|
|
38
|
-
| `secure` | All `/api/*` and `/admin/*` routes require a valid token or credential. |
|
|
39
|
-
|
|
40
|
-
**Legacy alias:** `mode: "users"` is treated as `"secure"` for backward compatibility.
|
|
41
|
-
|
|
42
|
-
**Default behaviour** — mode is inferred automatically if not set:
|
|
43
|
-
|
|
44
|
-
| Config present | Inferred mode |
|
|
45
|
-
| --------------------------------------------- | ------------- |
|
|
46
|
-
| `auth.admin` or `auth.tokens` or `api.tokens` | `secure` |
|
|
47
|
-
| Nothing | `open` |
|
|
48
|
-
|
|
49
|
-
---
|
|
31
|
+
A sync peer's Ed25519-signed request is authenticated before this, by the sync guard (Part 2).
|
|
50
32
|
|
|
51
|
-
###
|
|
52
|
-
|
|
53
|
-
A complete `.kdbconfig`:
|
|
54
|
-
|
|
55
|
-
```json
|
|
56
|
-
{
|
|
57
|
-
"port": 8484,
|
|
58
|
-
"path": "./okdb-data",
|
|
59
|
-
"auth": {
|
|
60
|
-
"mode": "secure",
|
|
61
|
-
"admin": "admin:password",
|
|
62
|
-
"tokens": {
|
|
63
|
-
"my-api-key": ["admin"],
|
|
64
|
-
"read-only-key": ["read"]
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
Or in code:
|
|
33
|
+
### Options
|
|
71
34
|
|
|
72
35
|
```js
|
|
73
36
|
const okdb = new OKDB('./data', {
|
|
74
37
|
auth: {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
'read-only-key': ['read'],
|
|
80
|
-
},
|
|
38
|
+
open: false, // true = localhost; or IP / CIDR / '*' string or array. CLI: --open [ip ...]
|
|
39
|
+
token: { secret: undefined, ttl: 3_600_000 }, // session-token HMAC secret + TTL (ms)
|
|
40
|
+
session: { secure: false, sameSite: 'Lax' }, // session cookie flags
|
|
41
|
+
providers: {}, // OAuth — see below
|
|
81
42
|
},
|
|
82
43
|
});
|
|
83
44
|
```
|
|
84
45
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
okdb --auth open # force open mode
|
|
89
|
-
okdb --admin admin:secret # set admin credential
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
---
|
|
93
|
-
|
|
94
|
-
### Credential types
|
|
95
|
-
|
|
96
|
-
There are exactly three credential types in the system:
|
|
97
|
-
|
|
98
|
-
#### 1. Admin credential (`auth.admin`)
|
|
99
|
-
|
|
100
|
-
```json
|
|
101
|
-
{ "auth": { "admin": "admin:password" } }
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
- A single `username:password` string in config.
|
|
105
|
-
- Used **only** by the admin UI login form (`POST /auth/login`).
|
|
106
|
-
- On success, the server issues an **HMAC session token**, sets it in a cookie, and returns it in the response body.
|
|
107
|
-
- Node-local. Not replicated. Not stored in the database.
|
|
108
|
-
|
|
109
|
-
#### 2. Static tokens (`auth.tokens`)
|
|
110
|
-
|
|
111
|
-
```json
|
|
112
|
-
{ "auth": { "tokens": { "my-key": ["admin"], "ro-key": ["read"] } } }
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
- Named tokens with explicit role lists, defined in config.
|
|
116
|
-
- Sent as `Authorization: Bearer <token>` on every request.
|
|
117
|
-
- Verified by a **direct map lookup** — no HMAC, no expiry.
|
|
118
|
-
- Node-local. Not replicated.
|
|
119
|
-
- Also accepted as a plain array (backward compat): `["my-key"]` → all get `["admin"]` role.
|
|
120
|
-
|
|
121
|
-
Legacy `api.tokens` is automatically merged into static tokens:
|
|
122
|
-
|
|
123
|
-
```json
|
|
124
|
-
{ "api": { "tokens": ["my-token"] } }
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
#### 3. Issued HMAC tokens
|
|
128
|
-
|
|
129
|
-
These are short-lived tokens created by the server after a successful login or OAuth exchange.
|
|
130
|
-
|
|
131
|
-
**Structure** (not a JWT):
|
|
132
|
-
|
|
133
|
-
```
|
|
134
|
-
base64url(payload).hmac_sha256(payload, tokenSecret)
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
Payload:
|
|
46
|
+
`bin/okdb` passes the `auth` block of `.kdbconfig` through unchanged; `--open` overrides
|
|
47
|
+
`auth.open`. `auth.token.secret` is only used the first time: the secret is persisted in the store
|
|
48
|
+
as `__tokenSecret` (local, never replicated) and reused from then on, generated if unset.
|
|
138
49
|
|
|
139
|
-
|
|
140
|
-
{
|
|
141
|
-
"iat": 1710400000000,
|
|
142
|
-
"exp": 1710403600000,
|
|
143
|
-
"sub": "admin",
|
|
144
|
-
"username": "admin",
|
|
145
|
-
"roles": ["admin"]
|
|
146
|
-
}
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
- Default TTL: **1 hour**
|
|
150
|
-
- Clock-skew tolerance: **30 seconds**
|
|
151
|
-
- `tokenSecret` is auto-generated per node and stored in the local LMDB as `__tokenSecret` (never replicated)
|
|
152
|
-
- Sent as `Authorization: Bearer <token>` or via the `okdb_session` cookie
|
|
153
|
-
|
|
154
|
-
---
|
|
155
|
-
|
|
156
|
-
### Token verification order
|
|
157
|
-
|
|
158
|
-
For every incoming request with a `Bearer` token:
|
|
159
|
-
|
|
160
|
-
```
|
|
161
|
-
1. Check static token map → immediate if found
|
|
162
|
-
2. Verify HMAC issued token → if signature valid and not expired
|
|
163
|
-
3. Validate OIDC access token → if oauth.issuer is configured
|
|
164
|
-
4. Reject → 401
|
|
165
|
-
```
|
|
50
|
+
### Bearer verification
|
|
166
51
|
|
|
167
|
-
|
|
52
|
+
For `Authorization: Bearer <token>`, in order:
|
|
168
53
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
2.
|
|
172
|
-
|
|
54
|
+
1. **Stored token** — the SHA-256 of the value matches a `~tokens` record → that record's
|
|
55
|
+
`permissions` and `grants`.
|
|
56
|
+
2. **HMAC session token** — `base64url(payload).hmac_sha256(payload, tokenSecret)`, issued by
|
|
57
|
+
OAuth login; payload `{ iat, exp, sub, username, roles }`, 30 s clock-skew tolerance.
|
|
58
|
+
3. **Login session value** — the `access_token` returned by `POST /auth/login` (below).
|
|
59
|
+
4. **OIDC access token** — when an OAuth `issuer` is configured: a JWT verified against the
|
|
60
|
+
issuer's JWKS (RS256/ES256, `iss`, optional `audience`, `exp`/`nbf`), then the email allowlist.
|
|
173
61
|
|
|
174
|
-
|
|
62
|
+
Anything else is rejected; an invalid bearer never falls back to the cookie.
|
|
175
63
|
|
|
176
|
-
### Login
|
|
64
|
+
### Login
|
|
177
65
|
|
|
178
66
|
```
|
|
179
67
|
POST /auth/login
|
|
180
|
-
{ "username": "admin", "password": "
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
Server:
|
|
184
|
-
|
|
185
|
-
1. Compares against `auth.admin` credential (case-insensitive username)
|
|
186
|
-
2. If match → issues HMAC token
|
|
187
|
-
3. Returns token in body + sets `okdb_session` cookie
|
|
188
|
-
4. Returns `{ access_token, expires_in }`
|
|
189
|
-
|
|
190
|
-
The issued token can then be used as a Bearer token for API calls.
|
|
191
|
-
|
|
192
|
-
---
|
|
193
|
-
|
|
194
|
-
### OAuth flow
|
|
195
|
-
|
|
196
|
-
OAuth is used **only to obtain a token**. It does not create users, does not store identities, and does not require a user database.
|
|
197
|
-
|
|
198
|
-
```
|
|
199
|
-
GET /admin/auth/start?redirect=/admin/index.html
|
|
200
|
-
→ redirect to OAuth provider
|
|
201
|
-
|
|
202
|
-
GET /admin/auth/callback?code=...&state=...
|
|
203
|
-
→ exchange code with provider
|
|
204
|
-
→ receive email + subject from provider
|
|
205
|
-
→ check email against allowedEmails / allowedDomains
|
|
206
|
-
→ if allowed: issue HMAC token, set okdb_session cookie, redirect
|
|
207
|
-
→ if not allowed: 403
|
|
68
|
+
{ "username": "admin", "password": "s3cret" }
|
|
208
69
|
```
|
|
209
70
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
#### Email allowlisting (required)
|
|
71
|
+
`username` is a token's `name` (case-insensitive), `password` its password. On success the server
|
|
72
|
+
sets the `okdb_session` cookie and returns `{ access_token, expires_in }`; `access_token` is also
|
|
73
|
+
usable as a Bearer token. The session value is sealed with the token's hash, so revoking the
|
|
74
|
+
token ends its sessions. Lifetime is `auth.token.ttl` (default 1 hour).
|
|
215
75
|
|
|
216
|
-
|
|
76
|
+
- If several tokens share the name and password, the response is
|
|
77
|
+
`{ ambiguous: true, tokens: [...] }`; finish with `POST /auth/login/select` adding `tokenId`.
|
|
78
|
+
- `POST /auth/token` is an OAuth2 password-grant alias for MCP clients (same body, returns
|
|
79
|
+
`{ access_token, token_type: "Bearer", expires_in }`, no cookie).
|
|
80
|
+
- `POST /auth/logout` clears the cookie. `GET /admin/session` returns
|
|
81
|
+
`{ authenticated, user, grants }` or `401`.
|
|
217
82
|
|
|
218
|
-
|
|
219
|
-
| ---------------- | ---------- | ------------------------------------------------------------ |
|
|
220
|
-
| `allowedEmails` | `string[]` | Exact email addresses, e.g. `["alice@corp.com"]` |
|
|
221
|
-
| `allowedDomains` | `string[]` | Domain suffixes, e.g. `["corp.com"]` matches `*@corp.com` |
|
|
222
|
-
| `defaultRoles` | `string[]` | Roles assigned to allowed OAuth users (default: `["admin"]`) |
|
|
83
|
+
### OAuth
|
|
223
84
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
Configure OAuth via the `auth.providers` block:
|
|
85
|
+
OAuth only obtains a session: it creates no users and stores nothing. Configure one provider
|
|
86
|
+
under `auth.providers` (only the first entry is used):
|
|
227
87
|
|
|
228
88
|
```js
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
getAuthorizationUrl: ({ state }) => `https://accounts.google.com/o/oauth2/v2/auth?...&state=${state}`,
|
|
246
|
-
exchangeCode: async ({ code }) => {
|
|
247
|
-
// exchange code for tokens, return { subject, email, claims }
|
|
248
|
-
},
|
|
89
|
+
auth: {
|
|
90
|
+
providers: {
|
|
91
|
+
google: {
|
|
92
|
+
issuer: 'https://accounts.google.com', // enables OIDC bearer validation
|
|
93
|
+
clientId: 'YOUR_CLIENT_ID',
|
|
94
|
+
redirectUri: 'http://localhost:8484/admin/auth/callback',
|
|
95
|
+
scopes: ['openid', 'email', 'profile'],
|
|
96
|
+
|
|
97
|
+
allowedDomains: ['mycompany.com'], // required: allowedDomains and/or allowedEmails
|
|
98
|
+
allowedEmails: ['contractor@gmail.com'],
|
|
99
|
+
defaultRoles: ['admin'], // default ['admin']
|
|
100
|
+
|
|
101
|
+
getAuthorizationUrl: ({ state, codeChallenge, redirectUri, scopes }) => '...',
|
|
102
|
+
exchangeCode: async ({ code, state, redirectUri }) => {
|
|
103
|
+
// return { subject, email, claims }
|
|
249
104
|
},
|
|
250
105
|
},
|
|
251
106
|
},
|
|
252
|
-
});
|
|
253
|
-
```
|
|
254
|
-
|
|
255
|
-
The same allowlist applies to direct OIDC bearer tokens (API/MCP clients sending a Google access token as `Authorization: Bearer <jwt>`).
|
|
256
|
-
|
|
257
|
-
---
|
|
258
|
-
|
|
259
|
-
### Route protection summary
|
|
260
|
-
|
|
261
|
-
| Route | Requirement |
|
|
262
|
-
| ------------------- | -------------------------------------------------------- |
|
|
263
|
-
| `GET /api/*` | Any valid token (static, issued, or OIDC), or open mode |
|
|
264
|
-
| `POST /auth/login` | No auth (public endpoint) |
|
|
265
|
-
| `POST /auth/logout` | No auth (public endpoint) |
|
|
266
|
-
| `GET /admin/login` | No auth (public endpoint) |
|
|
267
|
-
| `GET /admin/*` | Valid token in cookie or Bearer header with `admin` role |
|
|
268
|
-
| `/api/sync/*` | Valid token (same as API) — see note in Part 2 |
|
|
269
|
-
|
|
270
|
-
---
|
|
271
|
-
|
|
272
|
-
### `GET /admin/session`
|
|
273
|
-
|
|
274
|
-
Returns the current session state:
|
|
275
|
-
|
|
276
|
-
```json
|
|
277
|
-
{
|
|
278
|
-
"authenticated": true,
|
|
279
|
-
"user": {
|
|
280
|
-
"id": "admin",
|
|
281
|
-
"username": "admin",
|
|
282
|
-
"roles": ["admin"]
|
|
283
|
-
}
|
|
284
107
|
}
|
|
285
108
|
```
|
|
286
109
|
|
|
287
|
-
Returns `401` when not authenticated.
|
|
288
|
-
|
|
289
|
-
---
|
|
290
|
-
|
|
291
|
-
### Logout
|
|
292
|
-
|
|
293
110
|
```
|
|
294
|
-
|
|
111
|
+
GET /admin/auth/start?redirect=/admin/index.html → redirect to the provider (PKCE)
|
|
112
|
+
GET /admin/auth/callback?code=...&state=... → exchangeCode → email allowlist check
|
|
113
|
+
allowed → HMAC session token in okdb_session, redirect
|
|
114
|
+
not → 403
|
|
295
115
|
```
|
|
296
116
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
### How sync works
|
|
304
|
-
|
|
305
|
-
OKDB uses a **pull-based, last-write-wins (LWW)** replication model over HTTP. There is no central coordinator, no consensus protocol.
|
|
117
|
+
- **Allowlist is mandatory.** With neither `allowedEmails` nor `allowedDomains`, every OAuth login
|
|
118
|
+
and OIDC bearer is rejected. An email matching either list is allowed.
|
|
119
|
+
- The session's roles are those returned by `exchangeCode` (`user.roles`), else `defaultRoles`.
|
|
120
|
+
- The same allowlist applies to OIDC access tokens sent as `Authorization: Bearer <jwt>`
|
|
121
|
+
(optionally checked against `audience`; `allowedAlgs`, `discoveryTtlMs`, `jwksTtlMs` tune it).
|
|
306
122
|
|
|
307
|
-
|
|
308
|
-
1. A write increments the local HLC clock and logs a change entry
|
|
309
|
-
2. On clock advance, a UDP multicast poke is sent on the LAN
|
|
310
|
-
3. Peers that receive the poke pull a delta from the changed node
|
|
311
|
-
4. Each change is compared by HLC timestamp — the higher wins
|
|
312
|
-
5. Ties are broken by origin node ID (lexicographic)
|
|
313
|
-
6. Applied changes use origin= to prevent echo-loops
|
|
314
|
-
```
|
|
123
|
+
### Route protection
|
|
315
124
|
|
|
316
|
-
|
|
125
|
+
| Route | Requirement |
|
|
126
|
+
| ------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
|
|
127
|
+
| `/api/*` | Authenticated, with the route's permission |
|
|
128
|
+
| `/admin/*` | Authenticated session cookie or Bearer |
|
|
129
|
+
| `POST /auth/login`, `/auth/token`, `/auth/logout` | Public |
|
|
130
|
+
| `/admin/auth/start`, `/admin/auth/callback`, `/admin/session` | Public |
|
|
131
|
+
| `/api/sync/*` | Signed peer request, or the route's `sync:read` / `sync:peer` / `sync:write` (Part 2) |
|
|
317
132
|
|
|
318
133
|
---
|
|
319
134
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
Each node has a persistent `nodeId` (UUID) stored as `__identity` in the local LMDB. This is auto-generated on first open and never changes.
|
|
323
|
-
|
|
324
|
-
Nodes register themselves and discover peers via the `~sync_nodes` type in the `~system` environment, which itself replicates — so adding a peer on one node eventually propagates to all peers, forming a mesh.
|
|
135
|
+
## Part 2 — Sync
|
|
325
136
|
|
|
326
|
-
|
|
137
|
+
### How sync works
|
|
327
138
|
|
|
328
|
-
|
|
139
|
+
Pull-based, last-write-wins replication over HTTP; no coordinator, no consensus.
|
|
329
140
|
|
|
330
|
-
```js
|
|
331
|
-
const okdb = new OKDB('./node1', {
|
|
332
|
-
auth: {
|
|
333
|
-
mode: 'secure',
|
|
334
|
-
tokens: { 'cluster-secret': ['admin'] },
|
|
335
|
-
},
|
|
336
|
-
sync: {
|
|
337
|
-
token: 'cluster-secret', // token this node sends when calling peers
|
|
338
|
-
address: 'http://192.168.1.10:8484', // this node's public URL
|
|
339
|
-
},
|
|
340
|
-
});
|
|
341
|
-
await okdb.open();
|
|
342
|
-
okdb.http.listen(8484);
|
|
343
141
|
```
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
{
|
|
349
|
-
"auth": {
|
|
350
|
-
"mode": "secure",
|
|
351
|
-
"tokens": {
|
|
352
|
-
"cluster-secret": ["admin"]
|
|
353
|
-
}
|
|
354
|
-
},
|
|
355
|
-
"sync": {
|
|
356
|
-
"token": "cluster-secret",
|
|
357
|
-
"address": "http://192.168.1.10:8484"
|
|
358
|
-
}
|
|
359
|
-
}
|
|
142
|
+
1. A write advances the env's HLC clock and logs a change entry
|
|
143
|
+
2. Peers are poked; each pulls a delta (POST /api/sync/delta) from the changed node
|
|
144
|
+
3. Changes are compared by HLC — the higher wins; ties broken by origin node id
|
|
145
|
+
4. Applied changes carry their origin, so they are not echoed back
|
|
360
146
|
```
|
|
361
147
|
|
|
362
|
-
|
|
148
|
+
Each node has a persistent `nodeId` (`__identity`) and an Ed25519 keypair (`__nodePrivateKey` /
|
|
149
|
+
`__nodePublicKey`), generated on first open and never replicated. Peers are recorded in the
|
|
150
|
+
`~sync_nodes` type in `~system` — with their address and **public key** — which itself replicates,
|
|
151
|
+
so the mesh forms from one join.
|
|
363
152
|
|
|
364
|
-
### How
|
|
153
|
+
### How peers authenticate
|
|
365
154
|
|
|
366
|
-
|
|
155
|
+
**Ongoing sync is signed, not token-based.** Every outbound peer request carries
|
|
156
|
+
`X-OKDB-NodeId`, `X-OKDB-Timestamp`, `X-OKDB-BodyHash` and `X-OKDB-Signature`. The receiver looks
|
|
157
|
+
up the sender's public key in its `~sync_nodes`, checks the signature, a ±5 min timestamp window
|
|
158
|
+
and a replay cache, and treats the request as the peer node. No shared secret lives in config.
|
|
367
159
|
|
|
368
|
-
|
|
160
|
+
**Joining needs a credential once.** A node not yet in the remote's `~sync_nodes` has to pass the
|
|
161
|
+
remote's normal API auth for the handshake. `join()` takes either:
|
|
369
162
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
163
|
+
- `{ token }` → sent as `Authorization: Bearer <token>`; a raw token that exists in the remote's
|
|
164
|
+
`~tokens`, or
|
|
165
|
+
- `{ username, password }` → exchanged via the remote's `POST /auth/login` for a session token,
|
|
166
|
+
then sent as Bearer.
|
|
373
167
|
|
|
374
|
-
|
|
168
|
+
With neither, no header is sent — which only works if the remote is in open mode for this node's
|
|
169
|
+
IP. The joiner then POSTs its node record (with public key) to `/api/sync/join`; from there on
|
|
170
|
+
both sides use signatures. Because `~tokens` lives in `~system`, the remote's tokens replicate to
|
|
171
|
+
the joiner — cluster credentials work on every node.
|
|
375
172
|
|
|
376
|
-
|
|
173
|
+
The join credential needs `sync:read` (for `/api/sync/info`) and `sync:peer` (for
|
|
174
|
+
`/api/sync/join`) on the remote — nothing more (see
|
|
175
|
+
[Auth Permissions](./auth-permissions.md#syncpeer--machine-permission)).
|
|
377
176
|
|
|
378
|
-
|
|
177
|
+
### Connecting nodes
|
|
379
178
|
|
|
380
|
-
|
|
179
|
+
On node A, create a machine token for joiners:
|
|
381
180
|
|
|
382
|
-
```
|
|
383
|
-
|
|
384
|
-
node B has: sync.token: "cluster-secret"
|
|
181
|
+
```bash
|
|
182
|
+
okdb token create --label 'cluster join' --permissions 'sync:read,sync:peer' # prints the raw token once
|
|
385
183
|
```
|
|
386
184
|
|
|
387
|
-
|
|
185
|
+
Then join from node B — CLI (direct store access; B's own reachable address is required):
|
|
388
186
|
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
- Every node puts the shared token in `sync.token` (so it sends it outbound)
|
|
394
|
-
- Every node puts the shared token in `auth.tokens` (so it accepts it inbound)
|
|
395
|
-
|
|
396
|
-
```json
|
|
397
|
-
{
|
|
398
|
-
"auth": {
|
|
399
|
-
"tokens": { "cluster-secret": ["admin"] }
|
|
400
|
-
},
|
|
401
|
-
"sync": {
|
|
402
|
-
"token": "cluster-secret"
|
|
403
|
-
}
|
|
404
|
-
}
|
|
187
|
+
```bash
|
|
188
|
+
okdb join http://node-a:8484 --token <raw-token> --address http://node-b:8484
|
|
189
|
+
# or: --username <name> --password <pass>
|
|
405
190
|
```
|
|
406
191
|
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
---
|
|
410
|
-
|
|
411
|
-
### Connecting nodes
|
|
412
|
-
|
|
413
|
-
#### Via code
|
|
192
|
+
In code:
|
|
414
193
|
|
|
415
194
|
```js
|
|
416
|
-
|
|
417
|
-
await
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
This:
|
|
421
|
-
|
|
422
|
-
1. Fetches `GET /api/sync/info` from Node 1 to get its node ID
|
|
423
|
-
2. Stores Node 1's record in `~sync_nodes` (replicates to mesh)
|
|
424
|
-
3. Creates a bidirectional data link
|
|
425
|
-
4. POSTs to Node 1's `/api/sync/join` so Node 1 registers Node 2
|
|
426
|
-
5. Pulls an initial delta immediately
|
|
427
|
-
|
|
428
|
-
#### Via admin UI
|
|
429
|
-
|
|
430
|
-
Go to **System → Sync → Connect**. Enter the target node's URL and click Connect.
|
|
431
|
-
|
|
432
|
-
The UI calls `POST /api/sync/connect`:
|
|
433
|
-
|
|
434
|
-
```json
|
|
435
|
-
{ "address": "http://192.168.1.10:8484" }
|
|
195
|
+
const okdb = new OKDB('./node-b', { sync: { address: 'http://node-b:8484' } });
|
|
196
|
+
await okdb.open();
|
|
197
|
+
okdb.http.listen(8484);
|
|
198
|
+
await okdb.sync.join('http://node-a:8484', { token: process.env.OKDB_JOIN_TOKEN });
|
|
436
199
|
```
|
|
437
200
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
#### Via API
|
|
201
|
+
Over HTTP (the admin UI's **System → Sync → Connect** does this), on node B with a credential
|
|
202
|
+
for B that has `sync:peer`:
|
|
441
203
|
|
|
442
204
|
```
|
|
443
205
|
POST /api/sync/connect
|
|
444
|
-
|
|
445
|
-
{ "address": "http://192.168.1.10:8484" }
|
|
446
|
-
```
|
|
447
|
-
|
|
448
|
-
---
|
|
449
|
-
|
|
450
|
-
### Multi-node cluster example
|
|
451
|
-
|
|
452
|
-
```js
|
|
453
|
-
const nodes = [8484, 8485, 8486].map((port) => {
|
|
454
|
-
const okdb = new OKDB(`./node-${port}`, {
|
|
455
|
-
auth: {
|
|
456
|
-
mode: 'secure',
|
|
457
|
-
tokens: { 'cluster-secret': ['admin'] },
|
|
458
|
-
},
|
|
459
|
-
sync: {
|
|
460
|
-
token: 'cluster-secret',
|
|
461
|
-
address: `http://localhost:${port}`,
|
|
462
|
-
},
|
|
463
|
-
});
|
|
464
|
-
okdb._port = port;
|
|
465
|
-
return okdb;
|
|
466
|
-
});
|
|
467
|
-
|
|
468
|
-
await Promise.all(nodes.map((n) => n.open()));
|
|
469
|
-
for (const node of nodes) node.http.listen(node._port);
|
|
470
|
-
|
|
471
|
-
// Connect node 1 and 2 to node 0 (mesh forms automatically from ~sync_nodes sync)
|
|
472
|
-
await nodes[1].sync.join('http://localhost:8484');
|
|
473
|
-
await nodes[2].sync.join('http://localhost:8484');
|
|
206
|
+
{ "address": "http://node-a:8484", "token": "<raw-token-on-A>" }
|
|
474
207
|
```
|
|
475
208
|
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
| Data | Syncs | Notes |
|
|
481
|
-
| -------------------------------- | ----- | ------------------------------------- |
|
|
482
|
-
| Application data (default env) | ✅ | All `put`/`delete` operations |
|
|
483
|
-
| Custom `sync: true` environments | ✅ | Created via `createEnvironment` |
|
|
484
|
-
| `~system` environment | ✅ | Including `~sync_nodes` peer registry |
|
|
485
|
-
| `__identity` (node UUID) | ❌ | Raw LMDB key, never in changelog |
|
|
486
|
-
| `__tokenSecret` | ❌ | Raw LMDB key, never in changelog |
|
|
487
|
-
| `auth.admin` credential | ❌ | Config only, never in database |
|
|
488
|
-
| `auth.tokens` | ❌ | Config only, never in database |
|
|
489
|
-
| `sync.token` | ❌ | Config only, never in database |
|
|
209
|
+
`username` / `password` may replace `token`; `data_link: false` skips the bidirectional data
|
|
210
|
+
link. If B has no `sync.address`, it is taken from `my_address` in the body or the request's
|
|
211
|
+
`Host` header.
|
|
490
212
|
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
**Current state:** Sync uses a shared static token. This works but has one weakness — the sync token is also a full admin credential on every node it's registered on. There is no per-node identity or revocation.
|
|
496
|
-
|
|
497
|
-
**Planned (Phase 2):** Cryptographic node identity using Ed25519 keypairs.
|
|
498
|
-
|
|
499
|
-
- Each node generates a keypair on first open
|
|
500
|
-
- Private key: stored as `__nodePrivateKey` in local LMDB, never replicated
|
|
501
|
-
- Public key: stored in `~sync_nodes` (replicates to peers)
|
|
502
|
-
- Outbound sync requests: signed with private key (`X-OKDB-NodeId`, `X-OKDB-Signature`)
|
|
503
|
-
- Inbound sync requests: verified against stored public key
|
|
504
|
-
- Trust: local-only (`__sync:trusted_peers`), non-transitive
|
|
505
|
-
- Discovering a peer's identity (via `~sync_nodes` replication) does **not** grant trust
|
|
213
|
+
`join()` fetches `/api/sync/info` from A, stores A in `~sync_nodes`, creates a data link, POSTs to
|
|
214
|
+
A's `/api/sync/join` (where A enforces its `sync` license and peer limit — the joiner needs no
|
|
215
|
+
license of its own), and pulls an initial delta.
|
|
506
216
|
|
|
507
|
-
|
|
217
|
+
### Sync options
|
|
508
218
|
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
### Sync endpoints reference
|
|
219
|
+
Constructor `sync` block (the CLI server does not read a `sync` block from `.kdbconfig`; after
|
|
220
|
+
`okdb join`, the node restores its address from its own `~sync_nodes` record on restart):
|
|
512
221
|
|
|
513
|
-
|
|
|
514
|
-
|
|
|
515
|
-
| `
|
|
516
|
-
| `
|
|
517
|
-
| `
|
|
518
|
-
| `
|
|
519
|
-
| `GET` | `/api/sync/peers` | Yes | List known peers and status |
|
|
520
|
-
| `PATCH` | `/api/sync/self` | Yes | Update this node's address/meta |
|
|
222
|
+
| Key | Default | Description |
|
|
223
|
+
| ---------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
224
|
+
| `address` | — | This node's reachable URL, advertised to peers. Required for `join()` |
|
|
225
|
+
| `delta_limit` | `500` | Max changes per delta pull |
|
|
226
|
+
| `auto_reconcile` | `true` | Pull from peers on clock change |
|
|
227
|
+
| `token` | — | Optional Bearer also sent on peer requests — a fallback while a peer has not yet received this node's public key. Must be a token in the peer's `~tokens` |
|
|
521
228
|
|
|
522
|
-
|
|
229
|
+
### What syncs
|
|
523
230
|
|
|
524
|
-
|
|
231
|
+
| Data | Syncs | Notes |
|
|
232
|
+
| ------------------------------------------------- | ----- | ------------------------------------- |
|
|
233
|
+
| Every open env with a changelog, incl. `~system` | ✅ | `~sync_nodes`, `~tokens` travel here |
|
|
234
|
+
| `__identity`, `__nodePrivateKey`, `__tokenSecret` | ❌ | Raw keys in `~system`, not in the log |
|
|
525
235
|
|
|
526
|
-
### Sync
|
|
236
|
+
### Sync endpoints
|
|
527
237
|
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
238
|
+
| Method | Path | Permission |
|
|
239
|
+
| ---------------- | ---------------------------------------------------------------- | ------------ |
|
|
240
|
+
| `GET` | `/api/sync/info` | `sync:read` |
|
|
241
|
+
| `POST` | `/api/sync/join` | `sync:peer` |
|
|
242
|
+
| `POST` | `/api/sync/connect` | `sync:peer` |
|
|
243
|
+
| `POST` | `/api/sync/delta` | `sync:peer` |
|
|
244
|
+
| `POST` | `/api/sync/reconcile`, `reconcile-all`, `reconcile-peer`, `poke` | `sync:peer` |
|
|
245
|
+
| `GET` | `/api/sync/peers`, `links`, `topology`, `gc/status` | `sync:read` |
|
|
246
|
+
| `PATCH` | `/api/sync/self` | `sync:write` |
|
|
247
|
+
| `PUT` / `DELETE` | `/api/sync/links/:nodeA/:nodeB` | `sync:write` |
|
|
248
|
+
| `POST` | `/api/sync/gc/run`, `gc/peer/:id/resync` | `sync:write` |
|
|
538
249
|
|
|
539
|
-
|
|
540
|
-
| ---------------- | ------- | --------------------------------------------- |
|
|
541
|
-
| `token` | — | Bearer token sent on outbound sync calls |
|
|
542
|
-
| `address` | — | This node's public URL, advertised to peers |
|
|
543
|
-
| `delta_limit` | `500` | Max changes per delta pull |
|
|
544
|
-
| `auto_reconcile` | `true` | Automatically pull from peers on clock change |
|
|
250
|
+
A verified signed peer request passes all of them.
|
|
545
251
|
|
|
546
252
|
---
|
|
547
253
|
|
|
548
254
|
## Summary
|
|
549
255
|
|
|
550
|
-
| Concept
|
|
551
|
-
|
|
|
552
|
-
|
|
|
553
|
-
|
|
|
554
|
-
|
|
|
555
|
-
|
|
|
556
|
-
|
|
|
557
|
-
|
|
|
558
|
-
|
|
|
256
|
+
| Concept | Mechanism |
|
|
257
|
+
| -------------- | ---------------------------------------------------------------------------- |
|
|
258
|
+
| Credentials | Tokens in `~tokens` (`okdb token create`, admin UI, `POST /api/auth/tokens`) |
|
|
259
|
+
| First access | Bootstrap window: localhost only while `~tokens` is empty |
|
|
260
|
+
| API access | `Authorization: Bearer <token>`, or a login / OAuth session |
|
|
261
|
+
| Admin UI login | Token name + password → `POST /auth/login` → `okdb_session` cookie |
|
|
262
|
+
| OAuth | Provider code exchange → allowlist → HMAC session token; nothing stored |
|
|
263
|
+
| Open mode | `auth.open` / `--open [ip ...]` — trusted IPs skip auth |
|
|
264
|
+
| Sync join | One-time Bearer token or name + password on the remote |
|
|
265
|
+
| Ongoing sync | Ed25519-signed requests, public keys in replicated `~sync_nodes` |
|