public async createSession()

in src/objectExplorer/objectExplorerService.ts [398:474]


	public async createSession(promise: Deferred<vscode.TreeItem | undefined>, connectionCredentials?: IConnectionInfo,
		context?: vscode.ExtensionContext): Promise<string> {
		if (!connectionCredentials) {
			const connectionUI = this._connectionManager.connectionUI;
			connectionCredentials = await connectionUI.createAndSaveProfile();
		}
		if (connectionCredentials) {
			// connection string based credential
			if (connectionCredentials.connectionString) {
				if ((connectionCredentials as IConnectionProfile).savePassword) {
					// look up connection string
					let connectionString = await this._connectionManager.connectionStore.lookupPassword(connectionCredentials, true);
					connectionCredentials.connectionString = connectionString;
				}
			} else {
				if (ConnectionCredentials.isPasswordBasedCredential(connectionCredentials)) {
					// show password prompt if SQL Login and password isn't saved
					let password = connectionCredentials.password;
					if (Utils.isEmpty(password)) {
						// if password isn't saved
						if (!(<IConnectionProfile>connectionCredentials).savePassword) {
							// prompt for password
							password = await this._connectionManager.connectionUI.promptForPassword();
							if (!password) {
								promise.resolve(undefined);
								return undefined;
							}
						} else {
							// look up saved password
							password = await this._connectionManager.connectionStore.lookupPassword(connectionCredentials);
							if (connectionCredentials.authenticationType !== Constants.azureMfa) {
								connectionCredentials.azureAccountToken = undefined;
							}
						}
						connectionCredentials.password = password;
					}
				} else if (connectionCredentials.authenticationType === Utils.authTypeToString(AuthenticationTypes.Integrated)) {
					connectionCredentials.azureAccountToken = undefined;
				} else if (connectionCredentials.authenticationType === Constants.azureMfa) {
					let azureController = this._connectionManager.azureController;
					let account = this._connectionManager.accountStore.getAccount(connectionCredentials.accountId);
					let profile = new ConnectionProfile(connectionCredentials);
					if (!connectionCredentials.azureAccountToken) {
						let azureAccountToken = await azureController.refreshToken(
							account, this._connectionManager.accountStore, providerSettings.resources.databaseResource);
						if (!azureAccountToken) {
							let errorMessage = LocalizedConstants.msgAccountRefreshFailed;
							await this._connectionManager.vscodeWrapper.showErrorMessage(
								errorMessage, LocalizedConstants.refreshTokenLabel).then(async result => {
									if (result === LocalizedConstants.refreshTokenLabel) {
										await azureController.getTokens(
											profile, this._connectionManager.accountStore, providerSettings.resources.databaseResource);

									} else {
										return undefined;
									}
								});
						} else {
							connectionCredentials.azureAccountToken = azureAccountToken.token;
							connectionCredentials.expiresOn = azureAccountToken.expiresOn;
						}
					}
				}
			}
			const connectionDetails = ConnectionCredentials.createConnectionDetails(connectionCredentials);
			const response = await this._connectionManager.client.sendRequest(CreateSessionRequest.type, connectionDetails);
			if (response) {
				this._sessionIdToConnectionCredentialsMap.set(response.sessionId, connectionCredentials);
				this._sessionIdToPromiseMap.set(response.sessionId, promise);
				return response.sessionId;
			}
		} else {
			// no connection was made
			promise.resolve(undefined);
			return undefined;
		}
	}