-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
47 lines (42 loc) · 2.01 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import * as vscode from 'vscode';
import { WebTerminalMessage } from './webTerminalPanel';
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) {
await smExtension.activate();
}
const serverManagerApi = smExtension.exports;
if (serverManagerApi && serverManagerApi.getServerSpec) {
const serverSpec = await serverManagerApi.getServerSpec(serverId)
const scheme = serverSpec.webServer.scheme
const host = serverSpec.webServer.host
const port = serverSpec.webServer.port
const pathPrefix = serverSpec.webServer.pathPrefix
const query = namespace ? `ns=${namespace}` : "";
const app = withCredentials ? "terminal-vscode" : "terminal";
const message: WebTerminalMessage = {url: vscode.Uri.from({scheme, authority: `${host}:${port}`, path: `${pathPrefix}/${app}/`, query}).toString(true)};
if (withCredentials) {
let username = serverSpec.username;
let password = serverSpec.password;
// This arises when Server Manager 3+ defers to authentication provider
if (typeof password === 'undefined') {
const AUTHENTICATION_PROVIDER = 'intersystems-server-credentials';
const scopes = [serverSpec.name, username];
let session = await vscode.authentication.getSession(AUTHENTICATION_PROVIDER, scopes, { silent: true });
if (!session) {
session = await vscode.authentication.getSession(AUTHENTICATION_PROVIDER, scopes, { createIfNone: true });
}
if (session) {
username = username || session.scopes[1];
password = session.accessToken;
}
}
if (username && password) {
message.authToken = Buffer.from(`${username}:${password}`).toString("base64");
}
}
return message;
}
}
}