Skip to content

Commit

Permalink
Don't prompt for auth if the auth session that the user previously us…
Browse files Browse the repository at this point in the history
…ed for edit sessions is no longer available
  • Loading branch information
joyceerhl committed Sep 28, 2022
1 parent e1730b2 commit 689cfc7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -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();

Expand All @@ -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);
Expand Down Expand Up @@ -239,14 +244,16 @@ 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}`);
const existingSession = await this.getExistingSession();
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();
}
}

Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/contrib/editSessions/common/editSessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 689cfc7

Please sign in to comment.