Skip to content

Commit

Permalink
Use correct authorization token
Browse files Browse the repository at this point in the history
  • Loading branch information
gjsjohnmurray committed May 28, 2022
1 parent cb5f7a1 commit cf5b5bf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 40 deletions.
10 changes: 5 additions & 5 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
Expand Down
20 changes: 8 additions & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as vscode from 'vscode';
import { WebTerminalMessage } from './webterminalPanel';

export async function webTerminalUri(serverId: string, withCredentials: boolean, namespace?: string): Promise<vscode.Uri> {
export async function webViewMessage(serverId: string, withCredentials: boolean, namespace?: string): Promise<WebTerminalMessage> {
const smExtension = vscode.extensions.getExtension('intersystems-community.servermanager');
if (smExtension) {
if (!smExtension.isActive) {
Expand All @@ -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;

Expand All @@ -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;
}
}
}
29 changes: 10 additions & 19 deletions src/webTerminalPanel.ts
Original file line number Diff line number Diff line change
@@ -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;
};

/**
Expand All @@ -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");
Expand All @@ -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;
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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
*/
Expand Down
6 changes: 2 additions & 4 deletions webview/webTerminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cf5b5bf

Please sign in to comment.