Skip to content

Commit 833e936

Browse files
committed
Fix sonar issues + Writing to storage when connecting
1 parent 3e12008 commit 833e936

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

src/commands.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,8 @@ export class Commands {
111111
return;
112112
}
113113

114-
if (!result.user) {
115-
// Login might have happened in another process/window so we do not have the user yet.
116-
result.user = await this.extensionClient.getAuthenticatedUser();
117-
}
114+
// Login might have happened in another process/window so we do not have the user yet.
115+
result.user ??= await this.extensionClient.getAuthenticatedUser();
118116

119117
await this.deploymentManager.setDeployment({
120118
url,
@@ -137,6 +135,7 @@ export class Commands {
137135
vscode.commands.executeCommand("coder.open");
138136
}
139137
});
138+
this.logger.debug("Login complete to deployment:", url);
140139
}
141140

142141
/**
@@ -157,7 +156,7 @@ export class Commands {
157156
// Sort explicitly since fs.readdir order is platform-dependent
158157
const logFiles = files
159158
.filter((f) => f.endsWith(".log"))
160-
.sort()
159+
.sort((a, b) => a.localeCompare(b))
161160
.reverse();
162161

163162
if (logFiles.length === 0) {
@@ -220,6 +219,8 @@ export class Commands {
220219
this.login();
221220
}
222221
});
222+
223+
this.logger.debug("Logout complete");
223224
}
224225

225226
/**

src/deployment/deploymentManager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ export class DeploymentManager implements vscode.Disposable {
119119
this.#deployment = { ...deployment };
120120

121121
// Updates client credentials
122-
if (deployment.token !== undefined) {
123-
this.client.setCredentials(deployment.url, deployment.token);
124-
} else {
122+
if (deployment.token === undefined) {
125123
this.client.setHost(deployment.url);
124+
} else {
125+
this.client.setCredentials(deployment.url, deployment.token);
126126
}
127127

128128
this.registerAuthListener();

src/extension.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import * as vscode from "vscode";
88

99
import { errToStr } from "./api/api-helper";
1010
import { CoderApi } from "./api/coderApi";
11-
import { needToken } from "./api/utils";
1211
import { Commands } from "./commands";
1312
import { ServiceContainer } from "./core/container";
1413
import { type SecretsManager } from "./core/secretsManager";
@@ -449,15 +448,14 @@ async function setupDeploymentFromUri(
449448

450449
// If the token is missing we will get a 401 later and the user will be
451450
// prompted to sign in again, so we do not need to ensure it is set now.
452-
// For non-token auth, we write a blank token since the `vscodessh`
453-
// command currently always requires a token file. However, if there is
454-
// a query parameter for non-token auth go ahead and use it anyway;
455-
let token: string | undefined = params.get("token") ?? undefined;
456-
if (token === undefined) {
457-
if (needToken(vscode.workspace.getConfiguration())) {
458-
token = (await secretsManager.getSessionAuth(safeHost))?.token;
459-
} else {
460-
token = "";
451+
const token: string | null = params.get("token");
452+
if (token === null) {
453+
// We need to ensure there is at least an entry for this in storage
454+
// so that we know what URL to prompt the user with when logging in.
455+
const auth = await secretsManager.getSessionAuth(safeHost);
456+
if (!auth) {
457+
// Racy, we could accidentally overwrite the token that is written in the meantime.
458+
await secretsManager.setSessionAuth(safeHost, { url, token: "" });
461459
}
462460
} else {
463461
await secretsManager.setSessionAuth(safeHost, { url, token });

0 commit comments

Comments
 (0)