Skip to content

Commit

Permalink
Keep separate token caches for Simple and External browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
gjsjohnmurray committed May 7, 2021
1 parent 5ca74e3 commit 6c2198e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
15 changes: 10 additions & 5 deletions src/api/getPortalUriWithToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import { Uri } from 'vscode';
import { extensionId, ServerSpec } from '../extension';
import { makeRESTRequest } from '../makeRESTRequest';

const allTokens = new Map<string, string>();
export enum BrowserTarget {
SIMPLE = 0,
EXTERNAL = 1
}

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

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

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

Expand All @@ -16,19 +21,19 @@ export async function getPortalUriWithToken(name: string, scope?: vscode.Configu
if (typeof spec !== 'undefined') {

// Retrieve previously cached token
let token = allTokens.get(name) || '';
let token = allTokens[target].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);
allTokens[target].delete(name);
}
else {
token = response.data?.result?.content[0]?.token || '';
allTokens.set(name, token);
allTokens[target].set(name, token);
}

const webServer = spec.webServer;
Expand Down
6 changes: 3 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { importFromRegistry } from './commands/importFromRegistry';
import { ServerManagerView, ServerTreeItem, SMTreeItem } from './ui/serverManagerView';
import { addServer } from './api/addServer';
import { getServerSummary } from './api/getServerSummary';
import { getPortalUriWithToken } from './api/getPortalUriWithToken';
import { BrowserTarget, getPortalUriWithToken } from './api/getPortalUriWithToken';

export interface ServerName {
name: string,
Expand Down Expand Up @@ -78,7 +78,7 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand(`${extensionId}.openPortalExternal`, (server?: ServerTreeItem) => {
if (server?.contextValue?.match(/\.server\./) && server.name) {
getPortalUriWithToken(server.name).then((uriWithToken) => {
getPortalUriWithToken(BrowserTarget.EXTERNAL, server.name).then((uriWithToken) => {
if (uriWithToken) {
vscode.env.openExternal(uriWithToken);
}
Expand All @@ -89,7 +89,7 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand(`${extensionId}.openPortalTab`, (server?: ServerTreeItem) => {
if (server?.contextValue?.match(/\.server\./) && server.name) {
getPortalUriWithToken(server.name).then((uriWithToken) => {
getPortalUriWithToken(BrowserTarget.SIMPLE, server.name).then((uriWithToken) => {
if (uriWithToken) {
//
// It is essential to pass skipEncoding=true when converting the uri to a string,
Expand Down

0 comments on commit 6c2198e

Please sign in to comment.