@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
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"static_files.mjs","sources":["static_files.lango"],"sourcesContent":["import \"./trace\"\n trace(message)\nimport js \"node:fs\" as fs\nimport js \"node:path\" as path\nimport js \"serve-static\" as files\nimport js global RegExp\nimport js global decodeURIComponent\nimport js global String\n\nexport staticOptions(root, spec)\nexport class StaticFiles(settings)\n methods\n receive(req, res, failed)\n\nstaticOptions(root, spec)\n if spec == null\n return null\n if !is_object(spec) or is_array(spec)\n throw \"static must be a map\"\n allowed = [\"path\", \"directory\", \"index\", \"fallback\", \"runtime\"]\n for spec as key: value\n if !allowed.includes(key)\n throw \"Unknown static option: ${key}\"\n mount = spec.path\n if mount == null\n mount = \"/\"\n pattern = new RegExp(\"^/(?:[A-Za-z0-9_-]+/)*[A-Za-z0-9_-]*$\")\n if !is_string(mount) or !pattern.test(mount)\n throw \"Static path must be an absolute URL prefix\"\n if mount != \"/\" and mount.endsWith(\"/\")\n mount = mount.slice(0, -1)\n if !is_string(spec.directory) or spec.directory.trim() == \"\"\n throw \"Static directory is required\"\n index = spec.index\n if index == null\n index = \"index.html\"\n if !is_string(index) or index == \"\" or index.startsWith(\".\") or index.includes(\"/\") or index.includes(\"\\\\\")\n throw \"Static index must be a file name\"\n fallback = spec.fallback\n if fallback != null and (!is_string(fallback) or fallback == \"\" or fallback.startsWith(\".\") or fallback.includes(\"/\") or fallback.includes(\"\\\\\"))\n throw \"Static fallback must be a file name\"\n runtime = spec.runtime\n if runtime == null or runtime == \"false\"\n runtime = false\n if runtime == \"true\"\n runtime = true\n if !is_bool(runtime)\n throw \"Static runtime must be boolean\"\n return {\n path: mount\n directory: path/resolve(root, spec.directory)\n index: index\n fallback: fallback\n runtime: runtime\n }\n\nStaticFiles(settings)\n @settings = settings\n if !fs/statSync(settings.directory).isDirectory()\n throw \"Static directory must be a directory\"\n if settings.fallback != null and !fs/statSync(path/join(settings.directory, settings.fallback)).isFile()\n throw \"Static fallback must be a file\"\n @serve = files/default(settings.directory, {\n index: settings.index\n dotfiles: \"deny\"\n fallthrough: false\n redirect: false\n })\n @runtime = files/default(settings.directory, {\n index: false\n dotfiles: \"allow\"\n fallthrough: false\n redirect: false\n })\n\nStaticFiles:receive(req, res, failed)\n mount = @settings.path\n url = req.url\n pathname = url.split(\"?\")[0]\n if mount != \"/\" and pathname != mount and !pathname.startsWith(\"${mount}/\")\n failed(404, \"NotFound\")\n return null\n if mount != \"/\"\n req.url = url.slice(mount.length)\n if req.url == \"\" or req.url.startsWith(\"?\")\n req.url = \"/${req.url}\"\n local = req.url.split(\"?\")[0]\n decoded = null\n try\n decoded = decodeURIComponent(local)\n catch error\n failed(400, \"InvalidPath\")\n return null\n if decoded.includes(\"\\\\\") or decoded.includes(String/fromCharCode(0))\n failed(400, \"InvalidPath\")\n return null\n segments = decoded.split(\"/\")\n runtime = false\n for segments as index: segment\n if segment.startsWith(\".\")\n if index == 1 and segment == \".lango\" and @settings.runtime\n runtime = true\n else\n failed(403, \"Forbidden\")\n return null\n done = (error)\n capture req\n capture res\n capture failed\n capture decoded\n failure(req, res, failed, error, decoded)\n serve = @serve\n if runtime\n serve = @runtime\n trace(\"Static ${req.method} ${pathname}: directory=${@settings.directory}; index=${@settings.index}; runtime=${runtime}\")\n serve(req, res, done)\n return null\n\nStaticFiles:failure(req, res, failed, error, pathname = null)\n if res.headersSent\n res.destroy()\n return null\n status = 404\n code = \"NotFound\"\n if error != null\n switch error.status\n case 400\n status = 400\n code = \"InvalidPath\"\n case 403\n status = 403\n code = \"Forbidden\"\n case 416\n status = 416\n code = \"RangeNotSatisfiable\"\n case 404\n status = 404\n else\n status = 500\n code = \"InternalServerError\"\n if status == 404 and pathname != null and @settings.fallback != null and !pathname.startsWith(\"/.lango\") and path/posix/extname(pathname) == \"\" and acceptsHTML(req.headers.accept)\n req.url = \"/${@settings.fallback}\"\n done = (error)\n capture req\n capture res\n capture failed\n failure(req, res, failed, error)\n serve = @serve\n serve(req, res, done)\n return null\n failed(status, code)\n return null\n\nacceptsHTML(value)\n if !is_string(value)\n return false\n zero = new RegExp(\"^q\\\\s*=\\\\s*0(?:\\\\.0*)?$\", \"i\")\n for value.split(\",\") as range\n parts = range.split(\";\")\n media = parts[0]\n type = media.trim().toLowerCase()\n if type == \"text/html\" or type == \"application/xhtml+xml\"\n allowed = true\n for parts.slice(1) as parameter\n if zero.test(parameter.trim())\n allowed = false\n if allowed\n return true\n return false\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"static_files.mjs","sources":["static_files.lango"],"sourcesContent":["import \"./trace\"\n trace(message)\nimport js \"node:fs\" as fs\nimport js \"node:path\" as path\nimport js \"serve-static\" as files\nimport js global RegExp\nimport js global decodeURIComponent\nimport js global String\n\nexport staticOptions(root, spec)\nexport class StaticFiles(settings)\n methods\n receive(req, res, failed)\n\nstaticOptions(root, spec)\n if spec == null\n return null\n if !is_object(spec) or is_array(spec)\n throw \"static must be a map\"\n allowed = [\"path\", \"directory\", \"index\", \"fallback\", \"runtime\"]\n for spec as key: value\n if !allowed.includes(key)\n throw \"Unknown static option: ${key}\"\n mount = spec.path\n if mount == null\n mount = \"/\"\n pattern = new RegExp(\"^/(?:[A-Za-z0-9_-]+/)*[A-Za-z0-9_-]*$\")\n if !is_string(mount) or !pattern.test(mount)\n throw \"Static path must be an absolute URL prefix\"\n if mount != \"/\" and mount.endsWith(\"/\")\n mount = mount.slice(0, -1)\n if !is_string(spec.directory) or spec.directory.trim() == \"\"\n throw \"Static directory is required\"\n index = spec.index\n if index == null\n index = \"index.html\"\n if !is_string(index) or index == \"\" or index.startsWith(\".\") or index.includes(\"/\") or index.includes(\"\\\\\")\n throw \"Static index must be a file name\"\n fallback = spec.fallback\n if fallback != null and (!is_string(fallback) or fallback == \"\" or fallback.startsWith(\".\") or fallback.includes(\"/\") or fallback.includes(\"\\\\\"))\n throw \"Static fallback must be a file name\"\n runtime = spec.runtime\n if runtime == null or runtime == \"false\"\n runtime = false\n if runtime == \"true\"\n runtime = true\n if !is_bool(runtime)\n throw \"Static runtime must be boolean\"\n return {\n path: mount\n directory: path/resolve(root, spec.directory)\n index: index\n fallback: fallback\n runtime: runtime\n }\n\nStaticFiles(settings)\n @settings = settings\n if !fs/statSync(settings.directory).isDirectory()\n throw \"Static directory must be a directory\"\n if settings.fallback != null and !fs/statSync(path/join(settings.directory, settings.fallback)).isFile()\n throw \"Static fallback must be a file\"\n @serve = files/default(settings.directory, {\n index: settings.index\n dotfiles: \"deny\"\n fallthrough: false\n redirect: false\n })\n @runtime = files/default(settings.directory, {\n index: false\n dotfiles: \"allow\"\n fallthrough: false\n redirect: false\n })\n\nStaticFiles:receive(req, res, failed)\n mount = @settings.path\n url = req.url\n pathname = url.split(\"?\")[0]\n if mount != \"/\" and pathname != mount and !pathname.startsWith(\"${mount}/\")\n failed(404, \"NotFound\")\n return null\n if mount != \"/\"\n req.url = url.slice(mount.length)\n if req.url == \"\" or req.url.startsWith(\"?\")\n req.url = \"/${req.url}\"\n local = req.url.split(\"?\")[0]\n decoded = null\n try\n decoded = decodeURIComponent(local)\n catch error\n failed(400, \"InvalidPath\")\n return null\n if decoded.includes(\"\\\\\") or decoded.includes(String/fromCharCode(0))\n failed(400, \"InvalidPath\")\n return null\n segments = decoded.split(\"/\")\n runtime = false\n for segments as index: segment\n if segment.startsWith(\".\")\n if index == 1 and segment == \".lango\" and @settings.runtime\n runtime = true\n else\n failed(403, \"Forbidden\")\n return null\n done = (error)\n capture req\n capture res\n capture failed\n capture decoded\n failure(req, res, failed, error, decoded)\n serve = @serve\n if runtime\n serve = @runtime\n trace(\"Static ${req.method} ${pathname}: directory=${@settings.directory}; index=${@settings.index}; runtime=${runtime}\")\n serve(req, res, done)\n return null\n\nStaticFiles:failure(req, res, failed, error, pathname = null)\n if res.headersSent\n res.destroy()\n return null\n status = 404\n code = \"NotFound\"\n if error != null\n switch error.status\n case 400\n status = 400\n code = \"InvalidPath\"\n case 403\n status = 403\n code = \"Forbidden\"\n case 416\n status = 416\n code = \"RangeNotSatisfiable\"\n case 404\n status = 404\n else\n status = 500\n code = \"InternalServerError\"\n if status == 404 and pathname != null and @settings.fallback != null and !pathname.startsWith(\"/.lango\") and path/posix/extname(pathname) == \"\" and acceptsHTML(req.headers.accept)\n req.url = \"/${@settings.fallback}\"\n done = (error)\n capture req\n capture res\n capture failed\n failure(req, res, failed, error)\n serve = @serve\n serve(req, res, done)\n return null\n failed(status, code)\n return null\n\nacceptsHTML(value)\n if !is_string(value)\n return false\n zero = new RegExp(\"^q\\\\s*=\\\\s*0(?:\\\\.0*)?$\", \"i\")\n for value.split(\",\") as range\n parts = range.split(\";\")\n media = parts[0]\n type = media.trim().toLowerCase()\n if type == \"text/html\" or type == \"application/xhtml+xml\"\n allowed = true\n for parts.slice(1) as parameter\n if zero.test(parameter.trim())\n allowed = false\n if allowed\n return true\n return false\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAUA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2IA;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import * as crypto from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
hostGlobal as __hostGlobal,
|
|
4
|
+
hostGlobalCall as __hostGlobalCall,
|
|
5
|
+
contextGet as __contextGet,
|
|
6
|
+
contextSet as __contextSet,
|
|
7
|
+
contextRestore as __contextRestore,
|
|
8
|
+
isNumber as __isNumber,
|
|
9
|
+
isString as __isString,
|
|
10
|
+
isBool as __isBool,
|
|
11
|
+
isObject as __isObject,
|
|
12
|
+
isArray as __isArray,
|
|
13
|
+
requireBoolean as __requireBoolean,
|
|
14
|
+
normalize as __normalize,
|
|
15
|
+
finite as __finite,
|
|
16
|
+
numericAdd as __numericAdd,
|
|
17
|
+
boundary as __boundary,
|
|
18
|
+
startTrans as __startTrans,
|
|
19
|
+
commitTrans as __commitTrans,
|
|
20
|
+
rollbackTrans as __rollbackTrans,
|
|
21
|
+
transState as __transState,
|
|
22
|
+
query as __query,
|
|
23
|
+
requireArity as __requireArity,
|
|
24
|
+
} from "./.lango/runtime.mjs";
|
|
25
|
+
|
|
26
|
+
export class ClientIdentity {
|
|
27
|
+
#settings;
|
|
28
|
+
#database;
|
|
29
|
+
constructor(settings, database) {
|
|
30
|
+
__requireArity(arguments.length, [0, 1], "constructor");
|
|
31
|
+
settings = __boundary(settings);
|
|
32
|
+
database = __boundary(database);
|
|
33
|
+
this.#settings = settings;
|
|
34
|
+
this.#database = database;
|
|
35
|
+
}
|
|
36
|
+
initialize() {
|
|
37
|
+
const __contextScope = [];
|
|
38
|
+
try {
|
|
39
|
+
__contextScope.push(__contextSet("db", __normalize(this.#database.scope())));
|
|
40
|
+
try {
|
|
41
|
+
__startTrans();
|
|
42
|
+
if (__requireBoolean(__normalize(__contextGet("db").parameterStyle) === "postgresql")) {
|
|
43
|
+
let lock = __query("select pg_advisory_xact_lock(1818324591, 1)", []);
|
|
44
|
+
__normalize(lock.execute());
|
|
45
|
+
}
|
|
46
|
+
let row = __query("create table if not exists _lango_clients (\n client_id text primary key,\n secret_hash text not null unique,\n expires_at bigint not null\n)", []);
|
|
47
|
+
__normalize(row.execute());
|
|
48
|
+
__commitTrans();
|
|
49
|
+
} finally {
|
|
50
|
+
__normalize(__contextGet("db").finish());
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
return null;
|
|
54
|
+
} finally {
|
|
55
|
+
__contextRestore(__contextScope);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
begin(claims, tokens) {
|
|
59
|
+
const __contextScope = [];
|
|
60
|
+
try {
|
|
61
|
+
__requireArity(arguments.length, [0, 1], "begin");
|
|
62
|
+
claims = __boundary(claims);
|
|
63
|
+
tokens = __boundary(tokens);
|
|
64
|
+
let value = readCookie();
|
|
65
|
+
if (__requireBoolean(__requireBoolean(value === null) && __requireBoolean(claims !== null))) {
|
|
66
|
+
__normalize(__contextGet("response").reject(401, "ClientProofRequired"));
|
|
67
|
+
}
|
|
68
|
+
let hash = null;
|
|
69
|
+
let row = null;
|
|
70
|
+
if (__requireBoolean(value === null)) {
|
|
71
|
+
value = clientSecret();
|
|
72
|
+
claims = __normalize(tokens.create());
|
|
73
|
+
hash = clientDigest(value);
|
|
74
|
+
claims.clientProof = hash;
|
|
75
|
+
return { claims: claims, value: value, hash: hash, fresh: true, restored: false, rotate: false, closed: false };
|
|
76
|
+
}
|
|
77
|
+
hash = clientDigest(value);
|
|
78
|
+
row = __query("select client_id from _lango_clients where secret_hash = :hash and expires_at > :now", ["hash","now"]);
|
|
79
|
+
row.hash = hash;
|
|
80
|
+
row.now = __normalize(__hostGlobal(["Date"]).now());
|
|
81
|
+
__normalize(row.execute());
|
|
82
|
+
if (__requireBoolean(!__requireBoolean(__normalize(row.next())))) {
|
|
83
|
+
__normalize(__contextGet("response").reject(401, "ClientCookieRejected"));
|
|
84
|
+
}
|
|
85
|
+
let restored = claims === null;
|
|
86
|
+
if (__requireBoolean(restored)) {
|
|
87
|
+
claims = __normalize(tokens.create(__normalize(row.client_id)));
|
|
88
|
+
claims.clientProof = hash;
|
|
89
|
+
}
|
|
90
|
+
if (__requireBoolean(__requireBoolean(__normalize(claims.clientId) !== __normalize(row.client_id)) || __requireBoolean(!__requireBoolean(sameProof(__normalize(claims.clientProof), hash))))) {
|
|
91
|
+
__normalize(__contextGet("response").reject(401, "ClientProofRequired"));
|
|
92
|
+
}
|
|
93
|
+
return { claims: claims, value: value, hash: hash, fresh: false, restored: restored, rotate: false, closed: false };
|
|
94
|
+
return null;
|
|
95
|
+
} finally {
|
|
96
|
+
__contextRestore(__contextScope);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
publish(identity, claims, response, tokens) {
|
|
100
|
+
const __contextScope = [];
|
|
101
|
+
try {
|
|
102
|
+
__requireArity(arguments.length, [0, 1, 2, 3], "publish");
|
|
103
|
+
identity = __boundary(identity);
|
|
104
|
+
claims = __boundary(claims);
|
|
105
|
+
response = __boundary(response);
|
|
106
|
+
tokens = __boundary(tokens);
|
|
107
|
+
let value = __normalize(identity.value);
|
|
108
|
+
if (__requireBoolean(__normalize(identity.rotate))) {
|
|
109
|
+
value = clientSecret();
|
|
110
|
+
}
|
|
111
|
+
let hash = clientDigest(value);
|
|
112
|
+
claims.clientProof = hash;
|
|
113
|
+
let encoded = __normalize(tokens.sign(claims));
|
|
114
|
+
__contextScope.push(__contextSet("db", __normalize(this.#database.scope())));
|
|
115
|
+
let row = null;
|
|
116
|
+
try {
|
|
117
|
+
if (__requireBoolean(__normalize(identity.fresh))) {
|
|
118
|
+
row = __query("insert into _lango_clients (client_id, secret_hash, expires_at)\nvalues (:id, :hash, :expires)", ["id","hash","expires"]);
|
|
119
|
+
row.id = __normalize(claims.clientId);
|
|
120
|
+
row.hash = hash;
|
|
121
|
+
row.expires = __numericAdd(__normalize(__hostGlobal(["Date"]).now()), __finite(__normalize(this.#settings.maxAgeSeconds) * 1000));
|
|
122
|
+
__normalize(row.execute());
|
|
123
|
+
} else {
|
|
124
|
+
row = __query("update _lango_clients set secret_hash = :replacement, expires_at = :expires\nwhere client_id = :id and secret_hash = :previous and expires_at > :now\nreturning client_id", ["replacement","expires","id","previous","now"]);
|
|
125
|
+
row.id = __normalize(claims.clientId);
|
|
126
|
+
row.previous = __normalize(identity.hash);
|
|
127
|
+
row.replacement = hash;
|
|
128
|
+
row.now = __normalize(__hostGlobal(["Date"]).now());
|
|
129
|
+
row.expires = __numericAdd(__normalize(__hostGlobal(["Date"]).now()), __finite(__normalize(this.#settings.maxAgeSeconds) * 1000));
|
|
130
|
+
__normalize(row.execute());
|
|
131
|
+
if (__requireBoolean(!__requireBoolean(__normalize(row.next())))) {
|
|
132
|
+
__normalize(response.reject(401, "ClientProofRequired"));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
__normalize(response.header("Set-Cookie", `__Host-lango-client=${value}; Path=/; Secure; HttpOnly; SameSite=Strict; Max-Age=${__normalize(this.#settings.maxAgeSeconds)}`));
|
|
136
|
+
} finally {
|
|
137
|
+
__normalize(__contextGet("db").finish());
|
|
138
|
+
}
|
|
139
|
+
identity.closed = true;
|
|
140
|
+
return encoded;
|
|
141
|
+
return null;
|
|
142
|
+
} finally {
|
|
143
|
+
__contextRestore(__contextScope);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
export function clientOptions(spec, database, origin) {
|
|
148
|
+
__requireArity(arguments.length, [0, 1, 2], "clientOptions");
|
|
149
|
+
spec = __boundary(spec);
|
|
150
|
+
database = __boundary(database);
|
|
151
|
+
origin = __boundary(origin);
|
|
152
|
+
if (__requireBoolean(spec === null)) {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(spec))) || __requireBoolean(__isArray(spec)))) {
|
|
156
|
+
throw "Client configuration must be an object";
|
|
157
|
+
}
|
|
158
|
+
for (const name of __normalize(__hostGlobal(["Object"]).keys(spec))) {
|
|
159
|
+
if (__requireBoolean(name !== "maxAgeSeconds")) {
|
|
160
|
+
throw `Unknown client option: ${name}`;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (__requireBoolean(database === null)) {
|
|
164
|
+
throw "Managed client identity requires a database";
|
|
165
|
+
}
|
|
166
|
+
if (__requireBoolean(__requireBoolean(__normalize(database.type) === "sqlite") && __requireBoolean(__normalize(database.file) === ":memory:"))) {
|
|
167
|
+
throw "Managed client identity requires persistent database storage";
|
|
168
|
+
}
|
|
169
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(origin === null) || __requireBoolean(!__requireBoolean(__normalize(origin.required)))) || __requireBoolean(!__requireBoolean(__normalize(origin.url.startsWith("https:")))))) {
|
|
170
|
+
throw "Managed client identity requires a required HTTPS origin";
|
|
171
|
+
}
|
|
172
|
+
let lifetime = __normalize(spec.maxAgeSeconds);
|
|
173
|
+
if (__requireBoolean(lifetime === null)) {
|
|
174
|
+
lifetime = 2592000;
|
|
175
|
+
}
|
|
176
|
+
if (__requireBoolean(__isString(lifetime))) {
|
|
177
|
+
lifetime = __hostGlobalCall(["Number"], lifetime);
|
|
178
|
+
}
|
|
179
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(lifetime)))) || __requireBoolean(lifetime < 1)) || __requireBoolean(lifetime > 31536000))) {
|
|
180
|
+
throw "Client maxAgeSeconds must be between 1 and 31536000";
|
|
181
|
+
}
|
|
182
|
+
return { maxAgeSeconds: lifetime };
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function readCookie() {
|
|
187
|
+
const __contextScope = [];
|
|
188
|
+
try {
|
|
189
|
+
let pattern;
|
|
190
|
+
let headers = __normalize(__contextGet("request").headers);
|
|
191
|
+
let header = __normalize(headers.cookie);
|
|
192
|
+
if (__requireBoolean(header === null)) {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(header))) || __requireBoolean(__normalize(header.length) > 8192))) {
|
|
196
|
+
__normalize(__contextGet("response").reject(400, "InvalidCookie"));
|
|
197
|
+
}
|
|
198
|
+
let found = null;
|
|
199
|
+
for (const part of __normalize(header.split(";"))) {
|
|
200
|
+
let pair = __normalize(part.trim());
|
|
201
|
+
let index = __normalize(pair.indexOf("="));
|
|
202
|
+
if (__requireBoolean(__requireBoolean(index >= 0) && __requireBoolean(__normalize(pair.slice(0, index)) === "__Host-lango-client"))) {
|
|
203
|
+
if (__requireBoolean(found !== null)) {
|
|
204
|
+
__normalize(__contextGet("response").reject(400, "InvalidCookie"));
|
|
205
|
+
}
|
|
206
|
+
found = __normalize(pair.slice(__numericAdd(index, 1)));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (__requireBoolean(found !== null)) {
|
|
210
|
+
pattern = new (__hostGlobal(["RegExp"]))("^[a-f0-9]{64}$");
|
|
211
|
+
if (__requireBoolean(!__requireBoolean(__normalize(pattern.test(found))))) {
|
|
212
|
+
__normalize(__contextGet("response").reject(400, "InvalidCookie"));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return found;
|
|
216
|
+
return null;
|
|
217
|
+
} finally {
|
|
218
|
+
__contextRestore(__contextScope);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function clientSecret() {
|
|
223
|
+
let bytes = __normalize(crypto.randomBytes(32));
|
|
224
|
+
return __normalize(bytes.toString("hex"));
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function clientDigest(value) {
|
|
229
|
+
__requireArity(arguments.length, [0], "clientDigest");
|
|
230
|
+
value = __boundary(value);
|
|
231
|
+
let hash = __normalize(crypto.createHash("sha256"));
|
|
232
|
+
__normalize(hash.update(value));
|
|
233
|
+
return __normalize(hash.digest("hex"));
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function sameProof(left, right) {
|
|
238
|
+
__requireArity(arguments.length, [0, 1], "sameProof");
|
|
239
|
+
left = __boundary(left);
|
|
240
|
+
right = __boundary(right);
|
|
241
|
+
let pattern = new (__hostGlobal(["RegExp"]))("^[a-f0-9]{64}$");
|
|
242
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(left))) || __requireBoolean(!__requireBoolean(__normalize(pattern.test(left)))))) {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
let first = __normalize(__hostGlobal(["Buffer"]).from(left, "hex"));
|
|
246
|
+
let second = __normalize(__hostGlobal(["Buffer"]).from(right, "hex"));
|
|
247
|
+
return __normalize(crypto.timingSafeEqual(first, second));
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
//# sourceMappingURL=client_identity.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client_identity.mjs","sources":["client_identity.lango"],"sourcesContent":["import js \"node:crypto\" as crypto\nimport js global Date\nimport js global Object\nimport js global Number\nimport js global RegExp\nimport js global Buffer\n\nexport clientOptions(spec, database, origin)\nexport class ClientIdentity(settings, database)\n methods\n initialize()\n begin(claims, tokens)\n publish(identity, claims, response, tokens)\n\nclientOptions(spec, database, origin)\n if spec == null\n return null\n if !is_object(spec) or is_array(spec)\n throw \"Client configuration must be an object\"\n for Object/keys(spec) as name\n if name != \"maxAgeSeconds\"\n throw \"Unknown client option: ${name}\"\n if database == null\n throw \"Managed client identity requires a database\"\n if database.type == \"sqlite\" and database.file == \":memory:\"\n throw \"Managed client identity requires persistent database storage\"\n if origin == null or !origin.required or !origin.url.startsWith(\"https:\")\n throw \"Managed client identity requires a required HTTPS origin\"\n lifetime = spec.maxAgeSeconds\n if lifetime == null\n lifetime = 2592000\n if is_string(lifetime)\n lifetime = Number(lifetime)\n if !Number/isSafeInteger(lifetime) or lifetime < 1 or lifetime > 31536000\n throw \"Client maxAgeSeconds must be between 1 and 31536000\"\n return {\n maxAgeSeconds: lifetime\n }\n\nClientIdentity(settings, database)\n @settings = settings\n @database = database\n\nClientIdentity:initialize()\n context db = @database.scope()\n try\n start_trans()\n if db.parameterStyle == \"postgresql\"\n lock = SQL\n select pg_advisory_xact_lock(1818324591, 1)\n lock.execute()\n row = SQL\n create table if not exists _lango_clients (\n client_id text primary key,\n secret_hash text not null unique,\n expires_at bigint not null\n )\n row.execute()\n commit_trans()\n finally\n db.finish()\n return null\n\nClientIdentity:begin(claims, tokens)\n context response\n value = readCookie()\n if value == null and claims != null\n response.reject(401, \"ClientProofRequired\")\n hash = null\n row = null\n if value == null\n value = clientSecret()\n claims = tokens.create()\n hash = clientDigest(value)\n claims.clientProof = hash\n return {\n claims: claims\n value: value\n hash: hash\n fresh: true\n restored: false\n rotate: false\n closed: false\n }\n hash = clientDigest(value)\n row = SQL\n select client_id from _lango_clients where secret_hash = :hash and expires_at > :now\n row.hash = hash\n row.now = Date/now()\n row.execute()\n if !row.next()\n response.reject(401, \"ClientCookieRejected\")\n restored = claims == null\n if restored\n claims = tokens.create(row.client_id)\n claims.clientProof = hash\n if claims.clientId != row.client_id or !sameProof(claims.clientProof, hash)\n response.reject(401, \"ClientProofRequired\")\n return {\n claims: claims\n value: value\n hash: hash\n fresh: false\n restored: restored\n rotate: false\n closed: false\n }\n\nClientIdentity:publish(identity, claims, response, tokens)\n value = identity.value\n if identity.rotate\n value = clientSecret()\n hash = clientDigest(value)\n claims.clientProof = hash\n encoded = tokens.sign(claims)\n context db = @database.scope()\n row = null\n try\n if identity.fresh\n row = SQL\n insert into _lango_clients (client_id, secret_hash, expires_at)\n values (:id, :hash, :expires)\n row.id = claims.clientId\n row.hash = hash\n row.expires = Date/now() + @settings.maxAgeSeconds * 1000\n row.execute()\n else\n row = SQL\n update _lango_clients set secret_hash = :replacement, expires_at = :expires\n where client_id = :id and secret_hash = :previous and expires_at > :now\n returning client_id\n row.id = claims.clientId\n row.previous = identity.hash\n row.replacement = hash\n row.now = Date/now()\n row.expires = Date/now() + @settings.maxAgeSeconds * 1000\n row.execute()\n if !row.next()\n response.reject(401, \"ClientProofRequired\")\n response.header(\"Set-Cookie\", \"__Host-lango-client=${value}; Path=/; Secure; HttpOnly; SameSite=Strict; Max-Age=${@settings.maxAgeSeconds}\")\n finally\n db.finish()\n identity.closed = true\n return encoded\n\nreadCookie()\n context request\n context response\n headers = request.headers\n header = headers.cookie\n if header == null\n return null\n if !is_string(header) or header.length > 8192\n response.reject(400, \"InvalidCookie\")\n found = null\n for header.split(\";\") as part\n pair = part.trim()\n index = pair.indexOf(\"=\")\n if index >= 0 and pair.slice(0, index) == \"__Host-lango-client\"\n if found != null\n response.reject(400, \"InvalidCookie\")\n found = pair.slice(index + 1)\n if found != null\n pattern = new RegExp(\"^[a-f0-9]{64}$\")\n if !pattern.test(found)\n response.reject(400, \"InvalidCookie\")\n return found\n\nclientSecret()\n bytes = crypto/randomBytes(32)\n return bytes.toString(\"hex\")\n\nclientDigest(value)\n hash = crypto/createHash(\"sha256\")\n hash.update(value)\n return hash.digest(\"hex\")\n\nsameProof(left, right)\n pattern = new RegExp(\"^[a-f0-9]{64}$\")\n if !is_string(left) or !pattern.test(left)\n return false\n first = Buffer/from(left, \"hex\")\n second = Buffer/from(right, \"hex\")\n return crypto/timingSafeEqual(first, second)\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAQA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuBA;;;;;;AAIA;;;;;;;;;AAKA;;;;;;;;;;;;;;"}
|
package/dist/database.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ClientIdentity } from "./client_identity.mjs";
|
|
1
2
|
import { DatabaseScope } from "./database_scope.mjs";
|
|
2
3
|
import { SqliteAdapter } from "./sqlite_adapter.mjs";
|
|
3
4
|
import { PostgresAdapter } from "./postgres_adapter.mjs";
|
|
@@ -6,6 +7,7 @@ import {
|
|
|
6
7
|
contextGet as __contextGet,
|
|
7
8
|
contextSet as __contextSet,
|
|
8
9
|
contextRestore as __contextRestore,
|
|
10
|
+
bindContexts as __bindContexts,
|
|
9
11
|
requireBoolean as __requireBoolean,
|
|
10
12
|
normalize as __normalize,
|
|
11
13
|
boundary as __boundary,
|
|
@@ -34,7 +36,7 @@ export class Database {
|
|
|
34
36
|
if (__requireBoolean(__normalize(this.#settings.type) === "postgresql")) {
|
|
35
37
|
name = "node:worker_threads";
|
|
36
38
|
}
|
|
37
|
-
return import(name).then(__withArity((mod) => {
|
|
39
|
+
return import(name).then(__withArity(__bindContexts((mod) => {
|
|
38
40
|
const __contextScope = [];
|
|
39
41
|
try {
|
|
40
42
|
mod = __boundary(mod);
|
|
@@ -55,7 +57,7 @@ export class Database {
|
|
|
55
57
|
} finally {
|
|
56
58
|
__contextRestore(__contextScope);
|
|
57
59
|
}
|
|
58
|
-
}, [0], "closure")).catch(__withArity((error) => {
|
|
60
|
+
}), [0], "closure")).catch(__withArity(__bindContexts((error) => {
|
|
59
61
|
const __contextScope = [];
|
|
60
62
|
try {
|
|
61
63
|
error = __boundary(error);
|
|
@@ -66,7 +68,7 @@ export class Database {
|
|
|
66
68
|
} finally {
|
|
67
69
|
__contextRestore(__contextScope);
|
|
68
70
|
}
|
|
69
|
-
}, [0], "closure"));
|
|
71
|
+
}), [0], "closure"));
|
|
70
72
|
return null;
|
|
71
73
|
}
|
|
72
74
|
scope() {
|
|
@@ -85,17 +87,30 @@ export class Database {
|
|
|
85
87
|
return null;
|
|
86
88
|
}
|
|
87
89
|
}
|
|
88
|
-
export function prepareDatabase(settings) {
|
|
90
|
+
export function prepareDatabase(settings, root = null, client = null) {
|
|
89
91
|
__requireArity(arguments.length, [0], "prepareDatabase");
|
|
90
92
|
settings = __boundary(settings);
|
|
91
|
-
|
|
93
|
+
root = __boundary(root);
|
|
94
|
+
client = __boundary(client);
|
|
95
|
+
if (__requireBoolean(settings === null)) {
|
|
92
96
|
return __normalize(__hostGlobal(["Promise"]).resolve(null));
|
|
93
97
|
}
|
|
94
|
-
|
|
95
|
-
|
|
98
|
+
if (__requireBoolean(__requireBoolean(client === null) && __requireBoolean(__requireBoolean(__normalize(settings.type) !== "sqlite") || __requireBoolean(__normalize(settings.file) === ":memory:")))) {
|
|
99
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(null));
|
|
100
|
+
}
|
|
101
|
+
let database = new Database(settings, root);
|
|
102
|
+
return __normalize(database.open()).then(__withArity(__bindContexts((ignored) => {
|
|
103
|
+
ignored = __boundary(ignored);
|
|
104
|
+
if (__requireBoolean(client !== null)) {
|
|
105
|
+
let identity = new ClientIdentity(client, database);
|
|
106
|
+
__normalize(identity.initialize());
|
|
107
|
+
}
|
|
108
|
+
return null;
|
|
109
|
+
return null;
|
|
110
|
+
}), [0], "closure")).finally(__bindContexts(() => {
|
|
96
111
|
__normalize(database.close());
|
|
97
112
|
return null;
|
|
98
|
-
});
|
|
113
|
+
}));
|
|
99
114
|
return null;
|
|
100
115
|
}
|
|
101
116
|
//# sourceMappingURL=database.mjs.map
|
package/dist/database.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"database.mjs","sources":["database.lango"],"sourcesContent":["import js global Promise\nimport \"./database_scope\"\n class DatabaseScope\nimport \"./sqlite_adapter\"\n class SqliteAdapter\nimport \"./postgres_adapter\"\n class PostgresAdapter\n\nexport prepareDatabase(settings)\n\nexport class Database(settings, root = null)\n methods\n open()\n scope()\n close()\n\nDatabase(settings, root = null)\n @root = root\n @settings = settings\n @adapter = null\n\nDatabase:open()\n if @settings == null\n return Promise/resolve(null)\n name = \"node:sqlite\"\n if @settings.type == \"postgresql\"\n name = \"node:worker_threads\"\n return chain import_module(name)\n then(mod)\n if @settings.type == \"sqlite\"\n @adapter = new SqliteAdapter(mod, @settings)\n else\n @adapter = new PostgresAdapter(mod, @settings, @root)\n context db = scope()\n try\n query = SQL\n select 1\n query.execute()\n finally\n db.finish()\n return null\n catch(error)\n context log\n log.err(\"Database startup failed: ${error}\")\n close()\n throw \"Database startup failed; check configuration, compiler version and driver installation\"\n\nDatabase:scope()\n if @adapter == null\n return null\n return new DatabaseScope(@adapter)\n\nDatabase:close()\n if @adapter != null\n @adapter.close()\n @adapter = null\n return null\n\nprepareDatabase(settings)\n if settings == null
|
|
1
|
+
{"version":3,"file":"database.mjs","sources":["database.lango"],"sourcesContent":["import \"./client_identity\"\n class ClientIdentity\nimport js global Promise\nimport \"./database_scope\"\n class DatabaseScope\nimport \"./sqlite_adapter\"\n class SqliteAdapter\nimport \"./postgres_adapter\"\n class PostgresAdapter\n\nexport prepareDatabase(settings, root = null, client = null)\n\nexport class Database(settings, root = null)\n methods\n open()\n scope()\n close()\n\nDatabase(settings, root = null)\n @root = root\n @settings = settings\n @adapter = null\n\nDatabase:open()\n if @settings == null\n return Promise/resolve(null)\n name = \"node:sqlite\"\n if @settings.type == \"postgresql\"\n name = \"node:worker_threads\"\n return chain import_module(name)\n then(mod)\n if @settings.type == \"sqlite\"\n @adapter = new SqliteAdapter(mod, @settings)\n else\n @adapter = new PostgresAdapter(mod, @settings, @root)\n context db = scope()\n try\n query = SQL\n select 1\n query.execute()\n finally\n db.finish()\n return null\n catch(error)\n context log\n log.err(\"Database startup failed: ${error}\")\n close()\n throw \"Database startup failed; check configuration, compiler version and driver installation\"\n\nDatabase:scope()\n if @adapter == null\n return null\n return new DatabaseScope(@adapter)\n\nDatabase:close()\n if @adapter != null\n @adapter.close()\n @adapter = null\n return null\n\nprepareDatabase(settings, root = null, client = null)\n if settings == null\n return Promise/resolve(null)\n if client == null and (settings.type != \"sqlite\" or settings.file == \":memory:\")\n return Promise/resolve(null)\n database = new Database(settings, root)\n return chain database.open()\n then(ignored)\n capture database\n capture client\n if client != null\n identity = new ClientIdentity(client, database)\n identity.initialize()\n return null\n finally\n capture database\n database.close()\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAYA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/endpoint.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { policyOptions } from "./policy.mjs";
|
|
1
2
|
import { routePattern } from "./route_pattern.mjs";
|
|
2
3
|
import { middlewareName } from "./middleware.mjs";
|
|
3
4
|
import {
|
|
@@ -17,6 +18,10 @@ export class Endpoint {
|
|
|
17
18
|
module;
|
|
18
19
|
handler;
|
|
19
20
|
auth;
|
|
21
|
+
policy;
|
|
22
|
+
reauthSeconds;
|
|
23
|
+
reauthExplicit;
|
|
24
|
+
legacyAuth;
|
|
20
25
|
middleware;
|
|
21
26
|
constructor(spec) {
|
|
22
27
|
__requireArity(arguments.length, [0], "constructor");
|
|
@@ -34,13 +39,12 @@ export class Endpoint {
|
|
|
34
39
|
if (__requireBoolean(__normalize(this.middleware) !== null)) {
|
|
35
40
|
middlewareName(__normalize(this.middleware));
|
|
36
41
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
42
|
+
let selected = policyOptions(spec);
|
|
43
|
+
this.policy = __normalize(selected.name);
|
|
44
|
+
this.auth = __normalize(selected.name);
|
|
45
|
+
this.reauthSeconds = __normalize(selected.reauthSeconds);
|
|
46
|
+
this.reauthExplicit = __normalize(selected.explicit);
|
|
47
|
+
this.legacyAuth = __normalize(selected.legacy);
|
|
44
48
|
}
|
|
45
49
|
}
|
|
46
50
|
//# sourceMappingURL=endpoint.mjs.map
|
package/dist/endpoint.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint.mjs","sources":["endpoint.lango"],"sourcesContent":["import \"./route_pattern\"\n routePattern(path)\nimport \"./middleware\"\n middlewareName(name)\n\nexport class Endpoint(spec)\n properties\n path\n module\n handler\n auth\n middleware\n\nEndpoint(spec)\n routePattern(spec.path)\n for [\"module\", \"handler\"] as key\n if !is_string(spec[key]) or spec[key] == \"\"\n throw \"Endpoint requires ${key}\"\n @path = spec.path\n @module = spec.module\n @handler = spec.handler\n @middleware = spec.middleware\n if @middleware != null\n middlewareName(@middleware)\n
|
|
1
|
+
{"version":3,"file":"endpoint.mjs","sources":["endpoint.lango"],"sourcesContent":["import \"./policy\"\n policyOptions(spec)\nimport \"./route_pattern\"\n routePattern(path)\nimport \"./middleware\"\n middlewareName(name)\n\nexport class Endpoint(spec)\n properties\n path\n module\n handler\n auth\n policy\n reauthSeconds\n reauthExplicit\n legacyAuth\n middleware\n\nEndpoint(spec)\n routePattern(spec.path)\n for [\"module\", \"handler\"] as key\n if !is_string(spec[key]) or spec[key] == \"\"\n throw \"Endpoint requires ${key}\"\n @path = spec.path\n @module = spec.module\n @handler = spec.handler\n @middleware = spec.middleware\n if @middleware != null\n middlewareName(@middleware)\n selected = policyOptions(spec)\n @policy = selected.name\n @auth = selected.name\n @reauthSeconds = selected.reauthSeconds\n @reauthExplicit = selected.explicit\n @legacyAuth = selected.legacy\n"],"names":[],"mappings":";;;;;;;;;;;;;;;AAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/group.mjs
CHANGED
|
@@ -16,13 +16,17 @@ export class Group {
|
|
|
16
16
|
#prefix;
|
|
17
17
|
#module;
|
|
18
18
|
#auth;
|
|
19
|
-
|
|
19
|
+
#policy;
|
|
20
|
+
#reauthSeconds;
|
|
21
|
+
constructor(routes, spec, prefix = "", inheritedModule = null, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null) {
|
|
20
22
|
__requireArity(arguments.length, [0, 1], "constructor");
|
|
21
23
|
routes = __boundary(routes);
|
|
22
24
|
spec = __boundary(spec);
|
|
23
25
|
prefix = __boundary(prefix);
|
|
24
26
|
inheritedModule = __boundary(inheritedModule);
|
|
25
27
|
inheritedAuth = __boundary(inheritedAuth);
|
|
28
|
+
inheritedPolicy = __boundary(inheritedPolicy);
|
|
29
|
+
inheritedReauthSeconds = __boundary(inheritedReauthSeconds);
|
|
26
30
|
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.path)))) || __requireBoolean(!__requireBoolean(__normalize(spec.path.startsWith("/"))))) || __requireBoolean(__normalize(spec.path.endsWith("/")))) || __requireBoolean(__normalize(spec.path.includes("*"))))) {
|
|
27
31
|
throw "Group requires a path without a trailing slash or wildcard";
|
|
28
32
|
}
|
|
@@ -30,11 +34,17 @@ export class Group {
|
|
|
30
34
|
this.#prefix = `${prefix}${__normalize(spec.path)}`;
|
|
31
35
|
this.#module = __normalize(spec.module);
|
|
32
36
|
this.#auth = __normalize(spec.auth);
|
|
37
|
+
this.#policy = __normalize(spec.policy);
|
|
38
|
+
this.#reauthSeconds = __normalize(spec.reauthSeconds);
|
|
33
39
|
if (__requireBoolean(__normalize(this.#module) === null)) {
|
|
34
40
|
this.#module = inheritedModule;
|
|
35
41
|
}
|
|
36
|
-
if (__requireBoolean(__normalize(this.#auth) === null)) {
|
|
42
|
+
if (__requireBoolean(__requireBoolean(__normalize(this.#auth) === null) && __requireBoolean(__normalize(this.#policy) === null))) {
|
|
37
43
|
this.#auth = inheritedAuth;
|
|
44
|
+
this.#policy = inheritedPolicy;
|
|
45
|
+
}
|
|
46
|
+
if (__requireBoolean(__normalize(this.#reauthSeconds) === null)) {
|
|
47
|
+
this.#reauthSeconds = inheritedReauthSeconds;
|
|
38
48
|
}
|
|
39
49
|
}
|
|
40
50
|
endpoint(spec) {
|
|
@@ -43,26 +53,20 @@ export class Group {
|
|
|
43
53
|
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.path)))) || __requireBoolean(!__requireBoolean(__normalize(spec.path.startsWith("/")))))) {
|
|
44
54
|
throw "Endpoint path must begin with /";
|
|
45
55
|
}
|
|
46
|
-
let
|
|
47
|
-
|
|
48
|
-
if (__requireBoolean(
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
if (__requireBoolean(auth === null)) {
|
|
52
|
-
auth = __normalize(this.#auth);
|
|
56
|
+
let item = this.#merged(spec);
|
|
57
|
+
item.path = `${__normalize(this.#prefix)}${__normalize(spec.path)}`;
|
|
58
|
+
if (__requireBoolean(__normalize(item.module) === null)) {
|
|
59
|
+
item.module = __normalize(this.#module);
|
|
53
60
|
}
|
|
54
|
-
__normalize(this.#routes.endpoint(
|
|
61
|
+
__normalize(this.#routes.endpoint(item));
|
|
55
62
|
return this;
|
|
56
63
|
return null;
|
|
57
64
|
}
|
|
58
65
|
longpoll(spec) {
|
|
59
66
|
__requireArity(arguments.length, [0], "longpoll");
|
|
60
67
|
spec = __boundary(spec);
|
|
61
|
-
let item =
|
|
68
|
+
let item = this.#merged(spec);
|
|
62
69
|
item.path = `${__normalize(this.#prefix)}${__normalize(spec.path)}`;
|
|
63
|
-
if (__requireBoolean(__normalize(item.auth) === null)) {
|
|
64
|
-
item.auth = __normalize(this.#auth);
|
|
65
|
-
}
|
|
66
70
|
if (__requireBoolean(__normalize(item.fallback) !== null)) {
|
|
67
71
|
item.fallback = `${__normalize(this.#prefix)}${__normalize(item.fallback)}`;
|
|
68
72
|
}
|
|
@@ -73,7 +77,7 @@ export class Group {
|
|
|
73
77
|
group(spec) {
|
|
74
78
|
__requireArity(arguments.length, [0], "group");
|
|
75
79
|
spec = __boundary(spec);
|
|
76
|
-
return new Group(__normalize(this.#routes), spec, __normalize(this.#prefix), __normalize(this.#module), __normalize(this.#auth));
|
|
80
|
+
return new Group(__normalize(this.#routes), spec, __normalize(this.#prefix), __normalize(this.#module), __normalize(this.#auth), __normalize(this.#policy), __normalize(this.#reauthSeconds));
|
|
77
81
|
return null;
|
|
78
82
|
}
|
|
79
83
|
include(spec) {
|
|
@@ -83,9 +87,23 @@ export class Group {
|
|
|
83
87
|
if (__requireBoolean(mount === null)) {
|
|
84
88
|
mount = "/";
|
|
85
89
|
}
|
|
86
|
-
__normalize(this.#routes.include({ module: __normalize(spec.module), path: `${__normalize(this.#prefix)}${mount}` }, __normalize(this.#auth)));
|
|
90
|
+
__normalize(this.#routes.include({ module: __normalize(spec.module), path: `${__normalize(this.#prefix)}${mount}` }, __normalize(this.#auth), __normalize(this.#policy), __normalize(this.#reauthSeconds)));
|
|
87
91
|
return this;
|
|
88
92
|
return null;
|
|
89
93
|
}
|
|
94
|
+
#merged(spec) {
|
|
95
|
+
__requireArity(arguments.length, [0], "merged");
|
|
96
|
+
spec = __boundary(spec);
|
|
97
|
+
let item = __normalize(__hostGlobal(["Object"]).assign({ }, spec));
|
|
98
|
+
if (__requireBoolean(__requireBoolean(__normalize(item.auth) === null) && __requireBoolean(__normalize(item.policy) === null))) {
|
|
99
|
+
item.auth = __normalize(this.#auth);
|
|
100
|
+
item.policy = __normalize(this.#policy);
|
|
101
|
+
}
|
|
102
|
+
if (__requireBoolean(__normalize(item.reauthSeconds) === null)) {
|
|
103
|
+
item.reauthSeconds = __normalize(this.#reauthSeconds);
|
|
104
|
+
}
|
|
105
|
+
return item;
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
90
108
|
}
|
|
91
109
|
//# sourceMappingURL=group.mjs.map
|
package/dist/group.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"group.mjs","sources":["group.lango"],"sourcesContent":["import js global Object\n\nexport class Group(routes, spec, prefix = \"\", inheritedModule = null, inheritedAuth = null)\n methods\n endpoint(spec)\n longpoll(spec)\n group(spec)\n include(spec)\n\nGroup(routes, spec, prefix = \"\", inheritedModule = null, inheritedAuth = null)\n if !is_string(spec.path) or !spec.path.startsWith(\"/\") or spec.path.endsWith(\"/\") or spec.path.includes(\"*\")\n throw \"Group requires a path without a trailing slash or wildcard\"\n @routes = routes\n @prefix = \"${prefix}${spec.path}\"\n @module = spec.module\n @auth = spec.auth\n if @module == null\n @module = inheritedModule\n if @auth == null\n @auth = inheritedAuth\n\nGroup:endpoint(spec)\n if !is_string(spec.path) or !spec.path.startsWith(\"/\")\n throw \"Endpoint path must begin with /\"\n
|
|
1
|
+
{"version":3,"file":"group.mjs","sources":["group.lango"],"sourcesContent":["import js global Object\n\nexport class Group(routes, spec, prefix = \"\", inheritedModule = null, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null)\n methods\n endpoint(spec)\n longpoll(spec)\n group(spec)\n include(spec)\n\nGroup(routes, spec, prefix = \"\", inheritedModule = null, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null)\n if !is_string(spec.path) or !spec.path.startsWith(\"/\") or spec.path.endsWith(\"/\") or spec.path.includes(\"*\")\n throw \"Group requires a path without a trailing slash or wildcard\"\n @routes = routes\n @prefix = \"${prefix}${spec.path}\"\n @module = spec.module\n @auth = spec.auth\n @policy = spec.policy\n @reauthSeconds = spec.reauthSeconds\n if @module == null\n @module = inheritedModule\n if @auth == null and @policy == null\n @auth = inheritedAuth\n @policy = inheritedPolicy\n if @reauthSeconds == null\n @reauthSeconds = inheritedReauthSeconds\n\nGroup:endpoint(spec)\n if !is_string(spec.path) or !spec.path.startsWith(\"/\")\n throw \"Endpoint path must begin with /\"\n item = merged(spec)\n item.path = \"${@prefix}${spec.path}\"\n if item.module == null\n item.module = @module\n @routes.endpoint(item)\n return @this\n\nGroup:longpoll(spec)\n item = merged(spec)\n item.path = \"${@prefix}${spec.path}\"\n if item.fallback != null\n item.fallback = \"${@prefix}${item.fallback}\"\n @routes.longpoll(item)\n return @this\n\nGroup:group(spec)\n return new Group(@routes, spec, @prefix, @module, @auth, @policy, @reauthSeconds)\n\nGroup:include(spec)\n mount = spec.path\n if mount == null\n mount = \"/\"\n @routes.include({\n module: spec.module\n path: \"${@prefix}${mount}\"\n }, @auth, @policy, @reauthSeconds)\n return @this\n\nGroup:merged(spec)\n item = Object/assign({}, spec)\n if item.auth == null and item.policy == null\n item.auth = @auth\n item.policy = @policy\n if item.reauthSeconds == null\n item.reauthSeconds = @reauthSeconds\n return item\n"],"names":[],"mappings":";;;;;;;;;;;;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/http_failure.mjs
CHANGED
|
@@ -15,14 +15,16 @@ import {
|
|
|
15
15
|
} from "./.lango/runtime.mjs";
|
|
16
16
|
|
|
17
17
|
export class HttpFailure {
|
|
18
|
+
#details;
|
|
18
19
|
#status;
|
|
19
20
|
#code;
|
|
20
21
|
#headers;
|
|
21
|
-
constructor(status, code, headers) {
|
|
22
|
+
constructor(status, code, headers, details = null) {
|
|
22
23
|
__requireArity(arguments.length, [0, 1, 2], "constructor");
|
|
23
24
|
status = __boundary(status);
|
|
24
25
|
code = __boundary(code);
|
|
25
26
|
headers = __boundary(headers);
|
|
27
|
+
details = __boundary(details);
|
|
26
28
|
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isNumber(status))) || __requireBoolean(__finite(status % 1) !== 0)) || __requireBoolean(status < 400)) || __requireBoolean(status > 599))) {
|
|
27
29
|
throw "Rejected response requires an error status";
|
|
28
30
|
}
|
|
@@ -30,6 +32,7 @@ export class HttpFailure {
|
|
|
30
32
|
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(code))) || __requireBoolean(!__requireBoolean(__normalize(pattern.test(code)))))) {
|
|
31
33
|
throw "Rejected response requires an error code";
|
|
32
34
|
}
|
|
35
|
+
this.#details = details;
|
|
33
36
|
this.#status = status;
|
|
34
37
|
this.#code = code;
|
|
35
38
|
this.#headers = __fromJson(__toJson(headers));
|
|
@@ -38,7 +41,11 @@ export class HttpFailure {
|
|
|
38
41
|
}
|
|
39
42
|
}
|
|
40
43
|
result() {
|
|
41
|
-
|
|
44
|
+
let body = { error: __normalize(this.#code) };
|
|
45
|
+
if (__requireBoolean(__normalize(this.#details) !== null)) {
|
|
46
|
+
body.needLogin = __normalize(this.#details);
|
|
47
|
+
}
|
|
48
|
+
return { status: __normalize(this.#status), headers: __fromJson(__toJson(__normalize(this.#headers))), body: __toJson(body) };
|
|
42
49
|
return null;
|
|
43
50
|
}
|
|
44
51
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http_failure.mjs","sources":["http_failure.lango"],"sourcesContent":["import js global RegExp\n\nexport class HttpFailure(status, code, headers)\n methods\n result()\n\nHttpFailure(status, code, headers)\n if !is_number(status) or status % 1 != 0 or status < 400 or status > 599\n throw \"Rejected response requires an error status\"\n pattern = new RegExp(\"^[A-Za-z][A-Za-z0-9_]{0,63}$\")\n if !is_string(code) or !pattern.test(code)\n throw \"Rejected response requires an error code\"\n @status = status\n @code = code\n @headers = from_json(to_json(headers))\n if status == 401\n @headers[\"www-authenticate\"] = \"Bearer\"\n\nHttpFailure:result()\n return {\n status: @status\n headers: from_json(to_json(@headers))\n body: to_json(
|
|
1
|
+
{"version":3,"file":"http_failure.mjs","sources":["http_failure.lango"],"sourcesContent":["import js global RegExp\n\nexport class HttpFailure(status, code, headers, details = null)\n methods\n result()\n\nHttpFailure(status, code, headers, details = null)\n if !is_number(status) or status % 1 != 0 or status < 400 or status > 599\n throw \"Rejected response requires an error status\"\n pattern = new RegExp(\"^[A-Za-z][A-Za-z0-9_]{0,63}$\")\n if !is_string(code) or !pattern.test(code)\n throw \"Rejected response requires an error code\"\n @details = details\n @status = status\n @code = code\n @headers = from_json(to_json(headers))\n if status == 401\n @headers[\"www-authenticate\"] = \"Bearer\"\n\nHttpFailure:result()\n body = {\n error: @code\n }\n if @details != null\n body.needLogin = @details\n return {\n status: @status\n headers: from_json(to_json(@headers))\n body: to_json(body)\n }\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|