diff --git a/src/authenticationProvider.ts b/src/authenticationProvider.ts index f203791..43634e1 100644 --- a/src/authenticationProvider.ts +++ b/src/authenticationProvider.ts @@ -2,6 +2,7 @@ import { authentication, AuthenticationProvider, AuthenticationProviderAuthenticationSessionsChangeEvent, + AuthenticationProviderSessionOptions, AuthenticationSession, Disposable, Event, @@ -56,7 +57,7 @@ export class ServerManagerAuthenticationProvider implements AuthenticationProvid } // This function is called first when `vscode.authentication.getSession` is called. - public async getSessions(scopes: string[] = []): Promise { + public async getSessions(scopes: readonly string[] = [], options: AuthenticationProviderSessionOptions): Promise { await this._ensureInitialized(); let sessions = this._sessions; @@ -65,6 +66,15 @@ export class ServerManagerAuthenticationProvider implements AuthenticationProvid sessions = sessions.filter((session) => session.scopes[index] === scopes[index].toLowerCase()); } + if (options.account) { + const accountParts = options.account.id.split("/"); + const serverName = accountParts.shift(); + const userName = accountParts.join('/').toLowerCase(); + if (serverName && userName) { + sessions = sessions.filter((session) => session.scopes[0] === serverName && session.scopes[1] === userName); + } + } + if (sessions.length === 1) { if (!(await this._isStillValid(sessions[0]))) { sessions = []; diff --git a/src/extension.ts b/src/extension.ts index 45aa478..26c2a17 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -16,6 +16,11 @@ import { NamespaceTreeItem, ProjectTreeItem, ServerManagerView, ServerTreeItem, export const extensionId = "intersystems-community.servermanager"; export let globalState: vscode.Memento; +export function getAccountFromParts(serverName: string, userName?: string): vscode.AuthenticationSessionAccountInformation | undefined { + const accountId = userName ? `${serverName}/${userName.toLowerCase()}` : undefined; + return accountId ? { id: accountId, label: `${userName} on ${serverName}` } : undefined; +} + export function activate(context: vscode.ExtensionContext) { const _onDidChangePassword = new vscode.EventEmitter(); @@ -43,10 +48,11 @@ export function activate(context: vscode.ExtensionContext) { // Still logged in with the authentication provider? const scopes = [serverSession.serverName, serverSession.username.toLowerCase()]; + const account = getAccountFromParts(serverSession.serverName, serverSession.username); const session = await vscode.authentication.getSession( AUTHENTICATION_PROVIDER, scopes, - { silent: true }, + { silent: true, account }, ); // If not, try to log out on the server, then remove our record of its cookies @@ -407,6 +413,10 @@ export function activate(context: vscode.ExtensionContext) { return spec; }, + getAccount(serverSpec: IServerSpec): vscode.AuthenticationSessionAccountInformation | undefined { + return getAccountFromParts(serverSpec.name, serverSpec.username); + }, + onDidChangePassword( ): vscode.Event { return _onDidChangePassword.event; diff --git a/src/makeRESTRequest.ts b/src/makeRESTRequest.ts index 1658212..ac1adf0 100644 --- a/src/makeRESTRequest.ts +++ b/src/makeRESTRequest.ts @@ -7,6 +7,7 @@ import * as vscode from "vscode"; import { AUTHENTICATION_PROVIDER } from "./authenticationProvider"; import { IServerSpec } from "@intersystems-community/intersystems-servermanager"; import { getServerSpec } from "./api/getServerSpec"; +import { getAccountFromParts } from "./extension"; export interface IServerSession { serverName: string; @@ -221,16 +222,17 @@ async function resolveCredentials(serverSpec: IServerSpec) { // This arises if setting says to use authentication provider if (typeof serverSpec.password === "undefined") { const scopes = [serverSpec.name, (serverSpec.username || "").toLowerCase()]; + const account = getAccountFromParts(serverSpec.name, serverSpec.username); let session = await vscode.authentication.getSession( AUTHENTICATION_PROVIDER, scopes, - { silent: true }, + { silent: true, account }, ); if (!session) { session = await vscode.authentication.getSession( AUTHENTICATION_PROVIDER, scopes, - { createIfNone: true }, + { createIfNone: true, account }, ); } if (session) {