@microsoft/teamsfx 1.0.0-beta.ea516ab4c.0 → 2.0.0-beta.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';
@@ -554,51 +554,48 @@ class TeamsUserCredential {
554
554
  if (!this.initialized) {
555
555
  yield this.init();
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: (result) => __awaiter(this, void 0, void 0, function* () {
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
+ yield 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 = yield 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
  /**
@@ -739,52 +736,41 @@ class TeamsUserCredential {
739
736
  * @returns SSO token
740
737
  */
741
738
  getSSOToken() {
742
- return new Promise((resolve, reject) => {
739
+ return __awaiter(this, void 0, void 0, function* () {
743
740
  if (this.ssoToken) {
744
741
  if (this.ssoToken.expiresOnTimestamp - Date.now() > tokenRefreshTimeSpanInMillisecond) {
745
742
  internalLogger.verbose("Get SSO token from memory cache");
746
- resolve(this.ssoToken);
747
- return;
743
+ return this.ssoToken;
748
744
  }
749
745
  }
750
- if (this.checkInTeams()) {
751
- microsoftTeams.initialize(() => {
752
- microsoftTeams.authentication.getAuthToken({
753
- successCallback: (token) => {
754
- if (!token) {
755
- const errorMsg = "Get empty SSO token from Teams";
756
- internalLogger.error(errorMsg);
757
- reject(new ErrorWithCode(errorMsg, ErrorCode.InternalError));
758
- return;
759
- }
760
- const tokenObject = parseJwt(token);
761
- if (tokenObject.ver !== "1.0" && tokenObject.ver !== "2.0") {
762
- const errorMsg = "SSO token is not valid with an unknown version: " + tokenObject.ver;
763
- internalLogger.error(errorMsg);
764
- reject(new ErrorWithCode(errorMsg, ErrorCode.InternalError));
765
- return;
766
- }
767
- const ssoToken = {
768
- token,
769
- expiresOnTimestamp: tokenObject.exp * 1000,
770
- };
771
- this.ssoToken = ssoToken;
772
- resolve(ssoToken);
773
- },
774
- failureCallback: (errMessage) => {
775
- const errorMsg = "Get SSO token failed with error: " + errMessage;
776
- internalLogger.error(errorMsg);
777
- reject(new ErrorWithCode(errorMsg, ErrorCode.InternalError));
778
- },
779
- resources: [],
780
- });
781
- });
746
+ const params = {};
747
+ let token;
748
+ try {
749
+ yield app.initialize();
750
+ token = yield authentication.getAuthToken(params);
782
751
  }
783
- else {
784
- const errorMsg = "Initialize teams sdk failed due to not running inside Teams";
752
+ catch (err) {
753
+ const errorMsg = "Get SSO token failed with error: " + err.message;
785
754
  internalLogger.error(errorMsg);
786
- reject(new ErrorWithCode(errorMsg, ErrorCode.InternalError));
755
+ throw new ErrorWithCode(errorMsg, ErrorCode.InternalError);
787
756
  }
757
+ if (!token) {
758
+ const errorMsg = "Get empty SSO token from Teams";
759
+ internalLogger.error(errorMsg);
760
+ throw new ErrorWithCode(errorMsg, ErrorCode.InternalError);
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
+ throw new ErrorWithCode(errorMsg, ErrorCode.InternalError);
767
+ }
768
+ const ssoToken = {
769
+ token,
770
+ expiresOnTimestamp: tokenObject.exp * 1000,
771
+ };
772
+ this.ssoToken = ssoToken;
773
+ return ssoToken;
788
774
  });
789
775
  }
790
776
  /**