Skip to content

Commit d7cbb44

Browse files
Merge pull request #90 from gjsjohnmurray/preauth-Portal
Preauth portal pages by obtaining a valid CSPCHD
2 parents 8fc9697 + 6c2198e commit d7cbb44

File tree

4 files changed

+55
-46
lines changed

4 files changed

+55
-46
lines changed

src/api/getPortalUriWithCredentials.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/api/getPortalUriWithToken.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as vscode from 'vscode';
2+
import { Uri } from 'vscode';
3+
import { extensionId, ServerSpec } from '../extension';
4+
import { makeRESTRequest } from '../makeRESTRequest';
5+
6+
export enum BrowserTarget {
7+
SIMPLE = 0,
8+
EXTERNAL = 1
9+
}
10+
11+
const allTokens = [ new Map<string, string>(), new Map<string, string>()];
12+
13+
export async function getPortalUriWithToken(target: BrowserTarget, name: string, scope?: vscode.ConfigurationScope): Promise<Uri | undefined> {
14+
15+
const PORTAL_HOME = '/csp/sys/UtilHome.csp';
16+
17+
// Use our own API so that the Recent folder updates with our activity
18+
const myApi = vscode.extensions.getExtension(extensionId)?.exports;
19+
20+
const spec: ServerSpec | undefined = await myApi.getServerSpec(name, scope);
21+
if (typeof spec !== 'undefined') {
22+
23+
// Retrieve previously cached token
24+
let token = allTokens[target].get(name) || '';
25+
26+
// Revalidate and extend existing token, or obtain a new one
27+
const response = await makeRESTRequest("POST", spec, { apiVersion: 1, namespace: '%SYS', path:'/action/query' }, { query: 'select %Atelier_v1_Utils.General_GetCSPToken(?, ?) token', parameters: [PORTAL_HOME, token]});
28+
29+
if (!response) {
30+
// User will have to enter credentials
31+
token = '';
32+
allTokens[target].delete(name);
33+
}
34+
else {
35+
token = response.data?.result?.content[0]?.token || '';
36+
allTokens[target].set(name, token);
37+
}
38+
39+
const webServer = spec.webServer;
40+
let queryString = token ? `CSPCHD=${encodeURIComponent(token)}` : '';
41+
42+
return vscode.Uri.parse(`${webServer.scheme}://${webServer.host}:${webServer.port}${webServer.pathPrefix}${PORTAL_HOME}?${queryString}`, true);
43+
}
44+
}

src/extension.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { storePassword, clearPassword } from './commands/managePasswords';
99
import { importFromRegistry } from './commands/importFromRegistry';
1010
import { ServerManagerView, ServerTreeItem, SMTreeItem } from './ui/serverManagerView';
1111
import { addServer } from './api/addServer';
12-
import { getPortalUriWithCredentials } from './api/getPortalUriWithCredentials';
1312
import { getServerSummary } from './api/getServerSummary';
13+
import { BrowserTarget, getPortalUriWithToken } from './api/getPortalUriWithToken';
1414

1515
export interface ServerName {
1616
name: string,
@@ -78,9 +78,9 @@ export function activate(context: vscode.ExtensionContext) {
7878
context.subscriptions.push(
7979
vscode.commands.registerCommand(`${extensionId}.openPortalExternal`, (server?: ServerTreeItem) => {
8080
if (server?.contextValue?.match(/\.server\./) && server.name) {
81-
getPortalUriWithCredentials(server.name).then((uriWithCredentials) => {
82-
if (uriWithCredentials) {
83-
vscode.env.openExternal(uriWithCredentials);
81+
getPortalUriWithToken(BrowserTarget.EXTERNAL, server.name).then((uriWithToken) => {
82+
if (uriWithToken) {
83+
vscode.env.openExternal(uriWithToken);
8484
}
8585
});
8686
}
@@ -89,13 +89,12 @@ export function activate(context: vscode.ExtensionContext) {
8989
context.subscriptions.push(
9090
vscode.commands.registerCommand(`${extensionId}.openPortalTab`, (server?: ServerTreeItem) => {
9191
if (server?.contextValue?.match(/\.server\./) && server.name) {
92-
getPortalUriWithCredentials(server.name).then((uriWithCredentials) => {
93-
if (uriWithCredentials) {
94-
//vscode.commands.executeCommand('simpleBrowser.api.open', uriWithCredentials);
92+
getPortalUriWithToken(BrowserTarget.SIMPLE, server.name).then((uriWithToken) => {
93+
if (uriWithToken) {
9594
//
9695
// It is essential to pass skipEncoding=true when converting the uri to a string,
9796
// otherwise the encoding done within Simple Browser / webview causes double-encoding of the querystring.
98-
vscode.commands.executeCommand('simpleBrowser.show', uriWithCredentials.toString(true));
97+
vscode.commands.executeCommand('simpleBrowser.show', uriWithToken.toString(true));
9998
}
10099
});
101100
}

src/makeRESTRequest.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface AtelierRESTEndpoint {
3636
}
3737
url += "/api/atelier/";
3838
if (endpoint) {
39-
url += "/api/atelier/v" + String(endpoint.apiVersion) + "/" + endpoint.namespace + endpoint.path;
39+
url += "v" + String(endpoint.apiVersion) + "/" + endpoint.namespace + endpoint.path;
4040
}
4141

4242
// Make the request (SASchema support removed)
@@ -65,7 +65,7 @@ export interface AtelierRESTEndpoint {
6565
respdata = await axios.request(
6666
{
6767
method: method,
68-
url: url,
68+
url: encodeURI(url),
6969
data: data,
7070
headers: {
7171
'Content-Type': 'application/json'
@@ -85,7 +85,7 @@ export interface AtelierRESTEndpoint {
8585
respdata = await axios.request(
8686
{
8787
method: method,
88-
url: url,
88+
url: encodeURI(url),
8989
withCredentials: true,
9090
jar: cookieJar,
9191
validateStatus: function (status) {
@@ -99,7 +99,7 @@ export interface AtelierRESTEndpoint {
9999
respdata = await axios.request(
100100
{
101101
method: method,
102-
url: url,
102+
url: encodeURI(url),
103103
auth: {
104104
username: server.username,
105105
password: server.password

0 commit comments

Comments
 (0)