@lango-project/lango-server 0.2.2 → 0.2.3

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 (106) hide show
  1. package/README.md +239 -27
  2. package/dist/.lango/runtime.mjs +16 -0
  3. package/dist/body_reader.mjs +15 -14
  4. package/dist/body_reader.mjs.map +1 -1
  5. package/dist/cli/.lango/runtime.mjs +16 -0
  6. package/dist/cli/body_reader.mjs +15 -14
  7. package/dist/cli/body_reader.mjs.map +1 -1
  8. package/dist/cli/client_identity.mjs +250 -0
  9. package/dist/cli/client_identity.mjs.map +1 -0
  10. package/dist/cli/database.mjs +23 -8
  11. package/dist/cli/database.mjs.map +1 -1
  12. package/dist/cli/endpoint.mjs +11 -7
  13. package/dist/cli/endpoint.mjs.map +1 -1
  14. package/dist/cli/longpoll_broker.mjs +9 -8
  15. package/dist/cli/longpoll_broker.mjs.map +1 -1
  16. package/dist/cli/longpoll_options.mjs +8 -7
  17. package/dist/cli/longpoll_options.mjs.map +1 -1
  18. package/dist/cli/longpoll_session.mjs +11 -10
  19. package/dist/cli/longpoll_session.mjs.map +1 -1
  20. package/dist/cli/options.mjs +29 -3
  21. package/dist/cli/options.mjs.map +1 -1
  22. package/dist/cli/origin.mjs +87 -0
  23. package/dist/cli/origin.mjs.map +1 -0
  24. package/dist/cli/policy.mjs +121 -0
  25. package/dist/cli/policy.mjs.map +1 -0
  26. package/dist/cli/postgres_adapter.mjs +3 -2
  27. package/dist/cli/postgres_adapter.mjs.map +1 -1
  28. package/dist/cli/routes.mjs +33 -17
  29. package/dist/cli/routes.mjs.map +1 -1
  30. package/dist/cli/server.mjs +84 -80
  31. package/dist/cli/server.mjs.map +1 -1
  32. package/dist/cli/static_files.mjs +5 -4
  33. package/dist/cli/static_files.mjs.map +1 -1
  34. package/dist/client_identity.mjs +250 -0
  35. package/dist/client_identity.mjs.map +1 -0
  36. package/dist/database.mjs +23 -8
  37. package/dist/database.mjs.map +1 -1
  38. package/dist/endpoint.mjs +11 -7
  39. package/dist/endpoint.mjs.map +1 -1
  40. package/dist/group.mjs +34 -16
  41. package/dist/group.mjs.map +1 -1
  42. package/dist/http_failure.mjs +9 -2
  43. package/dist/http_failure.mjs.map +1 -1
  44. package/dist/longpoll_broker.mjs +9 -8
  45. package/dist/longpoll_broker.mjs.map +1 -1
  46. package/dist/longpoll_options.mjs +8 -7
  47. package/dist/longpoll_options.mjs.map +1 -1
  48. package/dist/longpoll_session.mjs +11 -10
  49. package/dist/longpoll_session.mjs.map +1 -1
  50. package/dist/options.mjs +29 -3
  51. package/dist/options.mjs.map +1 -1
  52. package/dist/origin.mjs +87 -0
  53. package/dist/origin.mjs.map +1 -0
  54. package/dist/policy.mjs +121 -0
  55. package/dist/policy.mjs.map +1 -0
  56. package/dist/postgres_adapter.mjs +3 -2
  57. package/dist/postgres_adapter.mjs.map +1 -1
  58. package/dist/postgres_worker.mjs +15 -14
  59. package/dist/postgres_worker.mjs.map +1 -1
  60. package/dist/response.mjs +1 -1
  61. package/dist/response.mjs.map +1 -1
  62. package/dist/route_info.mjs +1 -1
  63. package/dist/route_info.mjs.map +1 -1
  64. package/dist/routes.mjs +33 -17
  65. package/dist/routes.mjs.map +1 -1
  66. package/dist/runtime.mjs +227 -112
  67. package/dist/runtime.mjs.map +1 -1
  68. package/dist/server.mjs +84 -80
  69. package/dist/server.mjs.map +1 -1
  70. package/dist/server_context.mjs +21 -9
  71. package/dist/server_context.mjs.map +1 -1
  72. package/dist/static_files.mjs +5 -4
  73. package/dist/static_files.mjs.map +1 -1
  74. package/dist/token.mjs +34 -7
  75. package/dist/token.mjs.map +1 -1
  76. package/dist/tokens.mjs +11 -2
  77. package/dist/tokens.mjs.map +1 -1
  78. package/dist/worker.mjs +7 -6
  79. package/dist/worker.mjs.map +1 -1
  80. package/docs/security.md +420 -0
  81. package/examples/security/README.md +160 -0
  82. package/examples/security/deploy.xml +12 -0
  83. package/examples/security/handlers.lango +610 -0
  84. package/examples/security/package.json +19 -0
  85. package/examples/security/passwords.lango +61 -0
  86. package/examples/security/routes.xml +14 -0
  87. package/examples/security/schema.lango +91 -0
  88. package/examples/security/setup.lango +30 -0
  89. package/package.json +12 -6
  90. package/src/client_identity.lango +184 -0
  91. package/src/database.lango +15 -4
  92. package/src/endpoint.lango +12 -5
  93. package/src/group.lango +25 -21
  94. package/src/http_failure.lango +9 -5
  95. package/src/longpoll_options.lango +11 -5
  96. package/src/options.lango +34 -1
  97. package/src/origin.lango +56 -0
  98. package/src/policy.lango +84 -0
  99. package/src/response.lango +1 -1
  100. package/src/route_info.lango +2 -0
  101. package/src/routes.lango +30 -15
  102. package/src/runtime.lango +131 -80
  103. package/src/server.lango +6 -2
  104. package/src/server_context.lango +10 -2
  105. package/src/token.lango +26 -5
  106. package/src/tokens.lango +10 -3
