@microsoft/teamsfx 1.2.1 → 2.0.0-alpha.c10b77e4b.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,5 +1,5 @@
1
1
  import jwt_decode from 'jwt-decode';
2
- import * as microsoftTeams from '@microsoft/teams-js';
2
+ import { app, authentication } from '@microsoft/teams-js';
3
3
  import { PublicClientApplication } from '@azure/msal-browser';
4
4
  import { Client } from '@microsoft/microsoft-graph-client';
5
5
  import axios from 'axios';
@@ -554,51 +554,48 @@ class TeamsUserCredential {
554
554
  if (!this.initialized) {
555
555
  await this.init(resources);
556
556
  }
557
- return new Promise((resolve, reject) => {
558
- microsoftTeams.initialize(() => {
559
- microsoftTeams.authentication.authenticate({
560
- url: `${this.config.initiateLoginEndpoint}?clientId=${this.config.clientId}&scope=${encodeURI(scopesStr)}&loginHint=${this.loginHint}`,
561
- width: loginPageWidth,
562
- height: loginPageHeight,
563
- successCallback: async (result) => {
564
- if (!result) {
565
- const errorMsg = "Get empty authentication result from MSAL";
566
- internalLogger.error(errorMsg);
567
- reject(new ErrorWithCode(errorMsg, ErrorCode.InternalError));
568
- return;
569
- }
570
- let resultJson = {};
571
- try {
572
- resultJson = typeof result == "string" ? JSON.parse(result) : result;
573
- }
574
- catch (error) {
575
- // If can not parse result as Json, will throw error.
576
- const failedToParseResult = "Failed to parse response to Json.";
577
- internalLogger.error(failedToParseResult);
578
- reject(new ErrorWithCode(failedToParseResult, ErrorCode.InvalidResponse));
579
- }
580
- // If code exists in result, user may using previous auth-start and auth-end page.
581
- if (resultJson.code) {
582
- const helpLink = "https://aka.ms/teamsfx-auth-code-flow";
583
- const usingPreviousAuthPage = "Found auth code in response. Auth code is not support for current version of SDK. " +
584
- `Please refer to the help link for how to fix the issue: ${helpLink}.`;
585
- internalLogger.error(usingPreviousAuthPage);
586
- reject(new ErrorWithCode(usingPreviousAuthPage, ErrorCode.InvalidResponse));
587
- }
588
- // If sessionStorage exists in result, set the values in current session storage.
589
- if (resultJson.sessionStorage) {
590
- this.setSessionStorage(resultJson.sessionStorage);
591
- }
592
- resolve();
593
- },
594
- failureCallback: (reason) => {
595
- const errorMsg = `Consent failed for the scope ${scopesStr} with error: ${reason}`;
596
- internalLogger.error(errorMsg);
597
- reject(new ErrorWithCode(errorMsg, ErrorCode.ConsentFailed));
598
- },
599
- });
600
- });
601
- });
557
+ await app.initialize();
558
+ let result;
559
+ try {
560
+ const params = {
561
+ url: `${this.config.initiateLoginEndpoint}?clientId=${this.config.clientId}&scope=${encodeURI(scopesStr)}&loginHint=${this.loginHint}`,
562
+ width: loginPageWidth,
563
+ height: loginPageHeight,
564
+ };
565
+ result = await authentication.authenticate(params);
566
+ if (!result) {
567
+ const errorMsg = "Get empty authentication result from MSAL";
568
+ internalLogger.error(errorMsg);
569
+ throw new ErrorWithCode(errorMsg, ErrorCode.InternalError);
570
+ }
571
+ }
572
+ catch (err) {
573
+ const errorMsg = `Consent failed for the scope ${scopesStr} with error: ${err.message}`;
574
+ internalLogger.error(errorMsg);
575
+ throw new ErrorWithCode(errorMsg, ErrorCode.ConsentFailed);
576
+ }
577
+ let resultJson = {};
578
+ try {
579
+ resultJson = typeof result == "string" ? JSON.parse(result) : result;
580
+ }
581
+ catch (error) {
582
+ // If can not parse result as Json, will throw error.
583
+ const failedToParseResult = "Failed to parse response to Json.";
584
+ internalLogger.error(failedToParseResult);
585
+ throw new ErrorWithCode(failedToParseResult, ErrorCode.InvalidResponse);
586
+ }
587
+ // If code exists in result, user may using previous auth-start and auth-end page.
588
+ if (resultJson.code) {
589
+ const helpLink = "https://aka.ms/teamsfx-auth-code-flow";
590
+ const usingPreviousAuthPage = "Found auth code in response. Auth code is not support for current version of SDK. " +
591
+ `Please refer to the help link for how to fix the issue: ${helpLink}.`;
592
+ internalLogger.error(usingPreviousAuthPage);
593
+ throw new ErrorWithCode(usingPreviousAuthPage, ErrorCode.InvalidResponse);
594
+ }
595
+ // If sessionStorage exists in result, set the values in current session storage.
596
+ if (resultJson.sessionStorage) {
597
+ this.setSessionStorage(resultJson.sessionStorage);
598
+ }
602
599
  }
603
600
  /**
604
601
  * Get access token from credential.
@@ -740,54 +737,48 @@ class TeamsUserCredential {
740
737
  *
741
738
  * @returns SSO token
742
739
  */
