@lango-project/lango-server 0.2.2 → 0.2.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/README.md +252 -29
- package/dist/.lango/runtime.mjs +16 -0
- package/dist/body_reader.mjs +15 -14
- package/dist/body_reader.mjs.map +1 -1
- package/dist/builder.mjs +26 -2
- package/dist/builder.mjs.map +1 -1
- package/dist/cli/.lango/runtime.mjs +16 -0
- package/dist/cli/body_reader.mjs +15 -14
- package/dist/cli/body_reader.mjs.map +1 -1
- package/dist/cli/client_identity.mjs +250 -0
- package/dist/cli/client_identity.mjs.map +1 -0
- package/dist/cli/database.mjs +23 -8
- package/dist/cli/database.mjs.map +1 -1
- package/dist/cli/database_scope.mjs +11 -0
- package/dist/cli/database_scope.mjs.map +1 -1
- package/dist/cli/endpoint.mjs +11 -7
- package/dist/cli/endpoint.mjs.map +1 -1
- package/dist/cli/longpoll_broker.mjs +9 -8
- package/dist/cli/longpoll_broker.mjs.map +1 -1
- package/dist/cli/longpoll_options.mjs +8 -7
- package/dist/cli/longpoll_options.mjs.map +1 -1
- package/dist/cli/longpoll_session.mjs +11 -10
- package/dist/cli/longpoll_session.mjs.map +1 -1
- package/dist/cli/middleware.mjs +1 -1
- package/dist/cli/middleware.mjs.map +1 -1
- package/dist/cli/options.mjs +41 -4
- package/dist/cli/options.mjs.map +1 -1
- package/dist/cli/origin.mjs +87 -0
- package/dist/cli/origin.mjs.map +1 -0
- package/dist/cli/policy.mjs +121 -0
- package/dist/cli/policy.mjs.map +1 -0
- package/dist/cli/postgres_adapter.mjs +15 -3
- package/dist/cli/postgres_adapter.mjs.map +1 -1
- package/dist/cli/routes.mjs +69 -18
- package/dist/cli/routes.mjs.map +1 -1
- package/dist/cli/server.mjs +88 -81
- package/dist/cli/server.mjs.map +1 -1
- package/dist/cli/static_files.mjs +5 -4
- package/dist/cli/static_files.mjs.map +1 -1
- package/dist/client_identity.mjs +250 -0
- package/dist/client_identity.mjs.map +1 -0
- package/dist/database.mjs +23 -8
- package/dist/database.mjs.map +1 -1
- package/dist/database_action.mjs +71 -0
- package/dist/database_action.mjs.map +1 -0
- package/dist/database_scope.mjs +11 -0
- package/dist/database_scope.mjs.map +1 -1
- package/dist/endpoint.mjs +11 -7
- package/dist/endpoint.mjs.map +1 -1
- package/dist/execution_failure.mjs +30 -0
- package/dist/execution_failure.mjs.map +1 -0
- package/dist/execution_identity.mjs +103 -0
- package/dist/execution_identity.mjs.map +1 -0
- package/dist/execution_log.mjs +47 -0
- package/dist/execution_log.mjs.map +1 -0
- package/dist/execution_runtime.mjs +201 -0
- package/dist/execution_runtime.mjs.map +1 -0
- package/dist/execution_worker.mjs +41 -0
- package/dist/execution_worker.mjs.map +1 -0
- package/dist/executor.mjs +384 -0
- package/dist/executor.mjs.map +1 -0
- package/dist/group.mjs +49 -16
- package/dist/group.mjs.map +1 -1
- package/dist/http_failure.mjs +9 -2
- package/dist/http_failure.mjs.map +1 -1
- package/dist/job_configuration.mjs +37 -0
- package/dist/job_configuration.mjs.map +1 -0
- package/dist/job_context.mjs +61 -0
- package/dist/job_context.mjs.map +1 -0
- package/dist/library.mjs +31 -0
- package/dist/library.mjs.map +1 -1
- package/dist/longpoll_broker.mjs +9 -8
- package/dist/longpoll_broker.mjs.map +1 -1
- package/dist/longpoll_options.mjs +8 -7
- package/dist/longpoll_options.mjs.map +1 -1
- package/dist/longpoll_session.mjs +11 -10
- package/dist/longpoll_session.mjs.map +1 -1
- package/dist/middleware.mjs +1 -1
- package/dist/middleware.mjs.map +1 -1
- package/dist/module_loader.mjs +44 -0
- package/dist/module_loader.mjs.map +1 -0
- package/dist/options.mjs +41 -4
- package/dist/options.mjs.map +1 -1
- package/dist/origin.mjs +87 -0
- package/dist/origin.mjs.map +1 -0
- package/dist/policy.mjs +121 -0
- package/dist/policy.mjs.map +1 -0
- package/dist/postgres_adapter.mjs +15 -3
- package/dist/postgres_adapter.mjs.map +1 -1
- package/dist/postgres_worker.mjs +82 -19
- package/dist/postgres_worker.mjs.map +1 -1
- package/dist/queue_provider.mjs +52 -0
- package/dist/queue_provider.mjs.map +1 -0
- package/dist/response.mjs +1 -1
- package/dist/response.mjs.map +1 -1
- package/dist/route_info.mjs +1 -1
- package/dist/route_info.mjs.map +1 -1
- package/dist/routes.mjs +69 -18
- package/dist/routes.mjs.map +1 -1
- package/dist/runtime.mjs +287 -132
- package/dist/runtime.mjs.map +1 -1
- package/dist/server.mjs +88 -81
- package/dist/server.mjs.map +1 -1
- package/dist/server_context.mjs +21 -9
- package/dist/server_context.mjs.map +1 -1
- package/dist/static_files.mjs +5 -4
- package/dist/static_files.mjs.map +1 -1
- package/dist/token.mjs +34 -7
- package/dist/token.mjs.map +1 -1
- package/dist/tokens.mjs +11 -2
- package/dist/tokens.mjs.map +1 -1
- package/dist/worker.mjs +7 -6
- package/dist/worker.mjs.map +1 -1
- package/docs/jobs.md +123 -0
- package/docs/security.md +420 -0
- package/examples/security/README.md +160 -0
- package/examples/security/deploy.xml +12 -0
- package/examples/security/handlers.lango +610 -0
- package/examples/security/package.json +19 -0
- package/examples/security/passwords.lango +61 -0
- package/examples/security/routes.xml +14 -0
- package/examples/security/schema.lango +91 -0
- package/examples/security/setup.lango +30 -0
- package/package.json +14 -6
- package/src/builder.lango +22 -0
- package/src/client_identity.lango +184 -0
- package/src/database.lango +15 -4
- package/src/database_action.lango +30 -0
- package/src/database_scope.lango +7 -0
- package/src/endpoint.lango +12 -5
- package/src/execution_failure.lango +14 -0
- package/src/execution_identity.lango +71 -0
- package/src/execution_log.lango +28 -0
- package/src/execution_runtime.lango +135 -0
- package/src/execution_worker.lango +32 -0
- package/src/executor.lango +284 -0
- package/src/group.lango +36 -21
- package/src/http_failure.lango +9 -5
- package/src/job_configuration.lango +29 -0
- package/src/job_context.lango +44 -0
- package/src/library.lango +21 -0
- package/src/longpoll_options.lango +11 -5
- package/src/middleware.lango +1 -1
- package/src/module_loader.lango +25 -0
- package/src/options.lango +41 -2
- package/src/origin.lango +56 -0
- package/src/policy.lango +84 -0
- package/src/postgres_adapter.lango +12 -0
- package/src/postgres_worker.lango +54 -3
- package/src/queue_provider.lango +30 -0
- package/src/response.lango +1 -1
- package/src/route_info.lango +2 -0
- package/src/routes.lango +74 -15
- package/src/runtime.lango +173 -94
- package/src/server.lango +9 -2
- package/src/server_context.lango +10 -2
- package/src/token.lango +26 -5
- package/src/tokens.lango +10 -3
package/src/routes.lango
CHANGED
|
@@ -19,10 +19,12 @@ import "./longpoll_options"
|
|
|
19
19
|
export class Routes(root)
|
|
20
20
|
methods
|
|
21
21
|
endpoint(spec)
|
|
22
|
+
job(spec)
|
|
23
|
+
jobs()
|
|
22
24
|
addMiddleware(name, spec)
|
|
23
25
|
middlewares()
|
|
24
26
|
longpoll(spec)
|
|
25
|
-
include(spec, auth = null)
|
|
27
|
+
include(spec, auth = null, policy = null, reauthSeconds = null)
|
|
26
28
|
xml(file)
|
|
27
29
|
definitions()
|
|
28
30
|
events()
|
|
@@ -31,6 +33,7 @@ export class Routes(root)
|
|
|
31
33
|
Routes(root)
|
|
32
34
|
@root = path/resolve(root)
|
|
33
35
|
@items = []
|
|
36
|
+
@jobs = []
|
|
34
37
|
@events = {}
|
|
35
38
|
@middlewares = Object/create(null)
|
|
36
39
|
|
|
@@ -42,6 +45,10 @@ Routes:endpoint(spec)
|
|
|
42
45
|
module: resolveModule(item.module, @root)
|
|
43
46
|
handler: item.handler
|
|
44
47
|
auth: item.auth
|
|
48
|
+
policy: item.policy
|
|
49
|
+
reauthSeconds: item.reauthSeconds
|
|
50
|
+
reauthExplicit: item.reauthExplicit
|
|
51
|
+
legacyAuth: item.legacyAuth
|
|
45
52
|
middleware: item.middleware
|
|
46
53
|
})
|
|
47
54
|
return @this
|
|
@@ -56,6 +63,35 @@ Routes:checkPath(path)
|
|
|
56
63
|
throw "Conflicting endpoint pattern: ${path}"
|
|
57
64
|
return null
|
|
58
65
|
|
|
66
|
+
Routes:job(spec)
|
|
67
|
+
allowed = ["path", "module", "handler", "queue", "policy", "auth", "reauthSeconds"]
|
|
68
|
+
for spec as key: value
|
|
69
|
+
if !allowed.includes(key)
|
|
70
|
+
throw "Unknown job route option: ${key}"
|
|
71
|
+
item = new Endpoint(spec)
|
|
72
|
+
if item.path.includes("*") or item.path.includes("{") or item.path.includes("}")
|
|
73
|
+
throw "Job routes require an exact path"
|
|
74
|
+
if !is_string(spec.queue) or spec.queue.trim() != spec.queue or spec.queue == ""
|
|
75
|
+
throw "Job route requires a queue name"
|
|
76
|
+
for @jobs as other
|
|
77
|
+
if other.path == item.path
|
|
78
|
+
throw "Duplicate job route: ${item.path}"
|
|
79
|
+
@jobs.add({
|
|
80
|
+
path: item.path
|
|
81
|
+
module: resolveModule(item.module, @root)
|
|
82
|
+
handler: item.handler
|
|
83
|
+
queue: spec.queue
|
|
84
|
+
policy: item.policy
|
|
85
|
+
auth: item.auth
|
|
86
|
+
reauthSeconds: item.reauthSeconds
|
|
87
|
+
reauthExplicit: item.reauthExplicit
|
|
88
|
+
legacyAuth: item.legacyAuth
|
|
89
|
+
})
|
|
90
|
+
return @this
|
|
91
|
+
|
|
92
|
+
Routes:jobs()
|
|
93
|
+
return from_json(to_json(@jobs))
|
|
94
|
+
|
|
59
95
|
Routes:addMiddleware(name, spec)
|
|
60
96
|
middlewareName(name)
|
|
61
97
|
if Object/hasOwn(@middlewares, name)
|
|
@@ -98,8 +134,8 @@ Routes:event(spec)
|
|
|
98
134
|
}
|
|
99
135
|
return @this
|
|
100
136
|
|
|
101
|
-
Routes:include(spec, auth = null)
|
|
102
|
-
includeAt(spec, @root, "", [], auth)
|
|
137
|
+
Routes:include(spec, auth = null, policy = null, reauthSeconds = null)
|
|
138
|
+
includeAt(spec, @root, "", [], auth, policy, reauthSeconds)
|
|
103
139
|
return @this
|
|
104
140
|
|
|
105
141
|
Routes:xml(file)
|
|
@@ -130,7 +166,7 @@ joinPath(prefix, suffix)
|
|
|
130
166
|
return "${prefix}/"
|
|
131
167
|
return "${prefix}${suffix}"
|
|
132
168
|
|
|
133
|
-
Routes:includeAt(spec, directory, prefix, stack, inheritedAuth = null)
|
|
169
|
+
Routes:includeAt(spec, directory, prefix, stack, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null)
|
|
134
170
|
if !is_string(spec.module) or spec.module == "" or spec.module.startsWith(".") or spec.module.startsWith("/") or spec.module.startsWith("node:")
|
|
135
171
|
throw "Include requires an npm package name"
|
|
136
172
|
mount = spec.path
|
|
@@ -146,10 +182,10 @@ Routes:includeAt(spec, directory, prefix, stack, inheritedAuth = null)
|
|
|
146
182
|
if combined == "/"
|
|
147
183
|
combined = ""
|
|
148
184
|
file = resolveModule("${spec.module}/routes.xml", directory)
|
|
149
|
-
readXml(file, combined, path/dirname(file), stack, false, inheritedAuth)
|
|
185
|
+
readXml(file, combined, path/dirname(file), stack, false, inheritedAuth, inheritedPolicy, inheritedReauthSeconds)
|
|
150
186
|
return null
|
|
151
187
|
|
|
152
|
-
Routes:readXml(file, prefix, directory, stack, allowEvents, inheritedAuth = null)
|
|
188
|
+
Routes:readXml(file, prefix, directory, stack, allowEvents, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null)
|
|
153
189
|
real = fs/realpathSync(file)
|
|
154
190
|
if stack.includes(real)
|
|
155
191
|
throw "Route include cycle: ${real}"
|
|
@@ -159,45 +195,66 @@ Routes:readXml(file, prefix, directory, stack, allowEvents, inheritedAuth = null
|
|
|
159
195
|
attributes(root, [])
|
|
160
196
|
nextStack = stack.slice()
|
|
161
197
|
nextStack.add(real)
|
|
162
|
-
collect(root, prefix, null, inheritedAuth, directory, nextStack, allowEvents)
|
|
198
|
+
collect(root, prefix, null, inheritedAuth, directory, nextStack, allowEvents, inheritedPolicy, inheritedReauthSeconds)
|
|
163
199
|
return null
|
|
164
200
|
|
|
165
|
-
Routes:collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack, allowEvents)
|
|
201
|
+
Routes:collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack, allowEvents, inheritedPolicy = null, inheritedReauthSeconds = null)
|
|
166
202
|
if parent.innerText().trim() != ""
|
|
167
203
|
throw "Route XML cannot contain text"
|
|
168
204
|
for parent.children() as tag
|
|
169
205
|
spec = tag.attributes()
|
|
170
206
|
mod = spec.module
|
|
171
207
|
auth = spec.auth
|
|
208
|
+
policy = spec.policy
|
|
209
|
+
reauthSeconds = spec.reauthSeconds
|
|
172
210
|
if mod == null
|
|
173
211
|
mod = inheritedModule
|
|
174
|
-
if auth == null
|
|
212
|
+
if auth == null and policy == null
|
|
175
213
|
auth = inheritedAuth
|
|
214
|
+
policy = inheritedPolicy
|
|
215
|
+
if reauthSeconds == null
|
|
216
|
+
reauthSeconds = inheritedReauthSeconds
|
|
176
217
|
switch tag.tag()
|
|
218
|
+
case "job"
|
|
219
|
+
attributes(tag, ["path", "module", "handler", "queue", "auth", "policy", "reauthSeconds"])
|
|
220
|
+
leaf(tag)
|
|
221
|
+
job({
|
|
222
|
+
path: joinPath(prefix, spec.path)
|
|
223
|
+
module: resolveModule(mod, directory)
|
|
224
|
+
handler: spec.handler
|
|
225
|
+
queue: spec.queue
|
|
226
|
+
auth: auth
|
|
227
|
+
policy: policy
|
|
228
|
+
reauthSeconds: reauthSeconds
|
|
229
|
+
})
|
|
177
230
|
case "endpoint"
|
|
178
|
-
attributes(tag, ["path", "module", "handler", "auth"])
|
|
231
|
+
attributes(tag, ["path", "module", "handler", "auth", "policy", "reauthSeconds"])
|
|
179
232
|
leaf(tag)
|
|
180
233
|
endpoint({
|
|
181
234
|
path: joinPath(prefix, spec.path)
|
|
182
235
|
module: resolveModule(mod, directory)
|
|
183
236
|
handler: spec.handler
|
|
184
237
|
auth: auth
|
|
238
|
+
policy: policy
|
|
239
|
+
reauthSeconds: reauthSeconds
|
|
185
240
|
})
|
|
186
241
|
case "longpoll"
|
|
187
|
-
attributes(tag, ["path", "auth", "waitTimeoutMs", "retentionMs", "maxMessages", "fallback"])
|
|
242
|
+
attributes(tag, ["path", "auth", "policy", "reauthSeconds", "waitTimeoutMs", "retentionMs", "maxMessages", "fallback"])
|
|
188
243
|
leaf(tag)
|
|
189
244
|
spec.path = joinPath(prefix, spec.path)
|
|
190
245
|
spec.auth = auth
|
|
246
|
+
spec.policy = policy
|
|
247
|
+
spec.reauthSeconds = reauthSeconds
|
|
191
248
|
if spec.fallback != null
|
|
192
249
|
spec.fallback = joinPath(prefix, spec.fallback)
|
|
193
250
|
longpoll(spec)
|
|
194
251
|
case "group"
|
|
195
|
-
attributes(tag, ["path", "module", "auth"])
|
|
196
|
-
collect(tag, joinPath(prefix, spec.path), mod, auth, directory, stack, false)
|
|
252
|
+
attributes(tag, ["path", "module", "auth", "policy", "reauthSeconds"])
|
|
253
|
+
collect(tag, joinPath(prefix, spec.path), mod, auth, directory, stack, false, policy, reauthSeconds)
|
|
197
254
|
case "include"
|
|
198
255
|
attributes(tag, ["module", "path"])
|
|
199
256
|
leaf(tag)
|
|
200
|
-
includeAt(spec, directory, prefix, stack, auth)
|
|
257
|
+
includeAt(spec, directory, prefix, stack, auth, policy, reauthSeconds)
|
|
201
258
|
case "middleware"
|
|
202
259
|
attributes(tag, ["name", "module", "handler", "validate"])
|
|
203
260
|
leaf(tag)
|
|
@@ -219,13 +276,15 @@ Routes:collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack,
|
|
|
219
276
|
handler: spec.handler
|
|
220
277
|
})
|
|
221
278
|
else
|
|
222
|
-
attributes(tag, ["path", "module", "handler", "auth"])
|
|
279
|
+
attributes(tag, ["path", "module", "handler", "auth", "policy", "reauthSeconds"])
|
|
223
280
|
leaf(tag)
|
|
224
281
|
endpoint({
|
|
225
282
|
path: joinPath(prefix, spec.path)
|
|
226
283
|
module: resolveModule(mod, directory)
|
|
227
284
|
handler: spec.handler
|
|
228
285
|
auth: auth
|
|
286
|
+
policy: policy
|
|
287
|
+
reauthSeconds: reauthSeconds
|
|
229
288
|
middleware: tag.tag()
|
|
230
289
|
})
|
|
231
290
|
return null
|
package/src/runtime.lango
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
import js "node:crypto" as crypto
|
|
2
|
+
import "./module_loader"
|
|
3
|
+
class ModuleLoader
|
|
4
|
+
import "./route_info"
|
|
5
|
+
routeInfo(definitions)
|
|
6
|
+
import js global Number
|
|
7
|
+
import "./client_identity"
|
|
8
|
+
class ClientIdentity
|
|
9
|
+
import "./origin"
|
|
10
|
+
originAllowed(spec, request)
|
|
1
11
|
import "./trace"
|
|
2
12
|
trace(message)
|
|
3
13
|
import "./route_pattern"
|
|
@@ -38,19 +48,27 @@ export class Runtime(definition)
|
|
|
38
48
|
Runtime(definition)
|
|
39
49
|
refresh = (tokens)
|
|
40
50
|
return refreshTokens(tokens)
|
|
41
|
-
|
|
51
|
+
rotate = ()
|
|
52
|
+
return rotateClient()
|
|
53
|
+
@serverContext = new ServerContext(definition.routes, refresh, rotate)
|
|
42
54
|
@definition = definition
|
|
43
55
|
@routes = []
|
|
44
56
|
@events = {}
|
|
45
57
|
@tokens = new Tokens(definition.settings)
|
|
46
|
-
@
|
|
58
|
+
@loader = new ModuleLoader()
|
|
59
|
+
@queueFactory = null
|
|
47
60
|
@middlewares = Object/create(null)
|
|
48
61
|
@database = new Database(definition.settings.database, definition.settings.root)
|
|
62
|
+
@clients = null
|
|
63
|
+
if definition.settings.client != null
|
|
64
|
+
@clients = new ClientIdentity(definition.settings.client, @database)
|
|
49
65
|
|
|
50
66
|
Runtime:preload()
|
|
51
67
|
trace("Worker ${threads.threadId}: preparing database, middleware, routes and events")
|
|
52
68
|
definitions = @definition.routes
|
|
53
69
|
pending = @database.open()
|
|
70
|
+
if @definition.queueProvider != null
|
|
71
|
+
pending = prepareQueue(pending)
|
|
54
72
|
if @definition.middlewares != null
|
|
55
73
|
for @definition.middlewares as name: spec
|
|
56
74
|
pending = prepareMiddleware(pending, name, spec)
|
|
@@ -68,6 +86,20 @@ Runtime:close()
|
|
|
68
86
|
@database.close()
|
|
69
87
|
return null
|
|
70
88
|
|
|
89
|
+
Runtime:prepareQueue(pending)
|
|
90
|
+
return chain pending
|
|
91
|
+
then(ignored)
|
|
92
|
+
return @loader.target(@definition.queueProvider)
|
|
93
|
+
then(factory)
|
|
94
|
+
@queueFactory = factory
|
|
95
|
+
return null
|
|
96
|
+
|
|
97
|
+
Runtime:queueSender()
|
|
98
|
+
if @queueFactory == null
|
|
99
|
+
return null
|
|
100
|
+
factory = @queueFactory
|
|
101
|
+
return factory(@definition.queueProvider.options)
|
|
102
|
+
|
|
71
103
|
Runtime:prepareMiddleware(pending, name, spec)
|
|
72
104
|
return chain pending
|
|
73
105
|
then(ignored)
|
|
@@ -110,6 +142,9 @@ Runtime:prepareRoute(pending, route)
|
|
|
110
142
|
path: route.path
|
|
111
143
|
pattern: routePattern(route.path)
|
|
112
144
|
auth: route.auth
|
|
145
|
+
policy: route.policy
|
|
146
|
+
reauthSeconds: route.reauthSeconds
|
|
147
|
+
legacyAuth: route.legacyAuth
|
|
113
148
|
handler: handler
|
|
114
149
|
middleware: middleware
|
|
115
150
|
})
|
|
@@ -128,19 +163,7 @@ Runtime:prepareEvent(pending, name, spec)
|
|
|
128
163
|
Runtime:target(spec)
|
|
129
164
|
file = spec.module
|
|
130
165
|
trace("Worker ${threads.threadId}: resolving ${file} export ${spec.handler}")
|
|
131
|
-
|
|
132
|
-
return Promise/resolve(exported(@modules[file], spec.handler))
|
|
133
|
-
return chain import_module(file)
|
|
134
|
-
then(mod)
|
|
135
|
-
capture file
|
|
136
|
-
capture spec
|
|
137
|
-
@modules[file] = mod
|
|
138
|
-
return exported(mod, spec.handler)
|
|
139
|
-
|
|
140
|
-
exported(mod, name)
|
|
141
|
-
if !Object/hasOwn(mod, name) or !is_kindof(mod[name], Function)
|
|
142
|
-
throw "Handler is not exported: ${name}"
|
|
143
|
-
return mod[name]
|
|
166
|
+
return @loader.target(spec)
|
|
144
167
|
|
|
145
168
|
Runtime:match(path)
|
|
146
169
|
best = null
|
|
@@ -165,13 +188,13 @@ Runtime:handle(task)
|
|
|
165
188
|
context request = Object/freeze(task.request)
|
|
166
189
|
context response = new Response()
|
|
167
190
|
context log
|
|
191
|
+
if !originAllowed(@definition.settings.origin, request)
|
|
192
|
+
return failure(403, "OriginRejected")
|
|
168
193
|
fresh = request.headers.authorization == null
|
|
169
194
|
claims = null
|
|
170
195
|
try
|
|
171
|
-
if fresh
|
|
172
|
-
claims = @tokens.
|
|
173
|
-
else
|
|
174
|
-
claims = @tokens.read(request.headers.authorization)
|
|
196
|
+
if !fresh
|
|
197
|
+
claims = @tokens.read(request.headers.authorization)
|
|
175
198
|
catch error
|
|
176
199
|
log.warn("[${request.id}] ${request.method} ${request.path} Authorization rejected before handler: ${errorMessage(error)}")
|
|
177
200
|
return failure(401, "Unauthorized")
|
|
@@ -180,6 +203,11 @@ Runtime:handle(task)
|
|
|
180
203
|
trace("[${request.id}] No route matched ${request.path}")
|
|
181
204
|
return failure(404, "RouteNotFound")
|
|
182
205
|
route = matched.route
|
|
206
|
+
metadata = routeInfo([route])
|
|
207
|
+
selected = Object/freeze(metadata[0])
|
|
208
|
+
context request = Object/freeze(Object/assign({}, request, {
|
|
209
|
+
route: selected
|
|
210
|
+
}))
|
|
183
211
|
input = null
|
|
184
212
|
try
|
|
185
213
|
input = JSON/parse(task.body)
|
|
@@ -192,29 +220,70 @@ Runtime:handle(task)
|
|
|
192
220
|
input[name] = decodeURIComponent(value)
|
|
193
221
|
catch error
|
|
194
222
|
return failure(400, "InvalidPath")
|
|
195
|
-
context token = new Token(claims)
|
|
196
|
-
trace("${requestIdentity(request.id, token)} Matched route ${matched.route.path}; fresh=${fresh}")
|
|
197
223
|
context db = @database.scope()
|
|
224
|
+
context clientIdentity = null
|
|
225
|
+
restored = false
|
|
226
|
+
try
|
|
227
|
+
if @clients != null
|
|
228
|
+
context clientIdentity = @clients.begin(claims, @tokens)
|
|
229
|
+
claims = clientIdentity.claims
|
|
230
|
+
fresh = clientIdentity.fresh
|
|
231
|
+
restored = clientIdentity.restored
|
|
232
|
+
if claims == null
|
|
233
|
+
claims = @tokens.create()
|
|
234
|
+
catch error
|
|
235
|
+
closeScope(db, false)
|
|
236
|
+
if is_kindof(error, HttpFailure)
|
|
237
|
+
return error.result()
|
|
238
|
+
log.err("[${request.id}] Client identity failed: ${errorDetails(error)}")
|
|
239
|
+
return failure(500, "InternalServerError")
|
|
240
|
+
return process(task, route, input, claims, fresh, restored)
|
|
241
|
+
|
|
242
|
+
Runtime:rotateClient()
|
|
243
|
+
context clientIdentity
|
|
244
|
+
if @clients == null or clientIdentity == null
|
|
245
|
+
throw "Managed client identity is unavailable"
|
|
246
|
+
if clientIdentity.closed
|
|
247
|
+
throw "Client identity scope is closed"
|
|
248
|
+
clientIdentity.rotate = true
|
|
249
|
+
return null
|
|
250
|
+
|
|
251
|
+
Runtime:signResponse(claims)
|
|
252
|
+
context clientIdentity
|
|
253
|
+
context response
|
|
254
|
+
if @clients == null
|
|
255
|
+
return @tokens.sign(claims)
|
|
256
|
+
return @clients.publish(clientIdentity, claims, response, @tokens)
|
|
257
|
+
|
|
258
|
+
Runtime:process(task, route, input, claims, fresh, restored)
|
|
259
|
+
context request
|
|
260
|
+
context response
|
|
261
|
+
context log
|
|
262
|
+
context db
|
|
263
|
+
context token = new Token(claims)
|
|
264
|
+
sender = null
|
|
265
|
+
try
|
|
266
|
+
sender = queueSender()
|
|
267
|
+
catch error
|
|
268
|
+
closeScope(db, false)
|
|
269
|
+
closeClientScope()
|
|
270
|
+
log.err("${requestIdentity(request.id, token)} Queue preparation failed: ${errorDetails(error)}")
|
|
271
|
+
return failure(500, "InternalServerError")
|
|
272
|
+
context queue = sender
|
|
273
|
+
trace("${requestIdentity(request.id, token)} Matched route ${route.path}; fresh=${fresh}")
|
|
198
274
|
return chain Promise/resolve(null)
|
|
199
275
|
then(ignored)
|
|
200
|
-
capture server
|
|
201
|
-
capture request
|
|
202
|
-
capture response
|
|
203
|
-
capture token
|
|
204
|
-
capture log
|
|
205
|
-
capture db
|
|
206
276
|
capture fresh
|
|
277
|
+
capture restored
|
|
207
278
|
capture input
|
|
208
279
|
if fresh
|
|
209
|
-
|
|
210
|
-
|
|
280
|
+
registration = Object/assign({}, input, {
|
|
281
|
+
ip: request.remoteAddress
|
|
282
|
+
userAgent: request.headers["user-agent"]
|
|
283
|
+
})
|
|
284
|
+
return event("register", registration)
|
|
285
|
+
return validate(restored)
|
|
211
286
|
then(validatedAt)
|
|
212
|
-
capture server
|
|
213
|
-
capture request
|
|
214
|
-
capture response
|
|
215
|
-
capture token
|
|
216
|
-
capture log
|
|
217
|
-
capture db
|
|
218
287
|
capture route
|
|
219
288
|
capture input
|
|
220
289
|
capture claims
|
|
@@ -222,11 +291,22 @@ Runtime:handle(task)
|
|
|
222
291
|
if !fresh and is_number(validatedAt)
|
|
223
292
|
stampValidation(claims, validatedAt)
|
|
224
293
|
token.renew()
|
|
225
|
-
if route.
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
294
|
+
if route.policy != "anonymous" and !token.hasPolicy(route.policy)
|
|
295
|
+
if route.legacyAuth
|
|
296
|
+
response.reject(401, "Unauthorized")
|
|
297
|
+
if route.policy == "login" or !token.hasPolicy("login")
|
|
298
|
+
needLogin(route, 401, "LoginRequired")
|
|
299
|
+
response.reject(403, "Forbidden")
|
|
300
|
+
if route.reauthSeconds > 0
|
|
301
|
+
authenticatedAt = token.get("authenticatedAt")
|
|
302
|
+
now = Date/now()
|
|
303
|
+
if !Number/isSafeInteger(authenticatedAt) or authenticatedAt < 0 or authenticatedAt > now
|
|
304
|
+
needLogin(route, 403, "ReauthenticationRequired")
|
|
305
|
+
if now - authenticatedAt >= route.reauthSeconds * 1000
|
|
306
|
+
needLogin(route, 403, "ReauthenticationRequired")
|
|
307
|
+
expected = request.headers["x-lango-expected-user"]
|
|
308
|
+
if expected != null and expected != to_json(token.get("userId"))
|
|
309
|
+
response.reject(409, "IdentityChanged")
|
|
230
310
|
if route.kind == "longpoll"
|
|
231
311
|
return null
|
|
232
312
|
trace("${requestIdentity(request.id, token)} Invoking handler; middleware=${route.middleware != null}")
|
|
@@ -236,18 +316,14 @@ Runtime:handle(task)
|
|
|
236
316
|
return middleware(handler, request.path, input)
|
|
237
317
|
return handler(input)
|
|
238
318
|
then(output)
|
|
239
|
-
capture
|
|
240
|
-
capture request
|
|
241
|
-
capture response
|
|
242
|
-
capture token
|
|
243
|
-
capture log
|
|
244
|
-
capture db
|
|
319
|
+
capture crypto
|
|
245
320
|
capture task
|
|
246
321
|
capture claims
|
|
322
|
+
encoded = null
|
|
247
323
|
if task.authorize == true
|
|
248
324
|
closeScope(db, true)
|
|
249
325
|
authorizedClaims = tokenClaims(token, claims, true)
|
|
250
|
-
encoded =
|
|
326
|
+
encoded = signResponse(authorizedClaims)
|
|
251
327
|
if task.pollResult == null
|
|
252
328
|
return {
|
|
253
329
|
status: 200
|
|
@@ -268,18 +344,17 @@ Runtime:handle(task)
|
|
|
268
344
|
closeScope(db, true)
|
|
269
345
|
result = response.result(body)
|
|
270
346
|
updated = tokenClaims(token, claims, result.status < 400)
|
|
271
|
-
|
|
347
|
+
encoded = signResponse(updated)
|
|
348
|
+
result = response.result(body)
|
|
349
|
+
result.headers["x-lango-token"] = encoded
|
|
350
|
+
if result.status < 400 and authenticationChanged(claims, updated)
|
|
351
|
+
result.headers["x-lango-auth-change"] = "v1:${crypto/randomUUID()}"
|
|
272
352
|
if task.internal != true and @definition.settings.requestEvent and "request" in @events
|
|
273
353
|
return audit(request.id, result, task.body, body, updated)
|
|
274
354
|
trace("${requestIdentity(request.id, token)} Request completed status=${result.status}")
|
|
275
355
|
return result
|
|
276
356
|
catch(error)
|
|
277
|
-
capture server
|
|
278
|
-
capture request
|
|
279
|
-
capture log
|
|
280
|
-
capture db
|
|
281
357
|
capture HttpFailure
|
|
282
|
-
capture token
|
|
283
358
|
closeScope(db, false)
|
|
284
359
|
if is_kindof(error, HttpFailure)
|
|
285
360
|
return error.result()
|
|
@@ -289,7 +364,7 @@ Runtime:handle(task)
|
|
|
289
364
|
log.err("${requestIdentity(request.id, token)} Request failed: ${errorDetails(error)}")
|
|
290
365
|
return failure(500, "InternalServerError")
|
|
291
366
|
finally
|
|
292
|
-
|
|
367
|
+
closeClientScope()
|
|
293
368
|
closeScope(db, false)
|
|
294
369
|
|
|
295
370
|
Runtime:tokenClaims(token, claims, allowRenewal)
|
|
@@ -358,13 +433,12 @@ Runtime:refreshOne(pending, results, encoded, owner, child)
|
|
|
358
433
|
Runtime:event(name, input)
|
|
359
434
|
if !(name in @events)
|
|
360
435
|
return null
|
|
361
|
-
context server
|
|
362
436
|
context request
|
|
363
|
-
context response
|
|
364
437
|
context token
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
438
|
+
identity = "[${request.id}]"
|
|
439
|
+
if token != null
|
|
440
|
+
identity = requestIdentity(request.id, token)
|
|
441
|
+
trace("${identity} Running event ${name}")
|
|
368
442
|
handler = @events[name]
|
|
369
443
|
threads/parentPort/postMessage({
|
|
370
444
|
kind: "event"
|
|
@@ -372,33 +446,20 @@ Runtime:event(name, input)
|
|
|
372
446
|
})
|
|
373
447
|
return chain Promise/resolve(null)
|
|
374
448
|
then(ignored)
|
|
375
|
-
capture server
|
|
376
|
-
capture request
|
|
377
|
-
capture response
|
|
378
|
-
capture token
|
|
379
|
-
capture log
|
|
380
|
-
capture db
|
|
381
449
|
capture handler
|
|
382
450
|
capture input
|
|
383
451
|
return handler(input)
|
|
384
452
|
finally
|
|
385
|
-
capture server
|
|
386
|
-
capture request
|
|
387
453
|
capture threads
|
|
388
454
|
threads/parentPort/postMessage({
|
|
389
455
|
kind: "handler"
|
|
390
456
|
id: request.id
|
|
391
457
|
})
|
|
392
458
|
|
|
393
|
-
Runtime:validate()
|
|
394
|
-
context db
|
|
459
|
+
Runtime:validate(force = false)
|
|
395
460
|
context token
|
|
396
|
-
context response
|
|
397
|
-
context server
|
|
398
|
-
context request
|
|
399
|
-
context log
|
|
400
461
|
claims = token.snapshot()
|
|
401
|
-
if Date/now() - claims.validatedAt < 1000
|
|
462
|
+
if !force and Date/now() - claims.validatedAt < 1000
|
|
402
463
|
return null
|
|
403
464
|
if !("validate" in @events)
|
|
404
465
|
return Date/now()
|
|
@@ -406,15 +467,8 @@ Runtime:validate()
|
|
|
406
467
|
context token = new Token(claims, true)
|
|
407
468
|
return chain Promise/resolve(null)
|
|
408
469
|
then(ignored)
|
|
409
|
-
capture token
|
|
410
|
-
capture response
|
|
411
|
-
capture server
|
|
412
|
-
capture request
|
|
413
|
-
capture log
|
|
414
|
-
capture db
|
|
415
470
|
return event("validate", null)
|
|
416
471
|
then(valid)
|
|
417
|
-
capture token
|
|
418
472
|
capture original
|
|
419
473
|
capture claims
|
|
420
474
|
if !is_bool(valid)
|
|
@@ -431,11 +485,18 @@ Runtime:validate()
|
|
|
431
485
|
|
|
432
486
|
Runtime:audit(id, result, input, output, claims)
|
|
433
487
|
context db = @database.scope()
|
|
434
|
-
context server
|
|
435
488
|
context request
|
|
436
489
|
context log
|
|
437
490
|
context token = new Token(claims, true)
|
|
438
491
|
context response = Object/freeze({})
|
|
492
|
+
sender = null
|
|
493
|
+
try
|
|
494
|
+
sender = queueSender()
|
|
495
|
+
catch error
|
|
496
|
+
closeScope(db, false)
|
|
497
|
+
log.err("${requestIdentity(id, token)} Request event queue preparation failed: ${errorDetails(error)}")
|
|
498
|
+
return result
|
|
499
|
+
context queue = sender
|
|
439
500
|
metadata = {
|
|
440
501
|
input: input
|
|
441
502
|
output: output
|
|
@@ -449,23 +510,12 @@ Runtime:audit(id, result, input, output, claims)
|
|
|
449
510
|
})
|
|
450
511
|
return chain Promise/resolve(null)
|
|
451
512
|
then(ignored)
|
|
452
|
-
capture token
|
|
453
|
-
capture server
|
|
454
|
-
capture request
|
|
455
|
-
capture response
|
|
456
|
-
capture log
|
|
457
|
-
capture db
|
|
458
513
|
capture metadata
|
|
459
514
|
return event("request", Object/freeze(metadata))
|
|
460
515
|
then(ignored)
|
|
461
|
-
capture db
|
|
462
516
|
closeScope(db, true)
|
|
463
517
|
return null
|
|
464
518
|
catch(error)
|
|
465
|
-
capture log
|
|
466
|
-
capture db
|
|
467
|
-
capture request
|
|
468
|
-
capture token
|
|
469
519
|
closeScope(db, false)
|
|
470
520
|
log.err("${requestIdentity(request.id, token)} Request event failed: ${errorDetails(error)}")
|
|
471
521
|
return null
|
|
@@ -473,7 +523,6 @@ Runtime:audit(id, result, input, output, claims)
|
|
|
473
523
|
capture result
|
|
474
524
|
return result
|
|
475
525
|
finally
|
|
476
|
-
capture db
|
|
477
526
|
closeScope(db, false)
|
|
478
527
|
|
|
479
528
|
failure(status, code)
|
|
@@ -501,3 +550,33 @@ closeScope(db, strict)
|
|
|
501
550
|
if dirty and strict
|
|
502
551
|
throw "UnclosedTransaction"
|
|
503
552
|
return null
|
|
553
|
+
|
|
554
|
+
closeClientScope()
|
|
555
|
+
context clientIdentity
|
|
556
|
+
if clientIdentity != null
|
|
557
|
+
clientIdentity.closed = true
|
|
558
|
+
return null
|
|
559
|
+
|
|
560
|
+
needLogin(route, status, reason)
|
|
561
|
+
context request
|
|
562
|
+
context token
|
|
563
|
+
throw new HttpFailure(status, reason, {
|
|
564
|
+
"content-type": "application/json; charset=utf-8"
|
|
565
|
+
"cache-control": "no-store"
|
|
566
|
+
"x-lango-need-login": "1"
|
|
567
|
+
}, {
|
|
568
|
+
reason: reason
|
|
569
|
+
path: request.path
|
|
570
|
+
policy: route.policy
|
|
571
|
+
reauthSeconds: route.reauthSeconds
|
|
572
|
+
userId: token.get("userId")
|
|
573
|
+
})
|
|
574
|
+
|
|
575
|
+
authenticationChanged(previous, current)
|
|
576
|
+
context clientIdentity
|
|
577
|
+
if clientIdentity != null and clientIdentity.rotate
|
|
578
|
+
return true
|
|
579
|
+
for ["auth", "userId", "grantId", "authenticatedAt"] as name
|
|
580
|
+
if to_json(previous[name]) != to_json(current[name])
|
|
581
|
+
return true
|
|
582
|
+
return false
|
package/src/server.lango
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import "./policy"
|
|
2
|
+
resolvePolicies(routes, registry)
|
|
1
3
|
import "./trace"
|
|
2
4
|
trace(message)
|
|
3
5
|
responseLog(id, method, path, status)
|
|
@@ -6,7 +8,7 @@ import "./longpoll_broker"
|
|
|
6
8
|
import "./longpoll_session"
|
|
7
9
|
class LongpollSession
|
|
8
10
|
import "./database"
|
|
9
|
-
prepareDatabase(settings)
|
|
11
|
+
prepareDatabase(settings, root = null, client = null)
|
|
10
12
|
import "./error_details"
|
|
11
13
|
errorDetails(error)
|
|
12
14
|
import js "node:http" as http
|
|
@@ -49,12 +51,17 @@ Server(path, definition = null)
|
|
|
49
51
|
definition = {
|
|
50
52
|
settings: deployment(directory)
|
|
51
53
|
routes: routes.definitions()
|
|
54
|
+
jobs: routes.jobs()
|
|
52
55
|
events: routes.events()
|
|
53
56
|
middlewares: routes.middlewares()
|
|
54
57
|
}
|
|
55
58
|
if definition == null
|
|
56
59
|
throw "Use Server(directory) or Builder.build()"
|
|
57
60
|
@definition = from_json(to_json(definition))
|
|
61
|
+
settings = @definition.settings
|
|
62
|
+
resolvePolicies(@definition.routes, settings.policies)
|
|
63
|
+
if @definition.jobs != null
|
|
64
|
+
resolvePolicies(@definition.jobs, settings.policies)
|
|
58
65
|
@broker = new LongpollBroker(@definition.settings, @definition.routes)
|
|
59
66
|
@polls = Object/create(null)
|
|
60
67
|
@state = "new"
|
|
@@ -103,7 +110,7 @@ Server:launch()
|
|
|
103
110
|
return chain @broker.open()
|
|
104
111
|
then(ignored)
|
|
105
112
|
capture settings
|
|
106
|
-
return prepareDatabase(settings.database)
|
|
113
|
+
return prepareDatabase(settings.database, settings.root, settings.client)
|
|
107
114
|
then(ignored)
|
|
108
115
|
capture path
|
|
109
116
|
capture modules
|