From 6c2198e4fa75fab4cfe7d3c37cbec27b6d4a32c1 Mon Sep 17 00:00:00 2001 From: gjsjohnmurray Date: Fri, 7 May 2021 19:11:39 +0100 Subject: [PATCH] Keep separate token caches for Simple and External browsers --- src/api/getPortalUriWithToken.ts | 15 ++++++++++----- src/extension.ts | 6 +++--- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/api/getPortalUriWithToken.ts b/src/api/getPortalUriWithToken.ts index ed3fa98..0268390 100644 --- a/src/api/getPortalUriWithToken.ts +++ b/src/api/getPortalUriWithToken.ts @@ -3,9 +3,14 @@ import { Uri } from 'vscode'; import { extensionId, ServerSpec } from '../extension'; import { makeRESTRequest } from '../makeRESTRequest'; -const allTokens = new Map(); +export enum BrowserTarget { + SIMPLE = 0, + EXTERNAL = 1 +} + +const allTokens = [ new Map(), new Map()]; -export async function getPortalUriWithToken(name: string, scope?: vscode.ConfigurationScope): Promise { +export async function getPortalUriWithToken(target: BrowserTarget, name: string, scope?: vscode.ConfigurationScope): Promise { const PORTAL_HOME = '/csp/sys/UtilHome.csp'; @@ -16,7 +21,7 @@ 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]}); @@ -24,11 +29,11 @@ export async function getPortalUriWithToken(name: string, scope?: vscode.Configu 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; diff --git a/src/extension.ts b/src/extension.ts index 21b1c40..b2c49b3 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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, @@ -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); } @@ -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,