@microsoft/teamsfx 1.2.1 → 2.0.0-alpha.fcb7f02f1.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.
@@ -1,6 +1,6 @@
1
1
  import { __awaiter } from 'tslib';
2
2
  import jwt_decode from 'jwt-decode';
3
- import * as microsoftTeams from '@microsoft/teams-js';
3
+ import { app, authentication } from '@microsoft/teams-js';
4
4
  import { PublicClientApplication } from '@azure/msal-browser';
5
5
  import { Client } from '@microsoft/microsoft-graph-client';
6
6
  import axios from 'axios';
@@ -560,51 +560,48 @@ class TeamsUserCredential {
560
560
  if (!this.initialized) {
561
561
  yield this.init(resources);
562
562
  }
563
- return new Promise((resolve, reject) => {
564
- microsoftTeams.initialize(() => {
565
- microsoftTeams.authentication.authenticate({
566
- url: `${this.config.initiateLoginEndpoint}?clientId=${this.config.clientId}&scope=${encodeURI(scopesStr)}&loginHint=${this.loginHint}`,
567
- width: loginPageWidth,
568
- height: loginPageHeight,
569
- successCallback: (result) => __awaiter(this, void 0, void 0, function* () {
570
- if (!result) {
571
- const errorMsg = "Get empty authentication result from MSAL";
572
- internalLogger.error(errorMsg);
573
- reject(new ErrorWithCode(errorMsg, ErrorCode.InternalError));
574
- return;
575
- }
576
- let resultJson = {};
577
- try {
578
- resultJson = typeof result == "string" ? JSON.parse(result) : result;
579
- }
580
- catch (error) {
581
- // If can not parse result as Json, will throw error.
582
- const failedToParseResult = "Failed to parse response to Json.";
583
- internalLogger.error(failedToParseResult);
584
- reject(new ErrorWithCode(failedToParseResult, ErrorCode.InvalidResponse));
585
- }
586
- // If code exists in result, user may using previous auth-start and auth-end page.
587
- if (resultJson.code) {
588
- const helpLink = "https://aka.ms/teamsfx-auth-code-flow";
589
- const usingPreviousAuthPage = "Found auth code in response. Auth code is not support for current version of SDK. " +
590
- `Please refer to the help link for how to fix the issue: ${helpLink}.`;
591
- internalLogger.error(usingPreviousAuthPage);
592
- reject(new ErrorWithCode(usingPreviousAuthPage, ErrorCode.InvalidResponse));
593
- }
594
- // If sessionStorage exists in result, set the values in current session storage.
595
- if (resultJson.sessionStorage) {
596
- this.setSessionStorage(resultJson.sessionStorage);
597
- }
598
- resolve();
599
- }),
600
- failureCallback: (reason) => {
601
- const errorMsg = `Consent failed for the scope ${scopesStr} with error: ${reason}`;
602
- internalLogger.error(errorMsg);
603
- reject(new ErrorWithCode(errorMsg, ErrorCode.ConsentFailed));
604
- },
605
- });
606
- });
607
- });
563
+ yield app.initialize();
564
+ let result;
565
+ try {
566
+ const params = {
567
+ url: `${this.config.initiateLoginEndpoint}?clientId=${this.config.clientId}&scope=${encodeURI(scopesStr)}&loginHint=${this.loginHint}`,
568
+ width: loginPageWidth,
569
+ height: loginPageHeight,
570
+ };
571
+ result = yield authentication.authenticate(params);
572
+ if (!result) {
573
+ const errorMsg = "Get empty authentication result from MSAL";
574
+ internalLogger.error(errorMsg);
575
+ throw new ErrorWithCode(errorMsg, ErrorCode.InternalError);
576
+ }
577
+ }
578
+ catch (err) {
579
+ const errorMsg = `Consent failed for the scope ${scopesStr} with error: ${err.message}`;
580
+ internalLogger.error(errorMsg);
581
+ throw new ErrorWithCode(errorMsg, ErrorCode.ConsentFailed);
582
+ }
583
+ let resultJson = {};
584
+ try {
585
+ resultJson = typeof result == "string" ? JSON.parse(result) : result;
586
+ }
587
+ catch (error) {
588
+ // If can not parse result as Json, will throw error.
589
+ const failedToParseResult = "Failed to parse response to Json.";
590
+ internalLogger.error(failedToParseResult);
591
+ throw new ErrorWithCode(failedToParseResult, ErrorCode.InvalidResponse);
592
+ }
593
+ // If code exists in result, user may using previous auth-start and auth-end page.
594
+ if (resultJson.code) {
595
+ const helpLink = "https://aka.ms/teamsfx-auth-code-flow";
596
+ const usingPreviousAuthPage = "Found auth code in response. Auth code is not support for current version of SDK. " +
597
+ `Please refer to the help link for how to fix the issue: ${helpLink}.`;
598
+ internalLogger.error(usingPreviousAuthPage);
599
+ throw new ErrorWithCode(usingPreviousAuthPage, ErrorCode.InvalidResponse);
600
+ }
601
+ // If sessionStorage exists in result, set the values in current session storage.
602
+ if (resultJson.sessionStorage) {
603
+ this.setSessionStorage(resultJson.sessionStorage);
604
+ }
608
605
  });
