From cf5b5bf1ca218f1f87f6ec97fe222979571395e2 Mon Sep 17 00:00:00 2001 From: gjsjohnmurray Date: Sat, 28 May 2022 23:37:56 +0100 Subject: [PATCH] Use correct authorization token --- src/commands.ts | 10 +++++----- src/utils.ts | 20 ++++++++------------ src/webTerminalPanel.ts | 29 ++++++++++------------------- webview/webTerminal.js | 6 ++---- 4 files changed, 25 insertions(+), 40 deletions(-) diff --git a/src/commands.ts b/src/commands.ts index 0823b30..c0d4da4 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -1,5 +1,5 @@ import * as vscode from 'vscode'; -import { webTerminalUri } from './utils'; +import { webViewMessage } from './utils'; import { WebTerminalPanel } from './webterminalPanel'; export async function register(context: vscode.ExtensionContext) { @@ -15,13 +15,13 @@ export async function register(context: vscode.ExtensionContext) { if (!extension.isActive) await extension.activate(); const launchExternal = async (serverId, namespace?) => { - const uri = await webTerminalUri(serverId, false, namespace); - vscode.env.openExternal(uri); + const url = (await webViewMessage(serverId, false, namespace)).url; + vscode.env.openExternal(vscode.Uri.parse(url, true)); } const launchPanel = async (serverId, namespace?) => { - const uri = await webTerminalUri(serverId, true, namespace); - WebTerminalPanel.create(context.extensionUri, uri, serverId, namespace); + const message = await webViewMessage(serverId, true, namespace); + WebTerminalPanel.create(context.extensionUri, message, serverId, namespace); } // Register WebTerminal external browser launcher for server context menu diff --git a/src/utils.ts b/src/utils.ts index 9f959a8..6aeae36 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,7 @@ import * as vscode from 'vscode'; +import { WebTerminalMessage } from './webterminalPanel'; -export async function webTerminalUri(serverId: string, withCredentials: boolean, namespace?: string): Promise { +export async function webViewMessage(serverId: string, withCredentials: boolean, namespace?: string): Promise { const smExtension = vscode.extensions.getExtension('intersystems-community.servermanager'); if (smExtension) { if (!smExtension.isActive) { @@ -15,8 +16,9 @@ export async function webTerminalUri(serverId: string, withCredentials: boolean, const port = serverSpec.webServer.port const pathPrefix = serverSpec.webServer.pathPrefix - let query = namespace ? `&ns=${namespace}` : ""; - if (withCredentials && false) { + const query = namespace ? `ns=${namespace}` : ""; + const message: WebTerminalMessage = {url: vscode.Uri.from({scheme, authority: `${host}:${port}`, path: `${pathPrefix}/terminal-vscode/`, query}).toString(true)}; + if (withCredentials) { let username = serverSpec.username; let password = serverSpec.password; @@ -33,18 +35,12 @@ export async function webTerminalUri(serverId: string, withCredentials: boolean, password = session.accessToken; } } - if (username) { - const usernameEncoded = encodeURIComponent(username); - query += `&CacheUsername=${usernameEncoded}&IRISUsername=${usernameEncoded}`; - if (password) { - const passwordEncoded = encodeURIComponent(password); - query += `&CachePassword=${passwordEncoded}&IRISPassword=${passwordEncoded}`; - } + if (username && password) { + message.authToken = Buffer.from(`${username}:${password}`).toString("base64"); } } - query = query.slice(1); - return vscode.Uri.from({scheme, authority: `${host}:${port}`, path: `${pathPrefix}/terminal-vscode/`, query}) + return message; } } } diff --git a/src/webTerminalPanel.ts b/src/webTerminalPanel.ts index 8baf803..29ef61e 100644 --- a/src/webTerminalPanel.ts +++ b/src/webTerminalPanel.ts @@ -1,12 +1,14 @@ import * as vscode from "vscode"; -import { webTerminalUri } from "./utils"; +import { webViewMessage } from "./utils"; /** * The schema of the message that gets sent to the webview. */ -type WebviewMessage = { +export type WebTerminalMessage = { /** The url launching the WebTerminal */ url: string; + /** Token for the Authorization header */ + authToken?: string; }; /** @@ -22,7 +24,7 @@ export class WebTerminalPanel { private readonly _namespace: string; private _disposables: vscode.Disposable[] = []; - public static create(extensionUri: vscode.Uri, webTerminalUri: vscode.Uri, serverId: string, namespace?: string): WebTerminalPanel { + public static create(extensionUri: vscode.Uri, webTerminalMessage: WebTerminalMessage, serverId: string, namespace?: string): WebTerminalPanel { // Get the full path to the folder containing our webview files const webviewFolderUri: vscode.Uri = vscode.Uri.joinPath(extensionUri, "webview"); @@ -43,10 +45,10 @@ export class WebTerminalPanel { } ); - return new WebTerminalPanel(panel, webviewFolderUri, webTerminalUri, serverId); + return new WebTerminalPanel(panel, webviewFolderUri, webTerminalMessage, serverId); } - private constructor(panel: vscode.WebviewPanel, webviewFolderUri: vscode.Uri, webTerminalUri: vscode.Uri, serverId: string, namespace?: string) { + private constructor(panel: vscode.WebviewPanel, webviewFolderUri: vscode.Uri, webTerminalMessage: WebTerminalMessage, serverId: string, namespace?: string) { this._panel = panel; this._webviewFolderUri = webviewFolderUri; this._serverId = serverId; @@ -56,13 +58,13 @@ export class WebTerminalPanel { this._panel.iconPath = vscode.Uri.joinPath(webviewFolderUri, "favicon.ico"); // Set the webview's initial content - this.setWebviewHtml(webTerminalUri.toString(true)); + this.setWebviewHtml(webTerminalMessage.url); // Register handlers this.registerEventHandlers(); - // Send the initial message to the webview - this._panel.webview.postMessage(this.createMessage(webTerminalUri)); + // Send the message to the webview + this._panel.webview.postMessage(webTerminalMessage); } /** @@ -153,17 +155,6 @@ export class WebTerminalPanel { } } - /** - * Create the message to send to the webview. - */ - private createMessage(webTerminalUri: vscode.Uri): WebviewMessage { - - // Create the message - return { - url: webTerminalUri.toString(true), - }; - } - /** * Register handlers for events that may require us to update our webview */ diff --git a/webview/webTerminal.js b/webview/webTerminal.js index cfdf9c7..6a93fff 100644 --- a/webview/webTerminal.js +++ b/webview/webTerminal.js @@ -4,9 +4,7 @@ const showText = document.getElementById("showText"); const placeholder = document.getElementById("placeholder"); - let token = "am9obm06am9obm0="; //johnm:johnm hardcoded - TODO - fix this - - const loadIframe = (url) => { + const loadIframe = (url, token) => { // Send an XHR request first so we can supply an Authorization header var xhr = new XMLHttpRequest(); @@ -35,7 +33,7 @@ window.addEventListener("message", (event) => { const message = event.data; // The json data that the extension sent - loadIframe(message.url); + loadIframe(message.url, message.authToken); }); // This attempt to get focus into the WebTerminal doesn't seem to be working