mirror of
https://github.com/microsoft/vscode.git
synced 2026-07-10 16:58:55 -05:00
fix setting token. reset when token failed.
This commit is contained in:
@@ -53,6 +53,7 @@ export class AuthenticationTokenService extends Disposable implements IAuthentic
|
||||
}
|
||||
|
||||
sendTokenFailed(): void {
|
||||
this.setToken(undefined);
|
||||
this._onTokenFailed.fire();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,13 +29,16 @@ type UserAccountEvent = {
|
||||
id: string;
|
||||
};
|
||||
|
||||
type AccountQuickPickItem = { label: string, authenticationProvider: IAuthenticationProvider, account?: IUserDataSyncAccount, detail?: string };
|
||||
type AccountQuickPickItem = { label: string, authenticationProvider: IAuthenticationProvider, account?: UserDataSyncAccount, detail?: string };
|
||||
|
||||
export interface IUserDataSyncAccount {
|
||||
readonly authenticationProviderId: string;
|
||||
readonly sessionId: string;
|
||||
readonly accountName: string;
|
||||
readonly accountId: string;
|
||||
export class UserDataSyncAccount {
|
||||
|
||||
constructor(readonly authenticationProviderId: string, private readonly session: AuthenticationSession) { }
|
||||
|
||||
get sessionId(): string { return this.session.id; }
|
||||
get accountName(): string { return this.session.account.displayName; }
|
||||
get accountId(): string { return this.session.account.id; }
|
||||
getToken(): Thenable<string> { return this.session.getAccessToken(); }
|
||||
}
|
||||
|
||||
export const enum AccountStatus {
|
||||
@@ -61,10 +64,10 @@ export class UserDataSyncAccounts extends Disposable {
|
||||
private readonly _onDidSignOut = this._register(new Emitter<void>());
|
||||
readonly onDidSignOut = this._onDidSignOut.event;
|
||||
|
||||
private _all: Map<string, IUserDataSyncAccount[]> = new Map<string, IUserDataSyncAccount[]>();
|
||||
get all(): IUserDataSyncAccount[] { return flatten(values(this._all)); }
|
||||
private _all: Map<string, UserDataSyncAccount[]> = new Map<string, UserDataSyncAccount[]>();
|
||||
get all(): UserDataSyncAccount[] { return flatten(values(this._all)); }
|
||||
|
||||
get current(): IUserDataSyncAccount | undefined { return this.all.filter(account => this.isCurrentAccount(account))[0]; }
|
||||
get current(): UserDataSyncAccount | undefined { return this.all.filter(account => this.isCurrentAccount(account))[0]; }
|
||||
|
||||
constructor(
|
||||
@IAuthenticationService private readonly authenticationService: IAuthenticationService,
|
||||
@@ -124,19 +127,56 @@ export class UserDataSyncAccounts extends Disposable {
|
||||
}
|
||||
|
||||
private async update(): Promise<void> {
|
||||
const allAccounts: Map<string, IUserDataSyncAccount[]> = new Map<string, IUserDataSyncAccount[]>();
|
||||
const allAccounts: Map<string, UserDataSyncAccount[]> = new Map<string, UserDataSyncAccount[]>();
|
||||
for (const { id } of this.authenticationProviders) {
|
||||
const accounts = await this.getAccounts(id);
|
||||
allAccounts.set(id, accounts);
|
||||
}
|
||||
|
||||
this._all = allAccounts;
|
||||
const status = this.current ? AccountStatus.Available : AccountStatus.Unavailable;
|
||||
const current = this.current;
|
||||
await this.updateToken(current);
|
||||
this.updateStatus(current);
|
||||
}
|
||||
|
||||
if (status === AccountStatus.Unavailable) {
|
||||
await this.authenticationTokenService.setToken(undefined);
|
||||
private async getAccounts(authenticationProviderId: string): Promise<UserDataSyncAccount[]> {
|
||||
let accounts: Map<string, UserDataSyncAccount> = new Map<string, UserDataSyncAccount>();
|
||||
let currentAccount: UserDataSyncAccount | null = null;
|
||||
|
||||
const sessions = await this.authenticationService.getSessions(authenticationProviderId) || [];
|
||||
for (const session of sessions) {
|
||||
const account: UserDataSyncAccount = new UserDataSyncAccount(authenticationProviderId, session);
|
||||
accounts.set(account.accountName, account);
|
||||
if (this.isCurrentAccount(account)) {
|
||||
currentAccount = account;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentAccount) {
|
||||
// Always use current account if available
|
||||
accounts.set(currentAccount.accountName, currentAccount);
|
||||
}
|
||||
|
||||
return values(accounts);
|
||||
}
|
||||
|
||||
private async updateToken(current: UserDataSyncAccount | undefined): Promise<void> {
|
||||
let value: { token: string, authenticationProviderId: string } | undefined = undefined;
|
||||
if (current) {
|
||||
try {
|
||||
const token = await current.getToken();
|
||||
value = { token, authenticationProviderId: current.authenticationProviderId };
|
||||
} catch (e) {
|
||||
this.logService.error(e);
|
||||
}
|
||||
}
|
||||
await this.authenticationTokenService.setToken(value);
|
||||
}
|
||||
|
||||
private updateStatus(current: UserDataSyncAccount | undefined): void {
|
||||
// set status
|
||||
const status: AccountStatus = current ? AccountStatus.Available : AccountStatus.Unavailable;
|
||||
|
||||
if (this._status !== status) {
|
||||
const previous = this._status;
|
||||
this.logService.debug('Sync account status changed', previous, status);
|
||||
@@ -150,41 +190,7 @@ export class UserDataSyncAccounts extends Disposable {
|
||||
}
|
||||
}
|
||||
|
||||
private async getAccounts(authenticationProviderId: string): Promise<IUserDataSyncAccount[]> {
|
||||
|
||||
let accounts: Map<string, IUserDataSyncAccount> = new Map<string, IUserDataSyncAccount>();
|
||||
let currentAccount: IUserDataSyncAccount | null = null;
|
||||
let currentSession: AuthenticationSession | undefined = undefined;
|
||||
|
||||
const sessions = await this.authenticationService.getSessions(authenticationProviderId) || [];
|
||||
for (const session of sessions) {
|
||||
const account: IUserDataSyncAccount = { authenticationProviderId, sessionId: session.id, accountName: session.account.displayName, accountId: session.account.id };
|
||||
accounts.set(account.accountName, account);
|
||||
if (this.isCurrentAccount(account)) {
|
||||
currentAccount = account;
|
||||
currentSession = session;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentAccount) {
|
||||
// Always use current account if available
|
||||
accounts.set(currentAccount.accountName, currentAccount);
|
||||
}
|
||||
|
||||
// update access token
|
||||
if (currentSession) {
|
||||
try {
|
||||
const token = await currentSession.getAccessToken();
|
||||
await this.authenticationTokenService.setToken({ token, authenticationProviderId });
|
||||
} catch (e) {
|
||||
this.logService.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
return values(accounts);
|
||||
}
|
||||
|
||||
private isCurrentAccount(account: IUserDataSyncAccount): boolean {
|
||||
private isCurrentAccount(account: UserDataSyncAccount): boolean {
|
||||
return account.sessionId === this.currentSessionId;
|
||||
}
|
||||
|
||||
@@ -208,7 +214,7 @@ export class UserDataSyncAccounts extends Disposable {
|
||||
return true;
|
||||
}
|
||||
|
||||
private async doPick(): Promise<IUserDataSyncAccount | IAuthenticationProvider | undefined> {
|
||||
private async doPick(): Promise<UserDataSyncAccount | IAuthenticationProvider | undefined> {
|
||||
if (this.authenticationProviders.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
@@ -220,8 +226,8 @@ export class UserDataSyncAccounts extends Disposable {
|
||||
return this.authenticationProviders[0];
|
||||
}
|
||||
|
||||
return new Promise<IUserDataSyncAccount | IAuthenticationProvider | undefined>(async (c, e) => {
|
||||
let result: IUserDataSyncAccount | IAuthenticationProvider | undefined;
|
||||
return new Promise<UserDataSyncAccount | IAuthenticationProvider | undefined>(async (c, e) => {
|
||||
let result: UserDataSyncAccount | IAuthenticationProvider | undefined;
|
||||
const disposables: DisposableStore = new DisposableStore();
|
||||
const quickPick = this.quickInputService.createQuickPick<AccountQuickPickItem>();
|
||||
disposables.add(quickPick);
|
||||
|
||||
Reference in New Issue
Block a user