@microsoft/teamsfx 0.5.2-alpha.313131ea.0 → 0.6.0-rc.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.
@@ -59,6 +59,10 @@ var ErrorCode;
59
59
  * Invalid response error.
60
60
  */
61
61
  ErrorCode["InvalidResponse"] = "InvalidResponse";
62
+ /**
63
+ * Identity type error.
64
+ */
65
+ ErrorCode["IdentityTypeNotSupported"] = "IdentityTypeNotSupported";
62
66
  })(ErrorCode || (ErrorCode = {}));
63
67
  /**
64
68
  * @internal
@@ -78,6 +82,8 @@ ErrorMessage.NodejsRuntimeNotSupported = "{0} is not supported in Node.";
78
82
  ErrorMessage.FailToAcquireTokenOnBehalfOfUser = "Failed to acquire access token on behalf of user: {0}";
79
83
  // ChannelNotSupported Error
80
84
  ErrorMessage.OnlyMSTeamsChannelSupported = "{0} is only supported in MS Teams Channel";
85
+ // IdentityTypeNotSupported Error
86
+ ErrorMessage.IdentityTypeNotSupported = "{0} identity is not supported in {1}";
81
87
  /**
82
88
  * Error class with code and message thrown by the SDK.
83
89
  *
@@ -104,26 +110,6 @@ class ErrorWithCode extends Error {
104
110
  }
105
111
  }
106
112
 
107
- // Copyright (c) Microsoft Corporation.
108
- // Licensed under the MIT license.
109
- /**
110
- * Available resource type.
111
- * @beta
112
- */
113
- var ResourceType;
114
- (function (ResourceType) {
115
- /**
116
- * SQL database.
117
- *
118
- */
119
- ResourceType[ResourceType["SQL"] = 0] = "SQL";
120
- /**
121
- * Rest API.
122
- *
123
- */
124
- ResourceType[ResourceType["API"] = 1] = "API";
125
- })(ResourceType || (ResourceType = {}));
126
-
127
113
  // Copyright (c) Microsoft Corporation.
128
114
  // Licensed under the MIT license.
