-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathconnectionProfile.ts
More file actions
114 lines (101 loc) · 4.43 KB
/
connectionProfile.ts
File metadata and controls
114 lines (101 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as LocalizedConstants from "../constants/locConstants";
import { IConnectionProfile, AuthenticationTypes } from "./interfaces";
import { ConnectionCredentials } from "./connectionCredentials";
import { INameValueChoice } from "../prompts/question";
import * as utils from "./utils";
import { uuid } from "../utils/utils";
import { AccountStore } from "../azure/accountStore";
import { AzureAuthType } from "./contracts/azure";
import { ConfigTarget } from "../connectionconfig/connectionconfig";
// Concrete implementation of the IConnectionProfile interface
/**
* A concrete implementation of an IConnectionProfile with support for profile creation and validation
*/
export class ConnectionProfile extends ConnectionCredentials implements IConnectionProfile {
public profileName: string;
public id: string;
public groupId: string;
public configSource: ConfigTarget;
public savePassword: boolean;
public emptyPasswordInput: boolean;
public azureAuthType: AzureAuthType;
declare public azureAccountToken: string | undefined;
declare public expiresOn: number | undefined;
public accountStore: AccountStore;
declare public accountId: string;
declare public tenantId: string;
constructor(connectionCredentials?: ConnectionCredentials) {
super();
if (connectionCredentials) {
this.accountId = connectionCredentials.accountId;
this.tenantId = connectionCredentials.tenantId;
this.authenticationType = connectionCredentials.authenticationType;
this.azureAccountToken = connectionCredentials.azureAccountToken;
this.expiresOn = connectionCredentials.expiresOn;
this.database = connectionCredentials.database;
this.email = connectionCredentials.email;
this.user = connectionCredentials.email;
this.password = connectionCredentials.password;
this.server = connectionCredentials.server;
}
}
public static addIdIfMissing(profile: IConnectionProfile): boolean {
if (profile && profile.id === undefined) {
profile.id = uuid();
return true;
}
return false;
}
// Assumption: having connection string or server + profile name indicates all requirements were met
public isValidProfile(): boolean {
if (this.connectionString) {
return true;
}
if (this.authenticationType) {
if (
this.authenticationType === AuthenticationTypes[AuthenticationTypes.Integrated] ||
this.authenticationType === AuthenticationTypes[AuthenticationTypes.AzureMFA] ||
this.authenticationType ===
AuthenticationTypes[AuthenticationTypes.ActiveDirectoryDefault]
) {
return utils.isNotEmpty(this.server);
} else {
return utils.isNotEmpty(this.server) && utils.isNotEmpty(this.user);
}
}
return false;
}
public isAzureActiveDirectory(): boolean {
return this.authenticationType === AuthenticationTypes[AuthenticationTypes.AzureMFA];
}
public static getAzureAuthChoices(): INameValueChoice[] {
let choices: INameValueChoice[] = [
{
name: LocalizedConstants.azureAuthTypeCodeGrant,
value: utils.azureAuthTypeToString(AzureAuthType.AuthCodeGrant),
},
{
name: LocalizedConstants.azureAuthTypeDeviceCode,
value: utils.azureAuthTypeToString(AzureAuthType.DeviceCode),
},
];
return choices;
}
public static async getAccountChoices(accountStore: AccountStore): Promise<INameValueChoice[]> {
let accounts = await accountStore.getAccounts();
let choices: Array<INameValueChoice> = [];
if (accounts.length > 0) {
for (let account of accounts) {
choices.push({
name: account?.displayInfo?.displayName,
value: account,
});
}
}
return choices;
}
}