@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.
Files changed (121) hide show
  1. package/README.md +290 -27
  2. package/dist/.lango/runtime.mjs +24 -1
  3. package/dist/body_reader.mjs +15 -14
  4. package/dist/body_reader.mjs.map +1 -1
  5. package/dist/cli/.lango/runtime.mjs +24 -1
  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/error_details.mjs +45 -0
  15. package/dist/cli/error_details.mjs.map +1 -0
  16. package/dist/cli/longpoll_broker.mjs +9 -8
  17. package/dist/cli/longpoll_broker.mjs.map +1 -1
  18. package/dist/cli/longpoll_options.mjs +8 -7
  19. package/dist/cli/longpoll_options.mjs.map +1 -1
  20. package/dist/cli/longpoll_session.mjs +11 -10
  21. package/dist/cli/longpoll_session.mjs.map +1 -1
  22. package/dist/cli/options.mjs +29 -3
  23. package/dist/cli/options.mjs.map +1 -1
  24. package/dist/cli/origin.mjs +87 -0
  25. package/dist/cli/origin.mjs.map +1 -0
  26. package/dist/cli/policy.mjs +121 -0
  27. package/dist/cli/policy.mjs.map +1 -0
  28. package/dist/cli/postgres_adapter.mjs +3 -2
  29. package/dist/cli/postgres_adapter.mjs.map +1 -1
  30. package/dist/cli/routes.mjs +33 -17
  31. package/dist/cli/routes.mjs.map +1 -1
  32. package/dist/cli/server.mjs +148 -101
  33. package/dist/cli/server.mjs.map +1 -1
  34. package/dist/cli/static_files.mjs +7 -4
  35. package/dist/cli/static_files.mjs.map +1 -1
  36. package/dist/cli/trace.mjs +53 -0
  37. package/dist/cli/trace.mjs.map +1 -0
  38. package/dist/client_identity.mjs +250 -0
  39. package/dist/client_identity.mjs.map +1 -0
  40. package/dist/database.mjs +23 -8
  41. package/dist/database.mjs.map +1 -1
  42. package/dist/endpoint.mjs +11 -7
  43. package/dist/endpoint.mjs.map +1 -1
  44. package/dist/error_details.mjs +45 -0
  45. package/dist/error_details.mjs.map +1 -0
  46. package/dist/group.mjs +34 -16
  47. package/dist/group.mjs.map +1 -1
  48. package/dist/http_failure.mjs +9 -2
  49. package/dist/http_failure.mjs.map +1 -1
  50. package/dist/longpoll_broker.mjs +9 -8
  51. package/dist/longpoll_broker.mjs.map +1 -1
  52. package/dist/longpoll_options.mjs +8 -7
  53. package/dist/longpoll_options.mjs.map +1 -1
  54. package/dist/longpoll_session.mjs +11 -10
  55. package/dist/longpoll_session.mjs.map +1 -1
  56. package/dist/options.mjs +29 -3
  57. package/dist/options.mjs.map +1 -1
  58. package/dist/origin.mjs +87 -0
  59. package/dist/origin.mjs.map +1 -0
  60. package/dist/policy.mjs +121 -0
  61. package/dist/policy.mjs.map +1 -0
  62. package/dist/postgres_adapter.mjs +3 -2
  63. package/dist/postgres_adapter.mjs.map +1 -1
  64. package/dist/postgres_worker.mjs +15 -14
  65. package/dist/postgres_worker.mjs.map +1 -1
  66. package/dist/request_identity.mjs +22 -0
  67. package/dist/request_identity.mjs.map +1 -0
  68. package/dist/response.mjs +1 -1
  69. package/dist/response.mjs.map +1 -1
  70. package/dist/route_info.mjs +1 -1
  71. package/dist/route_info.mjs.map +1 -1
  72. package/dist/routes.mjs +33 -17
  73. package/dist/routes.mjs.map +1 -1
  74. package/dist/runtime.mjs +241 -110
  75. package/dist/runtime.mjs.map +1 -1
  76. package/dist/server.mjs +148 -101
  77. package/dist/server.mjs.map +1 -1
  78. package/dist/server_context.mjs +21 -9
  79. package/dist/server_context.mjs.map +1 -1
  80. package/dist/static_files.mjs +7 -4
  81. package/dist/static_files.mjs.map +1 -1
  82. package/dist/token.mjs +34 -7
  83. package/dist/token.mjs.map +1 -1
  84. package/dist/tokens.mjs +11 -2
  85. package/dist/tokens.mjs.map +1 -1
  86. package/dist/trace.mjs +53 -0
  87. package/dist/trace.mjs.map +1 -0
  88. package/dist/worker.mjs +8 -6
  89. package/dist/worker.mjs.map +1 -1
  90. package/docs/security.md +420 -0
  91. package/examples/security/README.md +160 -0
  92. package/examples/security/deploy.xml +12 -0
  93. package/examples/security/handlers.lango +610 -0
  94. package/examples/security/package.json +19 -0
  95. package/examples/security/passwords.lango +61 -0
  96. package/examples/security/routes.xml +14 -0
  97. package/examples/security/schema.lango +91 -0
  98. package/examples/security/setup.lango +30 -0
  99. package/package.json +12 -6
  100. package/src/client_identity.lango +184 -0
  101. package/src/database.lango +15 -4
  102. package/src/endpoint.lango +12 -5
  103. package/src/error_details.lango +21 -0
  104. package/src/group.lango +25 -21
  105. package/src/http_failure.lango +9 -5
  106. package/src/longpoll_options.lango +11 -5
  107. package/src/options.lango +34 -1
  108. package/src/origin.lango +56 -0
  109. package/src/policy.lango +84 -0
  110. package/src/request_identity.lango +9 -0
  111. package/src/response.lango +1 -1
  112. package/src/route_info.lango +2 -0
  113. package/src/routes.lango +30 -15
  114. package/src/runtime.lango +149 -77
  115. package/src/server.lango +48 -7
  116. package/src/server_context.lango +10 -2
  117. package/src/static_files.lango +3 -0
  118. package/src/token.lango +26 -5
  119. package/src/tokens.lango +10 -3
  120. package/src/trace.lango +23 -0
  121. package/src/worker.lango +1 -0
@@ -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"
@@ -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
@@ -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"
@@ -10,6 +10,8 @@ routeInfo(definitions)
10
10
  path: route.path
11
11
  kind: kind
12
12
  auth: route.auth
13
+ policy: route.policy
14
+ reauthSeconds: route.reauthSeconds
13
15
  middleware: route.middleware
14
16
  })
15
17
  return result
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