@heybejs/core 1.3.0 → 1.5.0
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/package.json +1 -1
- package/src/app.ts +5 -1
- package/src/auth/sessions.ts +111 -0
- package/src/auth/tokens.ts +20 -2
- package/src/collections/scaffold.ts +18 -0
- package/src/collections/store.ts +12 -8
- package/src/commands/args.ts +1 -0
- package/src/commands/help.ts +6 -0
- package/src/commands/serve.ts +1 -0
- package/src/cron/system.ts +6 -0
- package/src/hooks/system.ts +7 -5
- package/src/http/router.ts +2 -0
- package/src/http/routes/record-auth-flows.ts +1 -1
- package/src/http/routes/record-auth-oauth2.ts +2 -1
- package/src/http/routes/record-auth.ts +37 -3
- package/src/http/routes/sessions.ts +101 -0
- package/src/http/server.ts +2 -0
- package/src/import/decrypt.ts +12 -1
- package/src/realtime/broadcast.ts +12 -0
- package/src/settings.ts +60 -3
- package/ui/dist/assets/{index-HQ9u4sHS.js → index-B-BaSmc0.js} +3 -3
- package/ui/dist/index.html +1 -1
package/src/settings.ts
CHANGED
|
@@ -15,6 +15,7 @@ import { SettingsReloadEvent } from "./hooks/events";
|
|
|
15
15
|
import { isEmail } from "./fields/email";
|
|
16
16
|
import { isUrl } from "./fields/url";
|
|
17
17
|
import { isIpOrSubnet } from "./ip";
|
|
18
|
+
import { decryptPbValue, encryptPbValue } from "./import/decrypt";
|
|
18
19
|
import { ValidationError } from "./util";
|
|
19
20
|
|
|
20
21
|
export interface RateLimitRule {
|
|
@@ -557,7 +558,63 @@ export class Settings {
|
|
|
557
558
|
private cached: SettingsData | null = null;
|
|
558
559
|
app?: App;
|
|
559
560
|
|
|
560
|
-
|
|
561
|
+
// `encryptionEnv` is the --encryptionEnv flag: the env variable that holds the settings key
|
|
562
|
+
constructor(
|
|
563
|
+
private readonly db: Db,
|
|
564
|
+
private readonly encryptionEnv?: string,
|
|
565
|
+
) {}
|
|
566
|
+
|
|
567
|
+
private key(): string | undefined {
|
|
568
|
+
if (!this.encryptionEnv) return undefined;
|
|
569
|
+
const key = process.env[this.encryptionEnv];
|
|
570
|
+
if (!key)
|
|
571
|
+
throw new Error(
|
|
572
|
+
`the --encryptionEnv variable "${this.encryptionEnv}" is not set or empty`,
|
|
573
|
+
);
|
|
574
|
+
if (key.length !== 32)
|
|
575
|
+
throw new Error(
|
|
576
|
+
`the --encryptionEnv variable "${this.encryptionEnv}" must hold exactly 32 characters`,
|
|
577
|
+
);
|
|
578
|
+
return key;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// plain JSON always starts with "{", an encrypted value is base64
|
|
582
|
+
private decode(stored: string): unknown {
|
|
583
|
+
if (stored.startsWith("{")) return JSON.parse(stored);
|
|
584
|
+
const key = this.key();
|
|
585
|
+
if (!key)
|
|
586
|
+
throw new Error(
|
|
587
|
+
"the app settings are encrypted, start with --encryptionEnv and the name of the variable that holds the key",
|
|
588
|
+
);
|
|
589
|
+
try {
|
|
590
|
+
return JSON.parse(decryptPbValue(stored, key));
|
|
591
|
+
} catch {
|
|
592
|
+
throw new Error(
|
|
593
|
+
`failed to decrypt the app settings: the key in "${this.encryptionEnv}" does not match the one they were encrypted with`,
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
private encode(data: SettingsData): string {
|
|
599
|
+
const json = JSON.stringify(data);
|
|
600
|
+
const key = this.key();
|
|
601
|
+
return key ? encryptPbValue(json, key) : json;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
// Encrypts settings that were stored as plain JSON, so turning the flag on takes effect at start
|
|
605
|
+
async encryptStored(): Promise<void> {
|
|
606
|
+
const key = this.key();
|
|
607
|
+
if (!key) return;
|
|
608
|
+
await this.db.write(() => {
|
|
609
|
+
const row = this.db.get<{ value: string }>(
|
|
610
|
+
"SELECT value FROM _params WHERE key = 'settings'",
|
|
611
|
+
);
|
|
612
|
+
if (!row?.value.startsWith("{")) return;
|
|
613
|
+
this.db.run("UPDATE _params SET value = ? WHERE key = 'settings'", [
|
|
614
|
+
encryptPbValue(row.value, key),
|
|
615
|
+
]);
|
|
616
|
+
});
|
|
617
|
+
}
|
|
561
618
|
|
|
562
619
|
// The cached settings without a copy, for hot paths: callers must not modify them.
|
|
563
620
|
current(): Readonly<SettingsData> {
|
|
@@ -590,7 +647,7 @@ export class Settings {
|
|
|
590
647
|
);
|
|
591
648
|
return merge(
|
|
592
649
|
SCHEMA,
|
|
593
|
-
row ?
|
|
650
|
+
row ? this.decode(row.value) : {},
|
|
594
651
|
DEFAULTS,
|
|
595
652
|
) as SettingsData;
|
|
596
653
|
}
|
|
@@ -625,7 +682,7 @@ export class Settings {
|
|
|
625
682
|
);
|
|
626
683
|
this.db.run(
|
|
627
684
|
"INSERT INTO _params (key, value) VALUES ('settings', ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value",
|
|
628
|
-
[
|
|
685
|
+
[this.encode(next)],
|
|
629
686
|
);
|
|
630
687
|
this.db.onComplete((err) => {
|
|
631
688
|
if (err !== null) this.cached = null;
|