- Notifications
You must be signed in to change notification settings - Fork34
Improve consistency in client-side login/logout experience#590
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
2b6286f
f933177
df63df6
edb505c
21523b1
27569d2
fe96eb5
12c7c98
2aa197c
820cb1c
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -12,6 +12,7 @@ import { CoderApi } from "./api/coderApi"; | ||
import { needToken } from "./api/utils"; | ||
import { type CliManager } from "./core/cliManager"; | ||
import { type ServiceContainer } from "./core/container"; | ||
import { type ContextManager } from "./core/contextManager"; | ||
import { type MementoManager } from "./core/mementoManager"; | ||
import { type PathResolver } from "./core/pathResolver"; | ||
import { type SecretsManager } from "./core/secretsManager"; | ||
@@ -32,6 +33,7 @@ export class Commands { | ||
private readonly mementoManager: MementoManager; | ||
private readonly secretsManager: SecretsManager; | ||
private readonly cliManager: CliManager; | ||
private readonly contextManager: ContextManager; | ||
// These will only be populated when actively connected to a workspace and are | ||
// used in commands. Because commands can be executed by the user, it is not | ||
// possible to pass in arguments, so we have to store the current workspace | ||
@@ -53,6 +55,7 @@ export class Commands { | ||
this.mementoManager = serviceContainer.getMementoManager(); | ||
this.secretsManager = serviceContainer.getSecretsManager(); | ||
this.cliManager = serviceContainer.getCliManager(); | ||
this.contextManager = serviceContainer.getContextManager(); | ||
} | ||
/** | ||
@@ -179,31 +182,34 @@ export class Commands { | ||
} | ||
/** | ||
* Log into the provided deployment. If the deployment URL is not specified, | ||
* ask for it first with a menu showing recent URLs along with the default URL | ||
* and CODER_URL, if those are set. | ||
*/ | ||
public async login(args?: { | ||
url?: string; | ||
token?: string; | ||
label?: string; | ||
autoLogin?: boolean; | ||
}): Promise<void> { | ||
if (this.contextManager.get("coder.authenticated")) { | ||
return; | ||
} | ||
this.logger.info("Logging in"); | ||
const url = await this.maybeAskUrl(args?.url); | ||
if (!url) { | ||
return; // The user aborted. | ||
} | ||
// It is possible that we are trying to log into an old-style host, in which | ||
// case we want to write with the provided blank label instead of generating | ||
// a host label. | ||
const label = args?.label === undefined ? toSafeHost(url) : args.label; | ||
// Try to get a token from the user, if we need one, and their user. | ||
const autoLogin = args?.autoLogin === true; | ||
const res = await this.maybeAskToken(url, args?.token, autoLogin); | ||
if (!res) { | ||
return; // The user aborted, or unable to auth. | ||
} | ||
@@ -221,13 +227,9 @@ export class Commands { | ||
await this.cliManager.configure(label, url, res.token); | ||
// These contexts control various menu items and the sidebar. | ||
this.contextManager.set("coder.authenticated", true); | ||
if (res.user.roles.find((role) => role.name === "owner")) { | ||
this.contextManager.set("coder.isOwner", true); | ||
EhabY marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
vscode.window | ||
@@ -245,6 +247,7 @@ export class Commands { | ||
} | ||
}); | ||
await this.secretsManager.triggerLoginStateChange("login"); | ||
// Fetch workspaces for the new deployment. | ||
vscode.commands.executeCommand("coder.refreshWorkspaces"); | ||
} | ||
@@ -257,19 +260,21 @@ export class Commands { | ||
*/ | ||
private async maybeAskToken( | ||
url: string, | ||
token: string | undefined, | ||
isAutoLogin: boolean, | ||
): Promise<{ user: User; token: string } | null> { | ||
const client = CoderApi.create(url, token, this.logger); | ||
const needsToken = needToken(vscode.workspace.getConfiguration()); | ||
if (!needsToken || token) { | ||
try { | ||
const user = await client.getAuthenticatedUser(); | ||
// For non-token auth, we write a blank token since the `vscodessh` | ||
// command currently always requires a token file. | ||
// For token auth, we have valid access so we can just return the user here | ||
return { token: needsToken && token ? token : "", user }; | ||
} catch (err) { | ||
const message = getErrorMessage(err, "no response from the server"); | ||
if (isAutoLogin) { | ||
this.logger.warn("Failed to log in to Coder server:", message); | ||
} else { | ||
this.vscodeProposed.window.showErrorMessage( | ||
@@ -301,6 +306,9 @@ export class Commands { | ||
value: token || (await this.secretsManager.getSessionToken()), | ||
ignoreFocusOut: true, | ||
validateInput: async (value) => { | ||
if (!value) { | ||
return null; | ||
} | ||
client.setSessionToken(value); | ||
try { | ||
user = await client.getAuthenticatedUser(); | ||
Comment on lines +309 to 314 CollaboratorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Should we attempt to get the authenticated user when the | ||
@@ -369,7 +377,14 @@ export class Commands { | ||
// Sanity check; command should not be available if no url. | ||
throw new Error("You are not logged in"); | ||
} | ||
await this.forceLogout(); | ||
} | ||
public async forceLogout(): Promise<void> { | ||
if (!this.contextManager.get("coder.authenticated")) { | ||
return; | ||
} | ||
this.logger.info("Logging out"); | ||
// Clear from the REST client. An empty url will indicate to other parts of | ||
// the code that we are logged out. | ||
this.restClient.setHost(""); | ||
@@ -379,19 +394,16 @@ export class Commands { | ||
await this.mementoManager.setUrl(undefined); | ||
await this.secretsManager.setSessionToken(undefined); | ||
this.contextManager.set("coder.authenticated", false); | ||
vscode.window | ||
.showInformationMessage("You've been logged out of Coder!", "Login") | ||
.then((action) => { | ||
if (action === "Login") { | ||
this.login(); | ||
} | ||
}); | ||
await this.secretsManager.triggerLoginStateChange("logout"); | ||
// This will result in clearing the workspace list. | ||
vscode.commands.executeCommand("coder.refreshWorkspaces"); | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import*asvscodefrom"vscode"; | ||
constCONTEXT_DEFAULTS={ | ||
"coder.authenticated":false, | ||
"coder.isOwner":false, | ||
"coder.loaded":false, | ||
"coder.workspace.updatable":false, | ||
}asconst; | ||
typeCoderContext=keyoftypeofCONTEXT_DEFAULTS; | ||
exportclassContextManagerimplementsvscode.Disposable{ | ||
privatereadonlycontext=newMap<CoderContext,boolean>(); | ||
publicconstructor(){ | ||
(Object.keys(CONTEXT_DEFAULTS)asCoderContext[]).forEach((key)=>{ | ||
this.set(key,CONTEXT_DEFAULTS[key]); | ||
}); | ||
} | ||
publicset(key:CoderContext,value:boolean):void{ | ||
this.context.set(key,value); | ||
vscode.commands.executeCommand("setContext",key,value); | ||
} | ||
publicget(key:CoderContext):boolean{ | ||
returnthis.context.get(key)??CONTEXT_DEFAULTS[key]; | ||
EhabY marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
publicdispose(){ | ||
this.context.clear(); | ||
} | ||
} |
Uh oh!
There was an error while loading.Please reload this page.