@microsoft/teamsfx 0.6.3-alpha.8d048e1f1.0 → 0.6.3-alpha.f018de6e6.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.
@@ -12,6 +12,7 @@ var botbuilder = require('botbuilder');
12
12
  var botbuilderDialogs = require('botbuilder-dialogs');
13
13
  var uuid = require('uuid');
14
14
  var axios = require('axios');
15
+ var https = require('https');
15
16
  var path = require('path');
16
17
  var fs = require('fs');
17
18
 
@@ -100,6 +101,10 @@ exports.ErrorCode = void 0;
100
101
  * Identity type error.
101
102
  */
102
103
  ErrorCode["IdentityTypeNotSupported"] = "IdentityTypeNotSupported";
104
+ /**
105
+ * Authentication info already exists error.
106
+ */
107
+ ErrorCode["AuthorizationInfoAlreadyExists"] = "AuthorizationInfoAlreadyExists";
103
108
  })(exports.ErrorCode || (exports.ErrorCode = {}));
104
109
  /**
105
110
  * @internal
@@ -121,6 +126,12 @@ ErrorMessage.FailToAcquireTokenOnBehalfOfUser = "Failed to acquire access token
121
126
  ErrorMessage.OnlyMSTeamsChannelSupported = "{0} is only supported in MS Teams Channel";
122
127
  // IdentityTypeNotSupported Error
123
128
  ErrorMessage.IdentityTypeNotSupported = "{0} identity is not supported in {1}";
129
+ // AuthorizationInfoError
130
+ ErrorMessage.AuthorizationHeaderAlreadyExists = "Authorization header already exists!";
131
+ ErrorMessage.BasicCredentialAlreadyExists = "Basic credential already exists!";
132
+ // InvalidParameter Error
133
+ ErrorMessage.EmptyParameter = "Parameter {0} is empty";
134
+ ErrorMessage.DuplicateHttpsOptionProperty = "Axios HTTPS agent already defined value for property {0}";
124
135
  /**
125
136
  * Error class with code and message thrown by the SDK.
126
137
  *
@@ -1489,6 +1500,10 @@ class BearerTokenAuthProvider {
1489
1500
  * @param config - Contains all the request information and can be updated to include extra authentication info.
1490
1501
  * Refer https://axios-http.com/docs/req_config for detailed document.
1491
1502
  *
1503
+ * @returns Updated axios request config.
1504
+ *
1505
+ * @throws {@link ErrorCode|AuthorizationInfoAlreadyExists} - when Authorization header already exists in request configuration.
1506
+ *
1492
1507
  * @beta
1493
1508
  */
1494
1509
  AddAuthenticationInfo(config) {
@@ -1498,7 +1513,7 @@ class BearerTokenAuthProvider {
1498
1513
  config.headers = {};
1499
1514
  }
1500
1515
  if (config.headers["Authorization"]) {
1501
- throw new Error("Authorization header already exists!");
1516
+ throw new ErrorWithCode(ErrorMessage.AuthorizationHeaderAlreadyExists, exports.ErrorCode.AuthorizationInfoAlreadyExists);
1502
1517
  }
1503
1518
  config.headers["Authorization"] = `Bearer ${token}`;
1504
1519
  return config;
@@ -1506,6 +1521,158 @@ class BearerTokenAuthProvider {
1506
1521
  }
1507
1522
  }
1508
1523
 
