Skip to content

Commit c225dc6

Browse files
committed
Add getAccount to API, and use it internally to fix overprompting
1 parent b62ed04 commit c225dc6

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/authenticationProvider.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
authentication,
33
AuthenticationProvider,
44
AuthenticationProviderAuthenticationSessionsChangeEvent,
5+
AuthenticationProviderSessionOptions,
56
AuthenticationSession,
67
Disposable,
78
Event,
@@ -56,7 +57,7 @@ export class ServerManagerAuthenticationProvider implements AuthenticationProvid
5657
}
5758

5859
// This function is called first when `vscode.authentication.getSession` is called.
59-
public async getSessions(scopes: string[] = []): Promise<readonly AuthenticationSession[]> {
60+
public async getSessions(scopes: readonly string[] = [], options: AuthenticationProviderSessionOptions): Promise<AuthenticationSession[]> {
6061
await this._ensureInitialized();
6162
let sessions = this._sessions;
6263

@@ -65,6 +66,15 @@ export class ServerManagerAuthenticationProvider implements AuthenticationProvid
6566
sessions = sessions.filter((session) => session.scopes[index] === scopes[index].toLowerCase());
6667
}
6768

69+
if (options.account) {
70+
const accountParts = options.account.id.split("/");
71+
const serverName = accountParts.shift();
72+
const userName = accountParts.join('/').toLowerCase();
73+
if (serverName && userName) {
74+
sessions = sessions.filter((session) => session.scopes[0] === serverName && session.scopes[1] === userName);
75+
}
76+
}
77+
6878
if (sessions.length === 1) {
6979
if (!(await this._isStillValid(sessions[0]))) {
7080
sessions = [];

src/extension.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import { NamespaceTreeItem, ProjectTreeItem, ServerManagerView, ServerTreeItem,
1616
export const extensionId = "intersystems-community.servermanager";
1717
export let globalState: vscode.Memento;
1818

19+
export function getAccountFromParts(serverName: string, userName?: string): vscode.AuthenticationSessionAccountInformation | undefined {
20+
const accountId = userName ? `${serverName}/${userName.toLowerCase()}` : undefined;
21+
return accountId ? { id: accountId, label: `${userName} on ${serverName}` } : undefined;
22+
}
23+
1924
export function activate(context: vscode.ExtensionContext) {
2025

2126
const _onDidChangePassword = new vscode.EventEmitter<string>();
@@ -43,10 +48,11 @@ export function activate(context: vscode.ExtensionContext) {
4348

4449
// Still logged in with the authentication provider?
4550
const scopes = [serverSession.serverName, serverSession.username.toLowerCase()];
51+
const account = getAccountFromParts(serverSession.serverName, serverSession.username);
4652
const session = await vscode.authentication.getSession(
4753
AUTHENTICATION_PROVIDER,
4854
scopes,
49-
{ silent: true },
55+
{ silent: true, account },
5056
);
5157

5258
// 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) {
407413
return spec;
408414
},
409415

416+
getAccount(serverSpec: IServerSpec): vscode.AuthenticationSessionAccountInformation | undefined {
417+
return getAccountFromParts(serverSpec.name, serverSpec.username);
418+
},
419+
410420
onDidChangePassword(
411421
): vscode.Event<string> {
412422
return _onDidChangePassword.event;

src/makeRESTRequest.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as vscode from "vscode";
77
import { AUTHENTICATION_PROVIDER } from "./authenticationProvider";
88
import { IServerSpec } from "@intersystems-community/intersystems-servermanager";
99
import { getServerSpec } from "./api/getServerSpec";
10+
import { getAccountFromParts } from "./extension";
1011

1112
export interface IServerSession {
1213
serverName: string;
@@ -221,16 +222,17 @@ async function resolveCredentials(serverSpec: IServerSpec) {
221222
// This arises if setting says to use authentication provider
222223
if (typeof serverSpec.password === "undefined") {
223224
const scopes = [serverSpec.name, (serverSpec.username || "").toLowerCase()];
225+
const account = getAccountFromParts(serverSpec.name, serverSpec.username);
224226
let session = await vscode.authentication.getSession(
225227
AUTHENTICATION_PROVIDER,
226228
scopes,
227-
{ silent: true },
229+
{ silent: true, account },
228230
);
229231
if (!session) {
230232
session = await vscode.authentication.getSession(
231233
AUTHENTICATION_PROVIDER,
232234
scopes,
233-
{ createIfNone: true },
235+
{ createIfNone: true, account },
234236
);
235237
}
236238
if (session) {

0 commit comments

Comments
 (0)