Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid redundant prompt to sign into edit sessions #158427

Merged
merged 2 commits into from Aug 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -14,13 +14,15 @@ import { IProductService } from 'vs/platform/product/common/productService';
import { IQuickInputService, IQuickPickItem, IQuickPickSeparator } from 'vs/platform/quickinput/common/quickInput';
import { IRequestService } from 'vs/platform/request/common/request';
import { IStorageService, IStorageValueChangeEvent, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
import { createSyncHeaders, IAuthenticationProvider, IResourceRefHandle } from 'vs/platform/userDataSync/common/userDataSync';
import { createSyncHeaders, IAuthenticationProvider, IResourceRefHandle, IUserDataSyncEnablementService } from 'vs/platform/userDataSync/common/userDataSync';
import { UserDataSyncStoreClient } from 'vs/platform/userDataSync/common/userDataSyncStoreService';
import { AuthenticationSession, AuthenticationSessionsChangeEvent, IAuthenticationService } from 'vs/workbench/services/authentication/common/authentication';
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
import { EDIT_SESSIONS_SIGNED_IN, EditSession, EDIT_SESSION_SYNC_CATEGORY, IEditSessionsWorkbenchService, EDIT_SESSIONS_SIGNED_IN_KEY, IEditSessionsLogService } from 'vs/workbench/contrib/editSessions/common/editSessions';
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { generateUuid } from 'vs/base/common/uuid';
import { ICredentialsService } from 'vs/platform/credentials/common/credentials';
import { getCurrentAuthenticationSessionInfo } from 'vs/workbench/services/authentication/browser/authenticationService';

type ExistingSession = IQuickPickItem & { session: AuthenticationSession & { providerId: string } };
type AuthenticationProviderOption = IQuickPickItem & { provider: IAuthenticationProvider };
Expand Down Expand Up @@ -50,6 +52,8 @@ export class EditSessionsWorkbenchService extends Disposable implements IEditSes
@IContextKeyService private readonly contextKeyService: IContextKeyService,
@IRequestService private readonly requestService: IRequestService,
@IDialogService private readonly dialogService: IDialogService,
@ICredentialsService private readonly credentialsService: ICredentialsService,
@IUserDataSyncEnablementService private readonly userDataSyncEnablementService: IUserDataSyncEnablementService,
) {
super();

Expand Down Expand Up @@ -173,30 +177,45 @@ export class EditSessionsWorkbenchService extends Disposable implements IEditSes
return true;
}

const authenticationSession = await this.getAuthenticationSession();
if (authenticationSession !== undefined) {
joyceerhl marked this conversation as resolved.
Show resolved Hide resolved
this.#authenticationInfo = authenticationSession;
this.storeClient.setAuthToken(authenticationSession.token, authenticationSession.providerId);
}

return authenticationSession !== undefined;
}

private async getAuthenticationSession() {
// If the user signed in previously and the session is still available, reuse that without prompting the user again
const existingSessionId = this.existingSessionId;
if (existingSessionId) {
this.logService.trace(`Searching for existing authentication session with ID ${existingSessionId}`);
const existing = await this.getExistingSession();
if (existing !== undefined) {
this.logService.trace(`Found existing authentication session with ID ${existingSessionId}`);
this.#authenticationInfo = { sessionId: existing.session.id, token: existing.session.idToken ?? existing.session.accessToken, providerId: existing.session.providerId };
this.storeClient.setAuthToken(this.#authenticationInfo.token, this.#authenticationInfo.providerId);
return true;
if (this.existingSessionId) {
meganrogge marked this conversation as resolved.
Show resolved Hide resolved
this.logService.trace(`Searching for existing authentication session with ID ${this.existingSessionId}`);
const existingSession = await this.getExistingSession();
if (existingSession) {
this.logService.trace(`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 };
}
}

// If settings sync is already enabled, avoid asking again to authenticate
if (this.userDataSyncEnablementService.isEnabled()) {
this.logService.trace(`Reusing user data sync enablement`);
const authenticationSessionInfo = await getCurrentAuthenticationSessionInfo(this.credentialsService, this.productService);
if (authenticationSessionInfo !== undefined) {
meganrogge marked this conversation as resolved.
Show resolved Hide resolved
this.logService.trace(`Using current authentication session with ID ${authenticationSessionInfo.id}`);
this.existingSessionId = authenticationSessionInfo.id;
return { sessionId: authenticationSessionInfo.id, token: authenticationSessionInfo.accessToken, providerId: authenticationSessionInfo.providerId };
}
}

// Ask the user to pick a preferred account
const session = await this.getAccountPreference();
if (session !== undefined) {
this.#authenticationInfo = { sessionId: session.id, token: session.idToken ?? session.accessToken, providerId: session.providerId };
this.storeClient.setAuthToken(this.#authenticationInfo.token, this.#authenticationInfo.providerId);
this.existingSessionId = session.id;
this.logService.trace(`Saving authentication session preference for ID ${session.id}.`);
return true;
const authenticationSession = await this.getAccountPreference();
if (authenticationSession !== undefined) {
this.existingSessionId = authenticationSession.id;
return { sessionId: authenticationSession.id, token: authenticationSession.idToken ?? authenticationSession.accessToken, providerId: authenticationSession.providerId };
meganrogge marked this conversation as resolved.
Show resolved Hide resolved
}

return false;
return undefined;
}

/**
Expand Down Expand Up @@ -310,6 +329,7 @@ export class EditSessionsWorkbenchService extends Disposable implements IEditSes
}

private set existingSessionId(sessionId: string | undefined) {
this.logService.trace(`Saving authentication session preference for ID ${sessionId}.`);
if (sessionId === undefined) {
this.storageService.remove(EditSessionsWorkbenchService.CACHED_SESSION_STORAGE_KEY, StorageScope.APPLICATION);
} else {
Expand Down