Use a unique key for the accounts (#11380)

* Use a unique key for the accounts

* Fix some tests

* Simplify display name

* Change to a random UUID
This commit is contained in:
Amir Omidi
2020-07-17 13:39:53 -07:00
committed by GitHub
parent eb82cd3f4b
commit 5613a97fae
9 changed files with 31 additions and 23 deletions

View File

@@ -400,7 +400,7 @@ export abstract class AzureAuth implements vscode.Disposable {
const accessToken: AccessToken = {
token: tokenResponse.data.access_token,
key: tokenClaims.email || tokenClaims.unique_name || tokenClaims.name,
key: tokenClaims.oid ?? tokenClaims.email ?? tokenClaims.unique_name ?? tokenClaims.name,
};
const refreshToken: RefreshToken = {
@@ -614,7 +614,13 @@ export abstract class AzureAuth implements vscode.Disposable {
accountIssuer = 'msft';
}
const displayName = tokenClaims.name ?? tokenClaims.email ?? tokenClaims.unique_name;
const name = tokenClaims.name ?? tokenClaims.email ?? tokenClaims.unique_name;
const email = tokenClaims.email ?? tokenClaims.unique_name;
let displayName = name;
if (email) {
displayName = `${displayName} - ${email}`;
}
let contextualDisplayName: string;
switch (accountIssuer) {
@@ -637,12 +643,14 @@ export abstract class AzureAuth implements vscode.Disposable {
providerId: this.metadata.id,
accountId: key
},
name: key,
name: displayName,
displayInfo: {
accountType: accountType,
userId: key,
contextualDisplayName: contextualDisplayName,
displayName
displayName,
email,
name,
},
properties: {
providerSettings: this.metadata,

View File

@@ -101,7 +101,7 @@ export function registerAzureResourceCommands(appContext: AppContext, tree: Azur
}
} catch (error) {
account.isStale = true;
throw new AzureResourceCredentialError(localize('azure.resource.selectsubscriptions.credentialError', "Failed to get credential for account {0}. Please refresh the account.", account.key.accountId), error);
throw new AzureResourceCredentialError(localize('azure.resource.selectsubscriptions.credentialError', "Failed to get credential for account {0}. Please refresh the account.", account.displayInfo.displayName), error);
}
}

View File

@@ -155,7 +155,7 @@ export class AzureResourceAccountTreeNode extends AzureResourceContainerTreeNode
}
private generateLabel(): string {
let label = `${this.account.displayInfo.displayName} (${this.account.key.accountId})`;
let label = this.account.displayInfo.displayName;
if (this._totalSubscriptionCount !== 0) {
label += ` (${this._selectedSubscriptionCount} / ${this._totalSubscriptionCount} subscriptions)`;

View File

@@ -41,14 +41,15 @@ const mockTenantId = 'mock_tenant_id';
const mockAccount: azdata.Account = {
key: {
accountId: 'mock_account',
accountId: '97915f6d-84fa-4926-b60c-38db64327ad7',
providerId: 'mock_provider'
},
displayInfo: {
displayName: 'mock_account@test.com',
accountType: 'Microsoft',
contextualDisplayName: 'test',
userId: 'test@email.com'
userId: 'test@email.com',
email: '97915f6d-84fa-4926-b60c-38db64327ad7'
},
properties: {
tenants: [
@@ -120,18 +121,17 @@ describe('AzureResourceAccountTreeNode.info', function (): void {
const accountTreeNode = new AzureResourceAccountTreeNode(mockAccount, mockAppContext, mockTreeChangeHandler.object);
const accountTreeNodeId = `account_${mockAccount.key.accountId}`;
const accountTreeNodeLabel = `${mockAccount.displayInfo.displayName} (${mockAccount.key.accountId})`;
should(accountTreeNode.nodePathValue).equal(accountTreeNodeId);
const treeItem = await accountTreeNode.getTreeItem();
should(treeItem.id).equal(accountTreeNodeId);
should(treeItem.label).equal(accountTreeNodeLabel);
should(treeItem.label).equal(mockAccount.displayInfo.displayName);
should(treeItem.contextValue).equal(AzureResourceItemType.account);
should(treeItem.collapsibleState).equal(vscode.TreeItemCollapsibleState.Collapsed);
const nodeInfo = accountTreeNode.getNodeInfo();
should(nodeInfo.label).equal(accountTreeNodeLabel);
should(nodeInfo.label).equal(mockAccount.displayInfo.displayName);
should(nodeInfo.isLeaf).false();
should(nodeInfo.nodeType).equal(AzureResourceItemType.account);
should(nodeInfo.iconType).equal(AzureResourceItemType.account);
@@ -141,7 +141,7 @@ describe('AzureResourceAccountTreeNode.info', function (): void {
mockSubscriptionService.setup((o) => o.getSubscriptions(mockAccount, mockCredential)).returns(() => Promise.resolve(mockSubscriptions));
mockSubscriptionFilterService.setup((o) => o.getSelectedSubscriptions(mockAccount)).returns(() => Promise.resolve(undefined));
const accountTreeNodeLabel = `${mockAccount.displayInfo.displayName} (${mockAccount.key.accountId}) (${mockSubscriptions.length} / ${mockSubscriptions.length} subscriptions)`;
const accountTreeNodeLabel = `${mockAccount.displayInfo.displayName} (${mockSubscriptions.length} / ${mockSubscriptions.length} subscriptions)`;
const accountTreeNode = new AzureResourceAccountTreeNode(mockAccount, mockAppContext, mockTreeChangeHandler.object);
@@ -161,7 +161,7 @@ describe('AzureResourceAccountTreeNode.info', function (): void {
mockSubscriptionService.setup((o) => o.getSubscriptions(mockAccount, mockCredential)).returns(() => Promise.resolve(mockSubscriptions));
mockSubscriptionFilterService.setup((o) => o.getSelectedSubscriptions(mockAccount)).returns(() => Promise.resolve(mockFilteredSubscriptions));
const accountTreeNodeLabel = `${mockAccount.displayInfo.displayName} (${mockAccount.key.accountId}) (${mockFilteredSubscriptions.length} / ${mockSubscriptions.length} subscriptions)`;
const accountTreeNodeLabel = `${mockAccount.displayInfo.displayName} (${mockFilteredSubscriptions.length} / ${mockSubscriptions.length} subscriptions)`;
const accountTreeNode = new AzureResourceAccountTreeNode(mockAccount, mockAppContext, mockTreeChangeHandler.object);

View File

@@ -101,7 +101,7 @@ export class AccountFeature implements StaticFeature {
constructor(account: azdata.Account) {
this.account = account;
this.label = account.key.accountId;
this.label = account.displayInfo.displayName;
}
};
}

2
src/sql/azdata.d.ts vendored
View File

@@ -2160,7 +2160,7 @@ declare module 'azdata' {
displayName: string;
/**
* User id that identifies the account, such as "user@contoso.com".
* Unique user id that identifies the account.
*/
userId: string;
}

View File

@@ -484,4 +484,10 @@ declare module 'azdata' {
childProvider?: string;
type?: ExtensionNodeType;
}
export interface AccountDisplayInfo {
email?: string;
name?: string;
}
}

View File

@@ -71,13 +71,7 @@ export class AccountPickerListRenderer implements IListRenderer<azdata.Account,
templateData.icon.classList.add('account-logo', account.displayInfo.accountType);
templateData.contextualDisplayName.innerText = account.displayInfo.contextualDisplayName;
// show the account display name text, something like "User Name (user@email.com)"
if (account.displayInfo.userId && account.displayInfo.displayName) {
templateData.displayName.innerText = account.displayInfo.displayName + ' (' + account.displayInfo.userId + ')';
} else {
templateData.displayName.innerText = account.displayInfo.displayName;
}
templateData.displayName.innerText = account.displayInfo.displayName;
if (account.isStale) {
templateData.badgeContent.className = 'badge-content codicon warning-badge';

View File

@@ -520,7 +520,7 @@ export class ConnectionWidget extends lifecycle.Disposable {
let oldSelection = this._azureAccountDropdown.value;
const accounts = await this._accountManagementService.getAccounts();
this._azureAccountList = accounts.filter(a => a.key.providerId.startsWith('azure'));
let accountDropdownOptions = this._azureAccountList.map(account => account.key.accountId);
let accountDropdownOptions = this._azureAccountList.map(account => account.displayInfo.displayName);
if (accountDropdownOptions.length === 0) {
// If there are no accounts add a blank option so that add account isn't automatically selected
accountDropdownOptions.unshift('');