@icoretech/warden-mcp 0.2.12 → 0.2.13
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/bw/bwSession.js +78 -9
- package/package.json +1 -1
package/dist/bw/bwSession.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/bw/bwSession.ts
|
|
2
|
-
import { mkdir, rm } from 'node:fs/promises';
|
|
2
|
+
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import { runBw } from './bwCli.js';
|
|
5
5
|
import { Mutex } from './mutex.js';
|
|
@@ -54,11 +54,13 @@ export class BwSessionManager {
|
|
|
54
54
|
homeDir;
|
|
55
55
|
appDataDir;
|
|
56
56
|
processLockDir;
|
|
57
|
+
sessionStatePath;
|
|
57
58
|
constructor(env) {
|
|
58
59
|
this.env = env;
|
|
59
60
|
this.homeDir = env.homeDir ?? process.env.HOME ?? '/data';
|
|
60
61
|
this.appDataDir = join(this.homeDir, '.bitwarden-cli');
|
|
61
62
|
this.processLockDir = join(this.appDataDir, '.warden-mcp-auth-lock');
|
|
63
|
+
this.sessionStatePath = join(this.appDataDir, '.warden-mcp-session.json');
|
|
62
64
|
}
|
|
63
65
|
baseEnv(extra) {
|
|
64
66
|
return {
|
|
@@ -170,6 +172,7 @@ export class BwSessionManager {
|
|
|
170
172
|
this.session = null;
|
|
171
173
|
this.templateItem = null;
|
|
172
174
|
this.configuredHost = null;
|
|
175
|
+
await this.clearStoredSessionState();
|
|
173
176
|
await runBw(['logout'], { env: this.baseEnv(), timeoutMs: 30_000 }).catch(() => { });
|
|
174
177
|
const home = this.homeDir;
|
|
175
178
|
const cliStateDirs = [
|
|
@@ -188,6 +191,18 @@ export class BwSessionManager {
|
|
|
188
191
|
}
|
|
189
192
|
}
|
|
190
193
|
async currentServerUrl() {
|
|
194
|
+
try {
|
|
195
|
+
const dataPath = join(this.appDataDir, 'data.json');
|
|
196
|
+
const raw = await readFile(dataPath, 'utf8');
|
|
197
|
+
const parsed = JSON.parse(raw);
|
|
198
|
+
const base = parsed.global_environment_environment?.urls?.base;
|
|
199
|
+
if (typeof base === 'string' && base.length > 0) {
|
|
200
|
+
return base;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
// Fall back to the CLI if local state is absent or unreadable.
|
|
205
|
+
}
|
|
191
206
|
try {
|
|
192
207
|
const { stdout } = await runBw(['status'], {
|
|
193
208
|
env: this.baseEnv(),
|
|
@@ -206,17 +221,19 @@ export class BwSessionManager {
|
|
|
206
221
|
await this.ensureServerConfigured();
|
|
207
222
|
// If we already have a session, check if it still works.
|
|
208
223
|
if (this.session) {
|
|
209
|
-
|
|
210
|
-
const { stdout } = await runBw(['--session', this.session, 'unlock', '--check'], {
|
|
211
|
-
env: this.baseEnv(),
|
|
212
|
-
timeoutMs: 30_000,
|
|
213
|
-
});
|
|
214
|
-
void stdout;
|
|
224
|
+
if (await this.isSessionValid(this.session)) {
|
|
215
225
|
return this.session;
|
|
216
226
|
}
|
|
217
|
-
|
|
218
|
-
|
|
227
|
+
this.session = null;
|
|
228
|
+
}
|
|
229
|
+
const storedSession = await this.readStoredSessionState();
|
|
230
|
+
if (storedSession?.session) {
|
|
231
|
+
if (await this.isSessionValid(storedSession.session)) {
|
|
232
|
+
this.session = storedSession.session;
|
|
233
|
+
await this.writeStoredSessionState(storedSession.session);
|
|
234
|
+
return storedSession.session;
|
|
219
235
|
}
|
|
236
|
+
await this.clearStoredSessionState();
|
|
220
237
|
}
|
|
221
238
|
const unlockEnv = this.baseEnv({
|
|
222
239
|
BW_PASSWORD: this.env.password,
|
|
@@ -293,9 +310,61 @@ export class BwSessionManager {
|
|
|
293
310
|
if (!session)
|
|
294
311
|
throw new Error('bw login/unlock returned an empty session');
|
|
295
312
|
this.session = session;
|
|
313
|
+
await this.writeStoredSessionState(session);
|
|
296
314
|
return session;
|
|
297
315
|
});
|
|
298
316
|
}
|
|
317
|
+
sessionIdentity() {
|
|
318
|
+
return this.env.login.method === 'apikey'
|
|
319
|
+
? `apikey:${this.env.login.clientId}`
|
|
320
|
+
: `userpass:${this.env.login.user}`;
|
|
321
|
+
}
|
|
322
|
+
async isSessionValid(session) {
|
|
323
|
+
try {
|
|
324
|
+
const { stdout } = await runBw(['--session', session, 'unlock', '--check'], {
|
|
325
|
+
env: this.baseEnv(),
|
|
326
|
+
timeoutMs: 30_000,
|
|
327
|
+
});
|
|
328
|
+
void stdout;
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
catch {
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
async readStoredSessionState() {
|
|
336
|
+
try {
|
|
337
|
+
const raw = await readFile(this.sessionStatePath, 'utf8');
|
|
338
|
+
const parsed = JSON.parse(raw);
|
|
339
|
+
if (parsed.version !== 1 ||
|
|
340
|
+
parsed.host !== this.env.host ||
|
|
341
|
+
parsed.identity !== this.sessionIdentity() ||
|
|
342
|
+
typeof parsed.session !== 'string' ||
|
|
343
|
+
parsed.session.length === 0) {
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
return parsed;
|
|
347
|
+
}
|
|
348
|
+
catch {
|
|
349
|
+
return null;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
async writeStoredSessionState(session) {
|
|
353
|
+
const now = new Date().toISOString();
|
|
354
|
+
const state = {
|
|
355
|
+
version: 1,
|
|
356
|
+
host: this.env.host,
|
|
357
|
+
identity: this.sessionIdentity(),
|
|
358
|
+
session,
|
|
359
|
+
createdAt: now,
|
|
360
|
+
validatedAt: now,
|
|
361
|
+
};
|
|
362
|
+
await mkdir(this.appDataDir, { recursive: true });
|
|
363
|
+
await writeFile(this.sessionStatePath, JSON.stringify(state), 'utf8');
|
|
364
|
+
}
|
|
365
|
+
async clearStoredSessionState() {
|
|
366
|
+
await rm(this.sessionStatePath, { force: true }).catch(() => { });
|
|
367
|
+
}
|
|
299
368
|
async withProcessAuthLock(fn) {
|
|
300
369
|
const startedAt = Date.now();
|
|
301
370
|
await mkdir(this.appDataDir, { recursive: true });
|