129
115
  /**
@@ -403,131 +389,6 @@ function validateScopesType(value) {
403
389
  const errorMsg = "The type of scopes is not valid, it must be string or string array";
404
390
  internalLogger.error(errorMsg);
405
391
  throw new ErrorWithCode(errorMsg, ErrorCode.InvalidParameter);
406
- }
407
- /**
408
- * @internal
409
- */
410
- const isNode = typeof process !== "undefined" &&
411
- !!process.version &&
412
- !!process.versions &&
413
- !!process.versions.node;
414
-
415
- // Copyright (c) Microsoft Corporation.
416
- /**
417
- * Global configuration instance
418
- *
419
- */
420
- let config;
421
- /**
422
- * Initialize configuration from environment variables or configuration object and set the global instance
423
- *
424
- * @param {Configuration} configuration - Optional configuration that overrides the default configuration values. The override depth is 1.
425
- *
426
- * @throws {@link ErrorCode|InvalidParameter} when configuration is not passed in browser environment
427
- *
428
- * @beta
429
- */
430
- function loadConfiguration(configuration) {
431
- internalLogger.info("load configuration");
432
- // browser environment
433
- if (!isNode) {
434
- if (!configuration) {
435
- const errorMsg = "You are running the code in browser. Configuration must be passed in.";
436
- internalLogger.error(errorMsg);
437
- throw new ErrorWithCode(errorMsg, ErrorCode.InvalidParameter);
438
- }
439
- config = configuration;
440
- return;
441
- }
442
- // node environment
443
- let newAuthentication;
444
- let newResources = [];
445
- const defaultResourceName = "default";
446
- if (configuration === null || configuration === void 0 ? void 0 : configuration.authentication) {
447
- newAuthentication = configuration.authentication;
448
- }
449
- else {
450
- newAuthentication = {
451
- authorityHost: process.env.M365_AUTHORITY_HOST,
452
- tenantId: process.env.M365_TENANT_ID,
453
- clientId: process.env.M365_CLIENT_ID,
454
- clientSecret: process.env.M365_CLIENT_SECRET,
455
- simpleAuthEndpoint: process.env.SIMPLE_AUTH_ENDPOINT,
456
- initiateLoginEndpoint: process.env.INITIATE_LOGIN_ENDPOINT,
457
- applicationIdUri: process.env.M365_APPLICATION_ID_URI,
458
- };
459
- }
460
- if (configuration === null || configuration === void 0 ? void 0 : configuration.resources) {
461
- newResources = configuration.resources;
462
- }
463
- else {
464
- newResources = [
465
- {
466
- // SQL resource
467
- type: ResourceType.SQL,
468
- name: defaultResourceName,
469
- properties: {
470
- sqlServerEndpoint: process.env.SQL_ENDPOINT,
471
- sqlUsername: process.env.SQL_USER_NAME,
472
- sqlPassword: process.env.SQL_PASSWORD,
473
- sqlDatabaseName: process.env.SQL_DATABASE_NAME,
474
- sqlIdentityId: process.env.IDENTITY_ID,
475
- },
476
- },
477
- {
478
- // API resource
479
- type: ResourceType.API,
480
- name: defaultResourceName,
481
- properties: {
482
- endpoint: process.env.API_ENDPOINT,
483
- },
484
- },
485
- ];
486
- }
487
- config = {
488
- authentication: newAuthentication,
489
- resources: newResources,
490
- };
491
- }
492
- /**
493
- * Get configuration for a specific resource.
494
- * @param {ResourceType} resourceType - The type of resource
495
- * @param {string} resourceName - The name of resource, default value is "default".
496
- *
497
- * @returns Resource configuration for target resource from global configuration instance.
498
- *
499
- * @throws {@link ErrorCode|InvalidConfiguration} when resource configuration with the specific type and name is not found
500
- *
501
- * @beta
502
- */
503
- function getResourceConfiguration(resourceType, resourceName = "default") {
504
- var _a;
505
- internalLogger.info(`Get resource configuration of ${ResourceType[resourceType]} from ${resourceName}`);
506
- const result = (_a = config.resources) === null || _a === void 0 ? void 0 : _a.find((item) => item.type === resourceType && item.name === resourceName);
507
- if (result) {
508
- return result.properties;
509
- }
510
- const errorMsg = formatString(ErrorMessage.MissingResourceConfiguration, ResourceType[resourceType], resourceName);
511
- internalLogger.error(errorMsg);
512
- throw new ErrorWithCode(errorMsg, ErrorCode.InvalidConfiguration);
513
- }
514
- /**
515
- * Get configuration for authentication.
516
- *
517
- * @returns Authentication configuration from global configuration instance, the value may be undefined if no authentication config exists in current environment.
518
- *
519
- * @throws {@link ErrorCode|InvalidConfiguration} when global configuration does not exist
520
- *
521
- * @beta
522
- */
523
- function getAuthenticationConfiguration() {
524
- internalLogger.info("Get authentication configuration");
525
- if (config) {
526
- return config.authentication;
527
- }
528
- const errorMsg = "Please call loadConfiguration() first before calling getAuthenticationConfiguration().";
529
- internalLogger.error(errorMsg);
530
- throw new ErrorWithCode(formatString(ErrorMessage.ConfigurationNotExists, errorMsg), ErrorCode.InvalidConfiguration);
531
392
  }
532
393
 
533
394
  // Copyright (c) Microsoft Corporation.