package/dist/runtime.mjs CHANGED
@@ -1,3 +1,6 @@
1
+ import { routeInfo } from "./route_info.mjs";
2
+ import { ClientIdentity } from "./client_identity.mjs";
3
+ import { originAllowed } from "./origin.mjs";
1
4
  import { trace } from "./trace.mjs";
2
5
  import { routePattern, matchPath, moreSpecific } from "./route_pattern.mjs";
3
6
  import { ServerContext } from "./server_context.mjs";
@@ -8,6 +11,7 @@ import { Response } from "./response.mjs";
8
11
  import { HttpFailure } from "./http_failure.mjs";
9
12
  import { requestIdentity } from "./request_identity.mjs";
10
13
  import { errorDetails, errorMessage } from "./error_details.mjs";
14
+ import * as crypto from "node:crypto";
11
15
  import * as threads from "node:worker_threads";
12
16
  import {
13
17
  hostGlobal as __hostGlobal,
@@ -15,6 +19,9 @@ import {
15
19
  contextGet as __contextGet,
16
20
  contextSet as __contextSet,
17
21
  contextRestore as __contextRestore,
22
+ bindContexts as __bindContexts,
23
+ fromJson as __fromJson,
24
+ toJson as __toJson,
18
25
  getClass as __getClass,
19
26
  isKindof as __isKindof,
20
27
  isNumber as __isNumber,
@@ -43,15 +50,20 @@ export class Runtime {
43
50
  #modules;
44
51
  #middlewares;
45
52
  #database;
53
+ #clients;
46
54
  constructor(definition) {
47
55
  __requireArity(arguments.length, [0], "constructor");
48
56
  definition = __boundary(definition);
49
- let refresh = __withArity((tokens) => {
57
+ let refresh = __withArity(__bindContexts((tokens) => {
50
58
  tokens = __boundary(tokens);
51
59
  return this.refreshTokens(tokens);
52
60
  return null;
53
- }, [0], "closure");
54
- this.#serverContext = new ServerContext(__normalize(definition.routes), refresh);
61
+ }), [0], "closure");
62
+ let rotate = __bindContexts(() => {
63
+ return this.#rotateClient();
64
+ return null;
65
+ });
66
+ this.#serverContext = new ServerContext(__normalize(definition.routes), refresh, rotate);
55
67
  this.#definition = definition;
56
68
  this.#routes = [];
57
69
  this.#events = { };
@@ -59,6 +71,10 @@ export class Runtime {
59
71
  this.#modules = { };
60
72
  this.#middlewares = __normalize(__hostGlobal(["Object"]).create(null));
61
73
  this.#database = new Database(definition.settings.database, definition.settings.root);
74
+ this.#clients = null;
75
+ if (__requireBoolean(definition.settings.client !== null)) {
76
+ this.#clients = new ClientIdentity(definition.settings.client, __normalize(this.#database));
77
+ }
62
78
  }
63
79
  preload() {
64
80
  trace(`Worker ${__normalize(threads.threadId)}: preparing database, middleware, routes and events`);
@@ -76,12 +92,12 @@ export class Runtime {
76
92
  for (const [name, target] of __entries(events)) {
77
93
  pending = this.#prepareEvent(pending, name, target);
78
94
  }
79
- return pending.catch(__withArity((error) => {
95
+ return pending.catch(__withArity(__bindContexts((error) => {
80
96
  error = __boundary(error);
81
97
  this.close();
82
98
  throw error;
83
99
  return null;
84
- }, [0], "closure"));
100
+ }), [0], "closure"));
85
101
  return null;
86
102
  }
87
103
  close() {
@@ -94,11 +110,11 @@ export class Runtime {
94
110
  pending = __boundary(pending);
95
111
  name = __boundary(name);
96
112
  spec = __boundary(spec);
97
- return pending.then(__withArity((ignored) => {
113
+ return pending.then(__withArity(__bindContexts((ignored) => {
98
114
  ignored = __boundary(ignored);
99
115
  return this.#target(spec);
100
116
  return null;
101
- }, [0], "closure")).then(__withArity((handler) => {
117
+ }), [0], "closure")).then(__withArity(__bindContexts((handler) => {
102
118
  handler = __boundary(handler);
103
119
  this.#middlewares[name] = handler;
104
120
  if (__requireBoolean(__normalize(spec.validate) !== null)) {
@@ -106,14 +122,14 @@ export class Runtime {
106
122
  }
107
123
  return null;
108
124
  return null;
109
- }, [0], "closure")).then(__withArity((validator) => {
125
+ }), [0], "closure")).then(__withArity(__bindContexts((validator) => {
110
126
  validator = __boundary(validator);
111
127
  if (__requireBoolean(validator !== null)) {
112
128
  return __call(validator, __normalize(this.#serverContext.routes()));
113
129
  }
114
130
  return null;
115
131
  return null;
116
- }, [0], "closure"));
132
+ }), [0], "closure"));
117
133
  return null;
118
134
  }
119
135
  #prepareRoute(pending, route) {
@@ -126,11 +142,11 @@ export class Runtime {
126
142
  __add(this.#routes, prepared);
127
143
  return pending;
128
144
  }
129
- return pending.then(__withArity((ignored) => {
145
+ return pending.then(__withArity(__bindContexts((ignored) => {
130
146
  ignored = __boundary(ignored);
131
147
  return this.#target(route);
132
148
  return null;
133
- }, [0], "closure")).then(__withArity((handler) => {
149
+ }), [0], "closure")).then(__withArity(__bindContexts((handler) => {
134
150
  handler = __boundary(handler);
135
151
  let middleware = null;
136
152
  let name = __normalize(route.middleware);
@@ -140,10 +156,10 @@ export class Runtime {
140
156
  }
141
157
  middleware = __normalize(this.#middlewares[name]);
142
158
  }
143
- __add(this.#routes, { path: __normalize(route.path), pattern: routePattern(__normalize(route.path)), auth: __normalize(route.auth), handler: handler, middleware: middleware });
159
+ __add(this.#routes, { path: __normalize(route.path), pattern: routePattern(__normalize(route.path)), auth: __normalize(route.auth), policy: __normalize(route.policy), reauthSeconds: __normalize(route.reauthSeconds), legacyAuth: __normalize(route.legacyAuth), handler: handler, middleware: middleware });
144
160
  return null;
145
161
  return null;
146
- }, [0], "closure"));
162
+ }), [0], "closure"));
147
163
  return null;
148
164
  }
149
165
  #prepareEvent(pending, name, spec) {
@@ -151,16 +167,16 @@ export class Runtime {
151
167
  pending = __boundary(pending);
152
168
  name = __boundary(name);
153
169
  spec = __boundary(spec);
154
- return pending.then(__withArity((ignored) => {
170
+ return pending.then(__withArity(__bindContexts((ignored) => {
155
171
  ignored = __boundary(ignored);
156
172
  return this.#target(spec);
157
173
  return null;
158
- }, [0], "closure")).then(__withArity((handler) => {
174
+ }), [0], "closure")).then(__withArity(__bindContexts((handler) => {
159
175
  handler = __boundary(handler);
160
176
  this.#events[name] = handler;
161
177
  return null;
162
178
  return null;
163
- }, [0], "closure"));
179
+ }), [0], "closure"));
164
180
  return null;
165
181
  }
166
182
  #target(spec) {
@@ -171,12 +187,12 @@ export class Runtime {
171
187
  if (__requireBoolean(__includes(file, __normalize(this.#modules)))) {
172
188
  return __normalize(__hostGlobal(["Promise"]).resolve(exported(__normalize(this.#modules[file]), __normalize(spec.handler))));
173
189
  }
174
- return import(file).then(__withArity((mod) => {
190
+ return import(file).then(__withArity(__bindContexts((mod) => {
175
191
  mod = __boundary(mod);
176
192
  this.#modules[file] = mod;
177
193
  return exported(mod, __normalize(spec.handler));
178
194
  return null;
179
- }, [0], "closure"));
195
+ }), [0], "closure"));
180
196
  return null;
181
197
  }
182
198
  #match(path) {
@@ -208,12 +224,13 @@ export class Runtime {
208
224
  __contextScope.push(__contextSet("server", __normalize(this.#serverContext)));
209
225
  __contextScope.push(__contextSet("request", __normalize(__hostGlobal(["Object"]).freeze(__normalize(task.request)))));
210
226
  __contextScope.push(__contextSet("response", new Response()));
227
+ if (__requireBoolean(!__requireBoolean(originAllowed(__normalize(this.#definition.settings.origin), __contextGet("request"))))) {
228
+ return failure(403, "OriginRejected");
229
+ }
211
230
  let fresh = __normalize(__contextGet("request").headers.authorization) === null;
212
231
  let claims = null;
213
232
  try {
214
- if (__requireBoolean(fresh)) {
215
- claims = __normalize(this.#tokens.create());
216
- } else {
233
+ if (__requireBoolean(!__requireBoolean(fresh))) {
217
234
  claims = __normalize(this.#tokens.read(__normalize(__contextGet("request").headers.authorization)));
218
235
  }
219
236
  } catch (error) {
@@ -226,6 +243,9 @@ export class Runtime {
226
243
  return failure(404, "RouteNotFound");
227
244
  }
228
245
  let route = __normalize(matched.route);
246
+ let metadata = routeInfo([route]);
247
+ let selected = __normalize(__hostGlobal(["Object"]).freeze(__normalize(metadata[0])));
248
+ __contextScope.push(__contextSet("request", __normalize(__hostGlobal(["Object"]).freeze(__normalize(__hostGlobal(["Object"]).assign({ }, __contextGet("request"), { route: selected }))))));
229
249
  let input = null;
230
250
  try {
231
251
  input = __normalize(__hostGlobal(["JSON"]).parse(__normalize(task.body)));
@@ -242,43 +262,118 @@ export class Runtime {
242
262
  } catch (error) {
243
263
  return failure(400, "InvalidPath");
244
264
  }
245
- __contextScope.push(__contextSet("token", new Token(claims)));
246
- trace(`${requestIdentity(__normalize(__contextGet("request").id), __contextGet("token"))} Matched route ${matched.route.path}; fresh=${fresh}`);
247
265
  __contextScope.push(__contextSet("db", __normalize(this.#database.scope())));
248
- return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4, __capturedContext5) => (ignored) => {
266
+ __contextScope.push(__contextSet("clientIdentity", null));
267
+ let restored = false;
268
+ try {
269
+ if (__requireBoolean(__normalize(this.#clients) !== null)) {
270
+ __contextScope.push(__contextSet("clientIdentity", __normalize(this.#clients.begin(claims, __normalize(this.#tokens)))));
271
+ claims = __normalize(__contextGet("clientIdentity").claims);
272
+ fresh = __normalize(__contextGet("clientIdentity").fresh);
273
+ restored = __normalize(__contextGet("clientIdentity").restored);
274
+ }
275
+ if (__requireBoolean(claims === null)) {
276
+ claims = __normalize(this.#tokens.create());
277
+ }
278
+ } catch (error) {
279
+ closeScope(__contextGet("db"), false);
280
+ if (__requireBoolean(__isKindof(error, HttpFailure))) {
281
+ return __normalize(error.result());
282
+ }
283
+ __normalize(__contextGet("log").err(`[${__normalize(__contextGet("request").id)}] Client identity failed: ${errorDetails(error)}`));
284
+ return failure(500, "InternalServerError");
285
+ }
286
+ return this.#process(task, route, input, claims, fresh, restored);
287
+ return null;
288
+ } finally {
289
+ __contextRestore(__contextScope);
290
+ }
291
+ }
292
+ #rotateClient() {
293
+ const __contextScope = [];
294
+ try {
295
+ if (__requireBoolean(__requireBoolean(__normalize(this.#clients) === null) || __requireBoolean(__contextGet("clientIdentity") === null))) {
296
+ throw "Managed client identity is unavailable";
297
+ }
298
+ if (__requireBoolean(__normalize(__contextGet("clientIdentity").closed))) {
299
+ throw "Client identity scope is closed";
300
+ }
301
+ __contextGet("clientIdentity").rotate = true;
302
+ return null;
303
+ return null;
304
+ } finally {
305
+ __contextRestore(__contextScope);
306
+ }
307
+ }
308
+ #signResponse(claims) {
309
+ const __contextScope = [];
310
+ try {
311
+ __requireArity(arguments.length, [0], "signResponse");
312
+ claims = __boundary(claims);
313
+ if (__requireBoolean(__normalize(this.#clients) === null)) {
314
+ return __normalize(this.#tokens.sign(claims));
315
+ }
316
+ return __normalize(this.#clients.publish(__contextGet("clientIdentity"), claims, __contextGet("response"), __normalize(this.#tokens)));
317
+ return null;
318
+ } finally {
319
+ __contextRestore(__contextScope);
320
+ }
321
+ }
322
+ #process(task, route, input, claims, fresh, restored) {
323
+ const __contextScope = [];
324
+ try {
325
+ __requireArity(arguments.length, [0, 1, 2, 3, 4, 5], "process");
326
+ task = __boundary(task);
327
+ route = __boundary(route);
328
+ input = __boundary(input);
329
+ claims = __boundary(claims);
330
+ fresh = __boundary(fresh);
331
+ restored = __boundary(restored);
332
+ __contextScope.push(__contextSet("token", new Token(claims)));
333
+ trace(`${requestIdentity(__normalize(__contextGet("request").id), __contextGet("token"))} Matched route ${__normalize(route.path)}; fresh=${fresh}`);
334
+ return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(__bindContexts((ignored) => {
249
335
  const __contextScope = [];
250
336
  try {
251
- __contextScope.push(__contextSet("server", __capturedContext0));
252
- __contextScope.push(__contextSet("request", __capturedContext1));
253
- __contextScope.push(__contextSet("response", __capturedContext2));
254
- __contextScope.push(__contextSet("token", __capturedContext3));
255
- __contextScope.push(__contextSet("log", __capturedContext4));
256
- __contextScope.push(__contextSet("db", __capturedContext5));
257
337
  ignored = __boundary(ignored);
258
338
  if (__requireBoolean(fresh)) {
259
- return this.#event("register", input);
339
+ let registration = __normalize(__hostGlobal(["Object"]).assign({ }, input, { ip: __normalize(__contextGet("request").remoteAddress), userAgent: __normalize(__contextGet("request").headers["user-agent"]) }));
340
+ return this.#event("register", registration);
260
341
  }
261
- return this.#validate();
342
+ return this.#validate(restored);
262
343
  return null;
263
344
  } finally {
264
345
  __contextRestore(__contextScope);
265
346
  }
266
- })(__contextGet("server"), __contextGet("request"), __contextGet("response"), __contextGet("token"), __contextGet("log"), __contextGet("db")), [0], "closure")).then(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4, __capturedContext5) => (validatedAt) => {
347
+ }), [0], "closure")).then(__withArity(__bindContexts((validatedAt) => {
267
348
  const __contextScope = [];
268
349
  try {
269
- __contextScope.push(__contextSet("server", __capturedContext0));
270
- __contextScope.push(__contextSet("request", __capturedContext1));
271
- __contextScope.push(__contextSet("response", __capturedContext2));
272
- __contextScope.push(__contextSet("token", __capturedContext3));
273
- __contextScope.push(__contextSet("log", __capturedContext4));
274
- __contextScope.push(__contextSet("db", __capturedContext5));
275
350
  validatedAt = __boundary(validatedAt);
276
351
  if (__requireBoolean(__requireBoolean(!__requireBoolean(fresh)) && __requireBoolean(__isNumber(validatedAt)))) {
277
352
  stampValidation(claims, validatedAt);
278
353
  __normalize(__contextGet("token").renew());
279
354
  }
280
- if (__requireBoolean(__requireBoolean(__normalize(route.auth) !== "anonymous") && __requireBoolean(!__requireBoolean(__normalize(__contextGet("token").hasAuth(__normalize(route.auth))))))) {
281
- throw { status: 401, error: "Unauthorized" };
355
+ if (__requireBoolean(__requireBoolean(__normalize(route.policy) !== "anonymous") && __requireBoolean(!__requireBoolean(__normalize(__contextGet("token").hasPolicy(__normalize(route.policy))))))) {
356
+ if (__requireBoolean(__normalize(route.legacyAuth))) {
357
+ __normalize(__contextGet("response").reject(401, "Unauthorized"));
358
+ }
359
+ if (__requireBoolean(__requireBoolean(__normalize(route.policy) === "login") || __requireBoolean(!__requireBoolean(__normalize(__contextGet("token").hasPolicy("login")))))) {
360
+ needLogin(route, 401, "LoginRequired");
361
+ }
362
+ __normalize(__contextGet("response").reject(403, "Forbidden"));
363
+ }
364
+ if (__requireBoolean(__normalize(route.reauthSeconds) > 0)) {
365
+ let authenticatedAt = __normalize(__contextGet("token").get("authenticatedAt"));
366
+ let now = __normalize(__hostGlobal(["Date"]).now());
367
+ if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(authenticatedAt)))) || __requireBoolean(authenticatedAt < 0)) || __requireBoolean(authenticatedAt > now))) {
368
+ needLogin(route, 403, "ReauthenticationRequired");
369
+ }
370
+ if (__requireBoolean(__finite(now - authenticatedAt) >= __finite(__normalize(route.reauthSeconds) * 1000))) {
371
+ needLogin(route, 403, "ReauthenticationRequired");
372
+ }
373
+ }
374
+ let expected = __normalize(__contextGet("request").headers["x-lango-expected-user"]);
375
+ if (__requireBoolean(__requireBoolean(expected !== null) && __requireBoolean(expected !== __toJson(__normalize(__contextGet("token").get("userId")))))) {
376
+ __normalize(__contextGet("response").reject(409, "IdentityChanged"));
282
377
  }
283
378
  if (__requireBoolean(__normalize(route.kind) === "longpoll")) {
284
379
  return null;
@@ -294,20 +389,15 @@ export class Runtime {
294
389
  } finally {
295
390
  __contextRestore(__contextScope);
296
391
  }
297
- })(__contextGet("server"), __contextGet("request"), __contextGet("response"), __contextGet("token"), __contextGet("log"), __contextGet("db")), [0], "closure")).then(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4, __capturedContext5) => (output) => {
392
+ }), [0], "closure")).then(__withArity(__bindContexts((output) => {
298
393
  const __contextScope = [];
299
394
  try {
300
- __contextScope.push(__contextSet("server", __capturedContext0));
301
- __contextScope.push(__contextSet("request", __capturedContext1));
302
- __contextScope.push(__contextSet("response", __capturedContext2));
303
- __contextScope.push(__contextSet("token", __capturedContext3));
304
- __contextScope.push(__contextSet("log", __capturedContext4));
305
- __contextScope.push(__contextSet("db", __capturedContext5));
306
395
  output = __boundary(output);
396
+ let encoded = null;
307
397
  if (__requireBoolean(__normalize(task.authorize) === true)) {
308
398
  closeScope(__contextGet("db"), true);
309
399
  let authorizedClaims = this.#tokenClaims(__contextGet("token"), claims, true);
310
- let encoded = __normalize(this.#tokens.sign(authorizedClaims));
400
+ encoded = this.#signResponse(authorizedClaims);
311
401
  if (__requireBoolean(__normalize(task.pollResult) === null)) {
312
402
  return { status: 200, authorization: encoded };
313
403
  }
@@ -330,7 +420,12 @@ export class Runtime {
330
420
  closeScope(__contextGet("db"), true);
331
421
  let result = __normalize(__contextGet("response").result(body));
332
422
  let updated = this.#tokenClaims(__contextGet("token"), claims, __normalize(result.status) < 400);
333
- result.headers["x-lango-token"] = __normalize(this.#tokens.sign(updated));
423
+ encoded = this.#signResponse(updated);
424
+ result = __normalize(__contextGet("response").result(body));
425
+ result.headers["x-lango-token"] = encoded;
426
+ if (__requireBoolean(__requireBoolean(__normalize(result.status) < 400) && __requireBoolean(authenticationChanged(claims, updated)))) {
427
+ result.headers["x-lango-auth-change"] = `v1:${__normalize(crypto.randomUUID())}`;
428
+ }
334
429
  if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(task.internal) !== true) && __requireBoolean(__normalize(this.#definition.settings.requestEvent))) && __requireBoolean(__includes("request", __normalize(this.#events))))) {
335
430
  return this.#audit(__normalize(__contextGet("request").id), result, __normalize(task.body), body, updated);
336
431
  }
@@ -340,14 +435,9 @@ export class Runtime {
340
435
  } finally {
341
436
  __contextRestore(__contextScope);
342
437
  }
343
- })(__contextGet("server"), __contextGet("request"), __contextGet("response"), __contextGet("token"), __contextGet("log"), __contextGet("db")), [0], "closure")).catch(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4) => (error) => {
438
+ }), [0], "closure")).catch(__withArity(__bindContexts((error) => {
344
439
  const __contextScope = [];
345
440
  try {
346
- __contextScope.push(__contextSet("server", __capturedContext0));
347
- __contextScope.push(__contextSet("request", __capturedContext1));
348
- __contextScope.push(__contextSet("log", __capturedContext2));
349
- __contextScope.push(__contextSet("db", __capturedContext3));
350
- __contextScope.push(__contextSet("token", __capturedContext4));
351
441
  error = __boundary(error);
352
442
  closeScope(__contextGet("db"), false);
353
443
  if (__requireBoolean(__isKindof(error, HttpFailure))) {
@@ -363,16 +453,18 @@ export class Runtime {
363
453
  } finally {
364
454
  __contextRestore(__contextScope);
365
455
  }
366
- })(__contextGet("server"), __contextGet("request"), __contextGet("log"), __contextGet("db"), __contextGet("token")), [0], "closure")).finally(((__capturedContext0) => () => {
456
+ }), [0], "closure")).finally(__bindContexts(() => {
367
457
  const __contextScope = [];
368
458
  try {
369
- __contextScope.push(__contextSet("db", __capturedContext0));
459
+ if (__requireBoolean(__contextGet("clientIdentity") !== null)) {
460
+ __contextGet("clientIdentity").closed = true;
461
+ }
370
462
  closeScope(__contextGet("db"), false);
371
463
  return null;
372
464
  } finally {
373
465
  __contextRestore(__contextScope);
374
466
  }
375
- })(__contextGet("db")));
467
+ }));
376
468
  return null;
377
469
  } finally {
378
470
  __contextRestore(__contextScope);
@@ -413,11 +505,16 @@ export class Runtime {
413
505
  let child = __normalize(__hostGlobal(["Object"]).assign({ }, __contextGet("request"), { headers: __normalize(__hostGlobal(["Object"]).assign({ }, __normalize(__contextGet("request").headers), { authorization: `Bearer ${encoded}` })) }));
414
506
  pending = this.#refreshOne(pending, results, encoded, owner, child);
415
507
  }
416
- return pending.then(__withArity((ignored) => {
417
- ignored = __boundary(ignored);
418
- return results;
419
- return null;
420
- }, [0], "closure"));
508
+ return pending.then(__withArity(__bindContexts((ignored) => {
509
+ const __contextScope = [];
510
+ try {
511
+ ignored = __boundary(ignored);
512
+ return results;
513
+ return null;
514
+ } finally {
515
+ __contextRestore(__contextScope);
516
+ }
517
+ }), [0], "closure"));
421
518
  return null;
422
519
  } finally {
423
520
  __contextRestore(__contextScope);
@@ -430,7 +527,7 @@ export class Runtime {
430
527
  encoded = __boundary(encoded);
431
528
  owner = __boundary(owner);
432
529
  child = __boundary(child);
433
- return pending.then(__withArity((ignored) => {
530
+ return pending.then(__withArity(__bindContexts((ignored) => {
434
531
  ignored = __boundary(ignored);
435
532
  let checked = null;
436
533
  try {
@@ -443,7 +540,7 @@ export class Runtime {
443
540
  }
444
541
  return this.handle({ request: child, body: "{}", internal: true });
445
542
  return null;
446
- }, [0], "closure")).then(__withArity((result) => {
543
+ }), [0], "closure")).then(__withArity(__bindContexts((result) => {
447
544
  result = __boundary(result);
448
545
  let updated = null;
449
546
  if (__requireBoolean(__normalize(result.status) < 400)) {
@@ -452,7 +549,7 @@ export class Runtime {
452
549
  __add(results, { previous: encoded, token: updated, status: __normalize(result.status) });
453
550
  return null;
454
551
  return null;
455
- }, [0], "closure"));
552
+ }), [0], "closure"));
456
553
  return null;
457
554
  }
458
555
  #event(name, input) {
@@ -464,45 +561,42 @@ export class Runtime {
464
561
  if (__requireBoolean(!__requireBoolean(__includes(name, __normalize(this.#events))))) {
465
562
  return null;
466
563
  }
467
- trace(`${requestIdentity(__normalize(__contextGet("request").id), __contextGet("token"))} Running event ${name}`);
564
+ let identity = `[${__normalize(__contextGet("request").id)}]`;
565
+ if (__requireBoolean(__contextGet("token") !== null)) {
566
+ identity = requestIdentity(__normalize(__contextGet("request").id), __contextGet("token"));
567
+ }
568
+ trace(`${identity} Running event ${name}`);
468
569
  let handler = __normalize(this.#events[name]);
469
570
  __normalize(threads.parentPort.postMessage({ kind: "event", id: __normalize(__contextGet("request").id) }));
470
- return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4, __capturedContext5) => (ignored) => {
571
+ return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(__bindContexts((ignored) => {
471
572
  const __contextScope = [];
472
573
  try {
473
- __contextScope.push(__contextSet("server", __capturedContext0));
474
- __contextScope.push(__contextSet("request", __capturedContext1));
475
- __contextScope.push(__contextSet("response", __capturedContext2));
476
- __contextScope.push(__contextSet("token", __capturedContext3));
477
- __contextScope.push(__contextSet("log", __capturedContext4));
478
- __contextScope.push(__contextSet("db", __capturedContext5));
479
574
  ignored = __boundary(ignored);
480
575
  return __call(handler, input);
481
576
  return null;
482
577
  } finally {
483
578
  __contextRestore(__contextScope);
484
579
  }
485
- })(__contextGet("server"), __contextGet("request"), __contextGet("response"), __contextGet("token"), __contextGet("log"), __contextGet("db")), [0], "closure")).finally(((__capturedContext0, __capturedContext1) => () => {
580
+ }), [0], "closure")).finally(__bindContexts(() => {
486
581
  const __contextScope = [];
487
582
  try {
488
- __contextScope.push(__contextSet("server", __capturedContext0));
489
- __contextScope.push(__contextSet("request", __capturedContext1));
490
583
  __normalize(threads.parentPort.postMessage({ kind: "handler", id: __normalize(__contextGet("request").id) }));
491
584
  return null;
492
585
  } finally {
493
586
  __contextRestore(__contextScope);
494
587
  }
495
- })(__contextGet("server"), __contextGet("request")));
588
+ }));
496
589
  return null;
497
590
  } finally {
498
591
  __contextRestore(__contextScope);
499
592
  }
500
593
  }
501
- #validate() {
594
+ #validate(force = false) {
502
595
  const __contextScope = [];
503
596
  try {
597
+ force = __boundary(force);
504
598
  let claims = __normalize(__contextGet("token").snapshot());
505
- if (__requireBoolean(__finite(__normalize(__hostGlobal(["Date"]).now()) - __normalize(claims.validatedAt)) < 1000)) {
599
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(force)) && __requireBoolean(__finite(__normalize(__hostGlobal(["Date"]).now()) - __normalize(claims.validatedAt)) < 1000))) {
506
600
  return null;
507
601
  }
508
602
  if (__requireBoolean(!__requireBoolean(__includes("validate", __normalize(this.#events))))) {
@@ -510,25 +604,18 @@ export class Runtime {
510
604
  }
511
605
  let original = __normalize(__hostGlobal(["JSON"]).stringify(claims));
512
606
  __contextScope.push(__contextSet("token", new Token(claims, true)));
513
- return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4, __capturedContext5) => (ignored) => {
607
+ return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(__bindContexts((ignored) => {
514
608
  const __contextScope = [];
515
609
  try {
516
- __contextScope.push(__contextSet("token", __capturedContext0));
517
- __contextScope.push(__contextSet("response", __capturedContext1));
518
- __contextScope.push(__contextSet("server", __capturedContext2));
519
- __contextScope.push(__contextSet("request", __capturedContext3));
520
- __contextScope.push(__contextSet("log", __capturedContext4));
521
- __contextScope.push(__contextSet("db", __capturedContext5));
522
610
  ignored = __boundary(ignored);
523
611
  return this.#event("validate", null);
524
612
  return null;
525
613
  } finally {
526
614
  __contextRestore(__contextScope);
527
615
  }
528
- })(__contextGet("token"), __contextGet("response"), __contextGet("server"), __contextGet("request"), __contextGet("log"), __contextGet("db")), [0], "closure")).then(__withArity(((__capturedContext0) => (valid) => {
616
+ }), [0], "closure")).then(__withArity(__bindContexts((valid) => {
529
617
  const __contextScope = [];
530
618
  try {
531
- __contextScope.push(__contextSet("token", __capturedContext0));
532
619
  valid = __boundary(valid);
533
620
  if (__requireBoolean(!__requireBoolean(__isBool(valid)))) {
534
621
  throw "Validate must return boolean";
@@ -545,7 +632,7 @@ export class Runtime {
545
632
  } finally {
546
633
  __contextRestore(__contextScope);
547
634
  }
548
- })(__contextGet("token")), [0], "closure"));
635
+ }), [0], "closure"));
549
636
  return null;
550
637
  } finally {
551
638
  __contextRestore(__contextScope);
@@ -565,25 +652,18 @@ export class Runtime {
565
652
  __contextScope.push(__contextSet("response", __normalize(__hostGlobal(["Object"]).freeze({ }))));
566
653
  let metadata = { input: input, output: output, status: __normalize(result.status) };
567
654
  __normalize(threads.parentPort.postMessage({ kind: "prepared", logIdentity: requestIdentity(id, __contextGet("token")), id: id, result: result }));
568
- return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3, __capturedContext4, __capturedContext5) => (ignored) => {
655
+ return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(__bindContexts((ignored) => {
569
656
  const __contextScope = [];
570
657
  try {
571
- __contextScope.push(__contextSet("token", __capturedContext0));
572
- __contextScope.push(__contextSet("server", __capturedContext1));
573
- __contextScope.push(__contextSet("request", __capturedContext2));
574
- __contextScope.push(__contextSet("response", __capturedContext3));
575
- __contextScope.push(__contextSet("log", __capturedContext4));
576
- __contextScope.push(__contextSet("db", __capturedContext5));
577
658
  ignored = __boundary(ignored);
578
659
  return this.#event("request", __normalize(__hostGlobal(["Object"]).freeze(metadata)));
579
660
  return null;
580
661
  } finally {
581
662
  __contextRestore(__contextScope);
582
663
  }
583
- })(__contextGet("token"), __contextGet("server"), __contextGet("request"), __contextGet("response"), __contextGet("log"), __contextGet("db")), [0], "closure")).then(__withArity(((__capturedContext0) => (ignored) => {
664
+ }), [0], "closure")).then(__withArity(__bindContexts((ignored) => {
584
665
  const __contextScope = [];
585
666
  try {
586
- __contextScope.push(__contextSet("db", __capturedContext0));
587
667
  ignored = __boundary(ignored);
588
668
  closeScope(__contextGet("db"), true);
589
669
  return null;
@@ -591,13 +671,9 @@ export class Runtime {
591
671
  } finally {
592
672
  __contextRestore(__contextScope);
593
673
  }
594
- })(__contextGet("db")), [0], "closure")).catch(__withArity(((__capturedContext0, __capturedContext1, __capturedContext2, __capturedContext3) => (error) => {
674
+ }), [0], "closure")).catch(__withArity(__bindContexts((error) => {
595
675
  const __contextScope = [];
596
676
  try {
597
- __contextScope.push(__contextSet("log", __capturedContext0));
598
- __contextScope.push(__contextSet("db", __capturedContext1));
599
- __contextScope.push(__contextSet("request", __capturedContext2));
600
- __contextScope.push(__contextSet("token", __capturedContext3));
601
677
  error = __boundary(error);
602
678
  closeScope(__contextGet("db"), false);
603
679
  __normalize(__contextGet("log").err(`${requestIdentity(__normalize(__contextGet("request").id), __contextGet("token"))} Request event failed: ${errorDetails(error)}`));
@@ -606,20 +682,24 @@ export class Runtime {
606
682
  } finally {
607
683
  __contextRestore(__contextScope);
608
684
  }
609
- })(__contextGet("log"), __contextGet("db"), __contextGet("request"), __contextGet("token")), [0], "closure")).then(__withArity((ignored) => {
610
- ignored = __boundary(ignored);
611
- return result;
612
- return null;
613
- }, [0], "closure")).finally(((__capturedContext0) => () => {
685
+ }), [0], "closure")).then(__withArity(__bindContexts((ignored) => {
686
+ const __contextScope = [];
687
+ try {
688
+ ignored = __boundary(ignored);
689
+ return result;
690
+ return null;
691
+ } finally {
692
+ __contextRestore(__contextScope);
693
+ }
694
+ }), [0], "closure")).finally(__bindContexts(() => {
614
695
  const __contextScope = [];
615
696
  try {
616
- __contextScope.push(__contextSet("db", __capturedContext0));
617
697
  closeScope(__contextGet("db"), false);
618
698
  return null;
619
699
  } finally {
620
700
  __contextRestore(__contextScope);
621
701
  }
622
- })(__contextGet("db")));
702
+ }));
623
703
  return null;
624
704
  } finally {
625
705
  __contextRestore(__contextScope);
@@ -672,4 +752,39 @@ function closeScope(db, strict) {
672
752
  return null;
673
753
  return null;
674
754
  }
755
+
756
+ function needLogin(route, status, reason) {
757
+ const __contextScope = [];
758
+ try {
759
+ __requireArity(arguments.length, [0, 1, 2], "needLogin");
760
+ route = __boundary(route);
761
+ status = __boundary(status);
762
+ reason = __boundary(reason);
763
+ throw new HttpFailure(status, reason, { "content-type": "application/json; charset=utf-8", "cache-control": "no-store", "x-lango-need-login": "1" }, { reason: reason, path: __normalize(__contextGet("request").path), policy: __normalize(route.policy), reauthSeconds: __normalize(route.reauthSeconds), userId: __normalize(__contextGet("token").get("userId")) });
764
+ return null;
765
+ } finally {
766
+ __contextRestore(__contextScope);
767
+ }
768
+ }
769
+
770
+ function authenticationChanged(previous, current) {
771
+ const __contextScope = [];
772
+ try {
773
+ __requireArity(arguments.length, [0, 1], "authenticationChanged");
774
+ previous = __boundary(previous);
775
+ current = __boundary(current);
776
+ if (__requireBoolean(__requireBoolean(__contextGet("clientIdentity") !== null) && __requireBoolean(__normalize(__contextGet("clientIdentity").rotate)))) {
777
+ return true;
778
+ }
779
+ for (const name of ["auth", "userId", "grantId", "authenticatedAt"]) {
780
+ if (__requireBoolean(__toJson(__normalize(previous[name])) !== __toJson(__normalize(current[name])))) {
781
+ return true;
782
+ }
783
+ }
784
+ return false;
785
+ return null;
786
+ } finally {
787
+ __contextRestore(__contextScope);
788
+ }
789
+ }
675
790
  //# sourceMappingURL=runtime.mjs.map