609
606
  }
610
607
  /**
@@ -754,52 +751,48 @@ class TeamsUserCredential {
754
751
  * @returns SSO token
755
752
  */
756
753
  getSSOToken(resources) {
757
- return new Promise((resolve, reject) => {
754
+ return __awaiter(this, void 0, void 0, function* () {
758
755
  if (this.ssoToken) {
759
756
  if (this.ssoToken.expiresOnTimestamp - Date.now() > tokenRefreshTimeSpanInMillisecond) {
760
757
  internalLogger.verbose("Get SSO token from memory cache");
761
- resolve(this.ssoToken);
762
- return;
758
+ return this.ssoToken;
763
759
  }
764
760
  }
765
- if (this.checkInTeams()) {
766
- microsoftTeams.initialize(() => {
767
- microsoftTeams.authentication.getAuthToken({
768
- successCallback: (token) => {
769
- if (!token) {
770
- const errorMsg = "Get empty SSO token from Teams";
771
- internalLogger.error(errorMsg);
772
- reject(new ErrorWithCode(errorMsg, ErrorCode.InternalError));
773
- return;
774
- }
775
- const tokenObject = parseJwt(token);
776
- if (tokenObject.ver !== "1.0" && tokenObject.ver !== "2.0") {
777
- const errorMsg = "SSO token is not valid with an unknown version: " + tokenObject.ver;
778
- internalLogger.error(errorMsg);
779
- reject(new ErrorWithCode(errorMsg, ErrorCode.InternalError));
780
- return;
781
- }
782
- const ssoToken = {
783
- token,
784
- expiresOnTimestamp: tokenObject.exp * 1000,
785
- };
786
- this.ssoToken = ssoToken;
787
- resolve(ssoToken);
788
- },
789
- failureCallback: (errMessage) => {
790
- const errorMsg = "Get SSO token failed with error: " + errMessage;
791
- internalLogger.error(errorMsg);
792
- reject(new ErrorWithCode(errorMsg, ErrorCode.InternalError));
793
- },
794
- resources: resources !== null && resources !== void 0 ? resources : [],
795
- });
796
- });
761
+ const params = { resources: resources !== null && resources !== void 0 ? resources : [] };
762
+ let token;
763
+ try {
764
+ yield app.initialize();
797
765
  }
798
- else {
799
- const errorMsg = "Initialize teams sdk failed due to not running inside Teams";
766
+ catch (err) {
767
+ const errorMsg = "Initialize teams sdk failed due to not running inside Teams environment";
768
+ internalLogger.error(errorMsg);
769
+ throw new ErrorWithCode(errorMsg, ErrorCode.InternalError);
770
+ }
771
+ try {
772
+ token = yield authentication.getAuthToken(params);
773
+ }
774
+ catch (err) {
775
+ const errorMsg = "Get SSO token failed with error: " + err.message;
776
+ internalLogger.error(errorMsg);
777
+ throw new ErrorWithCode(errorMsg, ErrorCode.InternalError);
778
+ }
779
+ if (!token) {
780
+ const errorMsg = "Get empty SSO token from Teams";
800
781
  internalLogger.error(errorMsg);
801
- reject(new ErrorWithCode(errorMsg, ErrorCode.InternalError));
782
+ throw new ErrorWithCode(errorMsg, ErrorCode.InternalError);
802
783
  }
784
+ const tokenObject = parseJwt(token);
785
+ if (tokenObject.ver !== "1.0" && tokenObject.ver !== "2.0") {
786
+ const errorMsg = "SSO token is not valid with an unknown version: " + tokenObject.ver;
787
+ internalLogger.error(errorMsg);
788
+ throw new ErrorWithCode(errorMsg, ErrorCode.InternalError);
789
+ }
790
+ const ssoToken = {
791
+ token,
792
+ expiresOnTimestamp: tokenObject.exp * 1000,
793
+ };
794
+ this.ssoToken = ssoToken;
795
+ return ssoToken;
803
796
  });
804
797
  }
805
798
  /**
@@ -840,16 +833,6 @@ class TeamsUserCredential {
840
833
  throw new ErrorWithCode(errorMessage, ErrorCode.InternalError);
841
834
  }
842
835
  }
843
- // Come from here: https://github.com/wictorwilen/msteams-react-base-component/blob/master/src/useTeams.ts
844
- checkInTeams() {
845
- if ((window.parent === window.self && window.nativeInterface) ||
846
- window.navigator.userAgent.includes("Teams/") ||
847
- window.name === "embedded-page-container" ||
848
- window.name === "extension-tab-frame") {
849
- return true;
850
- }
851
- return false;
852
- }
853
836
  }
854
837
 
855
838
  // Copyright (c) Microsoft Corporation.
@@ -973,6 +956,9 @@ function createMicrosoftGraphClient(teamsfx, scopes) {
973
956
  // Copyright (c) Microsoft Corporation.
974
957
  /**
975
958
  * Generate connection configuration consumed by tedious.
959
+ *
960
+ * @deprecated we recommend you compose your own Tedious configuration for better flexibility.
961
+ *
976
962
  * @remarks
977
963
  * Only works in in server side.
978
964
  */