From a79db30ea931221279d450a6821ffc416a01c580 Mon Sep 17 00:00:00 2001 From: albnnc Date: Thu, 9 Jul 2026 09:21:04 +0300 Subject: [PATCH] w --- ews_client.ts | 33 +++++++++++++++++++++++++++++++-- models.ts | 1 - tools/auth.ts | 16 ++++++++++++++-- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/ews_client.ts b/ews_client.ts index 731d490..aff3de6 100644 --- a/ews_client.ts +++ b/ews_client.ts @@ -7,6 +7,24 @@ import { fileURLToPath } from "node:url"; import { promisify } from "node:util"; import type { Email, Folder, LoginConfig } from "./models.ts"; +let inMemoryPassword: string | null = null; + +export function setPassword(password: string): void { + inMemoryPassword = password; +} + +export function getPassword(): string | null { + return inMemoryPassword; +} + +export function hasPassword(): boolean { + return inMemoryPassword !== null; +} + +export function clearPassword(): void { + inMemoryPassword = null; +} + const ntlm: any = createRequire(import.meta.url)("httpntlm"); const postAsync = promisify(ntlm.post.bind(ntlm)); @@ -76,27 +94,38 @@ export function clearConfig(): void { if (fs.existsSync(filePath)) { fs.unlinkSync(filePath); } + clearPassword(); } export class EwsClient { private config: LoginConfig; private ewsUrl: string; - constructor(config: LoginConfig) { + constructor(config: LoginConfig, password?: string) { this.config = config; this.ewsUrl = `${config.serverUrl.replace(/\/+$/, "")}/EWS/Exchange.asmx`; + + if (password) { + setPassword(password); + } } private get domain(): string { return this.config.domain ?? "corp"; } + private get password(): string { + const pw = getPassword(); + if (!pw) throw new Error("Not logged in. Password not found in memory."); + return pw; + } + private async soapRequest(body: string, soapAction: string): Promise { const res = await postAsync({ url: this.ewsUrl, username: this.config.username, domain: this.domain, - password: this.config.password, + password: this.password, agent: AGENT, headers: { "Content-Type": "text/xml; charset=utf-8", diff --git a/models.ts b/models.ts index e374a83..0646dc5 100644 --- a/models.ts +++ b/models.ts @@ -2,7 +2,6 @@ export interface LoginConfig { serverUrl: string; email: string; username: string; - password: string; domain?: string; } diff --git a/tools/auth.ts b/tools/auth.ts index b5a6427..b566d3c 100644 --- a/tools/auth.ts +++ b/tools/auth.ts @@ -3,8 +3,10 @@ import { z } from "zod/v4"; import { clearConfig, EwsClient, + hasPassword, loadConfig, saveConfig, + setPassword, } from "../ews_client.ts"; export function registerAuthTools(server: McpServer): void { @@ -28,11 +30,10 @@ export function registerAuthTools(server: McpServer): void { serverUrl: serverUrl.replace(/\/+$/, ""), email, username, - password, domain: domain ?? "corp", }; - const client = new EwsClient(config); + const client = new EwsClient(config, password); const result = await client.verifyConnection(); if (!result.ok) { @@ -80,6 +81,17 @@ export function registerAuthTools(server: McpServer): void { }, async () => { try { + if (!hasPassword()) { + return { + content: [{ + type: "text" as const, + text: JSON.stringify({ + authenticated: false, + error: "Not logged in. Password not found in memory.", + }), + }], + }; + } const config = loadConfig(); const client = new EwsClient(config); const result = await client.verifyConnection();