@@ -19,18 +19,15 @@ import { type SecretsManager } from "./core/secretsManager";
1919import { CertificateError } from "./error" ;
2020import { getGlobalFlags } from "./globalFlags" ;
2121import { type Logger } from "./logging/logger" ;
22- import { OAuthMetadataClient } from "./oauth/metadataClient" ;
2322import { type OAuthSessionManager } from "./oauth/sessionManager" ;
24- import { maybeAskAgent , maybeAskUrl } from "./promptUtils" ;
23+ import { maybeAskAgent , maybeAskUrl , maybeAskAuthMethod } from "./promptUtils" ;
2524import { escapeCommandArg , toRemoteAuthority , toSafeHost } from "./util" ;
2625import {
2726 AgentTreeItem ,
2827 type OpenableTreeItem ,
2928 WorkspaceTreeItem ,
3029} from "./workspace/workspacesProvider" ;
3130
32- type AuthMethod = "oauth" | "legacy" ;
33-
3431export class Commands {
3532 private readonly vscodeProposed : typeof vscode ;
3633 private readonly logger : Logger ;
@@ -92,7 +89,7 @@ export class Commands {
9289 // Try to get a token from the user, if we need one, and their user.
9390 const autoLogin = args ?. autoLogin === true ;
9491
95- const res = await this . maybeAskToken ( url , args ?. token , autoLogin ) ;
92+ const res = await this . attemptLogin ( url , args ?. token , autoLogin ) ;
9693 if ( ! res ) {
9794 return ; // The user aborted, or unable to auth.
9895 }
@@ -136,12 +133,12 @@ export class Commands {
136133 }
137134
138135 /**
139- * If necessary, ask for a token, and keep asking until the token has been
140- * validated. Return the token and user that was fetched to validate the
141- * token. Null means the user aborted or we were unable to authenticate with
142- * mTLS (in the latter case, an error notification will have been displayed).
136+ * Attempt to authenticate using OAuth, token, or mTLS. If necessary, prompts
137+ * for authentication method and credentials. Returns the token and user upon
138+ * successful authentication. Null means the user aborted or authentication
139+ * failed (in which case an error notification will have been displayed).
143140 */
144- private async maybeAskToken (
141+ private async attemptLogin (
145142 url : string ,
146143 token : string | undefined ,
147144 isAutoLogin : boolean ,
@@ -174,58 +171,18 @@ export class Commands {
174171 }
175172 }
176173
177- // Check if server supports OAuth
178- const supportsOAuth = await this . checkOAuthSupport ( client ) ;
179-
180- let choice : AuthMethod | undefined = "legacy" ;
181- if ( supportsOAuth ) {
182- choice = await this . askAuthMethod ( ) ;
183- }
184-
185- if ( choice === "oauth" ) {
186- return this . loginWithOAuth ( client ) ;
187- } else if ( choice === "legacy" ) {
188- const initialToken =
189- token || ( await this . secretsManager . getSessionToken ( ) ) ;
190- return this . loginWithToken ( client , initialToken ) ;
174+ const authMethod = await maybeAskAuthMethod ( client ) ;
175+ switch ( authMethod ) {
176+ case "oauth" :
177+ return this . loginWithOAuth ( client ) ;
178+ case "legacy" : {
179+ const initialToken =
180+ token || ( await this . secretsManager . getSessionToken ( ) ) ;
181+ return this . loginWithToken ( client , initialToken ) ;
182+ }
183+ case undefined :
184+ return null ; // User aborted
191185 }
192-
193- return null ; // User aborted.
194- }
195-
196- private async checkOAuthSupport ( client : CoderApi ) : Promise < boolean > {
197- const metadataClient = new OAuthMetadataClient (
198- client . getAxiosInstance ( ) ,
199- this . logger ,
200- ) ;
201- return metadataClient . checkOAuthSupport ( ) ;
202- }
203-
204- /**
205- * Ask user to choose between OAuth and legacy API token authentication.
206- */
207- private async askAuthMethod ( ) : Promise < AuthMethod | undefined > {
208- const choice = await vscode . window . showQuickPick (
209- [
210- {
211- label : "$(key) OAuth (Recommended)" ,
212- detail : "Secure authentication with automatic token refresh" ,
213- value : "oauth" as const ,
214- } ,
215- {
216- label : "$(lock) API Token" ,
217- detail : "Use a manually created API key" ,
218- value : "legacy" as const ,
219- } ,
220- ] ,
221- {
222- title : "Choose Authentication Method" ,
223- placeHolder : "How would you like to authenticate?" ,
224- ignoreFocusOut : true ,
225- } ,
226- ) ;
227-
228- return choice ?. value ;
229186 }
230187
231188 private async loginWithToken (
@@ -297,7 +254,15 @@ export class Commands {
297254 try {
298255 this . logger . info ( "Starting OAuth authentication" ) ;
299256
300- const tokenResponse = await this . oauthSessionManager . login ( client ) ;
257+ const tokenResponse = await vscode . window . withProgress (
258+ {
259+ location : vscode . ProgressLocation . Notification ,
260+ title : "Authenticating" ,
261+ cancellable : false ,
262+ } ,
263+ async ( progress ) =>
264+ await this . oauthSessionManager . login ( client , progress ) ,
265+ ) ;
301266
302267 // Validate token by fetching user
303268 client . setSessionToken ( tokenResponse . access_token ) ;
0 commit comments