levix-bot 4.9.1 → 5.0.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/README.md +57 -3
- package/SETUP.md +63 -7
- package/app.cjs +159 -46
- package/deploy/install.sh +10 -5
- package/package.json +9 -6
- package/public/dashboard/assets/40af6d88b63079a0-BZ8ojJNu.woff2 +0 -0
- package/public/dashboard/assets/481acf6760031cee-BypgxfGb.woff2 +0 -0
- package/public/dashboard/assets/6d3351fc535f8ef3-D2-eand5.woff2 +0 -0
- package/public/dashboard/assets/8ab0af53a3bda9d3-CyCXRvzh.woff2 +0 -0
- package/public/dashboard/assets/8f262393e9249e61-TQp-UTiE.woff2 +0 -0
- package/public/dashboard/assets/94d65887a83f1282-DmpS2jIq.woff2 +0 -0
- package/public/dashboard/assets/99e400330f36ae77-D3oMJlXt.woff2 +0 -0
- package/public/dashboard/assets/9d1f41b13e519d90-BVNSOH3d.woff2 +0 -0
- package/public/dashboard/assets/a005256cb957ecc7-CoYeBiSI.woff2 +0 -0
- package/public/dashboard/assets/b19c2080652a3c2b-DIC32ArD.woff2 +0 -0
- package/public/dashboard/assets/c4064400db3750ce-BehTv68k.woff2 +0 -0
- package/public/dashboard/assets/c69d345fa1068c2d-CmI34b-g.woff2 +0 -0
- package/public/dashboard/assets/cc27b30471250057-6fWv1k7M.woff2 +0 -0
- package/public/dashboard/assets/d8c30647abfeffe6-eXO_dkmS.woff2 +0 -0
- package/public/dashboard/assets/eb83d8d40af4411f-Buze_B52.woff2 +0 -0
- package/public/dashboard/assets/effc6711249e7349-qRpaaN48.woff2 +0 -0
- package/public/dashboard/assets/gateway-BDYet0pI.css +1 -0
- package/public/dashboard/assets/gateway-BYt6-s1O.js +1 -0
- package/public/dashboard/assets/index-B9wFpD_z.css +2 -0
- package/public/dashboard/assets/index-DWcQmu0_.js +124 -0
- package/public/dashboard/assets/index-Da-j89O-.js +458 -0
- package/public/dashboard/font-licenses/jetbrainsmono-OFL.txt +93 -0
- package/public/dashboard/font-licenses/plusjakartasans-OFL.txt +93 -0
- package/public/dashboard/font-licenses/tajawal-OFL.txt +93 -0
- package/public/dashboard/gateway.html +17 -0
- package/public/dashboard/index.html +3 -5
- package/public/ui-boot.js +33 -14
- package/scheduler.cjs +76 -23
- package/src/auth/auth-storage.cjs +54 -7
- package/src/auth/use-database-auth-state.js +70 -39
- package/src/automation/engine.js +412 -0
- package/src/automation/limits.cjs +6 -0
- package/src/automation/manage.js +290 -0
- package/src/automation/reactions.js +471 -0
- package/src/automation/rules.cjs +114 -0
- package/src/automation/status.cjs +76 -0
- package/src/backup/account.cjs +29 -0
- package/src/backup/activation.cjs +247 -0
- package/src/backup/content.cjs +36 -0
- package/src/backup/create.cjs +280 -0
- package/src/backup/extensions.cjs +101 -0
- package/src/backup/format.cjs +565 -0
- package/src/backup/jobs.cjs +111 -0
- package/src/backup/journal.cjs +41 -0
- package/src/backup/plan.cjs +223 -0
- package/src/backup/recovery.cjs +110 -0
- package/src/backup/restore.cjs +460 -0
- package/src/backup/uploads.cjs +146 -0
- package/src/bootstrap/core.js +50 -12
- package/src/bootstrap/panel.js +53 -2
- package/src/bootstrap/session-startup-policy.js +8 -1
- package/src/cli.js +163 -22
- package/src/commands/autodelete.cjs +41 -57
- package/src/commands/autoschedule.cjs +7 -33
- package/src/commands/blacklist.cjs +17 -35
- package/src/commands/block.cjs +4 -18
- package/src/commands/calc.cjs +22 -74
- package/src/commands/debt.cjs +28 -59
- package/src/commands/deleteschedule.cjs +7 -23
- package/src/commands/digit.cjs +5 -39
- package/src/commands/gemini.cjs +135 -218
- package/src/commands/group/add.cjs +28 -76
- package/src/commands/group/all.cjs +4 -15
- package/src/commands/group/antilink.cjs +28 -41
- package/src/commands/group/antispam.cjs +25 -35
- package/src/commands/group/approveall.cjs +7 -21
- package/src/commands/group/clearwarns.cjs +4 -18
- package/src/commands/group/deletenote.cjs +4 -18
- package/src/commands/group/demote.cjs +6 -23
- package/src/commands/group/flagged.cjs +10 -19
- package/src/commands/group/kick.cjs +26 -36
- package/src/commands/group/media.cjs +21 -36
- package/src/commands/group/members.cjs +16 -28
- package/src/commands/group/note.cjs +3 -14
- package/src/commands/group/policy.cjs +16 -37
- package/src/commands/group/promote.cjs +5 -22
- package/src/commands/group/removeall.cjs +6 -38
- package/src/commands/group/rules.cjs +4 -19
- package/src/commands/group/save.cjs +4 -18
- package/src/commands/group/setname.cjs +5 -19
- package/src/commands/group/setpp.cjs +8 -26
- package/src/commands/group/setrules.cjs +4 -15
- package/src/commands/group/setwarn.cjs +6 -23
- package/src/commands/group/tagadmins.cjs +5 -16
- package/src/commands/group/warn.cjs +5 -16
- package/src/commands/group/warns.cjs +12 -33
- package/src/commands/group/welcome.cjs +17 -34
- package/src/commands/group.cjs +91 -68
- package/src/commands/help.cjs +48 -89
- package/src/commands/lang.cjs +38 -97
- package/src/commands/listschedules.cjs +19 -30
- package/src/commands/loop.cjs +9 -55
- package/src/commands/md.cjs +2 -13
- package/src/commands/memory.cjs +53 -87
- package/src/commands/mod.cjs +40 -79
- package/src/commands/notes.cjs +4 -15
- package/src/commands/pack.cjs +26 -49
- package/src/commands/packs.cjs +8 -18
- package/src/commands/perm.cjs +48 -51
- package/src/commands/ping.cjs +2 -10
- package/src/commands/poll.cjs +5 -38
- package/src/commands/prayer.cjs +55 -36
- package/src/commands/qr.cjs +4 -18
- package/src/commands/rand.cjs +9 -58
- package/src/commands/restart.cjs +15 -33
- package/src/commands/schedule.cjs +5 -25
- package/src/commands/score.cjs +6 -27
- package/src/commands/send.cjs +4 -18
- package/src/commands/setprefix.cjs +11 -23
- package/src/commands/shortlink.cjs +13 -39
- package/src/commands/status.cjs +188 -76
- package/src/commands/sticker.cjs +5 -16
- package/src/commands/stickers.cjs +16 -20
- package/src/commands/stopbot.cjs +9 -25
- package/src/commands/stt.cjs +75 -107
- package/src/commands/todo.cjs +11 -31
- package/src/commands/toimage.cjs +11 -27
- package/src/commands/translate.cjs +53 -36
- package/src/commands/tts.cjs +46 -40
- package/src/commands/unblock.cjs +3 -17
- package/src/commands/update.cjs +23 -0
- package/src/commands/weather.cjs +40 -47
- package/src/config/ai-identity.cjs +14 -1
- package/src/config/baileys.config.js +16 -3
- package/src/config/data-dir.cjs +37 -0
- package/src/config/defaults.cjs +1 -0
- package/src/config/exit-codes.cjs +11 -0
- package/src/config/lock.cjs +55 -81
- package/src/config/official-links.cjs +61 -0
- package/src/config/paths.cjs +4 -33
- package/src/config/release.cjs +87 -0
- package/src/config/settings.cjs +199 -10
- package/src/core/connection.js +77 -9
- package/src/core/events.js +119 -33
- package/src/core/session.js +536 -84
- package/src/core/socket.js +81 -10
- package/src/db/copy.cjs +136 -0
- package/src/db/db.cjs +241 -36
- package/src/db/startup-guard.js +5 -0
- package/src/db/store.cjs +561 -54
- package/src/diagnostics/baileysLog.cjs +87 -0
- package/src/diagnostics/debugLog.cjs +16 -4
- package/src/diagnostics/device.cjs +4 -2
- package/src/diagnostics/nodeRuntime.cjs +131 -0
- package/src/diagnostics/signalSessionInspect.cjs +38 -0
- package/src/domain/command.js +24 -22
- package/src/extensions/autoReply.cjs +521 -0
- package/src/extensions/context.cjs +73 -0
- package/src/extensions/contract.cjs +69 -0
- package/src/extensions/entitlement/client.cjs +381 -0
- package/src/extensions/entitlement/index.cjs +629 -0
- package/src/extensions/entitlement/installation.cjs +117 -0
- package/src/extensions/entitlement/keys.cjs +72 -0
- package/src/extensions/entitlement/state.cjs +201 -0
- package/src/extensions/entitlement/target.cjs +7 -0
- package/src/extensions/entitlement/token.cjs +225 -0
- package/src/extensions/host.cjs +519 -0
- package/src/extensions/templateKeys.cjs +81 -0
- package/src/handlers/command.handler.js +191 -64
- package/src/handlers/group.handler.js +78 -18
- package/src/handlers/message.handler.js +56 -25
- package/src/i18n/catalog.generated.cjs +3537 -0
- package/src/index.js +51 -12
- package/src/legal/bundled/ar.json +102 -0
- package/src/legal/bundled/en.json +102 -0
- package/src/legal/bundled/es-419.json +102 -0
- package/src/legal/bundled/pt-BR.json +102 -0
- package/src/legal/cli.cjs +200 -0
- package/src/legal/consent.cjs +187 -0
- package/src/legal/document.cjs +156 -0
- package/src/legal/keys.cjs +100 -0
- package/src/legal/release.cjs +226 -0
- package/src/legal/remote.cjs +419 -0
- package/src/legal/terms.cjs +142 -0
- package/src/messaging/escape.cjs +31 -0
- package/src/messaging/health.cjs +65 -0
- package/src/messaging/identity.cjs +42 -0
- package/src/messaging/message.cjs +136 -0
- package/src/messaging/parser.cjs +88 -0
- package/src/messaging/router.cjs +217 -0
- package/src/middleware/permissions.middleware.js +34 -2
- package/src/panel/account-profile.cjs +489 -0
- package/src/panel/fallback-page.cjs +177 -0
- package/src/panel/password-reset.cjs +13 -14
- package/src/panel/recovery-email.cjs +3 -1
- package/src/routes/backup.api.esm.js +175 -0
- package/src/routes/dashboard.api.esm.js +183 -7
- package/src/routes/plus.api.esm.js +172 -0
- package/src/routes/reactions.api.esm.js +60 -0
- package/src/services/aiAgent.cjs +56 -43
- package/src/services/aiDocs.cjs +187 -0
- package/src/services/aiImage.cjs +183 -0
- package/src/services/aiMedia.cjs +146 -0
- package/src/services/aiProviders.cjs +76 -80
- package/src/services/aiRouter.cjs +2 -2
- package/src/services/aiToolAuth.cjs +76 -4
- package/src/services/aiTools.cjs +431 -105
- package/src/services/autoDelete.cjs +9 -1
- package/src/services/levix-docs.generated.json +2623 -0
- package/src/services/memoryPolicy.cjs +56 -0
- package/src/services/updateCheck.cjs +362 -0
- package/src/stickers/errors.cjs +53 -97
- package/src/stickers/library.cjs +13 -6
- package/src/stickers/owner.cjs +1 -1
- package/src/utils/commandContext.cjs +13 -0
- package/src/utils/commandDocs.cjs +42 -20
- package/src/utils/geminiHttpOptions.cjs +56 -0
- package/src/utils/geminiMedia.cjs +28 -3
- package/src/utils/groupMetadataCache.cjs +386 -21
- package/src/utils/httpsFetch.cjs +113 -0
- package/src/utils/i18n.cjs +117 -7
- package/src/utils/linkPreview.cjs +90 -0
- package/src/utils/logger.cjs +6 -0
- package/src/utils/markdownParser.cjs +3 -3
- package/src/utils/memory.cjs +375 -28
- package/src/utils/messageContent.cjs +7 -1
- package/src/utils/moderationAction.cjs +116 -73
- package/src/utils/moderationSummary.cjs +32 -56
- package/src/utils/permissionLevel.cjs +7 -28
- package/src/utils/permissions.cjs +23 -14
- package/src/utils/permissions.esm.js +99 -166
- package/src/utils/recentMessageCache.esm.js +241 -37
- package/src/utils/recurrence.cjs +78 -66
- package/src/utils/sendBotMessage.cjs +14 -29
- package/src/utils/sendBotMessage.esm.js +28 -13
- package/src/utils/statusMessage.cjs +94 -33
- package/src/utils/stickerBot.cjs +18 -16
- package/src/utils/storage.cjs +2 -0
- package/src/utils/storage.esm.js +2 -0
- package/src/utils/thumbnail.cjs +5 -21
- package/public/dashboard/assets/index-B1sOW2VX.js +0 -561
- package/public/dashboard/assets/index-CK8osB4o.css +0 -2
- package/public/dashboard.css +0 -3127
- package/public/dashboard.js +0 -1981
- package/public/gateway-i18n.js +0 -321
- package/public/gateway.js +0 -70
- package/views/dashboard.ejs +0 -775
- package/views/login.ejs +0 -408
- package/views/qr.ejs +0 -155
- package/views/setup.ejs +0 -118
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// Installation identity for Levix Plus.
|
|
2
|
+
//
|
|
3
|
+
// The Ed25519 keypair is created the first time something asks for it, never
|
|
4
|
+
// at boot, so a Free install never writes one. The PKCS8 private key is sealed
|
|
5
|
+
// with the vault and stored at `plus:installation-key`. The installation id is
|
|
6
|
+
// base64url(SHA-256(raw public key)): 43 characters, no personal data.
|
|
7
|
+
|
|
8
|
+
const crypto = require("node:crypto");
|
|
9
|
+
|
|
10
|
+
const { rawFromKeyObject } = require("./keys.cjs");
|
|
11
|
+
|
|
12
|
+
const INSTALLATION_KEY = "plus:installation-key";
|
|
13
|
+
|
|
14
|
+
function createInstallation(deps = {}) {
|
|
15
|
+
const storage = () => deps.storage ?? require("../../db/store.cjs");
|
|
16
|
+
const vault = () => deps.vault ?? require("../../config/vault.cjs");
|
|
17
|
+
let cache = null;
|
|
18
|
+
|
|
19
|
+
function readSealed() {
|
|
20
|
+
const store = storage();
|
|
21
|
+
const value =
|
|
22
|
+
typeof store.getBotSetting === "function"
|
|
23
|
+
? store.getBotSetting(INSTALLATION_KEY, null)
|
|
24
|
+
: typeof store.get === "function"
|
|
25
|
+
? (store.get(INSTALLATION_KEY) ?? null)
|
|
26
|
+
: null;
|
|
27
|
+
if (typeof value !== "string" || value.length === 0) return null;
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function writeSealed(value) {
|
|
32
|
+
const store = storage();
|
|
33
|
+
if (typeof store.saveBotSetting === "function") store.saveBotSetting(INSTALLATION_KEY, value);
|
|
34
|
+
else if (typeof store.set === "function") store.set(INSTALLATION_KEY, value);
|
|
35
|
+
else throw unreadable();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function importSealed(sealed) {
|
|
39
|
+
const v = vault();
|
|
40
|
+
if (typeof v.isSealed === "function" && !v.isSealed(sealed)) throw unreadable();
|
|
41
|
+
let pkcs8;
|
|
42
|
+
try {
|
|
43
|
+
pkcs8 = v.open(sealed);
|
|
44
|
+
} catch {
|
|
45
|
+
throw unreadable();
|
|
46
|
+
}
|
|
47
|
+
if (typeof pkcs8 !== "string" || !/^[A-Za-z0-9+/]+={0,2}$/.test(pkcs8)) throw unreadable();
|
|
48
|
+
const der = Buffer.from(pkcs8, "base64");
|
|
49
|
+
if (der.length === 0 || der.toString("base64") !== pkcs8) throw unreadable();
|
|
50
|
+
try {
|
|
51
|
+
const privateKey = crypto.createPrivateKey({ key: der, format: "der", type: "pkcs8" });
|
|
52
|
+
const raw = rawFromKeyObject(crypto.createPublicKey(privateKey));
|
|
53
|
+
const installationId = crypto.createHash("sha256").update(raw).digest("base64url");
|
|
54
|
+
return { privateKey, raw, installationId };
|
|
55
|
+
} catch (error) {
|
|
56
|
+
if (error?.code === "INSTALLATION_UNREADABLE") throw error;
|
|
57
|
+
throw unreadable();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function viewFrom(sealed, installationId, raw) {
|
|
62
|
+
return Object.freeze({
|
|
63
|
+
installationId,
|
|
64
|
+
publicKeyRaw: Buffer.from(raw),
|
|
65
|
+
sign(bytes) {
|
|
66
|
+
const currentSealed = readSealed();
|
|
67
|
+
if (currentSealed == null || currentSealed !== sealed) throw unreadable();
|
|
68
|
+
const loaded = importSealed(currentSealed);
|
|
69
|
+
if (loaded.installationId !== installationId) throw unreadable();
|
|
70
|
+
const data = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes);
|
|
71
|
+
return crypto.sign(null, data, loaded.privateKey);
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function load() {
|
|
77
|
+
const sealed = readSealed();
|
|
78
|
+
if (sealed == null) {
|
|
79
|
+
cache = null;
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
if (cache && cache.sealed === sealed) return cache.view;
|
|
83
|
+
const { installationId, raw } = importSealed(sealed);
|
|
84
|
+
const view = viewFrom(sealed, installationId, raw);
|
|
85
|
+
cache = { sealed, view };
|
|
86
|
+
return view;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function getInstallation() {
|
|
90
|
+
return load();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function getOrCreateInstallation() {
|
|
94
|
+
if (readSealed() != null) return load();
|
|
95
|
+
const { privateKey } = crypto.generateKeyPairSync("ed25519");
|
|
96
|
+
const pkcs8 = privateKey.export({ format: "der", type: "pkcs8" }).toString("base64");
|
|
97
|
+
writeSealed(vault().seal(pkcs8));
|
|
98
|
+
return load();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return { getInstallation, getOrCreateInstallation };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function unreadable() {
|
|
105
|
+
const error = new Error("installation key cannot be opened");
|
|
106
|
+
error.code = "INSTALLATION_UNREADABLE";
|
|
107
|
+
return error;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const defaults = createInstallation();
|
|
111
|
+
|
|
112
|
+
module.exports = {
|
|
113
|
+
INSTALLATION_KEY,
|
|
114
|
+
createInstallation,
|
|
115
|
+
getInstallation: () => defaults.getInstallation(),
|
|
116
|
+
getOrCreateInstallation: () => defaults.getOrCreateInstallation(),
|
|
117
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Public keys that may sign a Levix Plus entitlement token.
|
|
2
|
+
//
|
|
3
|
+
// The private key never lives here. The owner generates it outside this
|
|
4
|
+
// repository and adds only the public half, the same way as a Terms key.
|
|
5
|
+
// An empty list is intentional: nothing verifies, and Plus stays off.
|
|
6
|
+
// Callers pass the list into the verifier. There is no setter.
|
|
7
|
+
|
|
8
|
+
const {
|
|
9
|
+
assertTrustedKeys,
|
|
10
|
+
keyIdForRaw,
|
|
11
|
+
publicKeyObjectFromRaw,
|
|
12
|
+
rawFromKeyObject,
|
|
13
|
+
rawPublicKeyFromBase64,
|
|
14
|
+
} = require("../../legal/keys.cjs");
|
|
15
|
+
|
|
16
|
+
const { TRUSTED_KEYS } = require("./target.cjs");
|
|
17
|
+
assertTrustedKeys(TRUSTED_KEYS);
|
|
18
|
+
const loadedKeyCache = new Map();
|
|
19
|
+
|
|
20
|
+
function keyIdFor(rawPublicKey) {
|
|
21
|
+
if (!Buffer.isBuffer(rawPublicKey) && !(rawPublicKey instanceof Uint8Array)) {
|
|
22
|
+
const error = new Error("public key must be 32 bytes");
|
|
23
|
+
error.code = "ENTITLEMENT_KEY_INVALID";
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
const raw = Buffer.from(rawPublicKey);
|
|
27
|
+
if (raw.length !== 32) {
|
|
28
|
+
const error = new Error("public key must be 32 bytes");
|
|
29
|
+
error.code = "ENTITLEMENT_KEY_INVALID";
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
return keyIdForRaw(raw);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** One frozen `{ keyId, publicKey, raw, keyObject }` from a trusted entry. Throws when the entry is not that key. */
|
|
36
|
+
function loadTrustedKey(entry) {
|
|
37
|
+
assertTrustedKeys([entry]);
|
|
38
|
+
const cacheKey = `${entry.keyId}:${entry.publicKey}`;
|
|
39
|
+
const cached = loadedKeyCache.get(cacheKey);
|
|
40
|
+
if (cached) return cached;
|
|
41
|
+
const raw = rawPublicKeyFromBase64(entry.publicKey);
|
|
42
|
+
const loaded = Object.freeze({
|
|
43
|
+
keyId: entry.keyId,
|
|
44
|
+
publicKey: entry.publicKey,
|
|
45
|
+
raw: Buffer.from(raw),
|
|
46
|
+
keyObject: publicKeyObjectFromRaw(raw),
|
|
47
|
+
});
|
|
48
|
+
loadedKeyCache.set(cacheKey, loaded);
|
|
49
|
+
return loaded;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The single trusted entry for `kid`, or null when it is missing, repeated, or unusable. */
|
|
53
|
+
function findTrustedKey(keys, kid) {
|
|
54
|
+
if (typeof kid !== "string" || !/^[0-9a-f]{16}$/.test(kid) || !Array.isArray(keys)) return null;
|
|
55
|
+
const matches = keys.filter((entry) => entry && entry.keyId === kid);
|
|
56
|
+
if (matches.length !== 1) return null;
|
|
57
|
+
try {
|
|
58
|
+
return loadTrustedKey(matches[0]);
|
|
59
|
+
} catch {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
module.exports = {
|
|
65
|
+
TRUSTED_KEYS,
|
|
66
|
+
assertTrustedKeys,
|
|
67
|
+
keyIdFor,
|
|
68
|
+
loadTrustedKey,
|
|
69
|
+
findTrustedKey,
|
|
70
|
+
publicKeyObjectFromRaw,
|
|
71
|
+
rawFromKeyObject,
|
|
72
|
+
};
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// Persisted entitlement cache and the clock rules that decide if it still counts.
|
|
2
|
+
//
|
|
3
|
+
// Trusted time is local time plus the offset captured when the token arrived
|
|
4
|
+
// (`iat` minus the local clock). The high-water mark only moves forward. A
|
|
5
|
+
// clock set back past that mark, or before `iat` by more than five minutes,
|
|
6
|
+
// fails closed until a newer token is accepted.
|
|
7
|
+
|
|
8
|
+
const { MAX_TOKEN_BYTES, verifyToken } = require("./token.cjs");
|
|
9
|
+
const { TRUSTED_KEYS } = require("./keys.cjs");
|
|
10
|
+
|
|
11
|
+
const RECORD_KEY = "plus:entitlement";
|
|
12
|
+
const HW_KEY = "plus:entitlement-hw";
|
|
13
|
+
const SKEW_MS = 300_000;
|
|
14
|
+
const HIGH_WATER_PERSIST_MS = 300_000;
|
|
15
|
+
const NONE = Object.freeze({ state: "none", until: null, reason: null, claims: null });
|
|
16
|
+
|
|
17
|
+
function unverified(reason) {
|
|
18
|
+
return { state: "unverified", until: null, reason, claims: null };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function untilFor(claims) {
|
|
22
|
+
return new Date(claims.exp * 1000).toISOString();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function recordShape(record) {
|
|
26
|
+
return (
|
|
27
|
+
!!record &&
|
|
28
|
+
typeof record === "object" &&
|
|
29
|
+
!Array.isArray(record) &&
|
|
30
|
+
typeof record.token === "string" &&
|
|
31
|
+
record.token.length > 0 &&
|
|
32
|
+
Buffer.byteLength(record.token, "utf8") <= MAX_TOKEN_BYTES
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function hwShape(hw) {
|
|
37
|
+
return (
|
|
38
|
+
!!hw &&
|
|
39
|
+
typeof hw === "object" &&
|
|
40
|
+
!Array.isArray(hw) &&
|
|
41
|
+
typeof hw.installationId === "string" &&
|
|
42
|
+
hw.installationId.length > 0 &&
|
|
43
|
+
Number.isSafeInteger(hw.maxSeq) &&
|
|
44
|
+
hw.maxSeq >= 0 &&
|
|
45
|
+
typeof hw.jti === "string" &&
|
|
46
|
+
hw.jti.length > 0 &&
|
|
47
|
+
Number.isFinite(hw.highWaterMs) &&
|
|
48
|
+
Number.isFinite(hw.serverOffsetMs)
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function sealHighWater(hw, vaultImpl) {
|
|
53
|
+
const v = vaultImpl ?? require("../../config/vault.cjs");
|
|
54
|
+
return v.seal(
|
|
55
|
+
JSON.stringify({
|
|
56
|
+
installationId: hw.installationId,
|
|
57
|
+
maxSeq: hw.maxSeq,
|
|
58
|
+
highWaterMs: hw.highWaterMs,
|
|
59
|
+
serverOffsetMs: hw.serverOffsetMs,
|
|
60
|
+
jti: hw.jti,
|
|
61
|
+
}),
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function openHighWater(sealed, vaultImpl) {
|
|
66
|
+
if (typeof sealed !== "string" || sealed.length === 0) return null;
|
|
67
|
+
const v = vaultImpl ?? require("../../config/vault.cjs");
|
|
68
|
+
if (typeof v.isSealed === "function" && !v.isSealed(sealed)) return null;
|
|
69
|
+
let raw;
|
|
70
|
+
try {
|
|
71
|
+
raw = v.open(sealed);
|
|
72
|
+
} catch {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
if (typeof raw !== "string" || raw.length === 0) return null;
|
|
76
|
+
let parsed;
|
|
77
|
+
try {
|
|
78
|
+
parsed = JSON.parse(raw);
|
|
79
|
+
} catch {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
return hwShape(parsed) ? parsed : null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @returns {{ state: string, until: string|null, reason: string|null, claims: object|null }}
|
|
87
|
+
*/
|
|
88
|
+
function evaluate(record, localNowMs, options = {}) {
|
|
89
|
+
if (record == null) return NONE;
|
|
90
|
+
if (!recordShape(record) || !Number.isFinite(localNowMs)) return unverified("bad_record");
|
|
91
|
+
let installationId = options.installationId;
|
|
92
|
+
if (!Object.hasOwn(options, "installationId")) {
|
|
93
|
+
try {
|
|
94
|
+
installationId = require("./installation.cjs").getInstallation()?.installationId ?? null;
|
|
95
|
+
} catch {
|
|
96
|
+
installationId = null;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const verified =
|
|
100
|
+
options.verified ??
|
|
101
|
+
verifyToken(record.token, {
|
|
102
|
+
keys: options.keys ?? TRUSTED_KEYS,
|
|
103
|
+
installationId,
|
|
104
|
+
storedSeq: null,
|
|
105
|
+
storedJti: null,
|
|
106
|
+
});
|
|
107
|
+
if (!verified.ok) return unverified(verified.reason);
|
|
108
|
+
|
|
109
|
+
const claims = verified.claims;
|
|
110
|
+
if (Number.isSafeInteger(options.maxSeq)) {
|
|
111
|
+
if (
|
|
112
|
+
claims.seq < options.maxSeq ||
|
|
113
|
+
(claims.seq === options.maxSeq &&
|
|
114
|
+
typeof options.maxJti === "string" &&
|
|
115
|
+
claims.jti !== options.maxJti)
|
|
116
|
+
) {
|
|
117
|
+
return unverified("rollback");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (
|
|
121
|
+
(record.seq != null && record.seq !== claims.seq) ||
|
|
122
|
+
(record.jti != null && record.jti !== claims.jti)
|
|
123
|
+
) {
|
|
124
|
+
return unverified("bad_record");
|
|
125
|
+
}
|
|
126
|
+
const hw = options.hw;
|
|
127
|
+
if (!hwShape(hw) || hw.installationId !== installationId) {
|
|
128
|
+
return unverified("bad_record");
|
|
129
|
+
}
|
|
130
|
+
if (claims.seq < hw.maxSeq || (claims.seq === hw.maxSeq && claims.jti !== hw.jti)) {
|
|
131
|
+
return unverified("rollback");
|
|
132
|
+
}
|
|
133
|
+
if (claims.seq > hw.maxSeq) {
|
|
134
|
+
return unverified("bad_record");
|
|
135
|
+
}
|
|
136
|
+
const trustedNow = localNowMs + hw.serverOffsetMs;
|
|
137
|
+
const until = untilFor(claims);
|
|
138
|
+
if (!Number.isFinite(trustedNow)) return unverified("bad_record");
|
|
139
|
+
if (trustedNow < hw.highWaterMs - SKEW_MS || trustedNow < claims.iat * 1000 - SKEW_MS) {
|
|
140
|
+
return { state: "clock", until, reason: "clock", claims };
|
|
141
|
+
}
|
|
142
|
+
if (trustedNow >= claims.exp * 1000) {
|
|
143
|
+
return { state: "expired", until, reason: "expired", claims };
|
|
144
|
+
}
|
|
145
|
+
if (claims.st === "inactive") {
|
|
146
|
+
return { state: "inactive", until, reason: claims.rsn, claims };
|
|
147
|
+
}
|
|
148
|
+
return { state: claims.st, until, reason: null, claims };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Move `highWaterMs` up to trusted now. The caller persists only when
|
|
153
|
+
* `persisted` is set, which is at most once per five minutes.
|
|
154
|
+
*/
|
|
155
|
+
function noteHighWater(target, localNowMs, lastPersistMs) {
|
|
156
|
+
if (
|
|
157
|
+
!target ||
|
|
158
|
+
typeof target !== "object" ||
|
|
159
|
+
!Number.isFinite(target.serverOffsetMs) ||
|
|
160
|
+
!Number.isFinite(target.highWaterMs) ||
|
|
161
|
+
!Number.isFinite(localNowMs)
|
|
162
|
+
) {
|
|
163
|
+
return { record: target, hw: target, lastPersistMs, persisted: false };
|
|
164
|
+
}
|
|
165
|
+
const trusted = localNowMs + target.serverOffsetMs;
|
|
166
|
+
if (!Number.isFinite(trusted) || trusted <= target.highWaterMs) {
|
|
167
|
+
return { record: target, hw: target, lastPersistMs, persisted: false };
|
|
168
|
+
}
|
|
169
|
+
const next = { ...target, highWaterMs: trusted };
|
|
170
|
+
if (lastPersistMs != null && localNowMs - lastPersistMs < HIGH_WATER_PERSIST_MS) {
|
|
171
|
+
return { record: next, hw: next, lastPersistMs, persisted: false };
|
|
172
|
+
}
|
|
173
|
+
return { record: next, hw: next, lastPersistMs: localNowMs, persisted: true };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function publicFields(record) {
|
|
177
|
+
return {
|
|
178
|
+
token: record.token,
|
|
179
|
+
seq: record.seq,
|
|
180
|
+
jti: record.jti,
|
|
181
|
+
serverOffsetMs: record.serverOffsetMs,
|
|
182
|
+
highWaterMs: record.highWaterMs,
|
|
183
|
+
lastRefreshAt: record.lastRefreshAt ?? null,
|
|
184
|
+
nextRefreshAt: record.nextRefreshAt ?? null,
|
|
185
|
+
lastError: record.lastError ?? null,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
module.exports = {
|
|
190
|
+
RECORD_KEY,
|
|
191
|
+
HW_KEY,
|
|
192
|
+
SKEW_MS,
|
|
193
|
+
HIGH_WATER_PERSIST_MS,
|
|
194
|
+
evaluate,
|
|
195
|
+
noteHighWater,
|
|
196
|
+
publicFields,
|
|
197
|
+
recordShape,
|
|
198
|
+
hwShape,
|
|
199
|
+
sealHighWater,
|
|
200
|
+
openHighWater,
|
|
201
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Code-owned build target. Public/production defaults; no runtime override.
|
|
2
|
+
// A private staging build pairs its staging origin with staging PUBLIC keys
|
|
3
|
+
// here before packaging. It must never ship or trust production credentials.
|
|
4
|
+
module.exports = Object.freeze({
|
|
5
|
+
ORIGIN: "https://levix-plus.leviro.net",
|
|
6
|
+
TRUSTED_KEYS: Object.freeze([]),
|
|
7
|
+
});
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// Entitlement token checks. Pure: no clock, no disk, no network.
|
|
2
|
+
//
|
|
3
|
+
// A compact JWS is accepted only when the header is the canonical three-field
|
|
4
|
+
// object, the kid is a trusted Ed25519 key, the signature covers the signing
|
|
5
|
+
// input, and every claim matches the wire contract. Time is judged later.
|
|
6
|
+
|
|
7
|
+
const crypto = require("node:crypto");
|
|
8
|
+
|
|
9
|
+
const { canonicalJson } = require("../../legal/document.cjs");
|
|
10
|
+
const { findTrustedKey, TRUSTED_KEYS } = require("./keys.cjs");
|
|
11
|
+
|
|
12
|
+
const MAX_TOKEN_BYTES = 4096;
|
|
13
|
+
const MAX_LIFETIME_SEC = 259200;
|
|
14
|
+
const MAX_GRACE_SEC = 86400;
|
|
15
|
+
const INACTIVE_LIFETIME_SEC = 86400;
|
|
16
|
+
const HEADER_TYPE = "levix-ent+jwt";
|
|
17
|
+
const ISSUER = "levix-plus-verifier";
|
|
18
|
+
const AUDIENCE = "levix-core";
|
|
19
|
+
const STATES = new Set(["active", "grace", "canceled", "inactive"]);
|
|
20
|
+
const REASONS = new Set([
|
|
21
|
+
"expired",
|
|
22
|
+
"on_hold",
|
|
23
|
+
"paused",
|
|
24
|
+
"pending",
|
|
25
|
+
"revoked",
|
|
26
|
+
"rebound",
|
|
27
|
+
"not_found",
|
|
28
|
+
]);
|
|
29
|
+
const PAYLOAD_KEYS = new Set([
|
|
30
|
+
"aud",
|
|
31
|
+
"ent",
|
|
32
|
+
"exp",
|
|
33
|
+
"iat",
|
|
34
|
+
"iss",
|
|
35
|
+
"jti",
|
|
36
|
+
"pexp",
|
|
37
|
+
"rsn",
|
|
38
|
+
"seq",
|
|
39
|
+
"st",
|
|
40
|
+
"sub",
|
|
41
|
+
]);
|
|
42
|
+
const REQUIRED_PAYLOAD_KEYS = Object.freeze([
|
|
43
|
+
"aud",
|
|
44
|
+
"ent",
|
|
45
|
+
"exp",
|
|
46
|
+
"iat",
|
|
47
|
+
"iss",
|
|
48
|
+
"jti",
|
|
49
|
+
"seq",
|
|
50
|
+
"st",
|
|
51
|
+
"sub",
|
|
52
|
+
]);
|
|
53
|
+
|
|
54
|
+
function fail(reason) {
|
|
55
|
+
return { ok: false, claims: null, reason };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function decodeBase64Url(value) {
|
|
59
|
+
if (typeof value !== "string" || !/^[A-Za-z0-9_-]+$/.test(value)) return null;
|
|
60
|
+
const bytes = Buffer.from(value, "base64url");
|
|
61
|
+
if (bytes.toString("base64url") !== value) return null;
|
|
62
|
+
return bytes;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function utf8Json(bytes) {
|
|
66
|
+
let text;
|
|
67
|
+
try {
|
|
68
|
+
text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
69
|
+
} catch {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
return { text, value: JSON.parse(text) };
|
|
74
|
+
} catch {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function exactKeys(value, keys) {
|
|
80
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
81
|
+
const actual = Object.keys(value);
|
|
82
|
+
return actual.length === keys.length && keys.every((key) => Object.hasOwn(value, key));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function payloadShape(value) {
|
|
86
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
87
|
+
const actual = Object.keys(value);
|
|
88
|
+
return (
|
|
89
|
+
actual.every((key) => PAYLOAD_KEYS.has(key)) &&
|
|
90
|
+
REQUIRED_PAYLOAD_KEYS.every((key) => Object.hasOwn(value, key))
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function integer(value, min = 0) {
|
|
95
|
+
return Number.isSafeInteger(value) && value >= min;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function claimsFrom(value, options) {
|
|
99
|
+
if (!payloadShape(value)) return fail("bad_payload");
|
|
100
|
+
if (value.iss !== ISSUER) return fail("bad_iss");
|
|
101
|
+
if (value.aud !== AUDIENCE) return fail("bad_aud");
|
|
102
|
+
if (
|
|
103
|
+
typeof value.sub !== "string" ||
|
|
104
|
+
value.sub.length === 0 ||
|
|
105
|
+
typeof options.installationId !== "string" ||
|
|
106
|
+
options.installationId.length === 0 ||
|
|
107
|
+
value.sub !== options.installationId
|
|
108
|
+
) {
|
|
109
|
+
return fail("bad_sub");
|
|
110
|
+
}
|
|
111
|
+
if (
|
|
112
|
+
!Array.isArray(value.ent) ||
|
|
113
|
+
value.ent.length > 1 ||
|
|
114
|
+
(value.ent.length === 1 && value.ent[0] !== "plus")
|
|
115
|
+
) {
|
|
116
|
+
return fail("bad_ent");
|
|
117
|
+
}
|
|
118
|
+
if (!STATES.has(value.st)) return fail("bad_st");
|
|
119
|
+
const rsn = Object.hasOwn(value, "rsn") ? value.rsn : null;
|
|
120
|
+
const pexp = Object.hasOwn(value, "pexp") ? value.pexp : null;
|
|
121
|
+
const inactive = value.st === "inactive";
|
|
122
|
+
if (inactive) {
|
|
123
|
+
if (value.ent.length !== 0) return fail("bad_ent");
|
|
124
|
+
if (!REASONS.has(rsn)) return fail("bad_rsn");
|
|
125
|
+
} else if (rsn !== null) {
|
|
126
|
+
return fail("bad_rsn");
|
|
127
|
+
}
|
|
128
|
+
if (!integer(value.iat) || !integer(value.exp)) return fail("bad_exp");
|
|
129
|
+
if (value.exp <= value.iat) return fail("bad_exp");
|
|
130
|
+
if (value.exp - value.iat > MAX_LIFETIME_SEC) return fail("bad_exp");
|
|
131
|
+
if (value.st === "grace" && value.exp - value.iat > MAX_GRACE_SEC) return fail("bad_exp");
|
|
132
|
+
if (inactive && value.exp - value.iat > INACTIVE_LIFETIME_SEC) return fail("bad_exp");
|
|
133
|
+
if (!integer(value.seq)) return fail("bad_seq");
|
|
134
|
+
if (typeof value.jti !== "string" || !/^[A-Za-z0-9_-]{1,64}$/.test(value.jti))
|
|
135
|
+
return fail("bad_jti");
|
|
136
|
+
if (pexp !== null && !integer(pexp)) return fail("bad_pexp");
|
|
137
|
+
if (value.st === "canceled" && pexp !== null && value.exp > pexp) return fail("bad_exp");
|
|
138
|
+
|
|
139
|
+
const storedSeq = options.storedSeq;
|
|
140
|
+
if (storedSeq != null) {
|
|
141
|
+
if (!integer(storedSeq)) return fail("bad_seq");
|
|
142
|
+
if (value.seq < storedSeq || (value.seq === storedSeq && value.jti !== options.storedJti)) {
|
|
143
|
+
return fail("replay");
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
ok: true,
|
|
149
|
+
reason: null,
|
|
150
|
+
claims: Object.freeze({
|
|
151
|
+
iss: value.iss,
|
|
152
|
+
aud: value.aud,
|
|
153
|
+
sub: value.sub,
|
|
154
|
+
ent: Object.freeze([...value.ent]),
|
|
155
|
+
st: value.st,
|
|
156
|
+
rsn,
|
|
157
|
+
iat: value.iat,
|
|
158
|
+
exp: value.exp,
|
|
159
|
+
seq: value.seq,
|
|
160
|
+
jti: value.jti,
|
|
161
|
+
pexp,
|
|
162
|
+
}),
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* @param {string} compact
|
|
168
|
+
* @param {{ keys?: Array<{keyId: string, publicKey: string}>, installationId?: string, storedSeq?: number|null, storedJti?: string|null }} [options]
|
|
169
|
+
* @returns {{ ok: boolean, claims: object|null, reason: string|null }}
|
|
170
|
+
*/
|
|
171
|
+
function verifyToken(compact, options = {}) {
|
|
172
|
+
if (typeof compact !== "string") return fail("malformed");
|
|
173
|
+
if (Buffer.byteLength(compact, "utf8") > MAX_TOKEN_BYTES) return fail("oversized");
|
|
174
|
+
const parts = compact.split(".");
|
|
175
|
+
if (parts.length !== 3 || parts.some((part) => part.length === 0)) return fail("malformed");
|
|
176
|
+
|
|
177
|
+
const headerBytes = decodeBase64Url(parts[0]);
|
|
178
|
+
const headerJson = headerBytes && utf8Json(headerBytes);
|
|
179
|
+
if (
|
|
180
|
+
!headerJson ||
|
|
181
|
+
!headerJson.value ||
|
|
182
|
+
typeof headerJson.value !== "object" ||
|
|
183
|
+
Array.isArray(headerJson.value)
|
|
184
|
+
) {
|
|
185
|
+
return fail("bad_header");
|
|
186
|
+
}
|
|
187
|
+
const header = headerJson.value;
|
|
188
|
+
if (!exactKeys(header, ["alg", "kid", "typ"])) return fail("bad_header");
|
|
189
|
+
if (header.alg !== "EdDSA" || header.typ !== HEADER_TYPE || !/^[0-9a-f]{16}$/.test(header.kid)) {
|
|
190
|
+
return fail("bad_header");
|
|
191
|
+
}
|
|
192
|
+
if (
|
|
193
|
+
headerJson.text !== JSON.stringify(header) &&
|
|
194
|
+
headerJson.text !== canonicalJson({ alg: "EdDSA", kid: header.kid, typ: HEADER_TYPE })
|
|
195
|
+
) {
|
|
196
|
+
return fail("bad_header");
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const key = findTrustedKey(options.keys ?? TRUSTED_KEYS, header.kid);
|
|
200
|
+
if (!key) return fail("unknown_kid");
|
|
201
|
+
const signature = decodeBase64Url(parts[2]);
|
|
202
|
+
if (!signature || signature.length !== 64) return fail("bad_signature");
|
|
203
|
+
let signed = false;
|
|
204
|
+
try {
|
|
205
|
+
signed = crypto.verify(
|
|
206
|
+
null,
|
|
207
|
+
Buffer.from(`${parts[0]}.${parts[1]}`, "utf8"),
|
|
208
|
+
key.keyObject,
|
|
209
|
+
signature,
|
|
210
|
+
);
|
|
211
|
+
} catch {
|
|
212
|
+
signed = false;
|
|
213
|
+
}
|
|
214
|
+
if (!signed) return fail("bad_signature");
|
|
215
|
+
|
|
216
|
+
const payloadBytes = decodeBase64Url(parts[1]);
|
|
217
|
+
const payloadJson = payloadBytes && utf8Json(payloadBytes);
|
|
218
|
+
if (!payloadJson) return fail("bad_payload");
|
|
219
|
+
return claimsFrom(payloadJson.value, options);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
module.exports = {
|
|
223
|
+
MAX_TOKEN_BYTES,
|
|
224
|
+
verifyToken,
|
|
225
|
+
};
|