Skip to content

Commit 5f4b674

Browse files
committed
Simplify the OAuth flow
1 parent 02ac993 commit 5f4b674

File tree

3 files changed

+189
-234
lines changed

3 files changed

+189
-234
lines changed

src/commands.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,11 @@ export class Commands {
305305
}
306306

307307
if (choice === "oauth") {
308-
return this.loginWithOAuth(url, client);
308+
return this.loginWithOAuth(client);
309309
} else if (choice === "legacy") {
310-
return this.loginWithToken(url, token, client);
310+
const initialToken =
311+
token || (await this.secretsManager.getSessionToken());
312+
return this.loginWithToken(client, initialToken);
311313
}
312314

313315
// User aborted.
@@ -350,10 +352,13 @@ export class Commands {
350352
}
351353

352354
private async loginWithToken(
353-
url: string,
354-
token: string | undefined,
355355
client: CoderApi,
356+
initialToken: string | undefined,
356357
): Promise<{ user: User; token: string } | null> {
358+
const url = client.getAxiosInstance().defaults.baseURL;
359+
if (!url) {
360+
throw new Error("No base URL set on REST client");
361+
}
357362
// This prompt is for convenience; do not error if they close it since
358363
// they may already have a token or already have the page opened.
359364
await vscode.env.openExternal(vscode.Uri.parse(`${url}/cli-auth`));
@@ -366,7 +371,7 @@ export class Commands {
366371
title: "Coder API Key",
367372
password: true,
368373
placeHolder: "Paste your API key.",
369-
value: token || (await this.secretsManager.getSessionToken()),
374+
value: initialToken,
370375
ignoreFocusOut: true,
371376
validateInput: async (value) => {
372377
if (!value) {
@@ -410,29 +415,17 @@ export class Commands {
410415
* Returns the access token and authenticated user, or null if failed/cancelled.
411416
*/
412417
private async loginWithOAuth(
413-
url: string,
414418
client: CoderApi,
415419
): Promise<{ user: User; token: string } | null> {
416420
try {
417421
this.logger.info("Starting OAuth authentication");
418422

419-
// Start OAuth authorization flow
420-
// TODO just pass the client here and do all the neccessary steps (If we are already logged in we'd have the right token and the OAuth client registration saved).
421-
const { code, verifier } =
422-
await this.oauthSessionManager.startAuthorization(url);
423-
424-
// Exchange authorization code for tokens
425-
const tokenResponse = await this.oauthSessionManager.exchangeToken(
426-
code,
427-
verifier,
428-
);
423+
const tokenResponse = await this.oauthSessionManager.login(client);
429424

430425
// Validate token by fetching user
431426
client.setSessionToken(tokenResponse.access_token);
432427
const user = await client.getAuthenticatedUser();
433428

434-
this.logger.info("OAuth authentication successful");
435-
436429
return {
437430
token: tokenResponse.access_token,
438431
user,
@@ -491,9 +484,9 @@ export class Commands {
491484
this.logger.info("Logging out");
492485

493486
// Check if using OAuth
494-
// TODO maybe just add this check inside oauthSessionManager
495-
const hasOAuthTokens = await this.secretsManager.getOAuthTokens();
496-
if (hasOAuthTokens) {
487+
const isOAuthLoggedIn =
488+
await this.oauthSessionManager.isLoggedInWithOAuth();
489+
if (isOAuthLoggedIn) {
497490
this.logger.info("Logging out via OAuth");
498491
try {
499492
await this.oauthSessionManager.logout();

src/oauth/clientRegistry.ts

Lines changed: 0 additions & 111 deletions
This file was deleted.

0 commit comments

Comments
 (0)