@lango-project/lango-server 0.2.0 → 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 +290 -27
- package/dist/.lango/runtime.mjs +24 -1
- package/dist/body_reader.mjs +15 -14
- package/dist/body_reader.mjs.map +1 -1
- package/dist/cli/.lango/runtime.mjs +24 -1
- 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/error_details.mjs +45 -0
- package/dist/cli/error_details.mjs.map +1 -0
- 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 +148 -101
- package/dist/cli/server.mjs.map +1 -1
- package/dist/cli/static_files.mjs +7 -4
- package/dist/cli/static_files.mjs.map +1 -1
- package/dist/cli/trace.mjs +53 -0
- package/dist/cli/trace.mjs.map +1 -0
- 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/error_details.mjs +45 -0
- package/dist/error_details.mjs.map +1 -0
- 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/request_identity.mjs +22 -0
- package/dist/request_identity.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 +33 -17
- package/dist/routes.mjs.map +1 -1
- package/dist/runtime.mjs +241 -110
- package/dist/runtime.mjs.map +1 -1
- package/dist/server.mjs +148 -101
- 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 +7 -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/trace.mjs +53 -0
- package/dist/trace.mjs.map +1 -0
- package/dist/worker.mjs +8 -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/error_details.lango +21 -0
- 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/request_identity.lango +9 -0
- package/src/response.lango +1 -1
- package/src/route_info.lango +2 -0
- package/src/routes.lango +30 -15
- package/src/runtime.lango +149 -77
- package/src/server.lango +48 -7
- package/src/server_context.lango +10 -2
- package/src/static_files.lango +3 -0
- package/src/token.lango +26 -5
- package/src/tokens.lango +10 -3
- package/src/trace.lango +23 -0
- package/src/worker.lango +1 -0
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
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export requestIdentity(id, token)
|
|
2
|
+
|
|
3
|
+
requestIdentity(id, token)
|
|
4
|
+
identity = "[${id}] [clientId=${token.clientId?}]"
|
|
5
|
+
for ["sessionId", "userId"] as name
|
|
6
|
+
value = token.get(name)
|
|
7
|
+
if value != null
|
|
8
|
+
identity = "${identity} [${name}=${value}]"
|
|
9
|
+
return identity
|
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
|