diff --git a/.gitignore b/.gitignore index e09a007..fb068c8 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,5 @@ data.json # Exclude macOS Finder (System Explorer) View States .DS_Store + +.env \ No newline at end of file diff --git a/src/DBService.ts b/src/DBService.ts index 290617d..7ec1e34 100644 --- a/src/DBService.ts +++ b/src/DBService.ts @@ -87,15 +87,23 @@ export class DBService { * @returns Promise resolving to an array of result objects */ async getQuery>(sql: string, params: any[] = []): Promise { - if (this.mode === "remote") { - const res = await fetch(`${this.apiBaseUrl}/query`, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ sql, params }), - }); - if (!res.ok) throw new Error(await res.text()); - return await res.json(); - } + if (this.mode === "remote") { + const res = await fetch(`${this.apiBaseUrl}/query`, { + method: "POST", + headers: { + "Content-Type": "application/json", + ...(this.settings?.cfAccessClientId && { + "CF-Access-Client-Id": this.settings.cfAccessClientId, + }), + ...(this.settings?.cfAccessClientSecret && { + "CF-Access-Client-Secret": this.settings.cfAccessClientSecret, + }), + }, + body: JSON.stringify({ sql, params }), + }); + if (!res.ok) throw new Error(await res.text()); + return await res.json(); + } if (!this.db) throw new Error("Database not loaded (local mode)."); @@ -125,8 +133,16 @@ export class DBService { if (this.mode === "remote") { const res = await fetch(`${this.apiBaseUrl}/execute`, { method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ sql, params }), + headers: { + "Content-Type": "application/json", + ...(this.settings?.cfAccessClientId && { + "CF-Access-Client-Id": this.settings.cfAccessClientId, + }), + ...(this.settings?.cfAccessClientSecret && { + "CF-Access-Client-Secret": this.settings.cfAccessClientSecret, + }), + }, + body: JSON.stringify({ sql, params }), }); if (!res.ok) throw new Error(await res.text()); return; diff --git a/src/settingTab.ts b/src/settingTab.ts index 4d5b07e..e6b44c5 100644 --- a/src/settingTab.ts +++ b/src/settingTab.ts @@ -69,5 +69,29 @@ export class SQLiteDBSettingTab extends PluginSettingTab { this.plugin.settings.journalTableName = value; await this.plugin.saveSettings(); })); + + new Setting(containerEl) + .setName("CF Access Client ID") + .setDesc("Cloudflare Access client ID for remote API authentication") + .addText(text => + text + .setPlaceholder("your-client-id") + .setValue(this.plugin.settings.cfAccessClientId || "") + .onChange(async (value) => { + this.plugin.settings.cfAccessClientId = value; + await this.plugin.saveSettings(); + })); + + new Setting(containerEl) + .setName("CF Access Client Secret") + .setDesc("Cloudflare Access client secret for remote API authentication") + .addText(text => + text + .setPlaceholder("your-client-secret") + .setValue(this.plugin.settings.cfAccessClientSecret || "") + .onChange(async (value) => { + this.plugin.settings.cfAccessClientSecret = value; + await this.plugin.saveSettings(); + })); } } diff --git a/src/types.ts b/src/types.ts index 8750849..40d3728 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,6 +4,8 @@ export interface SQLiteDBSettings { apiBaseUrl: string; journalFolderPath: string; journalTableName: string; + cfAccessClientId: string; + cfAccessClientSecret: string; } export const DEFAULT_SETTINGS: SQLiteDBSettings = { @@ -12,4 +14,6 @@ export const DEFAULT_SETTINGS: SQLiteDBSettings = { apiBaseUrl: "", journalFolderPath: "", journalTableName: "", + cfAccessClientId: "", + cfAccessClientSecret: "", };