@@ -19,6 +19,7 @@ import { IAuthenticationUsageService } from 'vs/workbench/services/authenticatio
1919import { getAuthenticationProviderActivationEvent } from 'vs/workbench/services/authentication/browser/authenticationService' ;
2020import { URI , UriComponents } from 'vs/base/common/uri' ;
2121import { IOpenerService } from 'vs/platform/opener/common/opener' ;
22+ import { CancellationError } from 'vs/base/common/errors' ;
2223
2324interface AuthenticationForceNewSessionOptions {
2425 detail ?: string ;
@@ -159,6 +160,31 @@ export class MainThreadAuthentication extends Disposable implements MainThreadAu
159160 return result ?? false ;
160161 }
161162
163+ private async continueWithIncorrectAccountPrompt ( chosenAccountLabel : string , requestedAccountLabel : string ) : Promise < boolean > {
164+ const result = await this . dialogService . prompt ( {
165+ message : nls . localize ( 'incorrectAccount' , "Incorrect account detected" ) ,
166+ detail : nls . localize ( 'incorrectAccountDetail' , "The chosen account, {0}, does not match the requested account, {1}." , chosenAccountLabel , requestedAccountLabel ) ,
167+ type : Severity . Warning ,
168+ cancelButton : true ,
169+ buttons : [
170+ {
171+ label : nls . localize ( 'keep' , 'Keep {0}' , chosenAccountLabel ) ,
172+ run : ( ) => chosenAccountLabel
173+ } ,
174+ {
175+ label : nls . localize ( 'loginWith' , 'Login with {0}' , requestedAccountLabel ) ,
176+ run : ( ) => requestedAccountLabel
177+ }
178+ ] ,
179+ } ) ;
180+
181+ if ( ! result . result ) {
182+ throw new CancellationError ( ) ;
183+ }
184+
185+ return result . result === chosenAccountLabel ;
186+ }
187+
162188 private async doGetSession ( providerId : string , scopes : string [ ] , extensionId : string , extensionName : string , options : AuthenticationGetSessionOptions ) : Promise < AuthenticationSession | undefined > {
163189 const sessions = await this . authenticationService . getSessions ( providerId , scopes , options . account , true ) ;
164190 const provider = this . authenticationService . getProvider ( providerId ) ;
@@ -212,18 +238,25 @@ export class MainThreadAuthentication extends Disposable implements MainThreadAu
212238 throw new Error ( 'User did not consent to login.' ) ;
213239 }
214240
215- let session ;
241+ let session : AuthenticationSession ;
216242 if ( sessions ?. length && ! options . forceNewSession ) {
217243 session = provider . supportsMultipleAccounts && ! options . account
218244 ? await this . authenticationExtensionsService . selectSession ( providerId , extensionId , extensionName , scopes , sessions )
219245 : sessions [ 0 ] ;
220246 } else {
221- let account : AuthenticationSessionAccount | undefined = options . account ;
222- if ( ! account ) {
247+ let accountToCreate : AuthenticationSessionAccount | undefined = options . account ;
248+ if ( ! accountToCreate ) {
223249 const sessionIdToRecreate = this . authenticationExtensionsService . getSessionPreference ( providerId , extensionId , scopes ) ;
224- account = sessionIdToRecreate ? sessions . find ( session => session . id === sessionIdToRecreate ) ?. account : undefined ;
250+ accountToCreate = sessionIdToRecreate ? sessions . find ( session => session . id === sessionIdToRecreate ) ?. account : undefined ;
225251 }
226- session = await this . authenticationService . createSession ( providerId , scopes , { activateImmediate : true , account } ) ;
252+
253+ do {
254+ session = await this . authenticationService . createSession ( providerId , scopes , { activateImmediate : true , account : accountToCreate } ) ;
255+ } while (
256+ accountToCreate
257+ && accountToCreate . label !== session . account . label
258+ && ! await this . continueWithIncorrectAccountPrompt ( session . account . label , accountToCreate . label )
259+ ) ;
227260 }
228261
229262 this . authenticationAccessService . updateAllowedExtensions ( providerId , session . account . label , [ { id : extensionId , name : extensionName , allowed : true } ] ) ;
0 commit comments