1524
+ // Copyright (c) Microsoft Corporation.
1525
+ /**
1526
+ * Provider that handles Basic authentication
1527
+ *
1528
+ * @beta
1529
+ */
1530
+ class BasicAuthProvider {
1531
+ /**
1532
+ *
1533
+ * @param userName - Username used in basic auth
1534
+ * @param password - Password used in basic auth
1535
+ *
1536
+ * @beta
1537
+ */
1538
+ constructor(userName, password) {
1539
+ if (!userName) {
1540
+ throw new ErrorWithCode(formatString(ErrorMessage.EmptyParameter, "username"), exports.ErrorCode.InvalidParameter);
1541
+ }
1542
+ if (!password) {
1543
+ throw new ErrorWithCode(formatString(ErrorMessage.EmptyParameter, "password"), exports.ErrorCode.InvalidParameter);
1544
+ }
1545
+ this.userName = userName;
1546
+ this.password = password;
1547
+ }
1548
+ /**
1549
+ * Adds authentication info to http requests
1550
+ *
1551
+ * @param config - Contains all the request information and can be updated to include extra authentication info.
1552
+ * Refer https://axios-http.com/docs/req_config for detailed document.
1553
+ *
1554
+ * @returns Updated axios request config.
1555
+ *
1556
+ * @throws {@link ErrorCode|AuthorizationInfoAlreadyExists} - when Authorization header or auth property already exists in request configuration.
1557
+ *
1558
+ * @beta
1559
+ */
1560
+ AddAuthenticationInfo(config) {
1561
+ return tslib.__awaiter(this, void 0, void 0, function* () {
1562
+ if (config.headers && config.headers["Authorization"]) {
1563
+ throw new ErrorWithCode(ErrorMessage.AuthorizationHeaderAlreadyExists, exports.ErrorCode.AuthorizationInfoAlreadyExists);
1564
+ }
1565
+ if (config.auth) {
1566
+ throw new ErrorWithCode(ErrorMessage.BasicCredentialAlreadyExists, exports.ErrorCode.AuthorizationInfoAlreadyExists);
1567
+ }
1568
+ config.auth = {
1569
+ username: this.userName,
1570
+ password: this.password,
1571
+ };
1572
+ return config;
1573
+ });
1574
+ }
1575
+ }
1576
+
1577
+ // Copyright (c) Microsoft Corporation.
1578
+ /**
1579
+ * Provider that handles Certificate authentication
1580
+ *
1581
+ * @beta
1582
+ */
1583
+ class CertificateAuthProvider {
1584
+ /**
1585
+ *
1586
+ * @param { SecureContextOptions } certOption - information about the cert used in http requests
1587
+ *
1588
+ * @beta
1589
+ */
1590
+ constructor(certOption) {
1591
+ if (certOption && Object.keys(certOption).length !== 0) {
1592
+ this.certOption = certOption;
1593
+ }
1594
+ else {
1595
+ throw new ErrorWithCode(formatString(ErrorMessage.EmptyParameter, "certOption"), exports.ErrorCode.InvalidParameter);
1596
+ }
1597
+ }
1598
+ /**
1599
+ * Adds authentication info to http requests.
1600
+ *
1601
+ * @param { AxiosRequestConfig } config - Contains all the request information and can be updated to include extra authentication info.
1602
+ * Refer https://axios-http.com/docs/req_config for detailed document.
1603
+ *
1604
+ * @returns Updated axios request config.
1605
+ *
1606
+ * @throws {@link ErrorCode|InvalidParameter} - when custom httpsAgent in the request has duplicate properties with certOption provided in constructor.
1607
+ *
1608
+ * @beta
1609
+ */
1610
+ AddAuthenticationInfo(config) {
1611
+ return tslib.__awaiter(this, void 0, void 0, function* () {
1612
+ if (!config.httpsAgent) {
1613
+ config.httpsAgent = new https.Agent(this.certOption);
1614
+ }
1615
+ else {
1616
+ const existingProperties = new Set(Object.keys(config.httpsAgent.options));
1617
+ for (const property of Object.keys(this.certOption)) {
1618
+ if (existingProperties.has(property)) {
1619
+ throw new ErrorWithCode(formatString(ErrorMessage.DuplicateHttpsOptionProperty, property), exports.ErrorCode.InvalidParameter);
1620
+ }
1621
+ }
1622
+ Object.assign(config.httpsAgent.options, this.certOption);
1623
+ }
1624
+ return config;
1625
+ });
1626
+ }
1627
+ }
1628
+ /**
1629
+ * Helper to create SecureContextOptions from PEM format cert
1630
+ *
1631
+ * @param { string | Buffer } cert - The cert chain in PEM format
1632
+ * @param { string | Buffer } key - The private key for the cert chain
1633
+ * @param { string? } passphrase - The passphrase for private key
1634
+ * @param { string? | Buffer? } ca - Overrides the trusted CA certificates
1635
+ *
1636
+ * @returns Instance of SecureContextOptions
1637
+ *
1638
+ * @throws {@link ErrorCode|InvalidParameter} - when any parameter is empty
1639
+ *
1640
+ */
1641
+ function createPemCertOption(cert, key, passphrase, ca) {
1642
+ if (cert.length === 0) {
1643
+ throw new ErrorWithCode(formatString(ErrorMessage.EmptyParameter, "cert"), exports.ErrorCode.InvalidParameter);
1644
+ }
1645
+ if (key.length === 0) {
1646
+ throw new ErrorWithCode(formatString(ErrorMessage.EmptyParameter, "key"), exports.ErrorCode.InvalidParameter);
1647
+ }
1648
+ return {
1649
+ cert,
1650
+ key,
1651
+ passphrase,
1652
+ ca,
1653
+ };
1654
+ }
1655
+ /**
1656
+ * Helper to create SecureContextOptions from PFX format cert
1657
+ *
1658
+ * @param { string | Buffer } pfx - The content of .pfx file
1659
+ * @param { string? } passphrase - Optional. The passphrase of .pfx file
1660
+ *
1661
+ * @returns Instance of SecureContextOptions
1662
+ *
1663
+ * @throws {@link ErrorCode|InvalidParameter} - when any parameter is empty
1664
+ *
1665
+ */
1666
+ function createPfxCertOption(pfx, passphrase) {
1667
+ if (pfx.length === 0) {
1668
+ throw new ErrorWithCode(formatString(ErrorMessage.EmptyParameter, "pfx"), exports.ErrorCode.InvalidParameter);
1669
+ }
1670
+ return {
1671
+ pfx,
1672
+ passphrase,
1673
+ };
1674
+ }
1675
+
1509
1676
  // Copyright (c) Microsoft Corporation.
