@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
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import js "node:crypto" as crypto
|
|
2
|
+
import js global RegExp
|
|
3
|
+
import "./passwords"
|
|
4
|
+
hashPassword(password)
|
|
5
|
+
|
|
6
|
+
export initialize()
|
|
7
|
+
export createUser(username, password, admin = false)
|
|
8
|
+
|
|
9
|
+
initialize()
|
|
10
|
+
users = SQL
|
|
11
|
+
create table if not exists security_users (
|
|
12
|
+
id text primary key,
|
|
13
|
+
username text not null unique,
|
|
14
|
+
password_hash text not null,
|
|
15
|
+
auth_version integer not null default 1,
|
|
16
|
+
is_admin integer not null default 0,
|
|
17
|
+
banned integer not null default 0
|
|
18
|
+
)
|
|
19
|
+
users.execute()
|
|
20
|
+
clients = SQL
|
|
21
|
+
create table if not exists security_clients (
|
|
22
|
+
client_id text primary key,
|
|
23
|
+
current_grant text,
|
|
24
|
+
created_at bigint not null,
|
|
25
|
+
last_seen bigint not null,
|
|
26
|
+
registered_ip text,
|
|
27
|
+
registered_user_agent text,
|
|
28
|
+
last_ip text,
|
|
29
|
+
last_user_agent text,
|
|
30
|
+
guest_used integer not null default 0
|
|
31
|
+
)
|
|
32
|
+
clients.execute()
|
|
33
|
+
grants = SQL
|
|
34
|
+
create table if not exists security_grants (
|
|
35
|
+
id text primary key,
|
|
36
|
+
client_id text not null references security_clients(client_id),
|
|
37
|
+
user_id text not null references security_users(id),
|
|
38
|
+
user_version integer not null,
|
|
39
|
+
created_at bigint not null,
|
|
40
|
+
last_active bigint not null,
|
|
41
|
+
expires_at bigint not null,
|
|
42
|
+
authenticated_at bigint not null,
|
|
43
|
+
remember integer not null,
|
|
44
|
+
revoked integer not null default 0
|
|
45
|
+
)
|
|
46
|
+
grants.execute()
|
|
47
|
+
audit = SQL
|
|
48
|
+
create table if not exists security_audit (
|
|
49
|
+
id text primary key,
|
|
50
|
+
occurred_at bigint not null,
|
|
51
|
+
request_id text not null,
|
|
52
|
+
client_id text not null,
|
|
53
|
+
user_id text,
|
|
54
|
+
grant_hash text,
|
|
55
|
+
event text not null,
|
|
56
|
+
target_id text,
|
|
57
|
+
peer_ip text,
|
|
58
|
+
user_agent text
|
|
59
|
+
)
|
|
60
|
+
audit.execute()
|
|
61
|
+
attempts = SQL
|
|
62
|
+
create table if not exists security_limits (
|
|
63
|
+
bucket text primary key,
|
|
64
|
+
started_at bigint not null,
|
|
65
|
+
hits integer not null
|
|
66
|
+
)
|
|
67
|
+
attempts.execute()
|
|
68
|
+
index = SQL
|
|
69
|
+
create index if not exists security_grants_user on security_grants(user_id, client_id)
|
|
70
|
+
index.execute()
|
|
71
|
+
return null
|
|
72
|
+
|
|
73
|
+
createUser(username, password, admin = false)
|
|
74
|
+
pattern = new RegExp("^[a-z0-9_.@+-]{3,128}$")
|
|
75
|
+
name = null
|
|
76
|
+
if is_string(username)
|
|
77
|
+
name = username.trim().toLowerCase()
|
|
78
|
+
if name == null or !pattern.test(name)
|
|
79
|
+
throw "Username must contain 3 to 128 supported ASCII characters"
|
|
80
|
+
id = crypto/randomUUID()
|
|
81
|
+
record = SQL
|
|
82
|
+
insert into security_users (id, username, password_hash, is_admin)
|
|
83
|
+
values (:id, :username, :hash, :admin)
|
|
84
|
+
record.id = id
|
|
85
|
+
record.username = name
|
|
86
|
+
record.hash = hashPassword(password)
|
|
87
|
+
record.admin = 0
|
|
88
|
+
if admin
|
|
89
|
+
record.admin = 1
|
|
90
|
+
record.execute()
|
|
91
|
+
return id
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import js "@lango-project/lango-js/database" as database
|
|
2
|
+
import js "node:fs" as fs
|
|
3
|
+
import js "node:path" as path
|
|
4
|
+
import js global process
|
|
5
|
+
import "./schema"
|
|
6
|
+
initialize()
|
|
7
|
+
createUser(username, password, admin = false)
|
|
8
|
+
|
|
9
|
+
export main(args)
|
|
10
|
+
|
|
11
|
+
main(args)
|
|
12
|
+
if args.length < 1 or args.length > 3
|
|
13
|
+
throw "Usage: setup <sqlite-file> [<username> [admin]] (password from SECURITY_SETUP_PASSWORD)"
|
|
14
|
+
if args.length == 3 and args[2] != "admin"
|
|
15
|
+
throw "Only the optional admin role is supported"
|
|
16
|
+
fs/mkdirSync(path/dirname(path/resolve(args[0])), {
|
|
17
|
+
recursive: true
|
|
18
|
+
})
|
|
19
|
+
adapter = database/createSqliteAdapter(args[0])
|
|
20
|
+
context db = adapter
|
|
21
|
+
try
|
|
22
|
+
initialize()
|
|
23
|
+
if args.length == 1
|
|
24
|
+
println("Initialized security schema")
|
|
25
|
+
else
|
|
26
|
+
id = createUser(args[1], process.env.SECURITY_SETUP_PASSWORD, args[2] == "admin")
|
|
27
|
+
println("Created user ${id}")
|
|
28
|
+
finally
|
|
29
|
+
adapter.close()
|
|
30
|
+
return null
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lango-project/lango-server",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "A small JSON HTTP server for Lango applications.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -22,16 +22,22 @@
|
|
|
22
22
|
"src/*.lua",
|
|
23
23
|
"README.md",
|
|
24
24
|
"LICENSE",
|
|
25
|
-
"NOTICE"
|
|
25
|
+
"NOTICE",
|
|
26
|
+
"docs/**",
|
|
27
|
+
"examples/security/*.lango",
|
|
28
|
+
"examples/security/*.xml",
|
|
29
|
+
"examples/security/package.json",
|
|
30
|
+
"examples/security/README.md"
|
|
26
31
|
],
|
|
27
32
|
"scripts": {
|
|
28
|
-
"build": "lango run tools/clean.lango && lango build src/library.lango --out dist/library.mjs && lango build src/server.lango --out dist/cli/server.mjs --executable && lango build src/worker.lango --out dist/worker.mjs && lango build examples/application/handlers.lango --out examples/application/dist/handlers.mjs && lango build src/postgres_worker.lango --out dist/postgres_worker.mjs && lango build examples/database/handlers.lango --out examples/database/dist/handlers.mjs",
|
|
29
|
-
"test": "npm run build && lango build src/test/handlers.lango --out .test-output/handlers.mjs && lango build src/test/sql_handlers.lango --out .test-output/sql_handlers.mjs && lango build src/test/middleware_handlers.lango --out .test-output/middleware_handlers.mjs && lango test src/test/database.lango --build-dir .test-output/database && lango test src/test/unit.lango --build-dir .test-output/unit && lango test src/test/route_parameters.lango --build-dir .test-output/route-parameters && lango test src/test/http.lango --build-dir .test-output/http && lango test src/test/middleware.lango --build-dir .test-output/middleware && lango test src/test/static.lango --build-dir .test-output/static && lango test src/test/standalone.lango --build-dir .test-output/standalone",
|
|
33
|
+
"build": "lango run tools/clean.lango && lango build src/library.lango --out dist/library.mjs && lango build src/server.lango --out dist/cli/server.mjs --executable && lango build src/worker.lango --out dist/worker.mjs && lango build examples/application/handlers.lango --out examples/application/dist/handlers.mjs && lango build src/postgres_worker.lango --out dist/postgres_worker.mjs && lango build examples/database/handlers.lango --out examples/database/dist/handlers.mjs && lango build examples/security/handlers.lango --out examples/security/dist/handlers.mjs && lango build examples/security/setup.lango --out examples/security/dist/setup.mjs --executable",
|
|
34
|
+
"test": "npm run build && lango build src/test/handlers.lango --out .test-output/handlers.mjs && lango build src/test/sql_handlers.lango --out .test-output/sql_handlers.mjs && lango build src/test/middleware_handlers.lango --out .test-output/middleware_handlers.mjs && lango test src/test/database.lango --build-dir .test-output/database && lango test src/test/unit.lango --build-dir .test-output/unit && lango test src/test/route_parameters.lango --build-dir .test-output/route-parameters && lango test src/test/http.lango --build-dir .test-output/http && lango test src/test/middleware.lango --build-dir .test-output/middleware && lango test src/test/static.lango --build-dir .test-output/static && lango test src/test/standalone.lango --build-dir .test-output/standalone && npm run test:security",
|
|
30
35
|
"start": "node dist/cli/server.mjs examples/application",
|
|
31
36
|
"prepack": "npm run build",
|
|
32
37
|
"test:postgres": "lango build src/test/sql_handlers.lango --out .test-output/sql_handlers.mjs && lango test src/test/database.lango --build-dir .test-output/database-postgres",
|
|
33
38
|
"test:longpoll": "lango build src/test/longpoll_handlers.lango --out .test-output/longpoll_handlers.mjs && lango test src/test/longpoll.lango --build-dir .test-output/longpoll && lango build src/test/middleware_handlers.lango --out .test-output/middleware_handlers.mjs && lango test src/test/standalone.lango --build-dir .test-output/standalone-longpoll",
|
|
34
|
-
"test:middleware": "lango build src/test/handlers.lango --out .test-output/handlers.mjs && lango build src/test/middleware_handlers.lango --out .test-output/middleware_handlers.mjs && lango test src/test/middleware.lango --build-dir .test-output/middleware"
|
|
39
|
+
"test:middleware": "lango build src/test/handlers.lango --out .test-output/handlers.mjs && lango build src/test/middleware_handlers.lango --out .test-output/middleware_handlers.mjs && lango test src/test/middleware.lango --build-dir .test-output/middleware",
|
|
40
|
+
"test:security": "lango build examples/security/handlers.lango --out .test-output/security-handlers.mjs && lango build examples/security/setup.lango --out examples/security/dist/setup.mjs --executable && lango test src/test/security.lango --build-dir .test-output/security"
|
|
35
41
|
},
|
|
36
42
|
"dependencies": {
|
|
37
43
|
"jsonwebtoken": "^9.0.3",
|
|
@@ -41,6 +47,6 @@
|
|
|
41
47
|
"serve-static": "^2.2.1"
|
|
42
48
|
},
|
|
43
49
|
"devDependencies": {
|
|
44
|
-
"@lango-project/lango-js": "^0.1.
|
|
50
|
+
"@lango-project/lango-js": "^0.1.18"
|
|
45
51
|
}
|
|
46
52
|
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import js "node:crypto" as crypto
|
|
2
|
+
import js global Date
|
|
3
|
+
import js global Object
|
|
4
|
+
import js global Number
|
|
5
|
+
import js global RegExp
|
|
6
|
+
import js global Buffer
|
|
7
|
+
|
|
8
|
+
export clientOptions(spec, database, origin)
|
|
9
|
+
export class ClientIdentity(settings, database)
|
|
10
|
+
methods
|
|
11
|
+
initialize()
|
|
12
|
+
begin(claims, tokens)
|
|
13
|
+
publish(identity, claims, response, tokens)
|
|
14
|
+
|
|
15
|
+
clientOptions(spec, database, origin)
|
|
16
|
+
if spec == null
|
|
17
|
+
return null
|
|
18
|
+
if !is_object(spec) or is_array(spec)
|
|
19
|
+
throw "Client configuration must be an object"
|
|
20
|
+
for Object/keys(spec) as name
|
|
21
|
+
if name != "maxAgeSeconds"
|
|
22
|
+
throw "Unknown client option: ${name}"
|
|
23
|
+
if database == null
|
|
24
|
+
throw "Managed client identity requires a database"
|
|
25
|
+
if database.type == "sqlite" and database.file == ":memory:"
|
|
26
|
+
throw "Managed client identity requires persistent database storage"
|
|
27
|
+
if origin == null or !origin.required or !origin.url.startsWith("https:")
|
|
28
|
+
throw "Managed client identity requires a required HTTPS origin"
|
|
29
|
+
lifetime = spec.maxAgeSeconds
|
|
30
|
+
if lifetime == null
|
|
31
|
+
lifetime = 2592000
|
|
32
|
+
if is_string(lifetime)
|
|
33
|
+
lifetime = Number(lifetime)
|
|
34
|
+
if !Number/isSafeInteger(lifetime) or lifetime < 1 or lifetime > 31536000
|
|
35
|
+
throw "Client maxAgeSeconds must be between 1 and 31536000"
|
|
36
|
+
return {
|
|
37
|
+
maxAgeSeconds: lifetime
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
ClientIdentity(settings, database)
|
|
41
|
+
@settings = settings
|
|
42
|
+
@database = database
|
|
43
|
+
|
|
44
|
+
ClientIdentity:initialize()
|
|
45
|
+
context db = @database.scope()
|
|
46
|
+
try
|
|
47
|
+
start_trans()
|
|
48
|
+
if db.parameterStyle == "postgresql"
|
|
49
|
+
lock = SQL
|
|
50
|
+
select pg_advisory_xact_lock(1818324591, 1)
|
|
51
|
+
lock.execute()
|
|
52
|
+
row = SQL
|
|
53
|
+
create table if not exists _lango_clients (
|
|
54
|
+
client_id text primary key,
|
|
55
|
+
secret_hash text not null unique,
|
|
56
|
+
expires_at bigint not null
|
|
57
|
+
)
|
|
58
|
+
row.execute()
|
|
59
|
+
commit_trans()
|
|
60
|
+
finally
|
|
61
|
+
db.finish()
|
|
62
|
+
return null
|
|
63
|
+
|
|
64
|
+
ClientIdentity:begin(claims, tokens)
|
|
65
|
+
context response
|
|
66
|
+
value = readCookie()
|
|
67
|
+
if value == null and claims != null
|
|
68
|
+
response.reject(401, "ClientProofRequired")
|
|
69
|
+
hash = null
|
|
70
|
+
row = null
|
|
71
|
+
if value == null
|
|
72
|
+
value = clientSecret()
|
|
73
|
+
claims = tokens.create()
|
|
74
|
+
hash = clientDigest(value)
|
|
75
|
+
claims.clientProof = hash
|
|
76
|
+
return {
|
|
77
|
+
claims: claims
|
|
78
|
+
value: value
|
|
79
|
+
hash: hash
|
|
80
|
+
fresh: true
|
|
81
|
+
restored: false
|
|
82
|
+
rotate: false
|
|
83
|
+
closed: false
|
|
84
|
+
}
|
|
85
|
+
hash = clientDigest(value)
|
|
86
|
+
row = SQL
|
|
87
|
+
select client_id from _lango_clients where secret_hash = :hash and expires_at > :now
|
|
88
|
+
row.hash = hash
|
|
89
|
+
row.now = Date/now()
|
|
90
|
+
row.execute()
|
|
91
|
+
if !row.next()
|
|
92
|
+
response.reject(401, "ClientCookieRejected")
|
|
93
|
+
restored = claims == null
|
|
94
|
+
if restored
|
|
95
|
+
claims = tokens.create(row.client_id)
|
|
96
|
+
claims.clientProof = hash
|
|
97
|
+
if claims.clientId != row.client_id or !sameProof(claims.clientProof, hash)
|
|
98
|
+
response.reject(401, "ClientProofRequired")
|
|
99
|
+
return {
|
|
100
|
+
claims: claims
|
|
101
|
+
value: value
|
|
102
|
+
hash: hash
|
|
103
|
+
fresh: false
|
|
104
|
+
restored: restored
|
|
105
|
+
rotate: false
|
|
106
|
+
closed: false
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
ClientIdentity:publish(identity, claims, response, tokens)
|
|
110
|
+
value = identity.value
|
|
111
|
+
if identity.rotate
|
|
112
|
+
value = clientSecret()
|
|
113
|
+
hash = clientDigest(value)
|
|
114
|
+
claims.clientProof = hash
|
|
115
|
+
encoded = tokens.sign(claims)
|
|
116
|
+
context db = @database.scope()
|
|
117
|
+
row = null
|
|
118
|
+
try
|
|
119
|
+
if identity.fresh
|
|
120
|
+
row = SQL
|
|
121
|
+
insert into _lango_clients (client_id, secret_hash, expires_at)
|
|
122
|
+
values (:id, :hash, :expires)
|
|
123
|
+
row.id = claims.clientId
|
|
124
|
+
row.hash = hash
|
|
125
|
+
row.expires = Date/now() + @settings.maxAgeSeconds * 1000
|
|
126
|
+
row.execute()
|
|
127
|
+
else
|
|
128
|
+
row = SQL
|
|
129
|
+
update _lango_clients set secret_hash = :replacement, expires_at = :expires
|
|
130
|
+
where client_id = :id and secret_hash = :previous and expires_at > :now
|
|
131
|
+
returning client_id
|
|
132
|
+
row.id = claims.clientId
|
|
133
|
+
row.previous = identity.hash
|
|
134
|
+
row.replacement = hash
|
|
135
|
+
row.now = Date/now()
|
|
136
|
+
row.expires = Date/now() + @settings.maxAgeSeconds * 1000
|
|
137
|
+
row.execute()
|
|
138
|
+
if !row.next()
|
|
139
|
+
response.reject(401, "ClientProofRequired")
|
|
140
|
+
response.header("Set-Cookie", "__Host-lango-client=${value}; Path=/; Secure; HttpOnly; SameSite=Strict; Max-Age=${@settings.maxAgeSeconds}")
|
|
141
|
+
finally
|
|
142
|
+
db.finish()
|
|
143
|
+
identity.closed = true
|
|
144
|
+
return encoded
|
|
145
|
+
|
|
146
|
+
readCookie()
|
|
147
|
+
context request
|
|
148
|
+
context response
|
|
149
|
+
headers = request.headers
|
|
150
|
+
header = headers.cookie
|
|
151
|
+
if header == null
|
|
152
|
+
return null
|
|
153
|
+
if !is_string(header) or header.length > 8192
|
|
154
|
+
response.reject(400, "InvalidCookie")
|
|
155
|
+
found = null
|
|
156
|
+
for header.split(";") as part
|
|
157
|
+
pair = part.trim()
|
|
158
|
+
index = pair.indexOf("=")
|
|
159
|
+
if index >= 0 and pair.slice(0, index) == "__Host-lango-client"
|
|
160
|
+
if found != null
|
|
161
|
+
response.reject(400, "InvalidCookie")
|
|
162
|
+
found = pair.slice(index + 1)
|
|
163
|
+
if found != null
|
|
164
|
+
pattern = new RegExp("^[a-f0-9]{64}$")
|
|
165
|
+
if !pattern.test(found)
|
|
166
|
+
response.reject(400, "InvalidCookie")
|
|
167
|
+
return found
|
|
168
|
+
|
|
169
|
+
clientSecret()
|
|
170
|
+
bytes = crypto/randomBytes(32)
|
|
171
|
+
return bytes.toString("hex")
|
|
172
|
+
|
|
173
|
+
clientDigest(value)
|
|
174
|
+
hash = crypto/createHash("sha256")
|
|
175
|
+
hash.update(value)
|
|
176
|
+
return hash.digest("hex")
|
|
177
|
+
|
|
178
|
+
sameProof(left, right)
|
|
179
|
+
pattern = new RegExp("^[a-f0-9]{64}$")
|
|
180
|
+
if !is_string(left) or !pattern.test(left)
|
|
181
|
+
return false
|
|
182
|
+
first = Buffer/from(left, "hex")
|
|
183
|
+
second = Buffer/from(right, "hex")
|
|
184
|
+
return crypto/timingSafeEqual(first, second)
|
package/src/database.lango
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import "./client_identity"
|
|
2
|
+
class ClientIdentity
|
|
1
3
|
import js global Promise
|
|
2
4
|
import "./database_scope"
|
|
3
5
|
class DatabaseScope
|
|
@@ -6,7 +8,7 @@ import "./sqlite_adapter"
|
|
|
6
8
|
import "./postgres_adapter"
|
|
7
9
|
class PostgresAdapter
|
|
8
10
|
|
|
9
|
-
export prepareDatabase(settings)
|
|
11
|
+
export prepareDatabase(settings, root = null, client = null)
|
|
10
12
|
|
|
11
13
|
export class Database(settings, root = null)
|
|
12
14
|
methods
|
|
@@ -56,11 +58,20 @@ Database:close()
|
|
|
56
58
|
@adapter = null
|
|
57
59
|
return null
|
|
58
60
|
|
|
59
|
-
prepareDatabase(settings)
|
|
60
|
-
if settings == null
|
|
61
|
+
prepareDatabase(settings, root = null, client = null)
|
|
62
|
+
if settings == null
|
|
61
63
|
return Promise/resolve(null)
|
|
62
|
-
|
|
64
|
+
if client == null and (settings.type != "sqlite" or settings.file == ":memory:")
|
|
65
|
+
return Promise/resolve(null)
|
|
66
|
+
database = new Database(settings, root)
|
|
63
67
|
return chain database.open()
|
|
68
|
+
then(ignored)
|
|
69
|
+
capture database
|
|
70
|
+
capture client
|
|
71
|
+
if client != null
|
|
72
|
+
identity = new ClientIdentity(client, database)
|
|
73
|
+
identity.initialize()
|
|
74
|
+
return null
|
|
64
75
|
finally
|
|
65
76
|
capture database
|
|
66
77
|
database.close()
|
package/src/endpoint.lango
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import "./policy"
|
|
2
|
+
policyOptions(spec)
|
|
1
3
|
import "./route_pattern"
|
|
2
4
|
routePattern(path)
|
|
3
5
|
import "./middleware"
|
|
@@ -9,6 +11,10 @@ export class Endpoint(spec)
|
|
|
9
11
|
module
|
|
10
12
|
handler
|
|
11
13
|
auth
|
|
14
|
+
policy
|
|
15
|
+
reauthSeconds
|
|
16
|
+
reauthExplicit
|
|
17
|
+
legacyAuth
|
|
12
18
|
middleware
|
|
13
19
|
|
|
14
20
|
Endpoint(spec)
|
|
@@ -22,8 +28,9 @@ Endpoint(spec)
|
|
|
22
28
|
@middleware = spec.middleware
|
|
23
29
|
if @middleware != null
|
|
24
30
|
middlewareName(@middleware)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
selected = policyOptions(spec)
|
|
32
|
+
@policy = selected.name
|
|
33
|
+
@auth = selected.name
|
|
34
|
+
@reauthSeconds = selected.reauthSeconds
|
|
35
|
+
@reauthExplicit = selected.explicit
|
|
36
|
+
@legacyAuth = selected.legacy
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export errorDetails(error)
|
|
2
|
+
export errorMessage(error)
|
|
3
|
+
|
|
4
|
+
errorDetails(error)
|
|
5
|
+
if is_object(error)
|
|
6
|
+
if is_string(error.stack) and error.stack != ""
|
|
7
|
+
return error.stack
|
|
8
|
+
if is_string(error.message) and error.message != ""
|
|
9
|
+
return error.message
|
|
10
|
+
if is_string(error.code)
|
|
11
|
+
return error.code
|
|
12
|
+
return "${error}"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
errorMessage(error)
|
|
16
|
+
if is_object(error)
|
|
17
|
+
if is_string(error.message) and error.message != ""
|
|
18
|
+
return error.message
|
|
19
|
+
if is_string(error.code)
|
|
20
|
+
return error.code
|
|
21
|
+
return "${error}"
|
package/src/group.lango
CHANGED
|
@@ -1,54 +1,49 @@
|
|
|
1
1
|
import js global Object
|
|
2
2
|
|
|
3
|
-
export class Group(routes, spec, prefix = "", inheritedModule = null, inheritedAuth = null)
|
|
3
|
+
export class Group(routes, spec, prefix = "", inheritedModule = null, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null)
|
|
4
4
|
methods
|
|
5
5
|
endpoint(spec)
|
|
6
6
|
longpoll(spec)
|
|
7
7
|
group(spec)
|
|
8
8
|
include(spec)
|
|
9
9
|
|
|
10
|
-
Group(routes, spec, prefix = "", inheritedModule = null, inheritedAuth = null)
|
|
10
|
+
Group(routes, spec, prefix = "", inheritedModule = null, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null)
|
|
11
11
|
if !is_string(spec.path) or !spec.path.startsWith("/") or spec.path.endsWith("/") or spec.path.includes("*")
|
|
12
12
|
throw "Group requires a path without a trailing slash or wildcard"
|
|
13
13
|
@routes = routes
|
|
14
14
|
@prefix = "${prefix}${spec.path}"
|
|
15
15
|
@module = spec.module
|
|
16
16
|
@auth = spec.auth
|
|
17
|
+
@policy = spec.policy
|
|
18
|
+
@reauthSeconds = spec.reauthSeconds
|
|
17
19
|
if @module == null
|
|
18
20
|
@module = inheritedModule
|
|
19
|
-
if @auth == null
|
|
21
|
+
if @auth == null and @policy == null
|
|
20
22
|
@auth = inheritedAuth
|
|
23
|
+
@policy = inheritedPolicy
|
|
24
|
+
if @reauthSeconds == null
|
|
25
|
+
@reauthSeconds = inheritedReauthSeconds
|
|
21
26
|
|
|
22
27
|
Group:endpoint(spec)
|
|
23
28
|
if !is_string(spec.path) or !spec.path.startsWith("/")
|
|
24
29
|
throw "Endpoint path must begin with /"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
auth = @auth
|
|
31
|
-
@routes.endpoint({
|
|
32
|
-
path: "${@prefix}${spec.path}"
|
|
33
|
-
module: mod
|
|
34
|
-
handler: spec.handler
|
|
35
|
-
auth: auth
|
|
36
|
-
middleware: spec.middleware
|
|
37
|
-
})
|
|
30
|
+
item = merged(spec)
|
|
31
|
+
item.path = "${@prefix}${spec.path}"
|
|
32
|
+
if item.module == null
|
|
33
|
+
item.module = @module
|
|
34
|
+
@routes.endpoint(item)
|
|
38
35
|
return @this
|
|
39
36
|
|
|
40
37
|
Group:longpoll(spec)
|
|
41
|
-
item =
|
|
38
|
+
item = merged(spec)
|
|
42
39
|
item.path = "${@prefix}${spec.path}"
|
|
43
|
-
if item.auth == null
|
|
44
|
-
item.auth = @auth
|
|
45
40
|
if item.fallback != null
|
|
46
41
|
item.fallback = "${@prefix}${item.fallback}"
|
|
47
42
|
@routes.longpoll(item)
|
|
48
43
|
return @this
|
|
49
44
|
|
|
50
45
|
Group:group(spec)
|
|
51
|
-
return new Group(@routes, spec, @prefix, @module, @auth)
|
|
46
|
+
return new Group(@routes, spec, @prefix, @module, @auth, @policy, @reauthSeconds)
|
|
52
47
|
|
|
53
48
|
Group:include(spec)
|
|
54
49
|
mount = spec.path
|
|
@@ -57,5 +52,14 @@ Group:include(spec)
|
|
|
57
52
|
@routes.include({
|
|
58
53
|
module: spec.module
|
|
59
54
|
path: "${@prefix}${mount}"
|
|
60
|
-
}, @auth)
|
|
55
|
+
}, @auth, @policy, @reauthSeconds)
|
|
61
56
|
return @this
|
|
57
|
+
|
|
58
|
+
Group:merged(spec)
|
|
59
|
+
item = Object/assign({}, spec)
|
|
60
|
+
if item.auth == null and item.policy == null
|
|
61
|
+
item.auth = @auth
|
|
62
|
+
item.policy = @policy
|
|
63
|
+
if item.reauthSeconds == null
|
|
64
|
+
item.reauthSeconds = @reauthSeconds
|
|
65
|
+
return item
|
package/src/http_failure.lango
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import js global RegExp
|
|
2
2
|
|
|
3
|
-
export class HttpFailure(status, code, headers)
|
|
3
|
+
export class HttpFailure(status, code, headers, details = null)
|
|
4
4
|
methods
|
|
5
5
|
result()
|
|
6
6
|
|
|
7
|
-
HttpFailure(status, code, headers)
|
|
7
|
+
HttpFailure(status, code, headers, details = null)
|
|
8
8
|
if !is_number(status) or status % 1 != 0 or status < 400 or status > 599
|
|
9
9
|
throw "Rejected response requires an error status"
|
|
10
10
|
pattern = new RegExp("^[A-Za-z][A-Za-z0-9_]{0,63}$")
|
|
11
11
|
if !is_string(code) or !pattern.test(code)
|
|
12
12
|
throw "Rejected response requires an error code"
|
|
13
|
+
@details = details
|
|
13
14
|
@status = status
|
|
14
15
|
@code = code
|
|
15
16
|
@headers = from_json(to_json(headers))
|
|
@@ -17,10 +18,13 @@ HttpFailure(status, code, headers)
|
|
|
17
18
|
@headers["www-authenticate"] = "Bearer"
|
|
18
19
|
|
|
19
20
|
HttpFailure:result()
|
|
21
|
+
body = {
|
|
22
|
+
error: @code
|
|
23
|
+
}
|
|
24
|
+
if @details != null
|
|
25
|
+
body.needLogin = @details
|
|
20
26
|
return {
|
|
21
27
|
status: @status
|
|
22
28
|
headers: from_json(to_json(@headers))
|
|
23
|
-
body: to_json(
|
|
24
|
-
error: @code
|
|
25
|
-
})
|
|
29
|
+
body: to_json(body)
|
|
26
30
|
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import "./policy"
|
|
2
|
+
policyOptions(spec)
|
|
1
3
|
import js global Number
|
|
2
4
|
import js global Object
|
|
3
5
|
import js global process
|
|
@@ -10,7 +12,9 @@ longpollOptions(spec)
|
|
|
10
12
|
result = {
|
|
11
13
|
kind: "longpoll"
|
|
12
14
|
path: spec.path
|
|
13
|
-
auth:
|
|
15
|
+
auth: null
|
|
16
|
+
policy: null
|
|
17
|
+
reauthSeconds: 0
|
|
14
18
|
waitTimeoutMs: 20000
|
|
15
19
|
retentionMs: 300000
|
|
16
20
|
maxMessages: 1000
|
|
@@ -20,11 +24,13 @@ longpollOptions(spec)
|
|
|
20
24
|
if !Object/hasOwn(result, key) or key == "kind"
|
|
21
25
|
throw "Unknown longpoll option: ${key}"
|
|
22
26
|
result[key] = value
|
|
23
|
-
|
|
24
|
-
|
|
27
|
+
selected = policyOptions(result)
|
|
28
|
+
result.policy = selected.name
|
|
29
|
+
result.auth = selected.name
|
|
30
|
+
result.reauthSeconds = selected.reauthSeconds
|
|
31
|
+
result.reauthExplicit = spec.reauthSeconds != null
|
|
32
|
+
result.legacyAuth = selected.legacy
|
|
25
33
|
validPath(result.path)
|
|
26
|
-
if !is_string(result.auth) or result.auth == "" or result.auth.includes(",")
|
|
27
|
-
throw "Longpoll auth must name one permission"
|
|
28
34
|
if result.fallback != null
|
|
29
35
|
validPath(result.fallback)
|
|
30
36
|
limits = {
|
package/src/options.lango
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import "./policy"
|
|
2
|
+
policyRegistry(spec)
|
|
3
|
+
import "./client_identity"
|
|
4
|
+
clientOptions(spec, database, origin)
|
|
5
|
+
import "./origin"
|
|
6
|
+
originOptions(spec)
|
|
1
7
|
import "./longpoll_options"
|
|
2
8
|
redisOptions(spec)
|
|
3
9
|
import "./database_options"
|
|
@@ -44,6 +50,9 @@ options(root, supplied = null)
|
|
|
44
50
|
database: null
|
|
45
51
|
redis: null
|
|
46
52
|
static: null
|
|
53
|
+
origin: null
|
|
54
|
+
client: null
|
|
55
|
+
policies: null
|
|
47
56
|
}
|
|
48
57
|
if supplied != null
|
|
49
58
|
for supplied as key: value
|
|
@@ -79,9 +88,12 @@ options(root, supplied = null)
|
|
|
79
88
|
throw "JWT keys must contain at least 32 characters"
|
|
80
89
|
result.keys = keys
|
|
81
90
|
result.key = null
|
|
91
|
+
result.policies = policyRegistry(result.policies)
|
|
82
92
|
result.database = databaseOptions(result.root, result.database, result.workers)
|
|
83
93
|
result.redis = redisOptions(result.redis)
|
|
84
94
|
result.static = staticOptions(result.root, result.static)
|
|
95
|
+
result.origin = originOptions(result.origin)
|
|
96
|
+
result.client = clientOptions(result.client, result.database, result.origin)
|
|
85
97
|
return result
|
|
86
98
|
|
|
87
99
|
deployment(directory)
|
|
@@ -94,10 +106,31 @@ deployment(directory)
|
|
|
94
106
|
seen = []
|
|
95
107
|
for root.children() as tag
|
|
96
108
|
name = tag.tag()
|
|
97
|
-
if seen.includes(name) or tag.children().length != 0 or tag.innerText().trim() != ""
|
|
109
|
+
if seen.includes(name) or (name != "policies" and tag.children().length != 0) or tag.innerText().trim() != ""
|
|
98
110
|
throw "Duplicate or nonempty deployment section: ${name}"
|
|
99
111
|
seen.add(name)
|
|
100
112
|
switch name
|
|
113
|
+
case "policies"
|
|
114
|
+
attributes(tag, ["reauthSeconds"])
|
|
115
|
+
items = []
|
|
116
|
+
for tag.children() as child
|
|
117
|
+
if child.tag() != "policy" or child.children().length != 0 or child.innerText().trim() != ""
|
|
118
|
+
throw "Policies may contain only empty policy elements"
|
|
119
|
+
attributes(child, ["name", "reauthSeconds"])
|
|
120
|
+
items.add(child.attributes())
|
|
121
|
+
settings.policies = {
|
|
122
|
+
reauthSeconds: tag.reauthSeconds
|
|
123
|
+
items: items
|
|
124
|
+
}
|
|
125
|
+
continue
|
|
126
|
+
case "client"
|
|
127
|
+
attributes(tag, ["maxAgeSeconds"])
|
|
128
|
+
settings.client = tag.attributes()
|
|
129
|
+
continue
|
|
130
|
+
case "origin"
|
|
131
|
+
attributes(tag, ["url", "urlEnv", "required"])
|
|
132
|
+
settings.origin = tag.attributes()
|
|
133
|
+
continue
|
|
101
134
|
case "static"
|
|
102
135
|
attributes(tag, ["path", "directory", "index", "fallback", "runtime"])
|
|
103
136
|
settings.static = tag.attributes()
|