Skip to content

Allow prompt for username if unauthenticated access fails #1372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 46 additions & 12 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ import {
isImportableLocalFile,
workspaceFolderOfUri,
uriOfWorkspaceFolder,
isUnauthenticated,
} from "./utils";
import { ObjectScriptDiagnosticProvider } from "./providers/ObjectScriptDiagnosticProvider";
import { DocumentRangeFormattingEditProvider } from "./providers/DocumentRangeFormattingEditProvider";
Expand Down Expand Up @@ -220,18 +221,16 @@ export async function resolveConnectionSpec(serverName: string): Promise<void> {
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export async function resolvePassword(serverSpec): Promise<void> {
export async function resolvePassword(serverSpec, ignoreUnauthenticated = false): Promise<void> {
const AUTHENTICATION_PROVIDER = "intersystems-server-credentials";
// This arises if setting says to use authentication provider
if (
// Connection isn't unauthenticated
serverSpec.username != undefined &&
serverSpec.username != "" &&
serverSpec.username.toLowerCase() != "unknownuser" &&
(!isUnauthenticated(serverSpec.username) || ignoreUnauthenticated) &&
// A password is missing
typeof serverSpec.password == "undefined"
) {
const scopes = [serverSpec.name, serverSpec.username];
const scopes = [serverSpec.name, serverSpec.username || ""];
let session = await vscode.authentication.getSession(AUTHENTICATION_PROVIDER, scopes, { silent: true });
if (!session) {
session = await vscode.authentication.getSession(AUTHENTICATION_PROVIDER, scopes, { createIfNone: true });
Expand Down Expand Up @@ -392,10 +391,47 @@ export async function checkConnection(
message = "Not Authorized.";
errorMessage = `Authorization error: Check your credentials in Settings, and that you have sufficient privileges on the /api/atelier web application on ${connInfo}`;
const username = api.config.username;
if (username === "") {
vscode.window.showErrorMessage(`Anonymous access rejected by ${connInfo}.`);
if (!api.externalServer) {
vscode.window.showErrorMessage("Connection has been disabled.");
if (isUnauthenticated(username)) {
vscode.window.showErrorMessage(
`Unauthenticated access rejected by '${api.serverId}'.${
!api.externalServer ? " Connection has been disabled." : ""
}`,
"Dismiss"
);
if (api.externalServer) {
// Attempt to resolve a username and password
const newSpec: { name: string; username?: string; password?: string } = {
name: api.config.serverName,
username,
};
await resolvePassword(newSpec, true);
if (newSpec.password) {
// Update the connection spec and try again
await workspaceState.update(wsKey + ":password", newSpec.password);
resolvedConnSpecs.set(api.config.serverName, {
...resolvedConnSpecs.get(api.config.serverName),
username: newSpec.username,
password: newSpec.password,
});
api = new AtelierAPI(apiTarget, false);
await api
.serverInfo()
.then(async (info) => {
await gotServerInfo(info);
_onDidChangeConnection.fire();
success = true;
})
.catch(async (error) => {
console.log(`Second connect failed: ${error}`);
await setConnectionState(configName, false);
await workspaceState.update(wsKey + ":password", undefined);
success = false;
})
.finally(() => {
checkingConnection = false;
});
}
} else {
await setConnectionState(configName, false);
}
} else {
Expand Down Expand Up @@ -440,10 +476,8 @@ export async function checkConnection(
}
);
});
if (success) {
return;
}
}
if (success) return;
} else {
errorMessage = `${message}\nCheck your server details in Settings (${connInfo}).`;
}
Expand Down
5 changes: 5 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,11 @@ export function methodOffsetToLine(
return line;
}

/** Return `true` if this username signals unauthenticated access */
export function isUnauthenticated(username: string): boolean {
return username == undefined || username == "" || username.toLowerCase() == "unknownuser";
}

// ---------------------------------------------------------------------
// Source: https://github.com/amsterdamharu/lib/blob/master/src/index.js

Expand Down