1510
1677
  /**
1511
1678
  * A class providing credential and configuration.
@@ -2347,7 +2514,7 @@ class TeamsBotInstallation {
2347
2514
  }
2348
2515
  }
2349
2516
  /**
2350
- * Provide utilities to send notification to varies targets (e.g., member, channel, incoming wehbook).
2517
+ * Provide utilities to send notification to varies targets (e.g., member, group, channel).
2351
2518
  *
2352
2519
  * @beta
2353
2520
  */
@@ -2418,7 +2585,7 @@ class NotificationBot {
2418
2585
  /**
2419
2586
  * Provide utilities for bot conversation, including:
2420
2587
  * - handle command and response.
2421
- * - send notification to varies targets (e.g., member, channel, incoming wehbook).
2588
+ * - send notification to varies targets (e.g., member, group, channel).
2422
2589
  *
2423
2590
  * @example
2424
2591
  * For command and response, you can register your commands through the constructor, or use the `registerCommand` and `registerCommands` API to add commands later.
@@ -2428,9 +2595,7 @@ class NotificationBot {
2428
2595
  * const conversationBot = new ConversationBot({
2429
2596
  * command: {
2430
2597
  * enabled: true,
2431
- * options: {
2432
- * commands: [ new HelloWorldCommandHandler() ],
2433
- * },
2598
+ * commands: [ new HelloWorldCommandHandler() ],
2434
2599
  * },
2435
2600
  * });
2436
2601
  *
@@ -2438,7 +2603,7 @@ class NotificationBot {
2438
2603
  * conversationBot.command.registerCommand(new HelpCommandHandler());
2439
2604
  * ```
2440
2605
  *
2441
- * For notification, you can enable notification at initialization, then send notificaations at any time.
2606
+ * For notification, you can enable notification at initialization, then send notifications at any time.
2442
2607
  *
2443
2608
  * ```typescript
2444
2609
  * // enable through constructor
@@ -2466,7 +2631,7 @@ class NotificationBot {
2466
2631
  *
2467
2632
  * For command and response, ensure each command should ONLY be registered with the command once, otherwise it'll cause unexpected behavior if you register the same command more than once.
2468
2633
  *
2469
- * For notification, set `notification.options.storage` in {@link ConversationOptions} to use your own storage implementation.
2634
+ * For notification, set `notification.storage` in {@link ConversationOptions} to use your own storage implementation.
2470
2635
  *
2471
2636
  * @beta
2472
2637
  */
@@ -2474,27 +2639,79 @@ class ConversationBot {
2474
2639
  /**
2475
2640
  * Creates new instance of the `ConversationBot`.
2476
2641
  *
2642
+ * @remarks
2643
+ * It's recommended to create your own adapter and storage for production environment instead of the default one.
2644
+ *
2477
2645
  * @param options - initialize options
2478
2646
  *
2479
2647
  * @beta
2480
2648
  */
2481
2649
  constructor(options) {
2650
+ var _a, _b;
2482
2651
  if (options.adapter) {
2483
2652
  this.adapter = options.adapter;
2484
2653
  }
2485
2654
  else {
2486
- this.adapter = new botbuilder.BotFrameworkAdapter({
2487
- appId: process.env.BOT_ID,
2488
- appPassword: process.env.BOT_PASSWORD,
2489
- });
2655
+ this.adapter = this.createDefaultAdapter(options.adapterConfig);
2490
2656
  }
2491
- if (options.command.enabled) {
2492
- this.command = new CommandBot(this.adapter, options.command.options);
2657
+ if ((_a = options.command) === null || _a === void 0 ? void 0 : _a.enabled) {
2658
+ this.command = new CommandBot(this.adapter, options.command);
2493
2659
  }
2494
- if (options.notification.enabled) {
2495
- this.notification = new NotificationBot(this.adapter, options.notification.options);
2660
+ if ((_b = options.notification) === null || _b === void 0 ? void 0 : _b.enabled) {
2661
+ this.notification = new NotificationBot(this.adapter, options.notification);
2496
2662
  }
2497
2663
  }
2664
+ createDefaultAdapter(adapterConfig) {
2665
+ const adapter = adapterConfig === undefined
2666
+ ? new botbuilder.BotFrameworkAdapter({
2667
+ appId: process.env.BOT_ID,
2668
+ appPassword: process.env.BOT_PASSWORD,
2669
+ })
2670
+ : new botbuilder.BotFrameworkAdapter(adapterConfig);
2671
+ // the default error handler
2672
+ adapter.onTurnError = (context, error) => tslib.__awaiter(this, void 0, void 0, function* () {
2673
+ // This check writes out errors to console.
2674
+ console.error(`[onTurnError] unhandled error: ${error}`);
2675
+ // Send a trace activity, which will be displayed in Bot Framework Emulator
2676
+ yield context.sendTraceActivity("OnTurnError Trace", `${error}`, "https://www.botframework.com/schemas/error", "TurnError");
2677
+ // Send a message to the user
2678
+ yield context.sendActivity(`The bot encountered unhandled error: ${error.message}`);
2679
+ yield context.sendActivity("To continue to run this bot, please fix the bot source code.");
2680
+ });
2681
+ return adapter;
2682
+ }
2683
+ /**
2684
+ * The request handler to integrate with web request.
2685
+ *
2686
+ * @param req - an Express or Restify style request object.
2687
+ * @param res - an Express or Restify style response object.
2688
+ * @param logic - the additional function to handle bot context.
2689
+ *
2690
+ * @example
2691
+ * For example, to use with Restify:
2692
+ * ``` typescript
2693
+ * // The default/empty behavior
2694
+ * server.post("api/messages", conversationBot.requestHandler);
2695
+ *
2696
+ * // Or, add your own logic
2697
+ * server.post("api/messages", async (req, res) => {
2698
+ * await conversationBot.requestHandler(req, res, async (context) => {
2699
+ * // your-own-context-logic
2700
+ * });
2701
+ * });
2702
+ * ```
2703
+ *
2704
+ * @beta
2705
+ */
2706
+ requestHandler(req, res, logic) {
2707
+ return tslib.__awaiter(this, void 0, void 0, function* () {
2708
+ if (logic === undefined) {
2709
+ // create empty logic
2710
+ logic = () => tslib.__awaiter(this, void 0, void 0, function* () { });
2711
+ }
2712
+ yield this.adapter.processActivity(req, res, logic);
2713
+ });
2714
+ }
2498
2715
  }
2499
2716
 
2500
2717
  // Copyright (c) Microsoft Corporation.
@@ -2506,8 +2723,8 @@ class MessageBuilder {
2506
2723
  /**
2507
2724
  * Build a bot message activity attached with adaptive card.
2508
2725
  *
2509
- * @param getCardData Function to prepare your card data.
2510
2726
  * @param cardTemplate The adaptive card template.
2727
+ * @param data card data used to render the template.
2511
2728
  * @returns A bot message activity attached with an adaptive card.
2512
2729
  *
2513
2730
  * @example
@@ -2532,19 +2749,18 @@ class MessageBuilder {
2532
2749
  * title: string,
2533
2750
  * description: string
2534
2751
  * };
2535
- * const card = MessageBuilder.attachAdaptiveCard<CardData>(() => {
2536
- * return {
2537
- * title: "sample card title",
2538
- * description: "sample card description"
2539
- * }}, cardTemplate);
2752
+ * const card = MessageBuilder.attachAdaptiveCard<CardData>(
2753
+ * cardTemplate, {
2754
+ * title: "sample card title",
2755
+ * description: "sample card description"
2756
+ * });
2540
2757
  * ```
2541
2758
  *
2542
2759
  * @beta
2543
2760
  */
2544
- static attachAdaptiveCard(getCardData, cardTemplate) {
2545
- const cardData = getCardData();
2761
+ static attachAdaptiveCard(cardTemplate, data) {
2546
2762
  return {
2547
- attachments: [botbuilder.CardFactory.adaptiveCard(AdaptiveCards.declare(cardTemplate).render(cardData))],
2763
+ attachments: [botbuilder.CardFactory.adaptiveCard(AdaptiveCards.declare(cardTemplate).render(data))],
2548
2764
  };
2549
2765
  }
2550
2766
  /**
@@ -2652,7 +2868,9 @@ class MessageBuilder {
2652
2868
  }
2653
2869
 
2654
2870
  exports.AppCredential = AppCredential;
2871
+ exports.BasicAuthProvider = BasicAuthProvider;
2655
2872
  exports.BearerTokenAuthProvider = BearerTokenAuthProvider;
2873
+ exports.CertificateAuthProvider = CertificateAuthProvider;
2656
2874
  exports.Channel = Channel;
2657
2875
  exports.CommandBot = CommandBot;
2658
2876
  exports.ConversationBot = ConversationBot;
@@ -2668,6 +2886,8 @@ exports.TeamsFx = TeamsFx;
2668
2886
  exports.TeamsUserCredential = TeamsUserCredential;
2669
2887
  exports.createApiClient = createApiClient;
2670
2888
  exports.createMicrosoftGraphClient = createMicrosoftGraphClient;
2889
+ exports.createPemCertOption = createPemCertOption;
2890
+ exports.createPfxCertOption = createPfxCertOption;
2671
2891
  exports.getLogLevel = getLogLevel;
2672
2892
  exports.getTediousConnectionConfig = getTediousConnectionConfig;
2673
2893
  exports.sendAdaptiveCard = sendAdaptiveCard;