@@ -539,16 +400,16 @@ function getAuthenticationConfiguration() {
539
400
  *
540
401
  * @beta
541
402
  */
542
- class M365TenantCredential {
403
+ class AppCredential {
543
404
  /**
544
- * Constructor of M365TenantCredential.
405
+ * Constructor of AppCredential.
545
406
  *
546
407
  * @remarks
547
408
  * Only works in in server side.
548
409
  * @beta
549
410
  */
550
- constructor() {
551
- throw new ErrorWithCode(formatString(ErrorMessage.BrowserRuntimeNotSupported, "M365TenantCredential"), ErrorCode.RuntimeNotSupported);
411
+ constructor(authConfig) {
412
+ throw new ErrorWithCode(formatString(ErrorMessage.BrowserRuntimeNotSupported, "AppCredential"), ErrorCode.RuntimeNotSupported);
552
413
  }
553
414
  /**
554
415
  * Get access token for credential.
@@ -558,7 +419,7 @@ class M365TenantCredential {
558
419
  * @beta
559
420
  */
560
421
  async getToken(scopes, options) {
561
- throw new ErrorWithCode(formatString(ErrorMessage.BrowserRuntimeNotSupported, "M365TenantCredential"), ErrorCode.RuntimeNotSupported);
422
+ throw new ErrorWithCode(formatString(ErrorMessage.BrowserRuntimeNotSupported, "AppCredential"), ErrorCode.RuntimeNotSupported);
562
423
  }
563
424
  }
564
425
 
@@ -579,7 +440,7 @@ class OnBehalfOfUserCredential {
579
440
  * Can Only works in in server side.
580
441
  * @beta
581
442
  */
582
- constructor(ssoToken) {
443
+ constructor(ssoToken, config) {
583
444
  throw new ErrorWithCode(formatString(ErrorMessage.BrowserRuntimeNotSupported, "OnBehalfOfUserCredential"), ErrorCode.RuntimeNotSupported);
584
445
  }
585
446
  /**
@@ -617,7 +478,6 @@ const loginPageHeight = 535;
617
478
  class TeamsUserCredential {
618
479
  /**
619
480
  * Constructor of TeamsUserCredential.
620
- * Developer need to call loadConfiguration(config) before using this class.
621
481
  *
622
482
  * @example
623
483
  * ```typescript
@@ -627,18 +487,22 @@ class TeamsUserCredential {
627
487
  * clientId: "xxx"
628
488
  * }
629
489
  * }
630
- loadConfiguration(config); // No default config from environment variables, developers must provide the config object.
631
- const credential = new TeamsUserCredential(["https://graph.microsoft.com/User.Read"]);
490
+ * // Use default configuration provided by Teams Toolkit
491
+ * const credential = new TeamsUserCredential();
492
+ * // Use a customized configuration
493
+ * const anotherCredential = new TeamsUserCredential(config);
632
494
  * ```
633
495
  *
496
+ * @param {AuthenticationConfiguration} authConfig - The authentication configuration. Use environment variables if not provided.
497
+ *
634
498
  * @throws {@link ErrorCode|InvalidConfiguration} when client id, initiate login endpoint or simple auth endpoint is not found in config.
635
499
  * @throws {@link ErrorCode|RuntimeNotSupported} when runtime is nodeJS.
636
500
  *
637
501
  * @beta
638
502
  */
639
- constructor() {
503
+ constructor(authConfig) {
640
504
  internalLogger.info("Create teams user credential");
641
- this.config = this.loadAndValidateConfig();
505
+ this.config = this.loadAndValidateConfig(authConfig);
642
506
  this.ssoToken = null;
643
507
  this.initialized = false;
644
508
  }
@@ -899,15 +763,13 @@ class TeamsUserCredential {
899
763
  }
900
764
  /**
901
765
  * Load and validate authentication configuration
766
+ *
767
+ * @param {AuthenticationConfiguration?} config - The authentication configuration. Use environment variables if not provided.
768
+ *
902
769
  * @returns Authentication configuration
903
770
  */
904
- loadAndValidateConfig() {
771
+ loadAndValidateConfig(config) {
905
772
  internalLogger.verbose("Validate authentication configuration");
906
- const config = getAuthenticationConfiguration();
907
- if (!config) {
908
- internalLogger.error(ErrorMessage.AuthenticationConfigurationNotExists);
909
- throw new ErrorWithCode(ErrorMessage.AuthenticationConfigurationNotExists, ErrorCode.InvalidConfiguration);
910
- }
911
773
  if (config.initiateLoginEndpoint && config.clientId) {
912
774
  return config;
913
775
  }
@@ -960,7 +822,7 @@ class MsGraphAuthProvider {
960
822
  /**
961
823
  * Constructor of MsGraphAuthProvider.
962
824
  *
963
- * @param {TokenCredential} credential - Credential used to invoke Microsoft Graph APIs.
825
+ * @param {TeamsFx} teamsfx - Used to provide configuration and auth.
964
826
  * @param {string | string[]} scopes - The list of scopes for which the token will have access.
965
827
  *
966
828
  * @throws {@link ErrorCode|InvalidParameter} when scopes is not a valid string or string array.
@@ -969,8 +831,8 @@ class MsGraphAuthProvider {
969
831
  *
970
832
  * @beta
971
833
  */
972
- constructor(credential, scopes) {
973
- this.credential = credential;
834
+ constructor(teamsfx, scopes) {
835
+ this.teamsfx = teamsfx;
974
836
  let scopesStr = defaultScope;
975
837
  if (scopes) {
976
838
  validateScopesType(scopes);
@@ -996,7 +858,7 @@ class MsGraphAuthProvider {
996
858
  */
997
859
  async getAccessToken() {
998
860
  internalLogger.info(`Get Graph Access token with scopes: '${this.scopes}'`);
999
- const accessToken = await this.credential.getToken(this.scopes);
861
+ const accessToken = await this.teamsfx.getCredential().getToken(this.scopes);
1000
862
  return new Promise((resolve, reject) => {
1001
863
  if (accessToken) {
1002
864
  resolve(accessToken.token);
@@ -1053,7 +915,7 @@ class MsGraphAuthProvider {
1053
915
  * }
1054
916
  * ```
1055
917
  *
1056
- * @param {TokenCredential} credential - token credential instance.
918
+ * @param {TeamsFx} teamsfx - Used to provide configuration and auth.
1057
919
  * @param scopes - The array of Microsoft Token scope of access. Default value is `[.default]`.
1058
920
  *
1059
921
  * @throws {@link ErrorCode|InvalidParameter} when scopes is not a valid string or string array.
@@ -1062,9 +924,9 @@ class MsGraphAuthProvider {
1062
924
  *
1063
925
  * @beta
1064
926
  */
1065
- function createMicrosoftGraphClient(credential, scopes) {
927
+ function createMicrosoftGraphClient(teamsfx, scopes) {
1066
928
  internalLogger.info("Create Microsoft Graph Client");
1067
- const authProvider = new MsGraphAuthProvider(credential, scopes);
929
+ const authProvider = new MsGraphAuthProvider(teamsfx, scopes);
1068
930
  const graphClient = Client.initWithMiddleware({
1069
931
  authProvider,
1070
932
  });
@@ -1078,19 +940,8 @@ function createMicrosoftGraphClient(credential, scopes) {
1078
940
  * Only works in in server side.
1079
941
  * @beta
1080
942
  */
1081
- class DefaultTediousConnectionConfiguration {
1082
- constructor() {
1083
- throw new ErrorWithCode(formatString(ErrorMessage.BrowserRuntimeNotSupported, "DefaultTediousConnectionConfiguration"), ErrorCode.RuntimeNotSupported);
1084
- }
1085
- /**
1086
- * Generate connection configuration consumed by tedious.
1087
- * @remarks
1088
- * Only works in in server side.
1089
- * @beta
1090
- */
1091
- async getConfig(databaseName) {
1092
- throw new ErrorWithCode(formatString(ErrorMessage.BrowserRuntimeNotSupported, "DefaultTediousConnectionConfiguration"), ErrorCode.RuntimeNotSupported);
1093
- }
943
+ async function getTediousConnectionConfig(teamsfx, databaseName) {
944
+ throw new ErrorWithCode(formatString(ErrorMessage.BrowserRuntimeNotSupported, "DefaultTediousConnectionConfiguration"), ErrorCode.RuntimeNotSupported);
1094
945
  }
1095
946
 
1096
947
  // Copyright (c) Microsoft Corporation.
@@ -1120,7 +971,6 @@ class DefaultTediousConnectionConfiguration {
1120
971
  * const dialogState = convoState.createProperty('dialogState');
1121
972
  * const dialogs = new DialogSet(dialogState);
1122
973
  *
1123
- * loadConfiguration();
1124
974
  * dialogs.add(new TeamsBotSsoPrompt('TeamsBotSsoPrompt', {
1125
975
  * scopes: ["User.Read"],
1126
976
  * }));
@@ -1157,7 +1007,8 @@ class TeamsBotSsoPrompt {
1157
1007
  *
1158
1008
  * @beta
1159
1009
  */
1160
- constructor(dialogId, settings) {
1010
+ constructor(teamsfx, dialogId, settings) {
1011
+ this.teamsfx = teamsfx;
1161
1012
  this.settings = settings;
1162
1013
  throw new ErrorWithCode(formatString(ErrorMessage.BrowserRuntimeNotSupported, "TeamsBotSsoPrompt"), ErrorCode.RuntimeNotSupported);
1163
1014
  }
@@ -1203,5 +1054,121 @@ class TeamsBotSsoPrompt {
1203
1054
  }
1204
1055
  }
1205
1056
 
1206
- export { DefaultTediousConnectionConfiguration, ErrorCode, ErrorWithCode, LogLevel, M365TenantCredential, MsGraphAuthProvider, OnBehalfOfUserCredential, ResourceType, TeamsBotSsoPrompt, TeamsUserCredential, createMicrosoftGraphClient, getAuthenticationConfiguration, getLogLevel, getResourceConfiguration, loadConfiguration, setLogFunction, setLogLevel, setLogger };
1057
+ // Copyright (c) Microsoft Corporation.
1058
+ // Licensed under the MIT license.
1059
+ /**
1060
+ * Identity type to use in authentication.
1061
+ *
1062
+ * @beta
1063
+ */
1064
+ var IdentityType;
1065
+ (function (IdentityType) {
1066
+ /**
1067
+ * Represents the current user of Teams.
1068
+ */
1069
+ IdentityType["User"] = "User";
1070
+ /**
1071
+ * Represents the application itself.
1072
+ */
1073
+ IdentityType["App"] = "Application";
1074
+ })(IdentityType || (IdentityType = {}));
1075
+
1076
+ // Copyright (c) Microsoft Corporation.
1077
+ /**
1078
+ * A class providing credential and configuration.
1079
+ * @beta
1080
+ */
1081
+ class TeamsFx {
1082
+ constructor(identityType, customConfig) {
1083
+ this.identityType = identityType !== null && identityType !== void 0 ? identityType : IdentityType.User;
1084
+ if (this.identityType !== IdentityType.User) {
1085
+ const errorMsg = formatString(ErrorMessage.IdentityTypeNotSupported, this.identityType.toString(), "TeamsFx");
1086
+ internalLogger.error(errorMsg);
1087
+ throw new ErrorWithCode(errorMsg, ErrorCode.IdentityTypeNotSupported);
1088
+ }
1089
+ this.configuration = new Map();
1090
+ this.loadFromEnv();
1091
+ if (customConfig) {
1092
+ for (const key of Object.keys(customConfig)) {
1093
+ const value = customConfig[key];
1094
+ if (value) {
1095
+ this.configuration.set(key, value);
1096
+ }
1097
+ }
1098
+ }
1099
+ if (this.configuration.size === 0) {
1100
+ internalLogger.warn("No configuration is loaded, please pass required configs to TeamsFx constructor");
1101
+ }
1102
+ }
1103
+ loadFromEnv() {
1104
+ if (window && window.__env__) {
1105
+ // testing purpose
1106
+ const env = window.__env__;
1107
+ this.configuration.set("authorityHost", env.REACT_APP_AUTHORITY_HOST);
1108
+ this.configuration.set("tenantId", env.REACT_APP_TENANT_ID);
1109
+ this.configuration.set("clientId", env.REACT_APP_CLIENT_ID);
1110
+ this.configuration.set("initiateLoginEndpoint", env.REACT_APP_START_LOGIN_PAGE_URL);
1111
+ this.configuration.set("applicationIdUri", env.M365_APPLICATION_ID_URI);
1112
+ this.configuration.set("apiEndpoint", env.REACT_APP_FUNC_ENDPOINT);
1113
+ this.configuration.set("apiName", env.REACT_APP_FUNC_NAME);
1114
+ }
1115
+ else {
1116
+ // TODO: support common environment variable name
1117
+ try {
1118
+ this.configuration.set("authorityHost", process.env.REACT_APP_AUTHORITY_HOST);
1119
+ this.configuration.set("tenantId", process.env.REACT_APP_TENANT_ID);
1120
+ this.configuration.set("clientId", process.env.REACT_APP_CLIENT_ID);
1121
+ this.configuration.set("initiateLoginEndpoint", process.env.REACT_APP_START_LOGIN_PAGE_URL);
1122
+ this.configuration.set("applicationIdUri", process.env.M365_APPLICATION_ID_URI);
1123
+ this.configuration.set("apiEndpoint", process.env.REACT_APP_FUNC_ENDPOINT);
1124
+ this.configuration.set("apiName", process.env.REACT_APP_FUNC_NAME);
1125
+ }
1126
+ catch (_) {
1127
+ internalLogger.warn("Cannot read process.env, please use webpack if you want to use environment variables.");
1128
+ return;
1129
+ }
1130
+ }
1131
+ }
1132
+ getIdentityType() {
1133
+ return this.identityType;
1134
+ }
1135
+ getCredential() {
1136
+ if (!this.teamsUserCredential) {
1137
+ this.teamsUserCredential = new TeamsUserCredential(Object.fromEntries(this.configuration));
1138
+ }
1139
+ return this.teamsUserCredential;
1140
+ }
1141
+ async getUserInfo() {
1142
+ return await this.getCredential().getUserInfo();
1143
+ }
1144
+ async login(scopes) {
1145
+ await this.getCredential().login(scopes);
1146
+ }
1147
+ setSsoToken(ssoToken) {
1148
+ return this;
1149
+ }
1150
+ getConfig(key) {
1151
+ const value = this.configuration.get(key);
1152
+ if (!value) {
1153
+ throw new Error();
1154
+ }
1155
+ return value;
1156
+ }
1157
+ hasConfig(key) {
1158
+ const value = this.configuration.get(key);
1159
+ return !!value;
1160
+ }
1161
+ getConfigs() {
1162
+ const config = {};
1163
+ for (const key of this.configuration.keys()) {
1164
+ const value = this.configuration.get(key);
1165
+ if (value) {
1166
+ config[key] = value;
1167
+ }
1168
+ }
1169
+ return config;
1170
+ }
1171
+ }
1172
+
1173
+ export { AppCredential, ErrorCode, ErrorWithCode, IdentityType, LogLevel, MsGraphAuthProvider, OnBehalfOfUserCredential, TeamsBotSsoPrompt, TeamsFx, TeamsUserCredential, createMicrosoftGraphClient, getLogLevel, getTediousConnectionConfig, setLogFunction, setLogLevel, setLogger };
1207
1174
  //# sourceMappingURL=index.esm2017.js.map