743
- getSSOToken(resources) {
744
- return new Promise((resolve, reject) => {
745
- if (this.ssoToken) {
746
- if (this.ssoToken.expiresOnTimestamp - Date.now() > tokenRefreshTimeSpanInMillisecond) {
747
- internalLogger.verbose("Get SSO token from memory cache");
748
- resolve(this.ssoToken);
749
- return;
750
- }
751
- }
752
- if (this.checkInTeams()) {
753
- microsoftTeams.initialize(() => {
754
- microsoftTeams.authentication.getAuthToken({
755
- successCallback: (token) => {
756
- if (!token) {
757
- const errorMsg = "Get empty SSO token from Teams";
758
- internalLogger.error(errorMsg);
759
- reject(new ErrorWithCode(errorMsg, ErrorCode.InternalError));
760
- return;
761
- }
762
- const tokenObject = parseJwt(token);
763
- if (tokenObject.ver !== "1.0" && tokenObject.ver !== "2.0") {
764
- const errorMsg = "SSO token is not valid with an unknown version: " + tokenObject.ver;
765
- internalLogger.error(errorMsg);
766
- reject(new ErrorWithCode(errorMsg, ErrorCode.InternalError));
767
- return;
768
- }
769
- const ssoToken = {
770
- token,
771
- expiresOnTimestamp: tokenObject.exp * 1000,
772
- };
773
- this.ssoToken = ssoToken;
774
- resolve(ssoToken);
775
- },
776
- failureCallback: (errMessage) => {
777
- const errorMsg = "Get SSO token failed with error: " + errMessage;
778
- internalLogger.error(errorMsg);
779
- reject(new ErrorWithCode(errorMsg, ErrorCode.InternalError));
780
- },
781
- resources: resources !== null && resources !== void 0 ? resources : [],
782
- });
783
- });
740
+ async getSSOToken(resources) {
741
+ if (this.ssoToken) {
742
+ if (this.ssoToken.expiresOnTimestamp - Date.now() > tokenRefreshTimeSpanInMillisecond) {
743
+ internalLogger.verbose("Get SSO token from memory cache");
744
+ return this.ssoToken;
784
745
  }
785
- else {
786
- const errorMsg = "Initialize teams sdk failed due to not running inside Teams";
787
- internalLogger.error(errorMsg);
788
- reject(new ErrorWithCode(errorMsg, ErrorCode.InternalError));
789
- }
790
- });
746
+ }
747
+ const params = { resources: resources !== null && resources !== void 0 ? resources : [] };
748
+ let token;
749
+ try {
750
+ await app.initialize();
751
+ }
752
+ catch (err) {
753
+ const errorMsg = "Initialize teams sdk failed due to not running inside Teams environment";
754
+ internalLogger.error(errorMsg);
755
+ throw new ErrorWithCode(errorMsg, ErrorCode.InternalError);
756
+ }
757
+ try {
758
+ token = await authentication.getAuthToken(params);
759
+ }
760
+ catch (err) {
761
+ const errorMsg = "Get SSO token failed with error: " + err.message;
762
+ internalLogger.error(errorMsg);
763
+ throw new ErrorWithCode(errorMsg, ErrorCode.InternalError);
764
+ }
765
+ if (!token) {
766
+ const errorMsg = "Get empty SSO token from Teams";
767
+ internalLogger.error(errorMsg);
768
+ throw new ErrorWithCode(errorMsg, ErrorCode.InternalError);
769
+ }
770
+ const tokenObject = parseJwt(token);
771
+ if (tokenObject.ver !== "1.0" && tokenObject.ver !== "2.0") {
772
+ const errorMsg = "SSO token is not valid with an unknown version: " + tokenObject.ver;
773
+ internalLogger.error(errorMsg);
774
+ throw new ErrorWithCode(errorMsg, ErrorCode.InternalError);
775
+ }
776
+ const ssoToken = {
777
+ token,
778
+ expiresOnTimestamp: tokenObject.exp * 1000,
779
+ };
780
+ this.ssoToken = ssoToken;
781
+ return ssoToken;
791
782
  }
792
783
  /**
793
784
  * Load and validate authentication configuration
@@ -827,16 +818,6 @@ class TeamsUserCredential {
827
818
  throw new ErrorWithCode(errorMessage, ErrorCode.InternalError);
828
819
  }
829
820
  }
830
- // Come from here: https://github.com/wictorwilen/msteams-react-base-component/blob/master/src/useTeams.ts
831
- checkInTeams() {
832
- if ((window.parent === window.self && window.nativeInterface) ||
833
- window.navigator.userAgent.includes("Teams/") ||
834
- window.name === "embedded-page-container" ||
835
- window.name === "extension-tab-frame") {
836
- return true;
837
- }
838
- return false;
839
- }
840
821
  }
841
822
 
842
823
  // Copyright (c) Microsoft Corporation.
@@ -958,6 +939,9 @@ function createMicrosoftGraphClient(teamsfx, scopes) {
958
939
  // Copyright (c) Microsoft Corporation.
959
940
  /**
960
941
  * Generate connection configuration consumed by tedious.
942
+ *
943
+ * @deprecated we recommend you compose your own Tedious configuration for better flexibility.
944
+ *
961
945
  * @remarks
962
946
  * Only works in in server side.
963
947
  */