Skip to content

Commit

Permalink
Add getAccount to API, and use it internally to fix overprompting
Browse files Browse the repository at this point in the history
  • Loading branch information
gjsjohnmurray committed Nov 12, 2024
1 parent b62ed04 commit c225dc6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/authenticationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
authentication,
AuthenticationProvider,
AuthenticationProviderAuthenticationSessionsChangeEvent,
AuthenticationProviderSessionOptions,
AuthenticationSession,
Disposable,
Event,
Expand Down Expand Up @@ -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<readonly AuthenticationSession[]> {
public async getSessions(scopes: readonly string[] = [], options: AuthenticationProviderSessionOptions): Promise<AuthenticationSession[]> {
await this._ensureInitialized();
let sessions = this._sessions;

Expand All @@ -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 = [];
Expand Down
12 changes: 11 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<string> {
return _onDidChangePassword.event;
Expand Down
6 changes: 4 additions & 2 deletions src/makeRESTRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c225dc6

Please sign in to comment.