w
This commit is contained in:
+31
-2
@@ -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<any> {
|
||||
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",
|
||||
|
||||
Reference in New Issue
Block a user