Skip to content

Commit ae8cfb6

Browse files
authored
Allow prompt for username if unauthenticated access fails (#1372)
1 parent 31beee5 commit ae8cfb6

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

src/extension.ts

+46-12
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ import {
9797
isImportableLocalFile,
9898
workspaceFolderOfUri,
9999
uriOfWorkspaceFolder,
100+
isUnauthenticated,
100101
} from "./utils";
101102
import { ObjectScriptDiagnosticProvider } from "./providers/ObjectScriptDiagnosticProvider";
102103
import { DocumentRangeFormattingEditProvider } from "./providers/DocumentRangeFormattingEditProvider";
@@ -220,18 +221,16 @@ export async function resolveConnectionSpec(serverName: string): Promise<void> {
220221
}
221222

222223
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
223-
export async function resolvePassword(serverSpec): Promise<void> {
224+
export async function resolvePassword(serverSpec, ignoreUnauthenticated = false): Promise<void> {
224225
const AUTHENTICATION_PROVIDER = "intersystems-server-credentials";
225226
// This arises if setting says to use authentication provider
226227
if (
227228
// Connection isn't unauthenticated
228-
serverSpec.username != undefined &&
229-
serverSpec.username != "" &&
230-
serverSpec.username.toLowerCase() != "unknownuser" &&
229+
(!isUnauthenticated(serverSpec.username) || ignoreUnauthenticated) &&
231230
// A password is missing
232231
typeof serverSpec.password == "undefined"
233232
) {
234-
const scopes = [serverSpec.name, serverSpec.username];
233+
const scopes = [serverSpec.name, serverSpec.username || ""];
235234
let session = await vscode.authentication.getSession(AUTHENTICATION_PROVIDER, scopes, { silent: true });
236235
if (!session) {
237236
session = await vscode.authentication.getSession(AUTHENTICATION_PROVIDER, scopes, { createIfNone: true });
@@ -392,10 +391,47 @@ export async function checkConnection(
392391
message = "Not Authorized.";
393392
errorMessage = `Authorization error: Check your credentials in Settings, and that you have sufficient privileges on the /api/atelier web application on ${connInfo}`;
394393
const username = api.config.username;
395-
if (username === "") {
396-
vscode.window.showErrorMessage(`Anonymous access rejected by ${connInfo}.`);
397-
if (!api.externalServer) {
398-
vscode.window.showErrorMessage("Connection has been disabled.");
394+
if (isUnauthenticated(username)) {
395+
vscode.window.showErrorMessage(
396+
`Unauthenticated access rejected by '${api.serverId}'.${
397+
!api.externalServer ? " Connection has been disabled." : ""
398+
}`,
399+
"Dismiss"
400+
);
401+
if (api.externalServer) {
402+
// Attempt to resolve a username and password
403+
const newSpec: { name: string; username?: string; password?: string } = {
404+
name: api.config.serverName,
405+
username,
406+
};
407+
await resolvePassword(newSpec, true);
408+
if (newSpec.password) {
409+
// Update the connection spec and try again
410+
await workspaceState.update(wsKey + ":password", newSpec.password);
411+
resolvedConnSpecs.set(api.config.serverName, {
412+
...resolvedConnSpecs.get(api.config.serverName),
413+
username: newSpec.username,
414+
password: newSpec.password,
415+
});
416+
api = new AtelierAPI(apiTarget, false);
417+
await api
418+
.serverInfo()
419+
.then(async (info) => {
420+
await gotServerInfo(info);
421+
_onDidChangeConnection.fire();
422+
success = true;
423+
})
424+
.catch(async (error) => {
425+
console.log(`Second connect failed: ${error}`);
426+
await setConnectionState(configName, false);
427+
await workspaceState.update(wsKey + ":password", undefined);
428+
success = false;
429+
})
430+
.finally(() => {
431+
checkingConnection = false;
432+
});
433+
}
434+
} else {
399435
await setConnectionState(configName, false);
400436
}
401437
} else {
@@ -440,10 +476,8 @@ export async function checkConnection(
440476
}
441477
);
442478
});
443-
if (success) {
444-
return;
445-
}
446479
}
480+
if (success) return;
447481
} else {
448482
errorMessage = `${message}\nCheck your server details in Settings (${connInfo}).`;
449483
}

src/utils/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,11 @@ export function methodOffsetToLine(
699699
return line;
700700
}
701701

702+
/** Return `true` if this username signals unauthenticated access */
703+
export function isUnauthenticated(username: string): boolean {
704+
return username == undefined || username == "" || username.toLowerCase() == "unknownuser";
705+
}
706+
702707
// ---------------------------------------------------------------------
703708
// Source: https://github.com/amsterdamharu/lib/blob/master/src/index.js
704709

0 commit comments

Comments
 (0)