@microsoft/teamsfx 0.4.1-alpha.fa070464.0 → 0.4.2-alpha.4f9464b2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,6 +14,8 @@ Use the library to:
14
14
 
15
15
  ## Getting started
16
16
 
17
+ > Important: Please be advised that access tokens are stored in sessionStorage for you by default. This can make it possible for malicious code in your app (or code pasted into a console on your page) to access APIs at the same privilege level as your client application. Please ensure you only request the minimum necessary scopes from your client application, and perform any sensitive operations from server side code that your client has to authenticate with.
18
+
17
19
  TeamsFx SDK is pre-configured in scaffolded project using Teams Toolkit extension for Visual Studio and vscode, or the `teamsfx` cli from the `teamsfx-cli` npm package.
18
20
  Please check the [README](https://github.com/OfficeDev/TeamsFx/blob/main/packages/vscode-extension/README.md) to see how to create a Teams App project.
19
21
 
@@ -2,7 +2,6 @@ import jwt_decode from 'jwt-decode';
2
2
  import * as microsoftTeams from '@microsoft/teams-js';
3
3
  import axios from 'axios';
4
4
  import { Client } from '@microsoft/microsoft-graph-client';
5
- import { ManagedIdentityCredential } from '@azure/identity';
6
5
 
7
6
  // Copyright (c) Microsoft Corporation.
8
7
  // Licensed under the MIT license.
@@ -168,7 +167,7 @@ function getLogLevel() {
168
167
  return internalLogger.level;
169
168
  }
170
169
  class InternalLogger {
171
- constructor() {
170
+ constructor(name, logLevel) {
172
171
  this.level = undefined;
173
172
  this.defaultLogger = {
174
173
  verbose: console.debug,
@@ -176,6 +175,8 @@ class InternalLogger {
176
175
  warn: console.warn,
177
176
  error: console.error,
178
177
  };
178
+ this.name = name;
179
+ this.level = logLevel;
179
180
  }
180
181
  error(message) {
181
182
  this.log(LogLevel.Error, (x) => x.error, message);
@@ -194,7 +195,13 @@ class InternalLogger {
194
195
  return;
195
196
  }
196
197
  const timestamp = new Date().toUTCString();
197
- const logHeader = `[${timestamp}] : @microsoft/teamsfx : ${LogLevel[logLevel]} - `;
198
+ let logHeader;
199
+ if (this.name) {
200
+ logHeader = `[${timestamp}] : @microsoft/teamsfx - ${this.name} : ${LogLevel[logLevel]} - `;
201
+ }
202
+ else {
203
+ logHeader = `[${timestamp}] : @microsoft/teamsfx : ${LogLevel[logLevel]} - `;
204
+ }
198
205
  const logMessage = `${logHeader}${message}`;
199
206
  if (this.level !== undefined && this.level <= logLevel) {
200
207
  if (this.customLogger) {
@@ -675,6 +682,8 @@ class TeamsUserCredential {
675
682
  /**
676
683
  * Get access token from credential.
677
684
  *
685
+ * Important: Access tokens are stored in sessionStorage, read more here: https://aka.ms/teamsfx-session-storage-notice
686
+ *
678
687
  * @example
679
688
  * ```typescript
680
689
  * await credential.getToken([]) // Get SSO token using empty string array
@@ -769,6 +778,7 @@ class TeamsUserCredential {
769
778
  });
770
779
  const tokenResult = response.data;
771
780
  const key = await this.getAccessTokenCacheKey(scopesStr);
781
+ // Important: tokens are stored in sessionStorage, read more here: https://aka.ms/teamsfx-session-storage-notice
772
782
  this.setTokenCache(key, {
773
783
  token: tokenResult.access_token,
774
784
  expiresOnTimestamp: tokenResult.expires_on,
@@ -1130,174 +1140,25 @@ function createMicrosoftGraphClient(credential, scopes) {
1130
1140
 
1131
1141
  // Copyright (c) Microsoft Corporation.
1132
1142
  /**
1133
- * SQL connection configuration instance.
1143
+ * Generate connection configuration consumed by tedious.
1134
1144
  * @remarks
1135
1145
  * Only works in in server side.
1136
- *
1137
1146
  * @beta
1138
- *
1139
1147
  */
1140
1148
  class DefaultTediousConnectionConfiguration {
1141
1149
  constructor() {
1142
- /**
1143
- * MSSQL default scope
1144
- * https://docs.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-connect-msi
1145
- */
1146
- this.defaultSQLScope = "https://database.windows.net/";
1150
+ throw new ErrorWithCode(formatString(ErrorMessage.BrowserRuntimeNotSupported, "DefaultTediousConnectionConfiguration"), ErrorCode.RuntimeNotSupported);
1147
1151
  }
1148
1152
  /**
1149
1153
  * Generate connection configuration consumed by tedious.
1150
- *
1151
- * @returns Connection configuration of tedious for the SQL.
1152
- *
1153
- * @throws {@link ErrorCode|InvalidConfiguration} when SQL config resource configuration is invalid.
1154
- * @throws {@link ErrorCode|InternalError} when get user MSI token failed or MSI token is invalid.
1155
- * @throws {@link ErrorCode|RuntimeNotSupported} when runtime is browser.
1156
- *
1154
+ * @remarks
1155
+ * Only works in in server side.
1157
1156
  * @beta
1158
1157
  */
1159
1158
  async getConfig() {
1160
- internalLogger.info("Get SQL configuration");
1161
- const configuration = getResourceConfiguration(ResourceType.SQL);
1162
- if (!configuration) {
1163
- const errMsg = "SQL resource configuration not exist";
1164
- internalLogger.error(errMsg);
1165
- throw new ErrorWithCode(errMsg, ErrorCode.InvalidConfiguration);
1166
- }
1167
- try {
1168
- this.isSQLConfigurationValid(configuration);
1169
- }
1170
- catch (err) {
1171
- throw err;
1172
- }
1173
- if (!this.isMsiAuthentication()) {
1174
- const configWithUPS = this.generateDefaultConfig(configuration);
1175
- internalLogger.verbose("SQL configuration with username and password generated");
1176
- return configWithUPS;
1177
- }
1178
- try {
1179
- const configWithToken = await this.generateTokenConfig(configuration);
1180
- internalLogger.verbose("SQL configuration with MSI token generated");
1181
- return configWithToken;
1182
- }
1183
- catch (error) {
1184
- throw error;
1185
- }
1186
- }
1187
- /**
1188
- * Check SQL use MSI identity or username and password.
1189
- *
1190
- * @returns false - login with SQL MSI identity, true - login with username and password.
1191
- * @internal
1192
- */
1193
- isMsiAuthentication() {
1194
- internalLogger.verbose("Check connection config using MSI access token or username and password");
1195
- const configuration = getResourceConfiguration(ResourceType.SQL);
1196
- if ((configuration === null || configuration === void 0 ? void 0 : configuration.sqlUsername) != null && (configuration === null || configuration === void 0 ? void 0 : configuration.sqlPassword) != null) {
1197
- internalLogger.verbose("Login with username and password");
1198
- return false;
1199
- }
1200
- internalLogger.verbose("Login with MSI identity");
1201
- return true;
1202
- }
1203
- /**
1204
- * check configuration is an available configurations.
1205
- * @param { SqlConfiguration } sqlConfig
1206
- *
1207
- * @returns true - SQL configuration has a valid SQL endpoints, SQL username with password or identity ID.
1208
- * false - configuration is not valid.
1209
- * @internal
1210
- */
1211
- isSQLConfigurationValid(sqlConfig) {
1212
- internalLogger.verbose("Check SQL configuration if valid");
1213
- if (!sqlConfig.sqlServerEndpoint) {
1214
- internalLogger.error("SQL configuration is not valid without SQL server endpoint exist");
1215
- throw new ErrorWithCode("SQL configuration error without SQL server endpoint exist", ErrorCode.InvalidConfiguration);
1216
- }
1217
- if (!(sqlConfig.sqlUsername && sqlConfig.sqlPassword) && !sqlConfig.sqlIdentityId) {
1218
- const errMsg = `SQL configuration is not valid without ${sqlConfig.sqlIdentityId ? "" : "identity id "} ${sqlConfig.sqlUsername ? "" : "SQL username "} ${sqlConfig.sqlPassword ? "" : "SQL password"} exist`;
1219
- internalLogger.error(errMsg);
1220
- throw new ErrorWithCode(errMsg, ErrorCode.InvalidConfiguration);
1221
- }
1222
- internalLogger.verbose("SQL configuration is valid");
1159
+ throw new ErrorWithCode(formatString(ErrorMessage.BrowserRuntimeNotSupported, "DefaultTediousConnectionConfiguration"), ErrorCode.RuntimeNotSupported);
1223
1160
  }
1224
- /**
1225
- * Generate tedious connection configuration with default authentication type.
1226
- *
1227
- * @param { SqlConfiguration } SQL configuration with username and password.
1228
- *
1229
- * @returns Tedious connection configuration with username and password.
1230
- * @internal
1231
- */
1232
- generateDefaultConfig(sqlConfig) {
1233
- internalLogger.verbose(`SQL server ${sqlConfig.sqlServerEndpoint}, user name ${sqlConfig.sqlUsername}, database name ${sqlConfig.sqlDatabaseName}`);
1234
- const config = {
1235
- server: sqlConfig.sqlServerEndpoint,
1236
- authentication: {
1237
- type: TediousAuthenticationType.default,
1238
- options: {
1239
- userName: sqlConfig.sqlUsername,
1240
- password: sqlConfig.sqlPassword,
1241
- },
1242
- },
1243
- options: {
1244
- database: sqlConfig.sqlDatabaseName,
1245
- encrypt: true,
1246
- },
1247
- };
1248
- return config;
1249
- }
1250
- /**
1251
- * Generate tedious connection configuration with azure-active-directory-access-token authentication type.
1252
- *
1253
- * @param { SqlConfiguration } SQL configuration with AAD access token.
1254
- *
1255
- * @returns Tedious connection configuration with access token.
1256
- * @internal
1257
- */
1258
- async generateTokenConfig(sqlConfig) {
1259
- internalLogger.verbose("Generate tedious config with MSI token");
1260
- let token;
1261
- try {
1262
- const credential = new ManagedIdentityCredential(sqlConfig.sqlIdentityId);
1263
- token = await credential.getToken(this.defaultSQLScope);
1264
- }
1265
- catch (error) {
1266
- const errMsg = "Get user MSI token failed";
1267
- internalLogger.error(errMsg);
1268
- throw new ErrorWithCode(errMsg, ErrorCode.InternalError);
1269
- }
1270
- if (token) {
1271
- const config = {
1272
- server: sqlConfig.sqlServerEndpoint,
1273
- authentication: {
1274
- type: TediousAuthenticationType.MSI,
1275
- options: {
1276
- token: token.token,
1277
- },
1278
- },
1279
- options: {
1280
- database: sqlConfig.sqlDatabaseName,
1281
- encrypt: true,
1282
- },
1283
- };
1284
- internalLogger.verbose(`Generate token configuration success, server endpoint is ${sqlConfig.sqlServerEndpoint}, database name is ${sqlConfig.sqlDatabaseName}`);
1285
- return config;
1286
- }
1287
- internalLogger.error(`Generate token configuration, server endpoint is ${sqlConfig.sqlServerEndpoint}, MSI token is not valid`);
1288
- throw new ErrorWithCode("MSI token is not valid", ErrorCode.InternalError);
1289
- }
1290
- }
1291
- /**
1292
- * tedious connection config authentication type.
1293
- * https://tediousjs.github.io/tedious/api-connection.html
1294
- * @internal
1295
- */
1296
- var TediousAuthenticationType;
1297
- (function (TediousAuthenticationType) {
1298
- TediousAuthenticationType["default"] = "default";
1299
- TediousAuthenticationType["MSI"] = "azure-active-directory-access-token";
1300
- })(TediousAuthenticationType || (TediousAuthenticationType = {}));
1161
+ }
1301
1162
 
1302
1163
  // Copyright (c) Microsoft Corporation.
1303
1164
  /**