-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathocm-core.js
More file actions
382 lines (334 loc) · 9.3 KB
/
Copy pathocm-core.js
File metadata and controls
382 lines (334 loc) · 9.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/**
* ocm-core.js - OCM protocol logic (pure, no HTTP/TLS handling)
*
* This module contains spec-aligned OCM protocol logic that can be tested
* independently of the HTTP transport layer.
*/
const OCM_BASE = "/ocm";
// URL/Path Helpers
function trimTrailingSlash(str) {
if (typeof str !== "string") return str;
return str.endsWith("/") ? str.slice(0, -1) : str;
}
function joinUrlPath(base, path) {
if (!base || !path) return base || path || "";
const cleanBase = trimTrailingSlash(base);
const cleanPath = path.startsWith("/") ? path : `/${path}`;
return `${cleanBase}${cleanPath}`;
}
/**
* Normalize a URL or path field to an absolute URL.
* Accepts: absolute URL, absolute path, or relative path.
* @param {string} baseUrl - The base URL (e.g., https://example.org)
* @param {string} field - The field value to normalize
* @returns {string|null} Absolute URL or null if invalid
*/
function normalizeUrlOrPath(baseUrl, field) {
if (!field) return null;
// Already an absolute URL
if (field.startsWith("https://") || field.startsWith("http://")) {
return trimTrailingSlash(field);
}
// Absolute path
if (field.startsWith("/")) {
const base = trimTrailingSlash(baseUrl);
return `${base}${field}`;
}
// Relative path (no leading slash)
const base = trimTrailingSlash(baseUrl);
return `${base}/${field}`;
}
// Local Discovery
/**
* Build the local discovery response for this server.
* @param {string} serverHost - The server's FQDN (e.g., ocmstub1.docker)
* @param {object} opts - Options including publicKey
* @returns {object} Discovery response per OCM spec
*/
function getLocalDiscovery(serverHost, opts = {}) {
return {
enabled: true,
apiVersion: "1.2.0",
endPoint: `https://${serverHost}${OCM_BASE}`,
resourceTypes: [
{
name: "file",
shareTypes: ["user", "group"],
protocols: {
webdav: "/webdav-api/",
},
},
],
capabilities: ["invites", "exchange-token"],
tokenEndPoint: `${OCM_BASE}/token`,
inviteAcceptDialog: "/accept-invite",
publicKey: opts.publicKey || null,
};
}
// Peer Discovery
/**
* Discover a remote OCM peer's configuration.
* @param {string} fqdn - The peer's FQDN
* @param {object} deps - Dependencies (fetch function)
* @returns {Promise<object>} Normalized discovery info
*/
async function discoverPeer(fqdn, deps = {}) {
const fetchFn = deps.fetch || globalThis.fetch;
const baseUrl = `https://${fqdn}`;
let discoveryData = null;
let lastError = null;
// Try /.well-known/ocm first, then /ocm-provider
for (const path of ["/.well-known/ocm", "/ocm-provider"]) {
try {
const url = `${baseUrl}${path}`;
const response = await fetchFn(url);
if (response.ok) {
discoveryData = await response.json();
break;
}
} catch (e) {
lastError = e;
}
}
if (!discoveryData) {
throw {
status: 502,
code: "DISCOVERY_FAILED",
message: `Could not discover OCM endpoint for ${fqdn}`,
details: lastError?.message,
};
}
// Validate required fields
const required = ["enabled", "apiVersion", "endPoint", "resourceTypes"];
for (const field of required) {
if (discoveryData[field] === undefined) {
throw {
status: 502,
code: "DISCOVERY_INVALID",
message: `Discovery response missing required field: ${field}`,
};
}
}
// Normalize endPoint (must be absolute https:// URL)
let endPoint = trimTrailingSlash(discoveryData.endPoint);
if (!endPoint.startsWith("https://")) {
throw {
status: 502,
code: "DISCOVERY_INVALID",
message: "Discovery endPoint must be an absolute https:// URL",
};
}
// Normalize optional URL/path fields
const tokenEndPoint = discoveryData.tokenEndPoint
? normalizeUrlOrPath(baseUrl, discoveryData.tokenEndPoint)
: null;
let inviteAcceptDialog = null;
if (discoveryData.inviteAcceptDialog) {
try {
inviteAcceptDialog = normalizeUrlOrPath(
baseUrl,
discoveryData.inviteAcceptDialog
);
} catch (e) {
console.warn(
`Could not normalize inviteAcceptDialog for ${fqdn}:`,
e.message
);
}
}
return {
enabled: discoveryData.enabled,
apiVersion: discoveryData.apiVersion,
endPoint,
resourceTypes: discoveryData.resourceTypes,
capabilities: discoveryData.capabilities || [],
publicKey: discoveryData.publicKey || null,
tokenEndPoint,
inviteAcceptDialog,
provider: discoveryData.provider || null,
_raw: discoveryData,
};
}
// Route Registry
const PROTOCOL_ROUTES = [
{
id: "ocm.inviteAccepted",
path: `${OCM_BASE}/invite-accepted`,
aliases: ["/invite-accepted"],
method: "POST",
},
{
id: "ocm.invitesAccept",
path: `${OCM_BASE}/invites/accept`,
aliases: ["/invites/accept"],
method: "POST",
},
{
id: "ocm.token",
path: `${OCM_BASE}/token`,
aliases: [],
method: "POST",
},
];
function getProtocolRoutes() {
return PROTOCOL_ROUTES;
}
/**
* Resolve a request path to a protocol route.
* @param {string} path - The request path (without query string)
* @returns {object|null} The matching route or null
*/
function resolveProtocolRoute(path) {
const normalizedPath = trimTrailingSlash(path);
for (const route of PROTOCOL_ROUTES) {
if (route.path === normalizedPath) {
return route;
}
for (const alias of route.aliases) {
if (alias === normalizedPath) {
return route;
}
}
}
return null;
}
// Core Handlers
/**
* Handle inbound invite-accepted request (spec-shaped).
* @param {object} payload - The AcceptedInvite payload
* @param {object} ctx - Context with validToken, acceptedInvites, localUser
* @returns {object} Response with status and body
*/
function handleInviteAcceptedInbound(payload, ctx) {
const { recipientProvider, token, userID, email, name } = payload || {};
// Validate required fields
if (!recipientProvider || !token || !userID) {
return {
status: 400,
body: {
message: "Missing required fields: recipientProvider, token, userID",
},
};
}
// Validate token
if (token !== ctx.validToken) {
return {
status: 400,
body: { message: "Invalid or expired invite token" },
};
}
// Check for duplicate acceptance
if (ctx.acceptedInvites[recipientProvider]) {
return {
status: 409,
body: { message: "Invite already accepted from this provider" },
};
}
// Store the accepted invite
ctx.acceptedInvites[recipientProvider] = { userID, email, name };
// Return local user info
return {
status: 200,
body: {
userID: ctx.localUser.userID,
email: ctx.localUser.email,
name: ctx.localUser.name,
},
};
}
/**
* Handle inbound invites/accept request (Reva-style).
* Unwraps the nested invite object if present.
* @param {object} payload - The request payload (may have nested invite)
* @param {object} ctx - Context with validToken, acceptedInvites, localUser
* @returns {object} Response with status and body
*/
function handleInvitesAcceptInbound(payload, ctx) {
// Reva sends: { invite: { token, userId, recipientProvider, name, email } }
const invite = payload?.invite || payload || {};
// Map Reva field names to spec field names
const normalized = {
recipientProvider: invite.recipientProvider,
token: invite.token,
userID: invite.userId || invite.userID,
email: invite.email,
name: invite.name,
};
return handleInviteAcceptedInbound(normalized, ctx);
}
/**
* Handle token exchange request.
* @param {object} request - Normalized request { grantType, clientId, code }
* @param {object} ctx - Context with grants table
* @returns {object} Response with status and body
*/
function handleTokenRequest(request, ctx) {
const { grantType, clientId, code } = request || {};
// Validate grant_type (accept both RFC and legacy)
if (
grantType !== "authorization_code" &&
grantType !== "ocm_authorization_code"
) {
return {
status: 400,
body: { message: "Invalid grant_type" },
};
}
// Validate client
if (!clientId || typeof ctx.grants[clientId] !== "object") {
return {
status: 403,
body: { message: `No grants found for client ${clientId}` },
};
}
// Validate code
if (!code || typeof ctx.grants[clientId][code] !== "string") {
return {
status: 403,
body: { message: `Grant ${code} not found for client ${clientId}` },
};
}
// Return token response
return {
status: 200,
body: {
access_token: ctx.grants[clientId][code],
token_type: "bearer",
expires_in: 3600,
refresh_token: "qwertyuiop",
},
};
}
// State Factory
/**
* Create invite state for a stub instance.
* @param {string} validToken - The valid invite token
* @returns {object} State object with acceptedInvites map
*/
function createInviteState(validToken) {
return {
acceptedInvites: {},
validToken,
};
}
// Exports
module.exports = {
// Constants
OCM_BASE,
// URL helpers
trimTrailingSlash,
joinUrlPath,
normalizeUrlOrPath,
// Discovery
getLocalDiscovery,
discoverPeer,
// Route registry
getProtocolRoutes,
resolveProtocolRoute,
// Handlers
handleInviteAcceptedInbound,
handleInvitesAcceptInbound,
handleTokenRequest,
// State
createInviteState,
};