From 97e97ebaa769ae18fa8753efd5ae256aa2f19ec4 Mon Sep 17 00:00:00 2001 From: ipmsteven Date: Fri, 24 Feb 2023 13:55:49 -0600 Subject: [PATCH] add auth token which is used when fetching external resource --- main.ts | 6 +++++- settings.ts | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/main.ts b/main.ts index edddc70..ec78721 100644 --- a/main.ts +++ b/main.ts @@ -51,7 +51,11 @@ export default class EmbedCodeFile extends Plugin { if (srcPath.startsWith("https://") || srcPath.startsWith("http://")) { try { - let httpResp = await requestUrl({url: srcPath, method: "GET"}) + let requestParams = {url: srcPath, method: "GET"} + if (this.settings.authToken) { + requestParams["headers"] = {"Authorization": `token ${this.settings.authToken}`} + } + let httpResp = await requestUrl(requestParams) fullSrc = httpResp.text } catch(e) { const errMsg = `\`ERROR: could't fetch '${srcPath}'\`` diff --git a/settings.ts b/settings.ts index 560673a..3f4ce0c 100644 --- a/settings.ts +++ b/settings.ts @@ -6,12 +6,14 @@ export interface EmbedCodeFileSettings { includedLanguages: string; titleBackgroundColor: string; titleFontColor: string; + authToken: string; } export const DEFAULT_SETTINGS: EmbedCodeFileSettings = { includedLanguages: 'c,cs,cpp,java,python,go,ruby,javascript,js,typescript,ts,shell,sh,bash', titleBackgroundColor: "#00000020", - titleFontColor: "" + titleFontColor: "", + authToken: "" } export class EmbedCodeFileSettingTab extends PluginSettingTab { @@ -48,7 +50,7 @@ export class EmbedCodeFileSettingTab extends PluginSettingTab { this.plugin.settings.titleFontColor = value; await this.plugin.saveSettings(); })); - + new Setting(containerEl) .setName('Background color of title') .addText(text => text @@ -58,5 +60,14 @@ export class EmbedCodeFileSettingTab extends PluginSettingTab { this.plugin.settings.titleBackgroundColor = value; await this.plugin.saveSettings(); })); + new Setting(containerEl) + .setName('API auth token to fetch code from private repos') + .addText(text => text + .setPlaceholder('Enter a token') + .setValue(this.plugin.settings.authToken) + .onChange(async (value) => { + this.plugin.settings.authToken = value; + await this.plugin.saveSettings(); + })); } }