@kyro-cms/core 0.3.2 → 0.3.4
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/dist/{WebhookService-BznDc2AT.d.ts → WebhookService-BCpW2dyL.d.ts} +1 -1
- package/dist/{WebhookService-mZZ75syh.d.cts → WebhookService-DxYSFvNg.d.cts} +1 -1
- package/dist/api-handler.cjs +52 -0
- package/dist/api-handler.cjs.map +1 -0
- package/dist/api-handler.d.cts +9 -0
- package/dist/api-handler.d.ts +9 -0
- package/dist/api-handler.js +46 -0
- package/dist/api-handler.js.map +1 -0
- package/dist/{base-Hu6ij8sZ.d.ts → base-DvvNqnM-.d.cts} +16 -5
- package/dist/{base-Db9LkB1N.d.cts → base-eVegJ_Pr.d.ts} +16 -5
- package/dist/bootstrap-DGJ3N7SO.js +6 -0
- package/dist/{bootstrap-LL6O7PWO.js.map → bootstrap-DGJ3N7SO.js.map} +1 -1
- package/dist/bootstrap-O5UGUTYU.cjs +31 -0
- package/dist/{bootstrap-BMWVB2T6.cjs.map → bootstrap-O5UGUTYU.cjs.map} +1 -1
- package/dist/{chunk-QKOFKITP.js → chunk-2HFJUUFZ.js} +3 -11
- package/dist/chunk-2HFJUUFZ.js.map +1 -0
- package/dist/chunk-2SJATAN4.js +5514 -0
- package/dist/chunk-2SJATAN4.js.map +1 -0
- package/dist/{chunk-DIC236EW.js → chunk-342BJNBI.js} +167 -24
- package/dist/chunk-342BJNBI.js.map +1 -0
- package/dist/{chunk-OUGKLCYF.js → chunk-3AJE4SEG.js} +4 -3
- package/dist/chunk-3AJE4SEG.js.map +1 -0
- package/dist/chunk-6LPNEC6D.js +617 -0
- package/dist/chunk-6LPNEC6D.js.map +1 -0
- package/dist/{chunk-BXMWDUED.js → chunk-A4USRVTQ.js} +2 -2
- package/dist/chunk-A4USRVTQ.js.map +1 -0
- package/dist/chunk-ADLJSJSN.cjs +13 -0
- package/dist/chunk-ADLJSJSN.cjs.map +1 -0
- package/dist/chunk-ATBOUGQP.cjs +513 -0
- package/dist/chunk-ATBOUGQP.cjs.map +1 -0
- package/dist/{chunk-KB6QF4HO.js → chunk-B76I67F3.js} +246 -141
- package/dist/chunk-B76I67F3.js.map +1 -0
- package/dist/chunk-BQ2T4WRS.js +140 -0
- package/dist/chunk-BQ2T4WRS.js.map +1 -0
- package/dist/chunk-CZ3HWX2X.cjs +622 -0
- package/dist/chunk-CZ3HWX2X.cjs.map +1 -0
- package/dist/{chunk-PNBZZ76A.cjs → chunk-DAIBBBOL.cjs} +246 -140
- package/dist/chunk-DAIBBBOL.cjs.map +1 -0
- package/dist/{chunk-U74F3YZU.js → chunk-DBUYB32X.js} +15 -3
- package/dist/chunk-DBUYB32X.js.map +1 -0
- package/dist/chunk-DLHUQO25.cjs +1746 -0
- package/dist/chunk-DLHUQO25.cjs.map +1 -0
- package/dist/{chunk-GE5DMB44.js → chunk-E3BZLMX6.js} +55 -49
- package/dist/chunk-E3BZLMX6.js.map +1 -0
- package/dist/{chunk-44BF6ALS.cjs → chunk-H4XCAPA6.cjs} +55 -49
- package/dist/chunk-H4XCAPA6.cjs.map +1 -0
- package/dist/{chunk-VIONYQ2K.cjs → chunk-IBG6V56E.cjs} +16 -32
- package/dist/chunk-IBG6V56E.cjs.map +1 -0
- package/dist/{chunk-LIJVWQKU.cjs → chunk-IX3ABYKZ.cjs} +43 -31
- package/dist/chunk-IX3ABYKZ.cjs.map +1 -0
- package/dist/chunk-JYGIFBBS.cjs +146 -0
- package/dist/chunk-JYGIFBBS.cjs.map +1 -0
- package/dist/{chunk-42JPONZU.cjs → chunk-K7JPTH3G.cjs} +17 -16
- package/dist/chunk-K7JPTH3G.cjs.map +1 -0
- package/dist/{chunk-RLTG4YZM.cjs → chunk-KOCTZKPV.cjs} +2 -2
- package/dist/chunk-KOCTZKPV.cjs.map +1 -0
- package/dist/chunk-MMYAIYHJ.cjs +5538 -0
- package/dist/chunk-MMYAIYHJ.cjs.map +1 -0
- package/dist/{chunk-EWP5AT6A.cjs → chunk-N4H37VN4.cjs} +2 -11
- package/dist/chunk-N4H37VN4.cjs.map +1 -0
- package/dist/chunk-P2YW545G.js +11 -0
- package/dist/chunk-P2YW545G.js.map +1 -0
- package/dist/chunk-Q23JB3KL.js +488 -0
- package/dist/chunk-Q23JB3KL.js.map +1 -0
- package/dist/{chunk-E5X75WNB.js → chunk-QXIQWPAP.js} +14 -30
- package/dist/chunk-QXIQWPAP.js.map +1 -0
- package/dist/chunk-R3XIBBAW.cjs +34 -0
- package/dist/chunk-R3XIBBAW.cjs.map +1 -0
- package/dist/{chunk-KWGNR4HM.js → chunk-REK7AYOC.js} +82 -9
- package/dist/chunk-REK7AYOC.js.map +1 -0
- package/dist/chunk-RGIQKTZ7.js +68 -0
- package/dist/chunk-RGIQKTZ7.js.map +1 -0
- package/dist/chunk-RYDGMBIG.js +1737 -0
- package/dist/chunk-RYDGMBIG.js.map +1 -0
- package/dist/chunk-SDMNUYVU.js +30 -0
- package/dist/chunk-SDMNUYVU.js.map +1 -0
- package/dist/chunk-VEI5KQVC.cjs +1246 -0
- package/dist/chunk-VEI5KQVC.cjs.map +1 -0
- package/dist/{chunk-FTSSDDZQ.cjs → chunk-VJT6P4N6.cjs} +82 -9
- package/dist/chunk-VJT6P4N6.cjs.map +1 -0
- package/dist/{chunk-HT6VE4NW.cjs → chunk-W3KPQX7V.cjs} +168 -25
- package/dist/chunk-W3KPQX7V.cjs.map +1 -0
- package/dist/{chunk-LTRCYJAG.js → chunk-WOWUL7ZY.js} +3 -2
- package/dist/chunk-WOWUL7ZY.js.map +1 -0
- package/dist/{chunk-7YITG2US.cjs → chunk-WQBRWOQT.cjs} +3 -2
- package/dist/chunk-WQBRWOQT.cjs.map +1 -0
- package/dist/chunk-X3CU27OO.cjs +78 -0
- package/dist/chunk-X3CU27OO.cjs.map +1 -0
- package/dist/chunk-XIXGJGQW.js +1228 -0
- package/dist/chunk-XIXGJGQW.js.map +1 -0
- package/dist/cli/index.cjs +2 -2
- package/dist/cli/index.js +2 -2
- package/dist/client.cjs +23 -13
- package/dist/client.d.cts +4 -2
- package/dist/client.d.ts +4 -2
- package/dist/client.js +3 -1
- package/dist/drizzle/index.cjs +20 -19
- package/dist/drizzle/index.d.cts +28 -7
- package/dist/drizzle/index.d.ts +28 -7
- package/dist/drizzle/index.js +5 -4
- package/dist/fields/index.cjs +105 -0
- package/dist/fields/index.cjs.map +1 -0
- package/dist/fields/index.d.cts +27 -0
- package/dist/fields/index.d.ts +27 -0
- package/dist/fields/index.js +4 -0
- package/dist/fields/index.js.map +1 -0
- package/dist/graphql/index.cjs +4 -3
- package/dist/graphql/index.d.cts +3 -2
- package/dist/graphql/index.d.ts +3 -2
- package/dist/graphql/index.js +2 -1
- package/dist/{index-Ci6r4xnN.d.ts → index-CLp-DRKA.d.ts} +2 -1
- package/dist/{index-11MDNKce.d.cts → index-DfO7G4kN.d.cts} +2 -1
- package/dist/index.cjs +2621 -6672
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +136 -47
- package/dist/index.d.ts +136 -47
- package/dist/index.js +2333 -6546
- package/dist/index.js.map +1 -1
- package/dist/integration.cjs +68 -0
- package/dist/integration.cjs.map +1 -0
- package/dist/integration.d.cts +27 -0
- package/dist/integration.d.ts +27 -0
- package/dist/integration.js +61 -0
- package/dist/integration.js.map +1 -0
- package/dist/mongodb/index.cjs +4 -4
- package/dist/mongodb/index.d.cts +20 -6
- package/dist/mongodb/index.d.ts +20 -6
- package/dist/mongodb/index.js +2 -2
- package/dist/postgres-auth-adapter-7F3ECO7I.js +5 -0
- package/dist/{postgres-auth-adapter-OTRWSTT5.js.map → postgres-auth-adapter-7F3ECO7I.js.map} +1 -1
- package/dist/postgres-auth-adapter-Z463NYJZ.cjs +14 -0
- package/dist/{postgres-auth-adapter-EVRPO7BQ.cjs.map → postgres-auth-adapter-Z463NYJZ.cjs.map} +1 -1
- package/dist/redis-adapter-LPUWLE4Y.cjs +13 -0
- package/dist/{redis-adapter-E7PMN5HW.cjs.map → redis-adapter-LPUWLE4Y.cjs.map} +1 -1
- package/dist/redis-adapter-THYDCGQR.js +4 -0
- package/dist/{redis-adapter-HOO67RBQ.js.map → redis-adapter-THYDCGQR.js.map} +1 -1
- package/dist/rest/index.cjs +8 -5
- package/dist/rest/index.d.cts +6 -3
- package/dist/rest/index.d.ts +6 -3
- package/dist/rest/index.js +6 -3
- package/dist/{schema-CNB2DDTX.js → schema-6Q4W6AE6.js} +3 -3
- package/dist/{schema-CNB2DDTX.js.map → schema-6Q4W6AE6.js.map} +1 -1
- package/dist/{schema-Y777CQQS.cjs → schema-TIYTCIKX.cjs} +14 -14
- package/dist/{schema-Y777CQQS.cjs.map → schema-TIYTCIKX.cjs.map} +1 -1
- package/dist/templates/index.cjs +27 -23
- package/dist/templates/index.d.cts +8 -2
- package/dist/templates/index.d.ts +8 -2
- package/dist/templates/index.js +1 -1
- package/dist/trpc/index.cjs +12 -11
- package/dist/trpc/index.d.cts +3 -2
- package/dist/trpc/index.d.ts +3 -2
- package/dist/trpc/index.js +3 -2
- package/dist/{types-kGfsGdos.d.cts → types-Bs1up4yP.d.ts} +76 -244
- package/dist/{types-1u353OHN.d.ts → types-Da83JLDk.d.cts} +6 -2
- package/dist/{types-1u353OHN.d.cts → types-Da83JLDk.d.ts} +6 -2
- package/dist/{types-kGfsGdos.d.ts → types-J3R9nVsZ.d.cts} +76 -244
- package/dist/types-VtjUxIMp.d.cts +246 -0
- package/dist/types-VtjUxIMp.d.ts +246 -0
- package/package.json +16 -9
- package/dist/bootstrap-BMWVB2T6.cjs +0 -31
- package/dist/bootstrap-LL6O7PWO.js +0 -6
- package/dist/chunk-42JPONZU.cjs.map +0 -1
- package/dist/chunk-44BF6ALS.cjs.map +0 -1
- package/dist/chunk-4M5PHMUE.cjs +0 -947
- package/dist/chunk-4M5PHMUE.cjs.map +0 -1
- package/dist/chunk-6MSSF46R.js +0 -941
- package/dist/chunk-6MSSF46R.js.map +0 -1
- package/dist/chunk-7YITG2US.cjs.map +0 -1
- package/dist/chunk-BTOE3VUK.js +0 -330
- package/dist/chunk-BTOE3VUK.js.map +0 -1
- package/dist/chunk-BXMWDUED.js.map +0 -1
- package/dist/chunk-DIC236EW.js.map +0 -1
- package/dist/chunk-E5X75WNB.js.map +0 -1
- package/dist/chunk-E63IF3MD.cjs +0 -951
- package/dist/chunk-E63IF3MD.cjs.map +0 -1
- package/dist/chunk-EWP5AT6A.cjs.map +0 -1
- package/dist/chunk-FTSSDDZQ.cjs.map +0 -1
- package/dist/chunk-GE5DMB44.js.map +0 -1
- package/dist/chunk-GVFB5C6O.cjs +0 -345
- package/dist/chunk-GVFB5C6O.cjs.map +0 -1
- package/dist/chunk-HT6VE4NW.cjs.map +0 -1
- package/dist/chunk-HVSQDZZJ.cjs +0 -765
- package/dist/chunk-HVSQDZZJ.cjs.map +0 -1
- package/dist/chunk-HYC4GNHX.js +0 -758
- package/dist/chunk-HYC4GNHX.js.map +0 -1
- package/dist/chunk-KB6QF4HO.js.map +0 -1
- package/dist/chunk-KWGNR4HM.js.map +0 -1
- package/dist/chunk-LIJVWQKU.cjs.map +0 -1
- package/dist/chunk-LTRCYJAG.js.map +0 -1
- package/dist/chunk-OUGKLCYF.js.map +0 -1
- package/dist/chunk-PNBZZ76A.cjs.map +0 -1
- package/dist/chunk-QKOFKITP.js.map +0 -1
- package/dist/chunk-RLTG4YZM.cjs.map +0 -1
- package/dist/chunk-RRYXQMZG.js +0 -935
- package/dist/chunk-RRYXQMZG.js.map +0 -1
- package/dist/chunk-U74F3YZU.js.map +0 -1
- package/dist/chunk-VIONYQ2K.cjs.map +0 -1
- package/dist/postgres-auth-adapter-EVRPO7BQ.cjs +0 -14
- package/dist/postgres-auth-adapter-OTRWSTT5.js +0 -5
- package/dist/redis-adapter-E7PMN5HW.cjs +0 -13
- package/dist/redis-adapter-HOO67RBQ.js +0 -4
package/dist/chunk-RRYXQMZG.js
DELETED
|
@@ -1,935 +0,0 @@
|
|
|
1
|
-
import { EmailTransport } from './chunk-HYC4GNHX.js';
|
|
2
|
-
import bcrypt from 'bcryptjs';
|
|
3
|
-
import { randomBytes } from 'crypto';
|
|
4
|
-
import { mkdirSync } from 'fs';
|
|
5
|
-
import { dirname } from 'path';
|
|
6
|
-
|
|
7
|
-
var DEFAULT_BUSY_TIMEOUT = 5e3;
|
|
8
|
-
var DEFAULT_WAL_CHECKPOINT = 1e3;
|
|
9
|
-
var DEFAULT_CACHE_SIZE = -64e3;
|
|
10
|
-
var DEFAULT_MMAP_SIZE = 268435456;
|
|
11
|
-
var SQLiteAuthAdapter = class {
|
|
12
|
-
db = null;
|
|
13
|
-
path;
|
|
14
|
-
saltRounds;
|
|
15
|
-
externalDb;
|
|
16
|
-
busyTimeout;
|
|
17
|
-
walAutoCheckpoint;
|
|
18
|
-
cacheSize;
|
|
19
|
-
mmapSize;
|
|
20
|
-
preparedStatements = /* @__PURE__ */ new Map();
|
|
21
|
-
constructor(options = {}) {
|
|
22
|
-
this.path = options.path || "./data/auth.db";
|
|
23
|
-
this.saltRounds = options.saltRounds || 12;
|
|
24
|
-
this.externalDb = !!options.db;
|
|
25
|
-
this.busyTimeout = options.busyTimeout ?? DEFAULT_BUSY_TIMEOUT;
|
|
26
|
-
this.walAutoCheckpoint = options.walAutoCheckpoint ?? DEFAULT_WAL_CHECKPOINT;
|
|
27
|
-
this.cacheSize = options.cacheSize ?? DEFAULT_CACHE_SIZE;
|
|
28
|
-
this.mmapSize = options.mmapSize ?? DEFAULT_MMAP_SIZE;
|
|
29
|
-
if (options.db) {
|
|
30
|
-
this.db = options.db;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
async connect() {
|
|
34
|
-
if (this.db) return;
|
|
35
|
-
const dir = dirname(this.path);
|
|
36
|
-
if (dir && dir !== ".") {
|
|
37
|
-
mkdirSync(dir, { recursive: true });
|
|
38
|
-
}
|
|
39
|
-
const Database = (await import('better-sqlite3')).default;
|
|
40
|
-
this.db = new Database(this.path, {
|
|
41
|
-
timeout: this.busyTimeout
|
|
42
|
-
});
|
|
43
|
-
this.db.pragma("journal_mode = WAL");
|
|
44
|
-
this.db.pragma("synchronous = NORMAL");
|
|
45
|
-
this.db.pragma("cache_size = " + this.cacheSize);
|
|
46
|
-
this.db.pragma("mmap_size = " + this.mmapSize);
|
|
47
|
-
this.db.pragma("wal_autocheckpoint = " + this.walAutoCheckpoint);
|
|
48
|
-
this.db.pragma("foreign_keys = ON");
|
|
49
|
-
this.db.pragma("temp_store = MEMORY");
|
|
50
|
-
this.ensureTables();
|
|
51
|
-
this.prepareStatements();
|
|
52
|
-
}
|
|
53
|
-
async disconnect() {
|
|
54
|
-
if (this.db && !this.externalDb) {
|
|
55
|
-
this.db.pragma("wal_checkpoint(TRUNCATE)");
|
|
56
|
-
this.db.close();
|
|
57
|
-
this.db = null;
|
|
58
|
-
this.preparedStatements.clear();
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
ensureTables() {
|
|
62
|
-
if (!this.db) return;
|
|
63
|
-
this.db.exec(`
|
|
64
|
-
CREATE TABLE IF NOT EXISTS kyro_users (
|
|
65
|
-
id TEXT PRIMARY KEY,
|
|
66
|
-
email TEXT UNIQUE NOT NULL,
|
|
67
|
-
password_hash TEXT NOT NULL,
|
|
68
|
-
role TEXT NOT NULL DEFAULT 'customer',
|
|
69
|
-
tenant_id TEXT,
|
|
70
|
-
email_verified INTEGER DEFAULT 0,
|
|
71
|
-
locked INTEGER DEFAULT 0,
|
|
72
|
-
last_login TEXT,
|
|
73
|
-
failed_login_attempts INTEGER DEFAULT 0,
|
|
74
|
-
locked_until TEXT,
|
|
75
|
-
created_at TEXT NOT NULL,
|
|
76
|
-
updated_at TEXT NOT NULL
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
CREATE TABLE IF NOT EXISTS kyro_sessions (
|
|
80
|
-
id TEXT PRIMARY KEY,
|
|
81
|
-
user_id TEXT NOT NULL,
|
|
82
|
-
token TEXT NOT NULL,
|
|
83
|
-
refresh_token TEXT,
|
|
84
|
-
expires_at TEXT NOT NULL,
|
|
85
|
-
created_at TEXT NOT NULL,
|
|
86
|
-
ip_address TEXT,
|
|
87
|
-
user_agent TEXT,
|
|
88
|
-
FOREIGN KEY (user_id) REFERENCES kyro_users(id) ON DELETE CASCADE
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
CREATE TABLE IF NOT EXISTS kyro_password_history (
|
|
92
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
93
|
-
user_id TEXT NOT NULL,
|
|
94
|
-
password_hash TEXT NOT NULL,
|
|
95
|
-
created_at TEXT NOT NULL,
|
|
96
|
-
FOREIGN KEY (user_id) REFERENCES kyro_users(id) ON DELETE CASCADE
|
|
97
|
-
);
|
|
98
|
-
|
|
99
|
-
CREATE TABLE IF NOT EXISTS kyro_rate_limits (
|
|
100
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
101
|
-
key TEXT NOT NULL,
|
|
102
|
-
window_start INTEGER NOT NULL,
|
|
103
|
-
count INTEGER NOT NULL DEFAULT 1,
|
|
104
|
-
UNIQUE(key, window_start)
|
|
105
|
-
);
|
|
106
|
-
|
|
107
|
-
CREATE TABLE IF NOT EXISTS kyro_lockouts (
|
|
108
|
-
user_id TEXT PRIMARY KEY,
|
|
109
|
-
attempts INTEGER NOT NULL DEFAULT 0,
|
|
110
|
-
last_attempt INTEGER,
|
|
111
|
-
locked_at INTEGER,
|
|
112
|
-
locked_until INTEGER
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
CREATE TABLE IF NOT EXISTS kyro_audit_logs (
|
|
116
|
-
id TEXT PRIMARY KEY,
|
|
117
|
-
timestamp TEXT NOT NULL,
|
|
118
|
-
action TEXT NOT NULL,
|
|
119
|
-
user_id TEXT,
|
|
120
|
-
user_email TEXT,
|
|
121
|
-
role TEXT,
|
|
122
|
-
resource TEXT NOT NULL,
|
|
123
|
-
resource_id TEXT,
|
|
124
|
-
ip_address TEXT,
|
|
125
|
-
user_agent TEXT,
|
|
126
|
-
success INTEGER NOT NULL,
|
|
127
|
-
error TEXT,
|
|
128
|
-
metadata TEXT,
|
|
129
|
-
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
130
|
-
);
|
|
131
|
-
|
|
132
|
-
CREATE INDEX IF NOT EXISTS idx_kyro_users_email ON kyro_users(email);
|
|
133
|
-
CREATE INDEX IF NOT EXISTS idx_kyro_sessions_user_id ON kyro_sessions(user_id);
|
|
134
|
-
CREATE INDEX IF NOT EXISTS idx_kyro_sessions_token ON kyro_sessions(token);
|
|
135
|
-
CREATE INDEX IF NOT EXISTS idx_kyro_sessions_refresh_token ON kyro_sessions(refresh_token);
|
|
136
|
-
CREATE INDEX IF NOT EXISTS idx_kyro_sessions_expires ON kyro_sessions(expires_at);
|
|
137
|
-
CREATE INDEX IF NOT EXISTS idx_kyro_password_history_user_id ON kyro_password_history(user_id);
|
|
138
|
-
CREATE INDEX IF NOT EXISTS idx_kyro_rate_limits_key ON kyro_rate_limits(key);
|
|
139
|
-
CREATE INDEX IF NOT EXISTS idx_kyro_rate_limits_window ON kyro_rate_limits(window_start);
|
|
140
|
-
CREATE INDEX IF NOT EXISTS idx_kyro_lockouts_locked_until ON kyro_lockouts(locked_until);
|
|
141
|
-
CREATE INDEX IF NOT EXISTS idx_kyro_audit_logs_timestamp ON kyro_audit_logs(timestamp);
|
|
142
|
-
CREATE INDEX IF NOT EXISTS idx_kyro_audit_logs_action ON kyro_audit_logs(action);
|
|
143
|
-
CREATE INDEX IF NOT EXISTS idx_kyro_audit_logs_user_id ON kyro_audit_logs(user_id);
|
|
144
|
-
CREATE INDEX IF NOT EXISTS idx_kyro_audit_logs_resource ON kyro_audit_logs(resource);
|
|
145
|
-
`);
|
|
146
|
-
}
|
|
147
|
-
prepareStatements() {
|
|
148
|
-
if (!this.db) return;
|
|
149
|
-
this.preparedStatements.set(
|
|
150
|
-
"findUserByEmail",
|
|
151
|
-
this.db.prepare("SELECT * FROM kyro_users WHERE email = ?")
|
|
152
|
-
);
|
|
153
|
-
this.preparedStatements.set(
|
|
154
|
-
"findUserById",
|
|
155
|
-
this.db.prepare("SELECT * FROM kyro_users WHERE id = ?")
|
|
156
|
-
);
|
|
157
|
-
this.preparedStatements.set(
|
|
158
|
-
"findSessionByToken",
|
|
159
|
-
this.db.prepare("SELECT * FROM kyro_sessions WHERE token = ?")
|
|
160
|
-
);
|
|
161
|
-
this.preparedStatements.set(
|
|
162
|
-
"findSessionByRefreshToken",
|
|
163
|
-
this.db.prepare("SELECT * FROM kyro_sessions WHERE refresh_token = ?")
|
|
164
|
-
);
|
|
165
|
-
this.preparedStatements.set(
|
|
166
|
-
"deleteSession",
|
|
167
|
-
this.db.prepare("DELETE FROM kyro_sessions WHERE id = ? OR token = ?")
|
|
168
|
-
);
|
|
169
|
-
this.preparedStatements.set(
|
|
170
|
-
"deleteUserSessions",
|
|
171
|
-
this.db.prepare("DELETE FROM kyro_sessions WHERE user_id = ?")
|
|
172
|
-
);
|
|
173
|
-
this.preparedStatements.set(
|
|
174
|
-
"countUsers",
|
|
175
|
-
this.db.prepare("SELECT COUNT(*) as count FROM kyro_users")
|
|
176
|
-
);
|
|
177
|
-
this.preparedStatements.set(
|
|
178
|
-
"deleteUser",
|
|
179
|
-
this.db.prepare("DELETE FROM kyro_users WHERE id = ?")
|
|
180
|
-
);
|
|
181
|
-
this.preparedStatements.set(
|
|
182
|
-
"getPasswordHistory",
|
|
183
|
-
this.db.prepare(
|
|
184
|
-
"SELECT password_hash FROM kyro_password_history WHERE user_id = ? ORDER BY created_at DESC LIMIT ?"
|
|
185
|
-
)
|
|
186
|
-
);
|
|
187
|
-
this.preparedStatements.set(
|
|
188
|
-
"addPasswordHistory",
|
|
189
|
-
this.db.prepare(
|
|
190
|
-
"INSERT INTO kyro_password_history (user_id, password_hash, created_at) VALUES (?, ?, ?)"
|
|
191
|
-
)
|
|
192
|
-
);
|
|
193
|
-
this.preparedStatements.set(
|
|
194
|
-
"trimPasswordHistory",
|
|
195
|
-
this.db.prepare(
|
|
196
|
-
`DELETE FROM kyro_password_history WHERE id IN (
|
|
197
|
-
SELECT id FROM kyro_password_history WHERE user_id = ? ORDER BY created_at DESC LIMIT -1 OFFSET 5
|
|
198
|
-
)`
|
|
199
|
-
)
|
|
200
|
-
);
|
|
201
|
-
this.preparedStatements.set(
|
|
202
|
-
"deleteExpiredSessions",
|
|
203
|
-
this.db.prepare("DELETE FROM kyro_sessions WHERE expires_at < ?")
|
|
204
|
-
);
|
|
205
|
-
this.preparedStatements.set(
|
|
206
|
-
"cleanupOldAuditLogs",
|
|
207
|
-
this.db.prepare("DELETE FROM kyro_audit_logs WHERE timestamp < ?")
|
|
208
|
-
);
|
|
209
|
-
this.preparedStatements.set(
|
|
210
|
-
"cleanupExpiredLockouts",
|
|
211
|
-
this.db.prepare(
|
|
212
|
-
"UPDATE kyro_lockouts SET attempts = 0, locked_at = NULL, locked_until = NULL WHERE locked_until < ?"
|
|
213
|
-
)
|
|
214
|
-
);
|
|
215
|
-
this.preparedStatements.set(
|
|
216
|
-
"getLockout",
|
|
217
|
-
this.db.prepare("SELECT * FROM kyro_lockouts WHERE user_id = ?")
|
|
218
|
-
);
|
|
219
|
-
this.preparedStatements.set(
|
|
220
|
-
"upsertLockout",
|
|
221
|
-
this.db.prepare(`
|
|
222
|
-
INSERT INTO kyro_lockouts (user_id, attempts, last_attempt, locked_at, locked_until)
|
|
223
|
-
VALUES (?, ?, ?, ?, ?)
|
|
224
|
-
ON CONFLICT(user_id) DO UPDATE SET
|
|
225
|
-
attempts = excluded.attempts,
|
|
226
|
-
last_attempt = excluded.last_attempt,
|
|
227
|
-
locked_at = excluded.locked_at,
|
|
228
|
-
locked_until = excluded.locked_until
|
|
229
|
-
`)
|
|
230
|
-
);
|
|
231
|
-
this.preparedStatements.set(
|
|
232
|
-
"resetLockout",
|
|
233
|
-
this.db.prepare(
|
|
234
|
-
"UPDATE kyro_lockouts SET attempts = 0, locked_at = NULL, locked_until = NULL WHERE user_id = ?"
|
|
235
|
-
)
|
|
236
|
-
);
|
|
237
|
-
}
|
|
238
|
-
stmt(name) {
|
|
239
|
-
const stmt = this.preparedStatements.get(name);
|
|
240
|
-
if (!stmt) throw new Error(`Prepared statement not found: ${name}`);
|
|
241
|
-
return stmt;
|
|
242
|
-
}
|
|
243
|
-
async cleanupExpiredSessions() {
|
|
244
|
-
if (!this.db) throw new Error("Not connected");
|
|
245
|
-
const result = this.stmt("deleteExpiredSessions").run(
|
|
246
|
-
(/* @__PURE__ */ new Date()).toISOString()
|
|
247
|
-
);
|
|
248
|
-
return result.changes;
|
|
249
|
-
}
|
|
250
|
-
async cleanupOldAuditLogs(retentionDays = 30) {
|
|
251
|
-
if (!this.db) throw new Error("Not connected");
|
|
252
|
-
const cutoff = new Date(
|
|
253
|
-
Date.now() - retentionDays * 24 * 60 * 60 * 1e3
|
|
254
|
-
).toISOString();
|
|
255
|
-
const result = this.stmt("cleanupOldAuditLogs").run(cutoff);
|
|
256
|
-
return result.changes;
|
|
257
|
-
}
|
|
258
|
-
async getStats() {
|
|
259
|
-
if (!this.db) throw new Error("Not connected");
|
|
260
|
-
const userCount = this.stmt("countUsers").get().count;
|
|
261
|
-
const activeSessionCount = this.db.prepare(
|
|
262
|
-
"SELECT COUNT(*) as count FROM kyro_sessions WHERE expires_at > ?"
|
|
263
|
-
).get((/* @__PURE__ */ new Date()).toISOString()).count;
|
|
264
|
-
const auditLogCount = this.db.prepare("SELECT COUNT(*) as count FROM kyro_audit_logs").get().count;
|
|
265
|
-
return { userCount, activeSessionCount, auditLogCount };
|
|
266
|
-
}
|
|
267
|
-
async createUser(data) {
|
|
268
|
-
if (!this.db) throw new Error("Not connected");
|
|
269
|
-
const id = randomBytes(16).toString("hex");
|
|
270
|
-
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
271
|
-
const passwordHash = await this.hashPassword(data.password);
|
|
272
|
-
const user = {
|
|
273
|
-
id,
|
|
274
|
-
email: data.email.toLowerCase(),
|
|
275
|
-
passwordHash,
|
|
276
|
-
role: data.role || "customer",
|
|
277
|
-
tenantId: data.tenantId,
|
|
278
|
-
createdAt: now,
|
|
279
|
-
updatedAt: now
|
|
280
|
-
};
|
|
281
|
-
this.db.prepare(
|
|
282
|
-
`INSERT INTO kyro_users (id, email, password_hash, role, tenant_id, created_at, updated_at)
|
|
283
|
-
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
|
284
|
-
).run(
|
|
285
|
-
id,
|
|
286
|
-
user.email,
|
|
287
|
-
user.passwordHash,
|
|
288
|
-
user.role,
|
|
289
|
-
user.tenantId,
|
|
290
|
-
now,
|
|
291
|
-
now
|
|
292
|
-
);
|
|
293
|
-
return user;
|
|
294
|
-
}
|
|
295
|
-
async findUserByEmail(email) {
|
|
296
|
-
if (!this.db) throw new Error("Not connected");
|
|
297
|
-
const row = this.stmt("findUserByEmail").get(email.toLowerCase());
|
|
298
|
-
if (!row) return null;
|
|
299
|
-
return this.rowToUser(row);
|
|
300
|
-
}
|
|
301
|
-
async findUserById(userId) {
|
|
302
|
-
if (!this.db) throw new Error("Not connected");
|
|
303
|
-
const row = this.stmt("findUserById").get(userId);
|
|
304
|
-
if (!row) return null;
|
|
305
|
-
return this.rowToUser(row);
|
|
306
|
-
}
|
|
307
|
-
async updateUser(userId, data) {
|
|
308
|
-
if (!this.db) throw new Error("Not connected");
|
|
309
|
-
const existing = await this.findUserById(userId);
|
|
310
|
-
if (!existing) return null;
|
|
311
|
-
const updates = [];
|
|
312
|
-
const values = [];
|
|
313
|
-
if (data.email !== void 0) {
|
|
314
|
-
updates.push("email = ?");
|
|
315
|
-
values.push(data.email.toLowerCase());
|
|
316
|
-
}
|
|
317
|
-
if (data.passwordHash !== void 0) {
|
|
318
|
-
updates.push("password_hash = ?");
|
|
319
|
-
values.push(data.passwordHash);
|
|
320
|
-
}
|
|
321
|
-
if (data.role !== void 0) {
|
|
322
|
-
updates.push("role = ?");
|
|
323
|
-
values.push(data.role);
|
|
324
|
-
}
|
|
325
|
-
if (data.tenantId !== void 0) {
|
|
326
|
-
updates.push("tenant_id = ?");
|
|
327
|
-
values.push(data.tenantId);
|
|
328
|
-
}
|
|
329
|
-
if (data.emailVerified !== void 0) {
|
|
330
|
-
updates.push("email_verified = ?");
|
|
331
|
-
values.push(data.emailVerified ? 1 : 0);
|
|
332
|
-
}
|
|
333
|
-
if (data.locked !== void 0) {
|
|
334
|
-
updates.push("locked = ?");
|
|
335
|
-
values.push(data.locked ? 1 : 0);
|
|
336
|
-
}
|
|
337
|
-
if (data.lastLogin !== void 0) {
|
|
338
|
-
updates.push("last_login = ?");
|
|
339
|
-
values.push(data.lastLogin);
|
|
340
|
-
}
|
|
341
|
-
if (data.failedLoginAttempts !== void 0) {
|
|
342
|
-
updates.push("failed_login_attempts = ?");
|
|
343
|
-
values.push(data.failedLoginAttempts);
|
|
344
|
-
}
|
|
345
|
-
updates.push("updated_at = ?");
|
|
346
|
-
values.push((/* @__PURE__ */ new Date()).toISOString());
|
|
347
|
-
values.push(userId);
|
|
348
|
-
this.db.prepare(`UPDATE kyro_users SET ${updates.join(", ")} WHERE id = ?`).run(...values);
|
|
349
|
-
return this.findUserById(userId);
|
|
350
|
-
}
|
|
351
|
-
async deleteUser(userId) {
|
|
352
|
-
if (!this.db) throw new Error("Not connected");
|
|
353
|
-
const result = this.stmt("deleteUser").run(userId);
|
|
354
|
-
return result.changes > 0;
|
|
355
|
-
}
|
|
356
|
-
async hashPassword(password) {
|
|
357
|
-
return bcrypt.hash(password, this.saltRounds);
|
|
358
|
-
}
|
|
359
|
-
async verifyPassword(email, password) {
|
|
360
|
-
if (!this.db) throw new Error("Not connected");
|
|
361
|
-
const user = await this.findUserByEmail(email);
|
|
362
|
-
if (!user) return null;
|
|
363
|
-
const stored = this.db.prepare("SELECT password_hash FROM users WHERE id = ?").get(user.id);
|
|
364
|
-
if (!stored?.password_hash) return null;
|
|
365
|
-
const valid = await bcrypt.compare(password, stored.password_hash);
|
|
366
|
-
return valid ? user : null;
|
|
367
|
-
}
|
|
368
|
-
async createSession(userId, data = {}) {
|
|
369
|
-
if (!this.db) throw new Error("Not connected");
|
|
370
|
-
const id = randomBytes(32).toString("hex");
|
|
371
|
-
const token = randomBytes(32).toString("base64url");
|
|
372
|
-
const refreshToken = randomBytes(32).toString("base64url");
|
|
373
|
-
const now = /* @__PURE__ */ new Date();
|
|
374
|
-
const expiresAt = new Date(now.getTime() + 864e5).toISOString();
|
|
375
|
-
const session = {
|
|
376
|
-
id,
|
|
377
|
-
userId,
|
|
378
|
-
token,
|
|
379
|
-
refreshToken,
|
|
380
|
-
expiresAt,
|
|
381
|
-
createdAt: now.toISOString(),
|
|
382
|
-
ipAddress: data.ipAddress,
|
|
383
|
-
userAgent: data.userAgent
|
|
384
|
-
};
|
|
385
|
-
this.db.prepare(
|
|
386
|
-
`INSERT INTO kyro_sessions (id, user_id, token, refresh_token, expires_at, created_at, ip_address, user_agent)
|
|
387
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
|
|
388
|
-
).run(
|
|
389
|
-
session.id,
|
|
390
|
-
session.userId,
|
|
391
|
-
session.token,
|
|
392
|
-
session.refreshToken,
|
|
393
|
-
session.expiresAt,
|
|
394
|
-
session.createdAt,
|
|
395
|
-
session.ipAddress,
|
|
396
|
-
session.userAgent
|
|
397
|
-
);
|
|
398
|
-
return session;
|
|
399
|
-
}
|
|
400
|
-
async findSessionByToken(token) {
|
|
401
|
-
if (!this.db) throw new Error("Not connected");
|
|
402
|
-
const row = this.stmt("findSessionByToken").get(token);
|
|
403
|
-
if (!row) return null;
|
|
404
|
-
return this.rowToSession(row);
|
|
405
|
-
}
|
|
406
|
-
async findSessionByRefreshToken(refreshToken) {
|
|
407
|
-
if (!this.db) throw new Error("Not connected");
|
|
408
|
-
const row = this.stmt("findSessionByRefreshToken").get(refreshToken);
|
|
409
|
-
if (!row) return null;
|
|
410
|
-
return this.rowToSession(row);
|
|
411
|
-
}
|
|
412
|
-
async deleteSession(sessionId) {
|
|
413
|
-
if (!this.db) throw new Error("Not connected");
|
|
414
|
-
const result = this.stmt("deleteSession").run(sessionId, sessionId);
|
|
415
|
-
return result.changes > 0;
|
|
416
|
-
}
|
|
417
|
-
async deleteUserSessions(userId) {
|
|
418
|
-
if (!this.db) throw new Error("Not connected");
|
|
419
|
-
const result = this.stmt("deleteUserSessions").run(userId);
|
|
420
|
-
return result.changes;
|
|
421
|
-
}
|
|
422
|
-
async hasAnyUsers() {
|
|
423
|
-
if (!this.db) throw new Error("Not connected");
|
|
424
|
-
const row = this.stmt("countUsers").get();
|
|
425
|
-
return row.count > 0;
|
|
426
|
-
}
|
|
427
|
-
async addPasswordToHistory(userId, passwordHash) {
|
|
428
|
-
if (!this.db) throw new Error("Not connected");
|
|
429
|
-
this.stmt("addPasswordHistory").run(
|
|
430
|
-
userId,
|
|
431
|
-
passwordHash,
|
|
432
|
-
(/* @__PURE__ */ new Date()).toISOString()
|
|
433
|
-
);
|
|
434
|
-
this.stmt("trimPasswordHistory").run(userId);
|
|
435
|
-
}
|
|
436
|
-
async getPasswordHistory(userId, count = 5) {
|
|
437
|
-
if (!this.db) throw new Error("Not connected");
|
|
438
|
-
const rows = this.stmt("getPasswordHistory").all(userId, count);
|
|
439
|
-
return rows.map((r) => r.password_hash);
|
|
440
|
-
}
|
|
441
|
-
async isPasswordInHistory(password, userId, historyCount = 5) {
|
|
442
|
-
const history = await this.getPasswordHistory(userId, historyCount);
|
|
443
|
-
for (const hash of history) {
|
|
444
|
-
if (await bcrypt.compare(password, hash)) {
|
|
445
|
-
return true;
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
return false;
|
|
449
|
-
}
|
|
450
|
-
async recordFailedAttempt(userId) {
|
|
451
|
-
if (!this.db) throw new Error("Not connected");
|
|
452
|
-
const now = Date.now();
|
|
453
|
-
const lockout = this.stmt("getLockout").get(userId);
|
|
454
|
-
const attempts = (lockout?.attempts || 0) + 1;
|
|
455
|
-
const lockedUntil = attempts >= 5 ? now + 15 * 60 * 1e3 : lockout?.locked_until || null;
|
|
456
|
-
this.stmt("upsertLockout").run(
|
|
457
|
-
userId,
|
|
458
|
-
attempts,
|
|
459
|
-
now,
|
|
460
|
-
lockedUntil !== null ? now : null,
|
|
461
|
-
lockedUntil
|
|
462
|
-
);
|
|
463
|
-
}
|
|
464
|
-
async resetAttempts(userId) {
|
|
465
|
-
if (!this.db) throw new Error("Not connected");
|
|
466
|
-
this.stmt("resetLockout").run(userId);
|
|
467
|
-
}
|
|
468
|
-
async checkLockout(userId) {
|
|
469
|
-
if (!this.db) throw new Error("Not connected");
|
|
470
|
-
this.stmt("cleanupExpiredLockouts").run(Date.now());
|
|
471
|
-
const lockout = this.stmt("getLockout").get(userId);
|
|
472
|
-
if (!lockout) {
|
|
473
|
-
return {
|
|
474
|
-
locked: false,
|
|
475
|
-
attemptsRemaining: 5,
|
|
476
|
-
totalAttempts: 0
|
|
477
|
-
};
|
|
478
|
-
}
|
|
479
|
-
if (lockout.locked_until !== null && lockout.locked_until > Date.now()) {
|
|
480
|
-
return {
|
|
481
|
-
locked: true,
|
|
482
|
-
attemptsRemaining: 0,
|
|
483
|
-
lockedUntil: new Date(lockout.locked_until),
|
|
484
|
-
totalAttempts: lockout.attempts
|
|
485
|
-
};
|
|
486
|
-
}
|
|
487
|
-
return {
|
|
488
|
-
locked: false,
|
|
489
|
-
attemptsRemaining: Math.max(0, 5 - lockout.attempts),
|
|
490
|
-
totalAttempts: lockout.attempts
|
|
491
|
-
};
|
|
492
|
-
}
|
|
493
|
-
async logAudit(data) {
|
|
494
|
-
if (!this.db) throw new Error("Not connected");
|
|
495
|
-
const id = randomBytes(16).toString("hex");
|
|
496
|
-
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
497
|
-
this.db.prepare(
|
|
498
|
-
`INSERT INTO kyro_audit_logs (
|
|
499
|
-
id, timestamp, action, user_id, user_email, role, resource, resource_id,
|
|
500
|
-
ip_address, user_agent, success, error, metadata, created_at
|
|
501
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
502
|
-
).run(
|
|
503
|
-
id,
|
|
504
|
-
timestamp,
|
|
505
|
-
data.action,
|
|
506
|
-
data.userId || null,
|
|
507
|
-
data.userEmail || null,
|
|
508
|
-
data.role || null,
|
|
509
|
-
data.resource,
|
|
510
|
-
data.resourceId || null,
|
|
511
|
-
data.ipAddress || null,
|
|
512
|
-
data.userAgent || null,
|
|
513
|
-
data.success ? 1 : 0,
|
|
514
|
-
data.error || null,
|
|
515
|
-
data.metadata ? JSON.stringify(data.metadata) : null,
|
|
516
|
-
(/* @__PURE__ */ new Date()).toISOString()
|
|
517
|
-
);
|
|
518
|
-
return id;
|
|
519
|
-
}
|
|
520
|
-
async queryAuditLogs(options = {}) {
|
|
521
|
-
if (!this.db) throw new Error("Not connected");
|
|
522
|
-
const conditions = [];
|
|
523
|
-
const params = [];
|
|
524
|
-
if (options.action) {
|
|
525
|
-
conditions.push("action = ?");
|
|
526
|
-
params.push(options.action);
|
|
527
|
-
}
|
|
528
|
-
if (options.userId) {
|
|
529
|
-
conditions.push("user_id = ?");
|
|
530
|
-
params.push(options.userId);
|
|
531
|
-
}
|
|
532
|
-
if (options.resource) {
|
|
533
|
-
conditions.push("resource = ?");
|
|
534
|
-
params.push(options.resource);
|
|
535
|
-
}
|
|
536
|
-
if (options.success !== void 0) {
|
|
537
|
-
conditions.push("success = ?");
|
|
538
|
-
params.push(options.success ? 1 : 0);
|
|
539
|
-
}
|
|
540
|
-
if (options.startDate) {
|
|
541
|
-
conditions.push("timestamp >= ?");
|
|
542
|
-
params.push(options.startDate.toISOString());
|
|
543
|
-
}
|
|
544
|
-
if (options.endDate) {
|
|
545
|
-
conditions.push("timestamp <= ?");
|
|
546
|
-
params.push(options.endDate.toISOString());
|
|
547
|
-
}
|
|
548
|
-
const where = conditions.length > 0 ? "WHERE " + conditions.join(" AND ") : "";
|
|
549
|
-
const limit = options.limit || 50;
|
|
550
|
-
const offset = options.offset || 0;
|
|
551
|
-
const totalResult = this.db.prepare(`SELECT COUNT(*) as count FROM kyro_audit_logs ${where}`).get(...params);
|
|
552
|
-
const rows = this.db.prepare(
|
|
553
|
-
`SELECT * FROM kyro_audit_logs ${where} ORDER BY timestamp DESC LIMIT ? OFFSET ?`
|
|
554
|
-
).all(...params, limit, offset);
|
|
555
|
-
return {
|
|
556
|
-
total: totalResult.count,
|
|
557
|
-
logs: rows.map((row) => ({
|
|
558
|
-
id: row.id,
|
|
559
|
-
timestamp: new Date(row.timestamp),
|
|
560
|
-
action: row.action,
|
|
561
|
-
userId: row.user_id || void 0,
|
|
562
|
-
userEmail: row.user_email || void 0,
|
|
563
|
-
resource: row.resource,
|
|
564
|
-
resourceId: row.resource_id || void 0,
|
|
565
|
-
ipAddress: row.ip_address || void 0,
|
|
566
|
-
userAgent: row.user_agent || void 0,
|
|
567
|
-
success: row.success === 1,
|
|
568
|
-
error: row.error || void 0,
|
|
569
|
-
metadata: row.metadata ? JSON.parse(row.metadata) : void 0
|
|
570
|
-
}))
|
|
571
|
-
};
|
|
572
|
-
}
|
|
573
|
-
rowToUser(row) {
|
|
574
|
-
return {
|
|
575
|
-
id: row.id,
|
|
576
|
-
email: row.email,
|
|
577
|
-
passwordHash: row.password_hash,
|
|
578
|
-
role: row.role,
|
|
579
|
-
tenantId: row.tenant_id,
|
|
580
|
-
emailVerified: row.email_verified === 1,
|
|
581
|
-
locked: row.locked === 1,
|
|
582
|
-
lastLogin: row.last_login,
|
|
583
|
-
failedLoginAttempts: row.failed_login_attempts || 0,
|
|
584
|
-
createdAt: row.created_at,
|
|
585
|
-
updatedAt: row.updated_at
|
|
586
|
-
};
|
|
587
|
-
}
|
|
588
|
-
rowToSession(row) {
|
|
589
|
-
return {
|
|
590
|
-
id: row.id,
|
|
591
|
-
userId: row.user_id,
|
|
592
|
-
token: row.token,
|
|
593
|
-
refreshToken: row.refresh_token,
|
|
594
|
-
expiresAt: row.expires_at,
|
|
595
|
-
createdAt: row.created_at,
|
|
596
|
-
ipAddress: row.ip_address,
|
|
597
|
-
userAgent: row.user_agent
|
|
598
|
-
};
|
|
599
|
-
}
|
|
600
|
-
async findAuditLogs(filter) {
|
|
601
|
-
const result = await this.queryAuditLogs({
|
|
602
|
-
action: filter.action,
|
|
603
|
-
userId: filter.userId,
|
|
604
|
-
resource: filter.resource,
|
|
605
|
-
success: filter.success,
|
|
606
|
-
startDate: filter.startDate,
|
|
607
|
-
endDate: filter.endDate,
|
|
608
|
-
limit: filter.limit,
|
|
609
|
-
offset: filter.offset
|
|
610
|
-
});
|
|
611
|
-
return {
|
|
612
|
-
logs: result.logs.map((log) => ({
|
|
613
|
-
...log,
|
|
614
|
-
action: log.action
|
|
615
|
-
})),
|
|
616
|
-
total: result.total
|
|
617
|
-
};
|
|
618
|
-
}
|
|
619
|
-
async createAuditLog(data) {
|
|
620
|
-
const id = await this.logAudit({
|
|
621
|
-
action: data.action,
|
|
622
|
-
userId: data.userId,
|
|
623
|
-
userEmail: data.userEmail,
|
|
624
|
-
role: data.role,
|
|
625
|
-
resource: data.resource,
|
|
626
|
-
resourceId: data.resourceId,
|
|
627
|
-
ipAddress: data.ipAddress,
|
|
628
|
-
userAgent: data.userAgent,
|
|
629
|
-
success: data.success,
|
|
630
|
-
error: data.error,
|
|
631
|
-
metadata: data.metadata
|
|
632
|
-
});
|
|
633
|
-
const row = this.db?.prepare("SELECT * FROM kyro_audit_logs WHERE id = ?").get(id);
|
|
634
|
-
return {
|
|
635
|
-
...data,
|
|
636
|
-
id,
|
|
637
|
-
timestamp: row ? new Date(row.timestamp) : /* @__PURE__ */ new Date()
|
|
638
|
-
};
|
|
639
|
-
}
|
|
640
|
-
};
|
|
641
|
-
|
|
642
|
-
// src/auth/security/password-policy.ts
|
|
643
|
-
var DEFAULT_PASSWORD_POLICY = {
|
|
644
|
-
minLength: 12,
|
|
645
|
-
requireUppercase: true,
|
|
646
|
-
requireLowercase: true,
|
|
647
|
-
requireNumbers: true,
|
|
648
|
-
requireSpecialChars: true,
|
|
649
|
-
preventReuse: 5,
|
|
650
|
-
maxLength: 128
|
|
651
|
-
};
|
|
652
|
-
var PasswordPolicy = class {
|
|
653
|
-
config;
|
|
654
|
-
constructor(config = {}) {
|
|
655
|
-
this.config = { ...DEFAULT_PASSWORD_POLICY, ...config };
|
|
656
|
-
}
|
|
657
|
-
validate(password) {
|
|
658
|
-
const errors = [];
|
|
659
|
-
if (this.config.maxLength && password.length > this.config.maxLength) {
|
|
660
|
-
errors.push(
|
|
661
|
-
`Password must not exceed ${this.config.maxLength} characters`
|
|
662
|
-
);
|
|
663
|
-
}
|
|
664
|
-
if (password.length < this.config.minLength) {
|
|
665
|
-
errors.push(
|
|
666
|
-
`Password must be at least ${this.config.minLength} characters`
|
|
667
|
-
);
|
|
668
|
-
}
|
|
669
|
-
if (this.config.requireUppercase && !/[A-Z]/.test(password)) {
|
|
670
|
-
errors.push("Password must contain at least one uppercase letter");
|
|
671
|
-
}
|
|
672
|
-
if (this.config.requireLowercase && !/[a-z]/.test(password)) {
|
|
673
|
-
errors.push("Password must contain at least one lowercase letter");
|
|
674
|
-
}
|
|
675
|
-
if (this.config.requireNumbers && !/[0-9]/.test(password)) {
|
|
676
|
-
errors.push("Password must contain at least one number");
|
|
677
|
-
}
|
|
678
|
-
if (this.config.requireSpecialChars && !/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password)) {
|
|
679
|
-
errors.push("Password must contain at least one special character");
|
|
680
|
-
}
|
|
681
|
-
const commonPasswords = [
|
|
682
|
-
"password",
|
|
683
|
-
"123456",
|
|
684
|
-
"12345678",
|
|
685
|
-
"qwerty",
|
|
686
|
-
"abc123",
|
|
687
|
-
"monkey",
|
|
688
|
-
"1234567",
|
|
689
|
-
"letmein",
|
|
690
|
-
"trustno1",
|
|
691
|
-
"dragon",
|
|
692
|
-
"baseball",
|
|
693
|
-
"iloveyou",
|
|
694
|
-
"master",
|
|
695
|
-
"sunshine",
|
|
696
|
-
"ashley",
|
|
697
|
-
"football",
|
|
698
|
-
"password1",
|
|
699
|
-
"shadow",
|
|
700
|
-
"123123",
|
|
701
|
-
"654321"
|
|
702
|
-
];
|
|
703
|
-
if (commonPasswords.includes(password.toLowerCase())) {
|
|
704
|
-
errors.push(
|
|
705
|
-
"This password is too common. Please choose a more secure password"
|
|
706
|
-
);
|
|
707
|
-
}
|
|
708
|
-
if (/^[a-zA-Z]+$/.test(password) || /^[0-9]+$/.test(password)) {
|
|
709
|
-
errors.push(
|
|
710
|
-
"Password must contain a mix of letters, numbers, and/or special characters"
|
|
711
|
-
);
|
|
712
|
-
}
|
|
713
|
-
if (/(.)\1{2,}/.test(password)) {
|
|
714
|
-
errors.push(
|
|
715
|
-
"Password must not contain more than 2 consecutive identical characters"
|
|
716
|
-
);
|
|
717
|
-
}
|
|
718
|
-
if (/^(012|123|234|345|456|567|678|789|890|098|987|876|765|654|543|432|321|210)+$/i.test(
|
|
719
|
-
password
|
|
720
|
-
)) {
|
|
721
|
-
errors.push("Password must not contain sequential numbers or letters");
|
|
722
|
-
}
|
|
723
|
-
return {
|
|
724
|
-
valid: errors.length === 0,
|
|
725
|
-
errors
|
|
726
|
-
};
|
|
727
|
-
}
|
|
728
|
-
async checkReuse(passwordHash, history, verifyFn) {
|
|
729
|
-
return {
|
|
730
|
-
valid: true,
|
|
731
|
-
errors: []
|
|
732
|
-
};
|
|
733
|
-
}
|
|
734
|
-
async isInHistory(password, history, verifyFn) {
|
|
735
|
-
for (const hash of history) {
|
|
736
|
-
if (await verifyFn(password, hash)) {
|
|
737
|
-
return true;
|
|
738
|
-
}
|
|
739
|
-
}
|
|
740
|
-
return false;
|
|
741
|
-
}
|
|
742
|
-
generatePassword(length = 16) {
|
|
743
|
-
const uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
744
|
-
const lowercase = "abcdefghijklmnopqrstuvwxyz";
|
|
745
|
-
const numbers = "0123456789";
|
|
746
|
-
const special = "!@#$%^&*()_+-=[]{}|;:,.<>?";
|
|
747
|
-
let password = "";
|
|
748
|
-
password += uppercase[Math.floor(Math.random() * uppercase.length)];
|
|
749
|
-
password += lowercase[Math.floor(Math.random() * lowercase.length)];
|
|
750
|
-
password += numbers[Math.floor(Math.random() * numbers.length)];
|
|
751
|
-
password += special[Math.floor(Math.random() * special.length)];
|
|
752
|
-
const allChars = uppercase + lowercase + numbers + special;
|
|
753
|
-
for (let i = password.length; i < length; i++) {
|
|
754
|
-
password += allChars[Math.floor(Math.random() * allChars.length)];
|
|
755
|
-
}
|
|
756
|
-
return password.split("").sort(() => Math.random() - 0.5).join("");
|
|
757
|
-
}
|
|
758
|
-
getStrength(password) {
|
|
759
|
-
let score = 0;
|
|
760
|
-
const feedback = [];
|
|
761
|
-
if (password.length >= 8) score += 1;
|
|
762
|
-
if (password.length >= 12) score += 1;
|
|
763
|
-
if (password.length >= 16) score += 1;
|
|
764
|
-
if (/[a-z]/.test(password)) score += 1;
|
|
765
|
-
if (/[A-Z]/.test(password)) score += 1;
|
|
766
|
-
if (/[0-9]/.test(password)) score += 1;
|
|
767
|
-
if (/[!@#$%^&*()_+\-=\[\]{}|;:,.<>?]/.test(password)) score += 1;
|
|
768
|
-
if (password.length > 8) score += 1;
|
|
769
|
-
if (password.length > 12) score += 1;
|
|
770
|
-
const uniqueChars = new Set(password).size;
|
|
771
|
-
if (uniqueChars > 6) score += 1;
|
|
772
|
-
if (uniqueChars > 10) score += 1;
|
|
773
|
-
let label;
|
|
774
|
-
if (score <= 3) {
|
|
775
|
-
label = "Weak";
|
|
776
|
-
feedback.push("Add more characters");
|
|
777
|
-
feedback.push("Include uppercase and lowercase letters");
|
|
778
|
-
} else if (score <= 5) {
|
|
779
|
-
label = "Fair";
|
|
780
|
-
feedback.push("Add special characters");
|
|
781
|
-
feedback.push("Consider making it longer");
|
|
782
|
-
} else if (score <= 7) {
|
|
783
|
-
label = "Good";
|
|
784
|
-
feedback.push("Consider making it longer for extra security");
|
|
785
|
-
} else {
|
|
786
|
-
label = "Strong";
|
|
787
|
-
}
|
|
788
|
-
return { score, label, feedback };
|
|
789
|
-
}
|
|
790
|
-
setConfig(config) {
|
|
791
|
-
this.config = { ...this.config, ...config };
|
|
792
|
-
}
|
|
793
|
-
getConfig() {
|
|
794
|
-
return { ...this.config };
|
|
795
|
-
}
|
|
796
|
-
};
|
|
797
|
-
|
|
798
|
-
// src/auth/bootstrap.ts
|
|
799
|
-
async function bootstrapAdmin(config) {
|
|
800
|
-
const {
|
|
801
|
-
adminEmail,
|
|
802
|
-
adminPassword,
|
|
803
|
-
adminRole = "super_admin",
|
|
804
|
-
tenantId,
|
|
805
|
-
emailConfig,
|
|
806
|
-
sendWelcomeEmail = false
|
|
807
|
-
} = config;
|
|
808
|
-
const authAdapter = config.authAdapter || new SQLiteAuthAdapter({
|
|
809
|
-
path: config.authDbPath || "./data/auth.db"
|
|
810
|
-
});
|
|
811
|
-
try {
|
|
812
|
-
await authAdapter.connect?.();
|
|
813
|
-
} catch (error) {
|
|
814
|
-
return {
|
|
815
|
-
success: false,
|
|
816
|
-
error: "Failed to connect to auth storage"
|
|
817
|
-
};
|
|
818
|
-
}
|
|
819
|
-
const passwordPolicy = new PasswordPolicy();
|
|
820
|
-
const passwordValidation = passwordPolicy.validate(adminPassword);
|
|
821
|
-
if (!passwordValidation.valid) {
|
|
822
|
-
await authAdapter.disconnect?.();
|
|
823
|
-
return {
|
|
824
|
-
success: false,
|
|
825
|
-
error: `Invalid password: ${passwordValidation.errors.join(", ")}`
|
|
826
|
-
};
|
|
827
|
-
}
|
|
828
|
-
const existingUser = await authAdapter.findUserByEmail(adminEmail);
|
|
829
|
-
if (existingUser) {
|
|
830
|
-
await authAdapter.disconnect?.();
|
|
831
|
-
return {
|
|
832
|
-
success: false,
|
|
833
|
-
error: "Admin user already exists"
|
|
834
|
-
};
|
|
835
|
-
}
|
|
836
|
-
try {
|
|
837
|
-
const user = await authAdapter.createUser({
|
|
838
|
-
email: adminEmail,
|
|
839
|
-
password: adminPassword,
|
|
840
|
-
role: adminRole || "admin",
|
|
841
|
-
tenantId
|
|
842
|
-
});
|
|
843
|
-
if (sendWelcomeEmail && emailConfig) {
|
|
844
|
-
const emailTransport = new EmailTransport(emailConfig);
|
|
845
|
-
const templates = emailTransport.getTemplates();
|
|
846
|
-
const welcomeTemplate = templates.welcome(adminEmail.split("@")[0]);
|
|
847
|
-
await emailTransport.send({
|
|
848
|
-
to: adminEmail,
|
|
849
|
-
...welcomeTemplate
|
|
850
|
-
});
|
|
851
|
-
}
|
|
852
|
-
await authAdapter.disconnect?.();
|
|
853
|
-
return {
|
|
854
|
-
success: true,
|
|
855
|
-
user
|
|
856
|
-
};
|
|
857
|
-
} catch (error) {
|
|
858
|
-
await authAdapter.disconnect?.();
|
|
859
|
-
return {
|
|
860
|
-
success: false,
|
|
861
|
-
error: error instanceof Error ? error.message : "Failed to create admin user"
|
|
862
|
-
};
|
|
863
|
-
}
|
|
864
|
-
}
|
|
865
|
-
async function checkBootstrapRequired(authAdapter, adminEmail) {
|
|
866
|
-
const existingUser = await authAdapter.findUserByEmail(adminEmail);
|
|
867
|
-
return !existingUser;
|
|
868
|
-
}
|
|
869
|
-
function getBootstrapFromEnv() {
|
|
870
|
-
const email = process.env.KYRO_ADMIN_EMAIL;
|
|
871
|
-
const password = process.env.KYRO_ADMIN_PASSWORD;
|
|
872
|
-
if (!email || !password) {
|
|
873
|
-
return null;
|
|
874
|
-
}
|
|
875
|
-
return {
|
|
876
|
-
authDbPath: process.env.KYRO_AUTH_DB_PATH || "./data/auth.db",
|
|
877
|
-
adminEmail: email,
|
|
878
|
-
adminPassword: password,
|
|
879
|
-
adminRole: process.env.KYRO_ADMIN_ROLE || "super_admin",
|
|
880
|
-
tenantId: process.env.KYRO_ADMIN_TENANT_ID,
|
|
881
|
-
emailConfig: process.env.SMTP_HOST ? {
|
|
882
|
-
provider: "smtp",
|
|
883
|
-
smtp: {
|
|
884
|
-
host: process.env.SMTP_HOST,
|
|
885
|
-
port: parseInt(process.env.SMTP_PORT || "587", 10),
|
|
886
|
-
secure: process.env.SMTP_SECURE === "true",
|
|
887
|
-
auth: {
|
|
888
|
-
user: process.env.SMTP_USER || "",
|
|
889
|
-
pass: process.env.SMTP_PASS || ""
|
|
890
|
-
}
|
|
891
|
-
},
|
|
892
|
-
from: process.env.SMTP_FROM || "noreply@example.com",
|
|
893
|
-
fromName: process.env.SMTP_FROM_NAME
|
|
894
|
-
} : void 0,
|
|
895
|
-
sendWelcomeEmail: process.env.KYRO_ADMIN_SEND_WELCOME === "true"
|
|
896
|
-
};
|
|
897
|
-
}
|
|
898
|
-
async function autoBootstrap() {
|
|
899
|
-
const config = getBootstrapFromEnv();
|
|
900
|
-
if (!config) {
|
|
901
|
-
return null;
|
|
902
|
-
}
|
|
903
|
-
console.log("Auto-bootstrapping admin user...");
|
|
904
|
-
const result = await bootstrapAdmin(config);
|
|
905
|
-
if (result.success) {
|
|
906
|
-
console.log(`Admin user created: ${config.adminEmail}`);
|
|
907
|
-
} else {
|
|
908
|
-
console.error(`Bootstrap failed: ${result.error}`);
|
|
909
|
-
}
|
|
910
|
-
return result;
|
|
911
|
-
}
|
|
912
|
-
async function bootstrapWithRetry(config, maxRetries = 3, retryDelayMs = 2e3) {
|
|
913
|
-
let lastError = "";
|
|
914
|
-
for (let i = 0; i < maxRetries; i++) {
|
|
915
|
-
const result = await bootstrapAdmin(config);
|
|
916
|
-
if (result.success) {
|
|
917
|
-
return result;
|
|
918
|
-
}
|
|
919
|
-
lastError = result.error || "Unknown error";
|
|
920
|
-
if (lastError.includes("already exists")) {
|
|
921
|
-
return result;
|
|
922
|
-
}
|
|
923
|
-
if (i < maxRetries - 1) {
|
|
924
|
-
await new Promise((resolve) => setTimeout(resolve, retryDelayMs));
|
|
925
|
-
}
|
|
926
|
-
}
|
|
927
|
-
return {
|
|
928
|
-
success: false,
|
|
929
|
-
error: `Failed after ${maxRetries} retries: ${lastError}`
|
|
930
|
-
};
|
|
931
|
-
}
|
|
932
|
-
|
|
933
|
-
export { PasswordPolicy, SQLiteAuthAdapter, autoBootstrap, bootstrapAdmin, bootstrapWithRetry, checkBootstrapRequired, getBootstrapFromEnv };
|
|
934
|
-
//# sourceMappingURL=chunk-RRYXQMZG.js.map
|
|
935
|
-
//# sourceMappingURL=chunk-RRYXQMZG.js.map
|