@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.
- package/README.md +239 -27
- package/dist/.lango/runtime.mjs +16 -0
- package/dist/body_reader.mjs +15 -14
- package/dist/body_reader.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/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/options.mjs +29 -3
- 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 +3 -2
- package/dist/cli/postgres_adapter.mjs.map +1 -1
- package/dist/cli/routes.mjs +33 -17
- package/dist/cli/routes.mjs.map +1 -1
- package/dist/cli/server.mjs +84 -80
- 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/endpoint.mjs +11 -7
- package/dist/endpoint.mjs.map +1 -1
- package/dist/group.mjs +34 -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/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/options.mjs +29 -3
- 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 +3 -2
- package/dist/postgres_adapter.mjs.map +1 -1
- package/dist/postgres_worker.mjs +15 -14
- package/dist/postgres_worker.mjs.map +1 -1
- 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 +33 -17
- package/dist/routes.mjs.map +1 -1
- package/dist/runtime.mjs +227 -112
- package/dist/runtime.mjs.map +1 -1
- package/dist/server.mjs +84 -80
- 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/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 +12 -6
- package/src/client_identity.lango +184 -0
- package/src/database.lango +15 -4
- package/src/endpoint.lango +12 -5
- package/src/group.lango +25 -21
- package/src/http_failure.lango +9 -5
- package/src/longpoll_options.lango +11 -5
- package/src/options.lango +34 -1
- package/src/origin.lango +56 -0
- package/src/policy.lango +84 -0
- package/src/response.lango +1 -1
- package/src/route_info.lango +2 -0
- package/src/routes.lango +30 -15
- package/src/runtime.lango +131 -80
- package/src/server.lango +6 -2
- package/src/server_context.lango +10 -2
- package/src/token.lango +26 -5
- package/src/tokens.lango +10 -3
package/src/origin.lango
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import js global Object
|
|
2
|
+
import js global URL
|
|
3
|
+
import js global process
|
|
4
|
+
|
|
5
|
+
export originOptions(spec)
|
|
6
|
+
export originAllowed(spec, request)
|
|
7
|
+
|
|
8
|
+
originOptions(spec)
|
|
9
|
+
if spec == null
|
|
10
|
+
return null
|
|
11
|
+
if !is_object(spec) or is_array(spec)
|
|
12
|
+
throw "Origin configuration must be an object"
|
|
13
|
+
for Object/keys(spec) as name
|
|
14
|
+
if name != "url" and name != "urlEnv" and name != "required"
|
|
15
|
+
throw "Unknown origin option: ${name}"
|
|
16
|
+
if spec.url != null and spec.urlEnv != null
|
|
17
|
+
throw "Origin requires either url or urlEnv, not both"
|
|
18
|
+
url = spec.url
|
|
19
|
+
if spec.urlEnv != null
|
|
20
|
+
if !is_string(spec.urlEnv) or spec.urlEnv == ""
|
|
21
|
+
throw "Origin urlEnv must name an environment variable"
|
|
22
|
+
envName = spec.urlEnv
|
|
23
|
+
url = process.env[envName]
|
|
24
|
+
if !is_string(url) or url == ""
|
|
25
|
+
throw "Origin URL is required"
|
|
26
|
+
parsed = null
|
|
27
|
+
try
|
|
28
|
+
parsed = new URL(url)
|
|
29
|
+
catch error
|
|
30
|
+
throw "Origin requires an exact HTTP or HTTPS origin"
|
|
31
|
+
if (parsed.protocol != "https:" and parsed.protocol != "http:") or parsed.origin != url
|
|
32
|
+
throw "Origin requires an exact HTTP or HTTPS origin without credentials, path, query or fragment"
|
|
33
|
+
required = spec.required
|
|
34
|
+
if required == null or required == "true"
|
|
35
|
+
required = true
|
|
36
|
+
if required == "false"
|
|
37
|
+
required = false
|
|
38
|
+
if !is_bool(required)
|
|
39
|
+
throw "Origin required must be true or false"
|
|
40
|
+
return {
|
|
41
|
+
url: url
|
|
42
|
+
required: required
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
originAllowed(spec, request)
|
|
46
|
+
if spec == null
|
|
47
|
+
return true
|
|
48
|
+
headers = request.headers
|
|
49
|
+
value = headers.origin
|
|
50
|
+
if value == null
|
|
51
|
+
if spec.required
|
|
52
|
+
return false
|
|
53
|
+
if value != null and value != spec.url
|
|
54
|
+
return false
|
|
55
|
+
site = headers["sec-fetch-site"]
|
|
56
|
+
return site == null or site == "same-origin" or site == "none"
|
package/src/policy.lango
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import js global Number
|
|
2
|
+
import js global Object
|
|
3
|
+
|
|
4
|
+
export policyOptions(spec)
|
|
5
|
+
export policyRegistry(spec)
|
|
6
|
+
export resolvePolicies(routes, registry)
|
|
7
|
+
|
|
8
|
+
policyOptions(spec)
|
|
9
|
+
name = spec.policy
|
|
10
|
+
if name != null and spec.auth != null and name != spec.auth
|
|
11
|
+
throw "Endpoint policy and legacy auth cannot disagree"
|
|
12
|
+
legacy = name == null
|
|
13
|
+
if name == null
|
|
14
|
+
name = spec.auth
|
|
15
|
+
if name == null
|
|
16
|
+
name = "login"
|
|
17
|
+
if !is_string(name) or name.trim() == "" or name.includes(",")
|
|
18
|
+
throw "Endpoint policy must name one permission"
|
|
19
|
+
seconds = spec.reauthSeconds
|
|
20
|
+
if seconds == null
|
|
21
|
+
seconds = 0
|
|
22
|
+
if is_string(seconds)
|
|
23
|
+
if seconds.trim() == ""
|
|
24
|
+
throw "reauthSeconds cannot be empty"
|
|
25
|
+
seconds = Number(seconds)
|
|
26
|
+
if !Number/isSafeInteger(seconds) or seconds < 0 or seconds > 31536000
|
|
27
|
+
throw "reauthSeconds must be an integer between 0 and 31536000"
|
|
28
|
+
if seconds > 0 and name == "anonymous"
|
|
29
|
+
throw "Anonymous policy cannot require password freshness"
|
|
30
|
+
return {
|
|
31
|
+
name: name
|
|
32
|
+
reauthSeconds: seconds
|
|
33
|
+
explicit: spec.reauthSeconds != null
|
|
34
|
+
legacy: legacy
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
policyRegistry(spec)
|
|
38
|
+
if spec == null
|
|
39
|
+
return null
|
|
40
|
+
if !is_object(spec) or is_array(spec)
|
|
41
|
+
throw "Policies must be a map"
|
|
42
|
+
for spec as key: value
|
|
43
|
+
if key != "reauthSeconds" and key != "items"
|
|
44
|
+
throw "Unknown policies option: ${key}"
|
|
45
|
+
base = policyOptions({
|
|
46
|
+
policy: "login"
|
|
47
|
+
reauthSeconds: spec.reauthSeconds
|
|
48
|
+
})
|
|
49
|
+
items = spec.items
|
|
50
|
+
if !is_array(items)
|
|
51
|
+
throw "Policies require an items array"
|
|
52
|
+
result = Object/create(null)
|
|
53
|
+
for items as item
|
|
54
|
+
if !is_object(item) or is_array(item)
|
|
55
|
+
throw "Policy must be a map"
|
|
56
|
+
for item as key: value
|
|
57
|
+
if key != "name" and key != "reauthSeconds"
|
|
58
|
+
throw "Unknown policy option: ${key}"
|
|
59
|
+
name = item.name
|
|
60
|
+
if !is_string(name) or name.trim() != name or name == "" or name == "anonymous" or Object/hasOwn(result, name)
|
|
61
|
+
throw "Invalid or duplicate policy name"
|
|
62
|
+
seconds = item.reauthSeconds
|
|
63
|
+
if seconds == null
|
|
64
|
+
seconds = base.reauthSeconds
|
|
65
|
+
selected = policyOptions({
|
|
66
|
+
policy: name
|
|
67
|
+
reauthSeconds: seconds
|
|
68
|
+
})
|
|
69
|
+
result[name] = selected.reauthSeconds
|
|
70
|
+
return result
|
|
71
|
+
|
|
72
|
+
resolvePolicies(routes, registry)
|
|
73
|
+
if registry == null
|
|
74
|
+
return routes
|
|
75
|
+
for routes as route
|
|
76
|
+
if route.policy != "anonymous"
|
|
77
|
+
if !Object/hasOwn(registry, route.policy)
|
|
78
|
+
throw "Undefined route policy: ${route.policy}"
|
|
79
|
+
maximum = registry[route.policy]
|
|
80
|
+
if route.reauthExplicit and maximum > 0 and (route.reauthSeconds == 0 or route.reauthSeconds > maximum)
|
|
81
|
+
throw "Route cannot weaken policy freshness: ${route.path}"
|
|
82
|
+
if !route.reauthExplicit
|
|
83
|
+
route.reauthSeconds = maximum
|
|
84
|
+
return routes
|
package/src/response.lango
CHANGED
|
@@ -28,7 +28,7 @@ Response:header(name, value)
|
|
|
28
28
|
if !is_string(name) or !is_string(value)
|
|
29
29
|
throw "Header name and value must be strings"
|
|
30
30
|
name = name.toLowerCase()
|
|
31
|
-
if contains(["x-lango-token", "content-length", "transfer-encoding", "connection", "content-type"], name)
|
|
31
|
+
if contains(["x-lango-auth-change", "x-lango-need-login", "x-lango-token", "content-length", "transfer-encoding", "connection", "content-type"], name)
|
|
32
32
|
throw "Reserved response header: ${name}"
|
|
33
33
|
if name.includes("\r") or name.includes("\n") or value.includes("\r") or value.includes("\n")
|
|
34
34
|
throw "Invalid response header"
|
package/src/route_info.lango
CHANGED
package/src/routes.lango
CHANGED
|
@@ -22,7 +22,7 @@ export class Routes(root)
|
|
|
22
22
|
addMiddleware(name, spec)
|
|
23
23
|
middlewares()
|
|
24
24
|
longpoll(spec)
|
|
25
|
-
include(spec, auth = null)
|
|
25
|
+
include(spec, auth = null, policy = null, reauthSeconds = null)
|
|
26
26
|
xml(file)
|
|
27
27
|
definitions()
|
|
28
28
|
events()
|
|
@@ -42,6 +42,10 @@ Routes:endpoint(spec)
|
|
|
42
42
|
module: resolveModule(item.module, @root)
|
|
43
43
|
handler: item.handler
|
|
44
44
|
auth: item.auth
|
|
45
|
+
policy: item.policy
|
|
46
|
+
reauthSeconds: item.reauthSeconds
|
|
47
|
+
reauthExplicit: item.reauthExplicit
|
|
48
|
+
legacyAuth: item.legacyAuth
|
|
45
49
|
middleware: item.middleware
|
|
46
50
|
})
|
|
47
51
|
return @this
|
|
@@ -98,8 +102,8 @@ Routes:event(spec)
|
|
|
98
102
|
}
|
|
99
103
|
return @this
|
|
100
104
|
|
|
101
|
-
Routes:include(spec, auth = null)
|
|
102
|
-
includeAt(spec, @root, "", [], auth)
|
|
105
|
+
Routes:include(spec, auth = null, policy = null, reauthSeconds = null)
|
|
106
|
+
includeAt(spec, @root, "", [], auth, policy, reauthSeconds)
|
|
103
107
|
return @this
|
|
104
108
|
|
|
105
109
|
Routes:xml(file)
|
|
@@ -130,7 +134,7 @@ joinPath(prefix, suffix)
|
|
|
130
134
|
return "${prefix}/"
|
|
131
135
|
return "${prefix}${suffix}"
|
|
132
136
|
|
|
133
|
-
Routes:includeAt(spec, directory, prefix, stack, inheritedAuth = null)
|
|
137
|
+
Routes:includeAt(spec, directory, prefix, stack, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null)
|
|
134
138
|
if !is_string(spec.module) or spec.module == "" or spec.module.startsWith(".") or spec.module.startsWith("/") or spec.module.startsWith("node:")
|
|
135
139
|
throw "Include requires an npm package name"
|
|
136
140
|
mount = spec.path
|
|
@@ -146,10 +150,10 @@ Routes:includeAt(spec, directory, prefix, stack, inheritedAuth = null)
|
|
|
146
150
|
if combined == "/"
|
|
147
151
|
combined = ""
|
|
148
152
|
file = resolveModule("${spec.module}/routes.xml", directory)
|
|
149
|
-
readXml(file, combined, path/dirname(file), stack, false, inheritedAuth)
|
|
153
|
+
readXml(file, combined, path/dirname(file), stack, false, inheritedAuth, inheritedPolicy, inheritedReauthSeconds)
|
|
150
154
|
return null
|
|
151
155
|
|
|
152
|
-
Routes:readXml(file, prefix, directory, stack, allowEvents, inheritedAuth = null)
|
|
156
|
+
Routes:readXml(file, prefix, directory, stack, allowEvents, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null)
|
|
153
157
|
real = fs/realpathSync(file)
|
|
154
158
|
if stack.includes(real)
|
|
155
159
|
throw "Route include cycle: ${real}"
|
|
@@ -159,45 +163,54 @@ Routes:readXml(file, prefix, directory, stack, allowEvents, inheritedAuth = null
|
|
|
159
163
|
attributes(root, [])
|
|
160
164
|
nextStack = stack.slice()
|
|
161
165
|
nextStack.add(real)
|
|
162
|
-
collect(root, prefix, null, inheritedAuth, directory, nextStack, allowEvents)
|
|
166
|
+
collect(root, prefix, null, inheritedAuth, directory, nextStack, allowEvents, inheritedPolicy, inheritedReauthSeconds)
|
|
163
167
|
return null
|
|
164
168
|
|
|
165
|
-
Routes:collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack, allowEvents)
|
|
169
|
+
Routes:collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack, allowEvents, inheritedPolicy = null, inheritedReauthSeconds = null)
|
|
166
170
|
if parent.innerText().trim() != ""
|
|
167
171
|
throw "Route XML cannot contain text"
|
|
168
172
|
for parent.children() as tag
|
|
169
173
|
spec = tag.attributes()
|
|
170
174
|
mod = spec.module
|
|
171
175
|
auth = spec.auth
|
|
176
|
+
policy = spec.policy
|
|
177
|
+
reauthSeconds = spec.reauthSeconds
|
|
172
178
|
if mod == null
|
|
173
179
|
mod = inheritedModule
|
|
174
|
-
if auth == null
|
|
180
|
+
if auth == null and policy == null
|
|
175
181
|
auth = inheritedAuth
|
|
182
|
+
policy = inheritedPolicy
|
|
183
|
+
if reauthSeconds == null
|
|
184
|
+
reauthSeconds = inheritedReauthSeconds
|
|
176
185
|
switch tag.tag()
|
|
177
186
|
case "endpoint"
|
|
178
|
-
attributes(tag, ["path", "module", "handler", "auth"])
|
|
187
|
+
attributes(tag, ["path", "module", "handler", "auth", "policy", "reauthSeconds"])
|
|
179
188
|
leaf(tag)
|
|
180
189
|
endpoint({
|
|
181
190
|
path: joinPath(prefix, spec.path)
|
|
182
191
|
module: resolveModule(mod, directory)
|
|
183
192
|
handler: spec.handler
|
|
184
193
|
auth: auth
|
|
194
|
+
policy: policy
|
|
195
|
+
reauthSeconds: reauthSeconds
|
|
185
196
|
})
|
|
186
197
|
case "longpoll"
|
|
187
|
-
attributes(tag, ["path", "auth", "waitTimeoutMs", "retentionMs", "maxMessages", "fallback"])
|
|
198
|
+
attributes(tag, ["path", "auth", "policy", "reauthSeconds", "waitTimeoutMs", "retentionMs", "maxMessages", "fallback"])
|
|
188
199
|
leaf(tag)
|
|
189
200
|
spec.path = joinPath(prefix, spec.path)
|
|
190
201
|
spec.auth = auth
|
|
202
|
+
spec.policy = policy
|
|
203
|
+
spec.reauthSeconds = reauthSeconds
|
|
191
204
|
if spec.fallback != null
|
|
192
205
|
spec.fallback = joinPath(prefix, spec.fallback)
|
|
193
206
|
longpoll(spec)
|
|
194
207
|
case "group"
|
|
195
|
-
attributes(tag, ["path", "module", "auth"])
|
|
196
|
-
collect(tag, joinPath(prefix, spec.path), mod, auth, directory, stack, false)
|
|
208
|
+
attributes(tag, ["path", "module", "auth", "policy", "reauthSeconds"])
|
|
209
|
+
collect(tag, joinPath(prefix, spec.path), mod, auth, directory, stack, false, policy, reauthSeconds)
|
|
197
210
|
case "include"
|
|
198
211
|
attributes(tag, ["module", "path"])
|
|
199
212
|
leaf(tag)
|
|
200
|
-
includeAt(spec, directory, prefix, stack, auth)
|
|
213
|
+
includeAt(spec, directory, prefix, stack, auth, policy, reauthSeconds)
|
|
201
214
|
case "middleware"
|
|
202
215
|
attributes(tag, ["name", "module", "handler", "validate"])
|
|
203
216
|
leaf(tag)
|
|
@@ -219,13 +232,15 @@ Routes:collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack,
|
|
|
219
232
|
handler: spec.handler
|
|
220
233
|
})
|
|
221
234
|
else
|
|
222
|
-
attributes(tag, ["path", "module", "handler", "auth"])
|
|
235
|
+
attributes(tag, ["path", "module", "handler", "auth", "policy", "reauthSeconds"])
|
|
223
236
|
leaf(tag)
|
|
224
237
|
endpoint({
|
|
225
238
|
path: joinPath(prefix, spec.path)
|
|
226
239
|
module: resolveModule(mod, directory)
|
|
227
240
|
handler: spec.handler
|
|
228
241
|
auth: auth
|
|
242
|
+
policy: policy
|
|
243
|
+
reauthSeconds: reauthSeconds
|
|
229
244
|
middleware: tag.tag()
|
|
230
245
|
})
|
|
231
246
|
return null
|
package/src/runtime.lango
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
import js "node:crypto" as crypto
|
|
2
|
+
import "./route_info"
|
|
3
|
+
routeInfo(definitions)
|
|
4
|
+
import js global Number
|
|
5
|
+
import "./client_identity"
|
|
6
|
+
class ClientIdentity
|
|
7
|
+
import "./origin"
|
|
8
|
+
originAllowed(spec, request)
|
|
1
9
|
import "./trace"
|
|
2
10
|
trace(message)
|
|
3
11
|
import "./route_pattern"
|
|
@@ -38,7 +46,9 @@ export class Runtime(definition)
|
|
|
38
46
|
Runtime(definition)
|
|
39
47
|
refresh = (tokens)
|
|
40
48
|
return refreshTokens(tokens)
|
|
41
|
-
|
|
49
|
+
rotate = ()
|
|
50
|
+
return rotateClient()
|
|
51
|
+
@serverContext = new ServerContext(definition.routes, refresh, rotate)
|
|
42
52
|
@definition = definition
|
|
43
53
|
@routes = []
|
|
44
54
|
@events = {}
|
|
@@ -46,6 +56,9 @@ Runtime(definition)
|
|
|
46
56
|
@modules = {}
|
|
47
57
|
@middlewares = Object/create(null)
|
|
48
58
|
@database = new Database(definition.settings.database, definition.settings.root)
|
|
59
|
+
@clients = null
|
|
60
|
+
if definition.settings.client != null
|
|
61
|
+
@clients = new ClientIdentity(definition.settings.client, @database)
|
|
49
62
|
|
|
50
63
|
Runtime:preload()
|
|
51
64
|
trace("Worker ${threads.threadId}: preparing database, middleware, routes and events")
|
|
@@ -110,6 +123,9 @@ Runtime:prepareRoute(pending, route)
|
|
|
110
123
|
path: route.path
|
|
111
124
|
pattern: routePattern(route.path)
|
|
112
125
|
auth: route.auth
|
|
126
|
+
policy: route.policy
|
|
127
|
+
reauthSeconds: route.reauthSeconds
|
|
128
|
+
legacyAuth: route.legacyAuth
|
|
113
129
|
handler: handler
|
|
114
130
|
middleware: middleware
|
|
115
131
|
})
|
|
@@ -165,13 +181,13 @@ Runtime:handle(task)
|
|
|
165
181
|
context request = Object/freeze(task.request)
|
|
166
182
|
context response = new Response()
|
|
167
183
|
context log
|
|
184
|
+
if !originAllowed(@definition.settings.origin, request)
|
|
185
|
+
return failure(403, "OriginRejected")
|
|
168
186
|
fresh = request.headers.authorization == null
|
|
169
187
|
claims = null
|
|
170
188
|
try
|
|
171
|
-
if fresh
|
|
172
|
-
claims = @tokens.
|
|
173
|
-
else
|
|
174
|
-
claims = @tokens.read(request.headers.authorization)
|
|
189
|
+
if !fresh
|
|
190
|
+
claims = @tokens.read(request.headers.authorization)
|
|
175
191
|
catch error
|
|
176
192
|
log.warn("[${request.id}] ${request.method} ${request.path} Authorization rejected before handler: ${errorMessage(error)}")
|
|
177
193
|
return failure(401, "Unauthorized")
|
|
@@ -180,6 +196,11 @@ Runtime:handle(task)
|
|
|
180
196
|
trace("[${request.id}] No route matched ${request.path}")
|
|
181
197
|
return failure(404, "RouteNotFound")
|
|
182
198
|
route = matched.route
|
|
199
|
+
metadata = routeInfo([route])
|
|
200
|
+
selected = Object/freeze(metadata[0])
|
|
201
|
+
context request = Object/freeze(Object/assign({}, request, {
|
|
202
|
+
route: selected
|
|
203
|
+
}))
|
|
183
204
|
input = null
|
|
184
205
|
try
|
|
185
206
|
input = JSON/parse(task.body)
|
|
@@ -192,29 +213,61 @@ Runtime:handle(task)
|
|
|
192
213
|
input[name] = decodeURIComponent(value)
|
|
193
214
|
catch error
|
|
194
215
|
return failure(400, "InvalidPath")
|
|
195
|
-
context token = new Token(claims)
|
|
196
|
-
trace("${requestIdentity(request.id, token)} Matched route ${matched.route.path}; fresh=${fresh}")
|
|
197
216
|
context db = @database.scope()
|
|
217
|
+
context clientIdentity = null
|
|
218
|
+
restored = false
|
|
219
|
+
try
|
|
220
|
+
if @clients != null
|
|
221
|
+
context clientIdentity = @clients.begin(claims, @tokens)
|
|
222
|
+
claims = clientIdentity.claims
|
|
223
|
+
fresh = clientIdentity.fresh
|
|
224
|
+
restored = clientIdentity.restored
|
|
225
|
+
if claims == null
|
|
226
|
+
claims = @tokens.create()
|
|
227
|
+
catch error
|
|
228
|
+
closeScope(db, false)
|
|
229
|
+
if is_kindof(error, HttpFailure)
|
|
230
|
+
return error.result()
|
|
231
|
+
log.err("[${request.id}] Client identity failed: ${errorDetails(error)}")
|
|
232
|
+
return failure(500, "InternalServerError")
|
|
233
|
+
return process(task, route, input, claims, fresh, restored)
|
|
234
|
+
|
|
235
|
+
Runtime:rotateClient()
|
|
236
|
+
context clientIdentity
|
|
237
|
+
if @clients == null or clientIdentity == null
|
|
238
|
+
throw "Managed client identity is unavailable"
|
|
239
|
+
if clientIdentity.closed
|
|
240
|
+
throw "Client identity scope is closed"
|
|
241
|
+
clientIdentity.rotate = true
|
|
242
|
+
return null
|
|
243
|
+
|
|
244
|
+
Runtime:signResponse(claims)
|
|
245
|
+
context clientIdentity
|
|
246
|
+
context response
|
|
247
|
+
if @clients == null
|
|
248
|
+
return @tokens.sign(claims)
|
|
249
|
+
return @clients.publish(clientIdentity, claims, response, @tokens)
|
|
250
|
+
|
|
251
|
+
Runtime:process(task, route, input, claims, fresh, restored)
|
|
252
|
+
context request
|
|
253
|
+
context response
|
|
254
|
+
context log
|
|
255
|
+
context db
|
|
256
|
+
context token = new Token(claims)
|
|
257
|
+
trace("${requestIdentity(request.id, token)} Matched route ${route.path}; fresh=${fresh}")
|
|
198
258
|
return chain Promise/resolve(null)
|
|
199
259
|
then(ignored)
|
|
200
|
-
capture server
|
|
201
|
-
capture request
|
|
202
|
-
capture response
|
|
203
|
-
capture token
|
|
204
|
-
capture log
|
|
205
|
-
capture db
|
|
206
260
|
capture fresh
|
|
261
|
+
capture restored
|
|
207
262
|
capture input
|
|
208
263
|
if fresh
|
|
209
|
-
|
|
210
|
-
|
|
264
|
+
registration = Object/assign({}, input, {
|
|
265
|
+
ip: request.remoteAddress
|
|
266
|
+
userAgent: request.headers["user-agent"]
|
|
267
|
+
})
|
|
268
|
+
return event("register", registration)
|
|
269
|
+
return validate(restored)
|
|
211
270
|
then(validatedAt)
|
|
212
|
-
capture server
|
|
213
|
-
capture request
|
|
214
|
-
capture response
|
|
215
|
-
capture token
|
|
216
|
-
capture log
|
|
217
|
-
capture db
|
|
218
271
|
capture route
|
|
219
272
|
capture input
|
|
220
273
|
capture claims
|
|
@@ -222,11 +275,22 @@ Runtime:handle(task)
|
|
|
222
275
|
if !fresh and is_number(validatedAt)
|
|
223
276
|
stampValidation(claims, validatedAt)
|
|
224
277
|
token.renew()
|
|
225
|
-
if route.
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
278
|
+
if route.policy != "anonymous" and !token.hasPolicy(route.policy)
|
|
279
|
+
if route.legacyAuth
|
|
280
|
+
response.reject(401, "Unauthorized")
|
|
281
|
+
if route.policy == "login" or !token.hasPolicy("login")
|
|
282
|
+
needLogin(route, 401, "LoginRequired")
|
|
283
|
+
response.reject(403, "Forbidden")
|
|
284
|
+
if route.reauthSeconds > 0
|
|
285
|
+
authenticatedAt = token.get("authenticatedAt")
|
|
286
|
+
now = Date/now()
|
|
287
|
+
if !Number/isSafeInteger(authenticatedAt) or authenticatedAt < 0 or authenticatedAt > now
|
|
288
|
+
needLogin(route, 403, "ReauthenticationRequired")
|
|
289
|
+
if now - authenticatedAt >= route.reauthSeconds * 1000
|
|
290
|
+
needLogin(route, 403, "ReauthenticationRequired")
|
|
291
|
+
expected = request.headers["x-lango-expected-user"]
|
|
292
|
+
if expected != null and expected != to_json(token.get("userId"))
|
|
293
|
+
response.reject(409, "IdentityChanged")
|
|
230
294
|
if route.kind == "longpoll"
|
|
231
295
|
return null
|
|
232
296
|
trace("${requestIdentity(request.id, token)} Invoking handler; middleware=${route.middleware != null}")
|
|
@@ -236,18 +300,14 @@ Runtime:handle(task)
|
|
|
236
300
|
return middleware(handler, request.path, input)
|
|
237
301
|
return handler(input)
|
|
238
302
|
then(output)
|
|
239
|
-
capture
|
|
240
|
-
capture request
|
|
241
|
-
capture response
|
|
242
|
-
capture token
|
|
243
|
-
capture log
|
|
244
|
-
capture db
|
|
303
|
+
capture crypto
|
|
245
304
|
capture task
|
|
246
305
|
capture claims
|
|
306
|
+
encoded = null
|
|
247
307
|
if task.authorize == true
|
|
248
308
|
closeScope(db, true)
|
|
249
309
|
authorizedClaims = tokenClaims(token, claims, true)
|
|
250
|
-
encoded =
|
|
310
|
+
encoded = signResponse(authorizedClaims)
|
|
251
311
|
if task.pollResult == null
|
|
252
312
|
return {
|
|
253
313
|
status: 200
|
|
@@ -268,18 +328,17 @@ Runtime:handle(task)
|
|
|
268
328
|
closeScope(db, true)
|
|
269
329
|
result = response.result(body)
|
|
270
330
|
updated = tokenClaims(token, claims, result.status < 400)
|
|
271
|
-
|
|
331
|
+
encoded = signResponse(updated)
|
|
332
|
+
result = response.result(body)
|
|
333
|
+
result.headers["x-lango-token"] = encoded
|
|
334
|
+
if result.status < 400 and authenticationChanged(claims, updated)
|
|
335
|
+
result.headers["x-lango-auth-change"] = "v1:${crypto/randomUUID()}"
|
|
272
336
|
if task.internal != true and @definition.settings.requestEvent and "request" in @events
|
|
273
337
|
return audit(request.id, result, task.body, body, updated)
|
|
274
338
|
trace("${requestIdentity(request.id, token)} Request completed status=${result.status}")
|
|
275
339
|
return result
|
|
276
340
|
catch(error)
|
|
277
|
-
capture server
|
|
278
|
-
capture request
|
|
279
|
-
capture log
|
|
280
|
-
capture db
|
|
281
341
|
capture HttpFailure
|
|
282
|
-
capture token
|
|
283
342
|
closeScope(db, false)
|
|
284
343
|
if is_kindof(error, HttpFailure)
|
|
285
344
|
return error.result()
|
|
@@ -289,7 +348,9 @@ Runtime:handle(task)
|
|
|
289
348
|
log.err("${requestIdentity(request.id, token)} Request failed: ${errorDetails(error)}")
|
|
290
349
|
return failure(500, "InternalServerError")
|
|
291
350
|
finally
|
|
292
|
-
|
|
351
|
+
context clientIdentity
|
|
352
|
+
if clientIdentity != null
|
|
353
|
+
clientIdentity.closed = true
|
|
293
354
|
closeScope(db, false)
|
|
294
355
|
|
|
295
356
|
Runtime:tokenClaims(token, claims, allowRenewal)
|
|
@@ -358,13 +419,12 @@ Runtime:refreshOne(pending, results, encoded, owner, child)
|
|
|
358
419
|
Runtime:event(name, input)
|
|
359
420
|
if !(name in @events)
|
|
360
421
|
return null
|
|
361
|
-
context server
|
|
362
422
|
context request
|
|
363
|
-
context response
|
|
364
423
|
context token
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
424
|
+
identity = "[${request.id}]"
|
|
425
|
+
if token != null
|
|
426
|
+
identity = requestIdentity(request.id, token)
|
|
427
|
+
trace("${identity} Running event ${name}")
|
|
368
428
|
handler = @events[name]
|
|
369
429
|
threads/parentPort/postMessage({
|
|
370
430
|
kind: "event"
|
|
@@ -372,33 +432,20 @@ Runtime:event(name, input)
|
|
|
372
432
|
})
|
|
373
433
|
return chain Promise/resolve(null)
|
|
374
434
|
then(ignored)
|
|
375
|
-
capture server
|
|
376
|
-
capture request
|
|
377
|
-
capture response
|
|
378
|
-
capture token
|
|
379
|
-
capture log
|
|
380
|
-
capture db
|
|
381
435
|
capture handler
|
|
382
436
|
capture input
|
|
383
437
|
return handler(input)
|
|
384
438
|
finally
|
|
385
|
-
capture server
|
|
386
|
-
capture request
|
|
387
439
|
capture threads
|
|
388
440
|
threads/parentPort/postMessage({
|
|
389
441
|
kind: "handler"
|
|
390
442
|
id: request.id
|
|
391
443
|
})
|
|
392
444
|
|
|
393
|
-
Runtime:validate()
|
|
394
|
-
context db
|
|
445
|
+
Runtime:validate(force = false)
|
|
395
446
|
context token
|
|
396
|
-
context response
|
|
397
|
-
context server
|
|
398
|
-
context request
|
|
399
|
-
context log
|
|
400
447
|
claims = token.snapshot()
|
|
401
|
-
if Date/now() - claims.validatedAt < 1000
|
|
448
|
+
if !force and Date/now() - claims.validatedAt < 1000
|
|
402
449
|
return null
|
|
403
450
|
if !("validate" in @events)
|
|
404
451
|
return Date/now()
|
|
@@ -406,15 +453,8 @@ Runtime:validate()
|
|
|
406
453
|
context token = new Token(claims, true)
|
|
407
454
|
return chain Promise/resolve(null)
|
|
408
455
|
then(ignored)
|
|
409
|
-
capture token
|
|
410
|
-
capture response
|
|
411
|
-
capture server
|
|
412
|
-
capture request
|
|
413
|
-
capture log
|
|
414
|
-
capture db
|
|
415
456
|
return event("validate", null)
|
|
416
457
|
then(valid)
|
|
417
|
-
capture token
|
|
418
458
|
capture original
|
|
419
459
|
capture claims
|
|
420
460
|
if !is_bool(valid)
|
|
@@ -431,7 +471,6 @@ Runtime:validate()
|
|
|
431
471
|
|
|
432
472
|
Runtime:audit(id, result, input, output, claims)
|
|
433
473
|
context db = @database.scope()
|
|
434
|
-
context server
|
|
435
474
|
context request
|
|
436
475
|
context log
|
|
437
476
|
context token = new Token(claims, true)
|
|
@@ -449,23 +488,12 @@ Runtime:audit(id, result, input, output, claims)
|
|
|
449
488
|
})
|
|
450
489
|
return chain Promise/resolve(null)
|
|
451
490
|
then(ignored)
|
|
452
|
-
capture token
|
|
453
|
-
capture server
|
|
454
|
-
capture request
|
|
455
|
-
capture response
|
|
456
|
-
capture log
|
|
457
|
-
capture db
|
|
458
491
|
capture metadata
|
|
459
492
|
return event("request", Object/freeze(metadata))
|
|
460
493
|
then(ignored)
|
|
461
|
-
capture db
|
|
462
494
|
closeScope(db, true)
|
|
463
495
|
return null
|
|
464
496
|
catch(error)
|
|
465
|
-
capture log
|
|
466
|
-
capture db
|
|
467
|
-
capture request
|
|
468
|
-
capture token
|
|
469
497
|
closeScope(db, false)
|
|
470
498
|
log.err("${requestIdentity(request.id, token)} Request event failed: ${errorDetails(error)}")
|
|
471
499
|
return null
|
|
@@ -473,7 +501,6 @@ Runtime:audit(id, result, input, output, claims)
|
|
|
473
501
|
capture result
|
|
474
502
|
return result
|
|
475
503
|
finally
|
|
476
|
-
capture db
|
|
477
504
|
closeScope(db, false)
|
|
478
505
|
|
|
479
506
|
failure(status, code)
|
|
@@ -501,3 +528,27 @@ closeScope(db, strict)
|
|
|
501
528
|
if dirty and strict
|
|
502
529
|
throw "UnclosedTransaction"
|
|
503
530
|
return null
|
|
531
|
+
|
|
532
|
+
needLogin(route, status, reason)
|
|
533
|
+
context request
|
|
534
|
+
context token
|
|
535
|
+
throw new HttpFailure(status, reason, {
|
|
536
|
+
"content-type": "application/json; charset=utf-8"
|
|
537
|
+
"cache-control": "no-store"
|
|
538
|
+
"x-lango-need-login": "1"
|
|
539
|
+
}, {
|
|
540
|
+
reason: reason
|
|
541
|
+
path: request.path
|
|
542
|
+
policy: route.policy
|
|
543
|
+
reauthSeconds: route.reauthSeconds
|
|
544
|
+
userId: token.get("userId")
|
|
545
|
+
})
|
|
546
|
+
|
|
547
|
+
authenticationChanged(previous, current)
|
|
548
|
+
context clientIdentity
|
|
549
|
+
if clientIdentity != null and clientIdentity.rotate
|
|
550
|
+
return true
|
|
551
|
+
for ["auth", "userId", "grantId", "authenticatedAt"] as name
|
|
552
|
+
if to_json(previous[name]) != to_json(current[name])
|
|
553
|
+
return true
|
|
554
|
+
return false
|