Don't prompt for auth if the auth session that the user previously used for edit sessions is no longer available

This commit is contained in:
Joyce Er 2022-09-27 20:33:09 -07:00
parent e1730b2f26
commit 689cfc73d3
No known key found for this signature in database
GPG key ID: A15311FAE9EC8CD6
4 changed files with 26 additions and 6 deletions

View file

@ -131,6 +131,7 @@ export class EditSessionsContribution extends Disposable implements IWorkbenchCo
this._register(this.fileService.registerProvider(EditSessionsFileSystemProvider.SCHEMA, new EditSessionsFileSystemProvider(this.editSessionsStorageService)));
this.lifecycleService.onWillShutdown((e) => e.join(this.autoStoreEditSession(), { id: 'autoStoreEditSession', label: localize('autoStoreEditSession', 'Storing current edit session...') }));
this._register(this.editSessionsStorageService.onDidSignIn(() => this.updateAccountsMenuBadge()));
this._register(this.editSessionsStorageService.onDidSignOut(() => this.updateAccountsMenuBadge()));
}
private autoResumeEditSession() {
@ -378,6 +379,10 @@ export class EditSessionsContribution extends Disposable implements IWorkbenchCo
this.logService.info(ref !== undefined ? `Resuming edit session with ref ${ref}...` : 'Resuming edit session...');
if (silent && !(await this.editSessionsStorageService.initialize(false, true))) {
return;
}
const data = await this.editSessionsStorageService.read(ref);
if (!data) {
if (ref === undefined && !silent) {

View file

@ -56,6 +56,11 @@ export class EditSessionsWorkbenchService extends Disposable implements IEditSes
return this._didSignIn.event;
}
private _didSignOut = new Emitter<void>();
get onDidSignOut() {
return this._didSignOut.event;
}
constructor(
@IFileService private readonly fileService: IFileService,
@IStorageService private readonly storageService: IStorageService,
@ -162,11 +167,11 @@ export class EditSessionsWorkbenchService extends Disposable implements IEditSes
return [];
}
public async initialize(fromContinueOn: boolean) {
public async initialize(fromContinueOn: boolean, silent: boolean = false) {
if (this.initialized) {
return true;
}
this.initialized = await this.doInitialize(fromContinueOn);
this.initialized = await this.doInitialize(fromContinueOn, silent);
this.signedInContext.set(this.initialized);
if (this.initialized) {
this._didSignIn.fire();
@ -181,7 +186,7 @@ export class EditSessionsWorkbenchService extends Disposable implements IEditSes
* meaning that authentication is configured and it
* can be used to communicate with the remote storage service
*/
private async doInitialize(fromContinueOn: boolean): Promise<boolean> {
private async doInitialize(fromContinueOn: boolean, silent: boolean): Promise<boolean> {
// Wait for authentication extensions to be registered
await this.extensionService.whenInstalledExtensionsRegistered();
@ -206,7 +211,7 @@ export class EditSessionsWorkbenchService extends Disposable implements IEditSes
return true;
}
const authenticationSession = await this.getAuthenticationSession(fromContinueOn);
const authenticationSession = await this.getAuthenticationSession(fromContinueOn, silent);
if (authenticationSession !== undefined) {
this.#authenticationInfo = authenticationSession;
this.storeClient.setAuthToken(authenticationSession.token, authenticationSession.providerId);
@ -239,7 +244,7 @@ export class EditSessionsWorkbenchService extends Disposable implements IEditSes
return currentMachineId;
}
private async getAuthenticationSession(fromContinueOn: boolean) {
private async getAuthenticationSession(fromContinueOn: boolean, silent: boolean) {
// If the user signed in previously and the session is still available, reuse that without prompting the user again
if (this.existingSessionId) {
this.logService.info(`Searching for existing authentication session with ID ${this.existingSessionId}`);
@ -247,6 +252,8 @@ export class EditSessionsWorkbenchService extends Disposable implements IEditSes
if (existingSession) {
this.logService.info(`Found existing authentication session with ID ${existingSession.session.id}`);
return { sessionId: existingSession.session.id, token: existingSession.session.idToken ?? existingSession.session.accessToken, providerId: existingSession.session.providerId };
} else {
this._didSignOut.fire();
}
}
@ -261,6 +268,12 @@ export class EditSessionsWorkbenchService extends Disposable implements IEditSes
}
}
// If we aren't supposed to prompt the user because
// we're in a silent flow, just return here
if (silent) {
return;
}
// Ask the user to pick a preferred account
const authenticationSession = await this.getAccountPreference(fromContinueOn);
if (authenticationSession !== undefined) {

View file

@ -25,8 +25,9 @@ export interface IEditSessionsStorageService {
readonly isSignedIn: boolean;
readonly onDidSignIn: Event<void>;
readonly onDidSignOut: Event<void>;
initialize(fromContinueOn: boolean): Promise<boolean>;
initialize(fromContinueOn: boolean, silent?: boolean): Promise<boolean>;
read(ref: string | undefined): Promise<{ ref: string; editSession: EditSession } | undefined>;
write(editSession: EditSession): Promise<string>;
delete(ref: string | null): Promise<void>;

View file

@ -68,6 +68,7 @@ suite('Edit session sync', () => {
instantiationService.stub(INotificationService, new TestNotificationService());
instantiationService.stub(IEditSessionsStorageService, new class extends mock<IEditSessionsStorageService>() {
override onDidSignIn = Event.None;
override onDidSignOut = Event.None;
});
instantiationService.stub(IProgressService, ProgressService);
instantiationService.stub(ISCMService, SCMService);