Skip to content

Commit

Permalink
Use REST to get a CSPCHD token with which to access Portal
Browse files Browse the repository at this point in the history
  • Loading branch information
gjsjohnmurray committed May 7, 2021
1 parent 8fc9697 commit 5ca74e3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 46 deletions.
34 changes: 0 additions & 34 deletions src/api/getPortalUriWithCredentials.ts

This file was deleted.

39 changes: 39 additions & 0 deletions src/api/getPortalUriWithToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as vscode from 'vscode';
import { Uri } from 'vscode';
import { extensionId, ServerSpec } from '../extension';
import { makeRESTRequest } from '../makeRESTRequest';

const allTokens = new Map<string, string>();

export async function getPortalUriWithToken(name: string, scope?: vscode.ConfigurationScope): Promise<Uri | undefined> {

const PORTAL_HOME = '/csp/sys/UtilHome.csp';

// Use our own API so that the Recent folder updates with our activity
const myApi = vscode.extensions.getExtension(extensionId)?.exports;

const spec: ServerSpec | undefined = await myApi.getServerSpec(name, scope);
if (typeof spec !== 'undefined') {

// Retrieve previously cached token
let token = allTokens.get(name) || '';

// Revalidate and extend existing token, or obtain a new one
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]});

if (!response) {
// User will have to enter credentials
token = '';
allTokens.delete(name);
}
else {
token = response.data?.result?.content[0]?.token || '';
allTokens.set(name, token);
}

const webServer = spec.webServer;
let queryString = token ? `CSPCHD=${encodeURIComponent(token)}` : '';

return vscode.Uri.parse(`${webServer.scheme}://${webServer.host}:${webServer.port}${webServer.pathPrefix}${PORTAL_HOME}?${queryString}`, true);
}
}
15 changes: 7 additions & 8 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { storePassword, clearPassword } from './commands/managePasswords';
import { importFromRegistry } from './commands/importFromRegistry';
import { ServerManagerView, ServerTreeItem, SMTreeItem } from './ui/serverManagerView';
import { addServer } from './api/addServer';
import { getPortalUriWithCredentials } from './api/getPortalUriWithCredentials';
import { getServerSummary } from './api/getServerSummary';
import { getPortalUriWithToken } from './api/getPortalUriWithToken';

export interface ServerName {
name: string,
Expand Down Expand Up @@ -78,9 +78,9 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand(`${extensionId}.openPortalExternal`, (server?: ServerTreeItem) => {
if (server?.contextValue?.match(/\.server\./) && server.name) {
getPortalUriWithCredentials(server.name).then((uriWithCredentials) => {
if (uriWithCredentials) {
vscode.env.openExternal(uriWithCredentials);
getPortalUriWithToken(server.name).then((uriWithToken) => {
if (uriWithToken) {
vscode.env.openExternal(uriWithToken);
}
});
}
Expand All @@ -89,13 +89,12 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand(`${extensionId}.openPortalTab`, (server?: ServerTreeItem) => {
if (server?.contextValue?.match(/\.server\./) && server.name) {
getPortalUriWithCredentials(server.name).then((uriWithCredentials) => {
if (uriWithCredentials) {
//vscode.commands.executeCommand('simpleBrowser.api.open', uriWithCredentials);
getPortalUriWithToken(server.name).then((uriWithToken) => {
if (uriWithToken) {
//
// It is essential to pass skipEncoding=true when converting the uri to a string,
// otherwise the encoding done within Simple Browser / webview causes double-encoding of the querystring.
vscode.commands.executeCommand('simpleBrowser.show', uriWithCredentials.toString(true));
vscode.commands.executeCommand('simpleBrowser.show', uriWithToken.toString(true));
}
});
}
Expand Down
8 changes: 4 additions & 4 deletions src/makeRESTRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface AtelierRESTEndpoint {
}
url += "/api/atelier/";
if (endpoint) {
url += "/api/atelier/v" + String(endpoint.apiVersion) + "/" + endpoint.namespace + endpoint.path;
url += "v" + String(endpoint.apiVersion) + "/" + endpoint.namespace + endpoint.path;
}

// Make the request (SASchema support removed)
Expand Down Expand Up @@ -65,7 +65,7 @@ export interface AtelierRESTEndpoint {
respdata = await axios.request(
{
method: method,
url: url,
url: encodeURI(url),
data: data,
headers: {
'Content-Type': 'application/json'
Expand All @@ -85,7 +85,7 @@ export interface AtelierRESTEndpoint {
respdata = await axios.request(
{
method: method,
url: url,
url: encodeURI(url),
withCredentials: true,
jar: cookieJar,
validateStatus: function (status) {
Expand All @@ -99,7 +99,7 @@ export interface AtelierRESTEndpoint {
respdata = await axios.request(
{
method: method,
url: url,
url: encodeURI(url),
auth: {
username: server.username,
password: server.password
Expand Down

0 comments on commit 5ca74e3

Please sign in to comment.