Skip to content

Commit 5ca74e3

Browse files
committed
Use REST to get a CSPCHD token with which to access Portal
1 parent 8fc9697 commit 5ca74e3

File tree

4 files changed

+50
-46
lines changed

4 files changed

+50
-46
lines changed

src/api/getPortalUriWithCredentials.ts

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

src/api/getPortalUriWithToken.ts

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

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 { 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(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(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)