@microsoft/teamsfx 0.4.1-alpha.fcc60ca0.0 → 0.4.2-alpha.e84c0d19.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.
- package/dist/index.esm2017.js +116 -224
- package/dist/index.esm2017.js.map +1 -1
- package/dist/index.esm2017.mjs.map +1 -1
- package/dist/index.esm5.js +167 -311
- package/dist/index.esm5.js.map +1 -1
- package/dist/index.node.cjs.js.map +1 -1
- package/package.json +6 -5
- package/types/teamsfx.d.ts +1 -1
package/dist/index.esm5.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { __extends, __awaiter, __generator } from 'tslib';
|
|
2
2
|
import jwt_decode from 'jwt-decode';
|
|
3
3
|
import * as microsoftTeams from '@microsoft/teams-js';
|
|
4
|
-
import
|
|
4
|
+
import { PublicClientApplication } from '@azure/msal-browser';
|
|
5
5
|
import { Client } from '@microsoft/microsoft-graph-client';
|
|
6
6
|
|
|
7
7
|
// Copyright (c) Microsoft Corporation.
|
|
@@ -318,6 +318,57 @@ function getUserInfoFromSsoToken(ssoToken) {
|
|
|
318
318
|
}
|
|
319
319
|
return userInfo;
|
|
320
320
|
}
|
|
321
|
+
/**
|
|
322
|
+
* @internal
|
|
323
|
+
*/
|
|
324
|
+
function getTenantIdAndLoginHintFromSsoToken(ssoToken) {
|
|
325
|
+
if (!ssoToken) {
|
|
326
|
+
var errorMsg = "SSO token is undefined.";
|
|
327
|
+
internalLogger.error(errorMsg);
|
|
328
|
+
throw new ErrorWithCode(errorMsg, ErrorCode.InvalidParameter);
|
|
329
|
+
}
|
|
330
|
+
var tokenObject = parseJwt(ssoToken);
|
|
331
|
+
var userInfo = {
|
|
332
|
+
tid: tokenObject.tid,
|
|
333
|
+
loginHint: tokenObject.ver === "2.0"
|
|
334
|
+
? tokenObject.preferred_username
|
|
335
|
+
: tokenObject.upn,
|
|
336
|
+
};
|
|
337
|
+
return userInfo;
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* @internal
|
|
341
|
+
*/
|
|
342
|
+
function parseAccessTokenFromAuthCodeTokenResponse(tokenResponse) {
|
|
343
|
+
try {
|
|
344
|
+
var tokenResponseObject = typeof tokenResponse == "string"
|
|
345
|
+
? JSON.parse(tokenResponse)
|
|
346
|
+
: tokenResponse;
|
|
347
|
+
if (!tokenResponseObject || !tokenResponseObject.accessToken) {
|
|
348
|
+
var errorMsg = "Get empty access token from Auth Code token response.";
|
|
349
|
+
internalLogger.error(errorMsg);
|
|
350
|
+
throw new Error(errorMsg);
|
|
351
|
+
}
|
|
352
|
+
var token = tokenResponseObject.accessToken;
|
|
353
|
+
var tokenObject = parseJwt(token);
|
|
354
|
+
if (tokenObject.ver !== "1.0" && tokenObject.ver !== "2.0") {
|
|
355
|
+
var errorMsg = "SSO token is not valid with an unknown version: " + tokenObject.ver;
|
|
356
|
+
internalLogger.error(errorMsg);
|
|
357
|
+
throw new Error(errorMsg);
|
|
358
|
+
}
|
|
359
|
+
var accessToken = {
|
|
360
|
+
token: token,
|
|
361
|
+
expiresOnTimestamp: tokenObject.exp * 1000,
|
|
362
|
+
};
|
|
363
|
+
return accessToken;
|
|
364
|
+
}
|
|
365
|
+
catch (error) {
|
|
366
|
+
var errorMsg = "Parse access token failed from Auth Code token response in node env with error: " +
|
|
367
|
+
error.message;
|
|
368
|
+
internalLogger.error(errorMsg);
|
|
369
|
+
throw new ErrorWithCode(errorMsg, ErrorCode.InternalError);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
321
372
|
/**
|
|
322
373
|
* Format string template with replacements
|
|
323
374
|
*
|
|
@@ -572,46 +623,10 @@ var OnBehalfOfUserCredential = /** @class */ (function () {
|
|
|
572
623
|
}());
|
|
573
624
|
|
|
574
625
|
// Copyright (c) Microsoft Corporation.
|
|
575
|
-
// Licensed under the MIT license.
|
|
576
|
-
/**
|
|
577
|
-
* Configuration used in initialization.
|
|
578
|
-
* @internal
|
|
579
|
-
*/
|
|
580
|
-
var Cache = /** @class */ (function () {
|
|
581
|
-
function Cache() {
|
|
582
|
-
}
|
|
583
|
-
Cache.get = function (key) {
|
|
584
|
-
return sessionStorage.getItem(key);
|
|
585
|
-
};
|
|
586
|
-
Cache.set = function (key, value) {
|
|
587
|
-
sessionStorage.setItem(key, value);
|
|
588
|
-
};
|
|
589
|
-
Cache.remove = function (key) {
|
|
590
|
-
sessionStorage.removeItem(key);
|
|
591
|
-
};
|
|
592
|
-
return Cache;
|
|
593
|
-
}());
|
|
594
|
-
|
|
595
|
-
// Copyright (c) Microsoft Corporation.
|
|
596
|
-
// Licensed under the MIT license.
|
|
597
|
-
/**
|
|
598
|
-
* @internal
|
|
599
|
-
*/
|
|
600
|
-
var GrantType;
|
|
601
|
-
(function (GrantType) {
|
|
602
|
-
GrantType["authCode"] = "authorization_code";
|
|
603
|
-
GrantType["ssoToken"] = "sso_token";
|
|
604
|
-
})(GrantType || (GrantType = {}));
|
|
605
|
-
|
|
606
|
-
// Copyright (c) Microsoft Corporation.
|
|
607
|
-
var accessTokenCacheKeyPrefix = "accessToken";
|
|
608
|
-
var separator = "-";
|
|
609
626
|
var tokenRefreshTimeSpanInMillisecond = 5 * 60 * 1000;
|
|
610
627
|
var initializeTeamsSdkTimeoutInMillisecond = 5000;
|
|
611
628
|
var loginPageWidth = 600;
|
|
612
629
|
var loginPageHeight = 535;
|
|
613
|
-
var maxRetryCount = 3;
|
|
614
|
-
var retryTimeSpanInMillisecond = 3000;
|
|
615
630
|
/**
|
|
616
631
|
* Represent Teams current user's identity, and it is used within Teams tab application.
|
|
617
632
|
*
|
|
@@ -629,7 +644,6 @@ var TeamsUserCredential = /** @class */ (function () {
|
|
|
629
644
|
* ```typescript
|
|
630
645
|
* const config = {
|
|
631
646
|
* authentication: {
|
|
632
|
-
* runtimeConnectorEndpoint: "https://xxx.xxx.com",
|
|
633
647
|
* initiateLoginEndpoint: "https://localhost:3000/auth-start.html",
|
|
634
648
|
* clientId: "xxx"
|
|
635
649
|
* }
|
|
@@ -647,6 +661,7 @@ var TeamsUserCredential = /** @class */ (function () {
|
|
|
647
661
|
internalLogger.info("Create teams user credential");
|
|
648
662
|
this.config = this.loadAndValidateConfig();
|
|
649
663
|
this.ssoToken = null;
|
|
664
|
+
this.initialized = false;
|
|
650
665
|
}
|
|
651
666
|
/**
|
|
652
667
|
* Popup login page to get user's access token with specific scopes.
|
|
@@ -664,7 +679,6 @@ var TeamsUserCredential = /** @class */ (function () {
|
|
|
664
679
|
* @param scopes - The list of scopes for which the token will have access, before that, we will request user to consent.
|
|
665
680
|
*
|
|
666
681
|
* @throws {@link ErrorCode|InternalError} when failed to login with unknown error.
|
|
667
|
-
* @throws {@link ErrorCode|ServiceError} when simple auth server failed to exchange access token.
|
|
668
682
|
* @throws {@link ErrorCode|ConsentFailed} when user canceled or failed to consent.
|
|
669
683
|
* @throws {@link ErrorCode|InvalidParameter} when scopes is not a valid string or string array.
|
|
670
684
|
* @throws {@link ErrorCode|RuntimeNotSupported} when runtime is nodeJS.
|
|
@@ -676,51 +690,50 @@ var TeamsUserCredential = /** @class */ (function () {
|
|
|
676
690
|
var scopesStr;
|
|
677
691
|
var _this = this;
|
|
678
692
|
return __generator(this, function (_a) {
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
resolve();
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
},
|
|
693
|
+
switch (_a.label) {
|
|
694
|
+
case 0:
|
|
695
|
+
validateScopesType(scopes);
|
|
696
|
+
scopesStr = typeof scopes === "string" ? scopes : scopes.join(" ");
|
|
697
|
+
internalLogger.info("Popup login page to get user's access token with scopes: " + scopesStr);
|
|
698
|
+
if (!!this.initialized) return [3 /*break*/, 2];
|
|
699
|
+
return [4 /*yield*/, this.init()];
|
|
700
|
+
case 1:
|
|
701
|
+
_a.sent();
|
|
702
|
+
_a.label = 2;
|
|
703
|
+
case 2: return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
704
|
+
microsoftTeams.initialize(function () {
|
|
705
|
+
microsoftTeams.authentication.authenticate({
|
|
706
|
+
url: _this.config.initiateLoginEndpoint + "?clientId=" + _this.config.clientId + "&scope=" + encodeURI(scopesStr) + "&loginHint=" + _this.loginHint,
|
|
707
|
+
width: loginPageWidth,
|
|
708
|
+
height: loginPageHeight,
|
|
709
|
+
successCallback: function (result) { return __awaiter(_this, void 0, void 0, function () {
|
|
710
|
+
var errorMsg, accessToken;
|
|
711
|
+
return __generator(this, function (_a) {
|
|
712
|
+
if (!result) {
|
|
713
|
+
errorMsg = "Get empty authentication result from MSAL";
|
|
714
|
+
internalLogger.error(errorMsg);
|
|
715
|
+
reject(new ErrorWithCode(errorMsg, ErrorCode.InternalError));
|
|
716
|
+
return [2 /*return*/];
|
|
717
|
+
}
|
|
718
|
+
try {
|
|
719
|
+
accessToken = parseAccessTokenFromAuthCodeTokenResponse(result);
|
|
720
|
+
resolve(accessToken);
|
|
721
|
+
}
|
|
722
|
+
catch (error) {
|
|
723
|
+
reject(error);
|
|
724
|
+
}
|
|
725
|
+
return [2 /*return*/];
|
|
726
|
+
});
|
|
727
|
+
}); },
|
|
728
|
+
failureCallback: function (reason) {
|
|
729
|
+
var errorMsg = "Consent failed for the scope " + scopesStr + " with error: " + reason;
|
|
730
|
+
internalLogger.error(errorMsg);
|
|
731
|
+
reject(new ErrorWithCode(errorMsg, ErrorCode.ConsentFailed));
|
|
732
|
+
},
|
|
733
|
+
});
|
|
721
734
|
});
|
|
722
|
-
});
|
|
723
|
-
|
|
735
|
+
})];
|
|
736
|
+
}
|
|
724
737
|
});
|
|
725
738
|
});
|
|
726
739
|
};
|
|
@@ -748,7 +761,6 @@ var TeamsUserCredential = /** @class */ (function () {
|
|
|
748
761
|
*
|
|
749
762
|
* @throws {@link ErrorCode|InternalError} when failed to get access token with unknown error.
|
|
750
763
|
* @throws {@link ErrorCode|UiRequiredError} when need user consent to get access token.
|
|
751
|
-
* @throws {@link ErrorCode|ServiceError} when failed to get access token from simple auth server.
|
|
752
764
|
* @throws {@link ErrorCode|InvalidParameter} when scopes is not a valid string or string array.
|
|
753
765
|
* @throws {@link ErrorCode|RuntimeNotSupported} when runtime is nodeJS.
|
|
754
766
|
*
|
|
@@ -761,7 +773,7 @@ var TeamsUserCredential = /** @class */ (function () {
|
|
|
761
773
|
*/
|
|
762
774
|
TeamsUserCredential.prototype.getToken = function (scopes, options) {
|
|
763
775
|
return __awaiter(this, void 0, void 0, function () {
|
|
764
|
-
var ssoToken, scopeStr,
|
|
776
|
+
var ssoToken, scopeStr, tokenResponse, scopesArray, domain, account, scopesRequestForAcquireTokenSilent, error_1, acquireTokenSilentFailedMessage, scopesRequestForSsoSilent, error_2, ssoSilentFailedMessage, errorMsg, accessToken;
|
|
765
777
|
return __generator(this, function (_a) {
|
|
766
778
|
switch (_a.label) {
|
|
767
779
|
case 0:
|
|
@@ -775,25 +787,59 @@ var TeamsUserCredential = /** @class */ (function () {
|
|
|
775
787
|
return [2 /*return*/, ssoToken];
|
|
776
788
|
case 2:
|
|
777
789
|
internalLogger.info("Get access token with scopes: " + scopeStr);
|
|
778
|
-
return [
|
|
790
|
+
if (!!this.initialized) return [3 /*break*/, 4];
|
|
791
|
+
return [4 /*yield*/, this.init()];
|
|
779
792
|
case 3:
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
if (cachedToken) {
|
|
783
|
-
if (!this.isAccessTokenNearExpired(cachedToken)) {
|
|
784
|
-
internalLogger.verbose("Get access token from cache");
|
|
785
|
-
return [2 /*return*/, cachedToken];
|
|
786
|
-
}
|
|
787
|
-
else {
|
|
788
|
-
internalLogger.verbose("Cached access token is expired");
|
|
789
|
-
}
|
|
790
|
-
}
|
|
791
|
-
else {
|
|
792
|
-
internalLogger.verbose("No cached access token");
|
|
793
|
-
}
|
|
794
|
-
return [4 /*yield*/, this.getAndCacheAccessTokenFromSimpleAuthServer(scopeStr)];
|
|
793
|
+
_a.sent();
|
|
794
|
+
_a.label = 4;
|
|
795
795
|
case 4:
|
|
796
|
-
|
|
796
|
+
tokenResponse = void 0;
|
|
797
|
+
scopesArray = typeof scopes === "string" ? scopes.split(" ") : scopes;
|
|
798
|
+
domain = window.location.origin;
|
|
799
|
+
_a.label = 5;
|
|
800
|
+
case 5:
|
|
801
|
+
_a.trys.push([5, 7, , 8]);
|
|
802
|
+
account = this.msalInstance.getAccountByUsername(this.loginHint);
|
|
803
|
+
scopesRequestForAcquireTokenSilent = {
|
|
804
|
+
scopes: scopesArray,
|
|
805
|
+
account: account !== null && account !== void 0 ? account : undefined,
|
|
806
|
+
redirectUri: domain + "/blank-auth-end.html",
|
|
807
|
+
};
|
|
808
|
+
return [4 /*yield*/, this.msalInstance.acquireTokenSilent(scopesRequestForAcquireTokenSilent)];
|
|
809
|
+
case 6:
|
|
810
|
+
tokenResponse = _a.sent();
|
|
811
|
+
return [3 /*break*/, 8];
|
|
812
|
+
case 7:
|
|
813
|
+
error_1 = _a.sent();
|
|
814
|
+
acquireTokenSilentFailedMessage = "Failed to call acquireTokenSilent. Reason: " + (error_1 === null || error_1 === void 0 ? void 0 : error_1.message) + ". ";
|
|
815
|
+
internalLogger.verbose(acquireTokenSilentFailedMessage);
|
|
816
|
+
return [3 /*break*/, 8];
|
|
817
|
+
case 8:
|
|
818
|
+
if (!!tokenResponse) return [3 /*break*/, 12];
|
|
819
|
+
_a.label = 9;
|
|
820
|
+
case 9:
|
|
821
|
+
_a.trys.push([9, 11, , 12]);
|
|
822
|
+
scopesRequestForSsoSilent = {
|
|
823
|
+
scopes: scopesArray,
|
|
824
|
+
loginHint: this.loginHint,
|
|
825
|
+
redirectUri: domain + "/blank-auth-end.html",
|
|
826
|
+
};
|
|
827
|
+
return [4 /*yield*/, this.msalInstance.ssoSilent(scopesRequestForSsoSilent)];
|
|
828
|
+
case 10:
|
|
829
|
+
tokenResponse = _a.sent();
|
|
830
|
+
return [3 /*break*/, 12];
|
|
831
|
+
case 11:
|
|
832
|
+
error_2 = _a.sent();
|
|
833
|
+
ssoSilentFailedMessage = "Failed to call ssoSilent. Reason: " + (error_2 === null || error_2 === void 0 ? void 0 : error_2.message) + ". ";
|
|
834
|
+
internalLogger.verbose(ssoSilentFailedMessage);
|
|
835
|
+
return [3 /*break*/, 12];
|
|
836
|
+
case 12:
|
|
837
|
+
if (!tokenResponse) {
|
|
838
|
+
errorMsg = "Failed to get access token cache silently, please login first: you need login first before get access token.";
|
|
839
|
+
internalLogger.error(errorMsg);
|
|
840
|
+
throw new ErrorWithCode(errorMsg, ErrorCode.UiRequiredError);
|
|
841
|
+
}
|
|
842
|
+
accessToken = parseAccessTokenFromAuthCodeTokenResponse(tokenResponse);
|
|
797
843
|
return [2 /*return*/, accessToken];
|
|
798
844
|
}
|
|
799
845
|
});
|
|
@@ -830,92 +876,29 @@ var TeamsUserCredential = /** @class */ (function () {
|
|
|
830
876
|
});
|
|
831
877
|
});
|
|
832
878
|
};
|
|
833
|
-
TeamsUserCredential.prototype.
|
|
834
|
-
var _a, _b;
|
|
835
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
836
|
-
var axiosInstance, retryCount, response, tokenResult, key, err_2;
|
|
837
|
-
return __generator(this, function (_c) {
|
|
838
|
-
switch (_c.label) {
|
|
839
|
-
case 0: return [4 /*yield*/, this.getAxiosInstance()];
|
|
840
|
-
case 1:
|
|
841
|
-
axiosInstance = _c.sent();
|
|
842
|
-
retryCount = 0;
|
|
843
|
-
_c.label = 2;
|
|
844
|
-
case 2:
|
|
845
|
-
_c.label = 3;
|
|
846
|
-
case 3:
|
|
847
|
-
_c.trys.push([3, 6, , 9]);
|
|
848
|
-
return [4 /*yield*/, axiosInstance.post("/auth/token", {
|
|
849
|
-
scope: scopesStr,
|
|
850
|
-
code: authCodeResult.code,
|
|
851
|
-
code_verifier: authCodeResult.codeVerifier,
|
|
852
|
-
redirect_uri: authCodeResult.redirectUri,
|
|
853
|
-
grant_type: GrantType.authCode,
|
|
854
|
-
})];
|
|
855
|
-
case 4:
|
|
856
|
-
response = _c.sent();
|
|
857
|
-
tokenResult = response.data;
|
|
858
|
-
return [4 /*yield*/, this.getAccessTokenCacheKey(scopesStr)];
|
|
859
|
-
case 5:
|
|
860
|
-
key = _c.sent();
|
|
861
|
-
// Important: tokens are stored in sessionStorage, read more here: https://aka.ms/teamsfx-session-storage-notice
|
|
862
|
-
this.setTokenCache(key, {
|
|
863
|
-
token: tokenResult.access_token,
|
|
864
|
-
expiresOnTimestamp: tokenResult.expires_on,
|
|
865
|
-
});
|
|
866
|
-
return [2 /*return*/];
|
|
867
|
-
case 6:
|
|
868
|
-
err_2 = _c.sent();
|
|
869
|
-
if (!(((_b = (_a = err_2.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.type) && err_2.response.data.type === "AadUiRequiredException")) return [3 /*break*/, 8];
|
|
870
|
-
internalLogger.warn("Exchange access token failed, retry...");
|
|
871
|
-
if (!(retryCount < maxRetryCount)) return [3 /*break*/, 8];
|
|
872
|
-
return [4 /*yield*/, this.sleep(retryTimeSpanInMillisecond)];
|
|
873
|
-
case 7:
|
|
874
|
-
_c.sent();
|
|
875
|
-
retryCount++;
|
|
876
|
-
return [3 /*break*/, 2];
|
|
877
|
-
case 8: throw err_2;
|
|
878
|
-
case 9: return [3 /*break*/, 2];
|
|
879
|
-
case 10: return [2 /*return*/];
|
|
880
|
-
}
|
|
881
|
-
});
|
|
882
|
-
});
|
|
883
|
-
};
|
|
884
|
-
/**
|
|
885
|
-
* Get access token cache from authentication server
|
|
886
|
-
* @returns Access token
|
|
887
|
-
*/
|
|
888
|
-
TeamsUserCredential.prototype.getAndCacheAccessTokenFromSimpleAuthServer = function (scopesStr) {
|
|
879
|
+
TeamsUserCredential.prototype.init = function () {
|
|
889
880
|
return __awaiter(this, void 0, void 0, function () {
|
|
890
|
-
var
|
|
881
|
+
var ssoToken, info, msalConfig;
|
|
891
882
|
return __generator(this, function (_a) {
|
|
892
883
|
switch (_a.label) {
|
|
893
|
-
case 0:
|
|
894
|
-
_a.trys.push([0, 4, , 5]);
|
|
895
|
-
internalLogger.verbose("Get access token from authentication server with scopes: " + scopesStr);
|
|
896
|
-
return [4 /*yield*/, this.getAxiosInstance()];
|
|
884
|
+
case 0: return [4 /*yield*/, this.getSSOToken()];
|
|
897
885
|
case 1:
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
886
|
+
ssoToken = _a.sent();
|
|
887
|
+
info = getTenantIdAndLoginHintFromSsoToken(ssoToken.token);
|
|
888
|
+
this.loginHint = info.loginHint;
|
|
889
|
+
this.tid = info.tid;
|
|
890
|
+
msalConfig = {
|
|
891
|
+
auth: {
|
|
892
|
+
clientId: this.config.clientId,
|
|
893
|
+
authority: "https://login.microsoftonline.com/" + this.tid,
|
|
894
|
+
},
|
|
895
|
+
cache: {
|
|
896
|
+
cacheLocation: "sessionStorage",
|
|
897
|
+
},
|
|
909
898
|
};
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
this.setTokenCache(cacheKey, accessToken);
|
|
914
|
-
return [2 /*return*/, accessToken];
|
|
915
|
-
case 4:
|
|
916
|
-
err_3 = _a.sent();
|
|
917
|
-
throw this.generateAuthServerError(err_3);
|
|
918
|
-
case 5: return [2 /*return*/];
|
|
899
|
+
this.msalInstance = new PublicClientApplication(msalConfig);
|
|
900
|
+
this.initialized = true;
|
|
901
|
+
return [2 /*return*/];
|
|
919
902
|
}
|
|
920
903
|
});
|
|
921
904
|
});
|
|
@@ -989,16 +972,13 @@ var TeamsUserCredential = /** @class */ (function () {
|
|
|
989
972
|
internalLogger.error(ErrorMessage.AuthenticationConfigurationNotExists);
|
|
990
973
|
throw new ErrorWithCode(ErrorMessage.AuthenticationConfigurationNotExists, ErrorCode.InvalidConfiguration);
|
|
991
974
|
}
|
|
992
|
-
if (config.initiateLoginEndpoint && config.
|
|
975
|
+
if (config.initiateLoginEndpoint && config.clientId) {
|
|
993
976
|
return config;
|
|
994
977
|
}
|
|
995
978
|
var missingValues = [];
|
|
996
979
|
if (!config.initiateLoginEndpoint) {
|
|
997
980
|
missingValues.push("initiateLoginEndpoint");
|
|
998
981
|
}
|
|
999
|
-
if (!config.simpleAuthEndpoint) {
|
|
1000
|
-
missingValues.push("simpleAuthEndpoint");
|
|
1001
|
-
}
|
|
1002
982
|
if (!config.clientId) {
|
|
1003
983
|
missingValues.push("clientId");
|
|
1004
984
|
}
|
|
@@ -1006,130 +986,6 @@ var TeamsUserCredential = /** @class */ (function () {
|
|
|
1006
986
|
internalLogger.error(errorMsg);
|
|
1007
987
|
throw new ErrorWithCode(errorMsg, ErrorCode.InvalidConfiguration);
|
|
1008
988
|
};
|
|
1009
|
-
/**
|
|
1010
|
-
* Get axios instance with sso token bearer header
|
|
1011
|
-
* @returns AxiosInstance
|
|
1012
|
-
*/
|
|
1013
|
-
TeamsUserCredential.prototype.getAxiosInstance = function () {
|
|
1014
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1015
|
-
var ssoToken, axiosInstance;
|
|
1016
|
-
return __generator(this, function (_a) {
|
|
1017
|
-
switch (_a.label) {
|
|
1018
|
-
case 0: return [4 /*yield*/, this.getSSOToken()];
|
|
1019
|
-
case 1:
|
|
1020
|
-
ssoToken = _a.sent();
|
|
1021
|
-
axiosInstance = axios.create({
|
|
1022
|
-
baseURL: this.config.simpleAuthEndpoint,
|
|
1023
|
-
});
|
|
1024
|
-
axiosInstance.interceptors.request.use(function (config) {
|
|
1025
|
-
config.headers.Authorization = "Bearer " + ssoToken.token;
|
|
1026
|
-
return config;
|
|
1027
|
-
});
|
|
1028
|
-
return [2 /*return*/, axiosInstance];
|
|
1029
|
-
}
|
|
1030
|
-
});
|
|
1031
|
-
});
|
|
1032
|
-
};
|
|
1033
|
-
/**
|
|
1034
|
-
* Set access token to cache
|
|
1035
|
-
* @param key
|
|
1036
|
-
* @param token
|
|
1037
|
-
*/
|
|
1038
|
-
TeamsUserCredential.prototype.setTokenCache = function (key, token) {
|
|
1039
|
-
Cache.set(key, JSON.stringify(token));
|
|
1040
|
-
};
|
|
1041
|
-
/**
|
|
1042
|
-
* Get access token from cache.
|
|
1043
|
-
* If there is no cache or cannot be parsed, then it will return null
|
|
1044
|
-
* @param key
|
|
1045
|
-
* @returns Access token or null
|
|
1046
|
-
*/
|
|
1047
|
-
TeamsUserCredential.prototype.getTokenCache = function (key) {
|
|
1048
|
-
var value = Cache.get(key);
|
|
1049
|
-
if (value === null) {
|
|
1050
|
-
return null;
|
|
1051
|
-
}
|
|
1052
|
-
var accessToken = this.validateAndParseJson(value);
|
|
1053
|
-
return accessToken;
|
|
1054
|
-
};
|
|
1055
|
-
/**
|
|
1056
|
-
* Parses passed value as JSON access token, if value is not a valid json string JSON.parse() will throw an error.
|
|
1057
|
-
* @param jsonValue
|
|
1058
|
-
*/
|
|
1059
|
-
TeamsUserCredential.prototype.validateAndParseJson = function (jsonValue) {
|
|
1060
|
-
try {
|
|
1061
|
-
var parsedJson = JSON.parse(jsonValue);
|
|
1062
|
-
/**
|
|
1063
|
-
* There are edge cases in which JSON.parse will successfully parse a non-valid JSON object
|
|
1064
|
-
* (e.g. JSON.parse will parse an escaped string into an unescaped string), so adding a type check
|
|
1065
|
-
* of the parsed value is necessary in order to be certain that the string represents a valid JSON object.
|
|
1066
|
-
*
|
|
1067
|
-
*/
|
|
1068
|
-
return parsedJson && typeof parsedJson === "object" ? parsedJson : null;
|
|
1069
|
-
}
|
|
1070
|
-
catch (error) {
|
|
1071
|
-
return null;
|
|
1072
|
-
}
|
|
1073
|
-
};
|
|
1074
|
-
/**
|
|
1075
|
-
* Generate cache key
|
|
1076
|
-
* @param scopesStr
|
|
1077
|
-
* @returns Access token cache key, a key example: accessToken-userId-clientId-tenantId-scopes
|
|
1078
|
-
*/
|
|
1079
|
-
TeamsUserCredential.prototype.getAccessTokenCacheKey = function (scopesStr) {
|
|
1080
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1081
|
-
var ssoToken, ssoTokenObj, clientId, userObjectId, tenantId, key;
|
|
1082
|
-
return __generator(this, function (_a) {
|
|
1083
|
-
switch (_a.label) {
|
|
1084
|
-
case 0: return [4 /*yield*/, this.getSSOToken()];
|
|
1085
|
-
case 1:
|
|
1086
|
-
ssoToken = _a.sent();
|
|
1087
|
-
ssoTokenObj = parseJwt(ssoToken.token);
|
|
1088
|
-
clientId = this.config.clientId;
|
|
1089
|
-
userObjectId = ssoTokenObj.oid;
|
|
1090
|
-
tenantId = ssoTokenObj.tid;
|
|
1091
|
-
key = [accessTokenCacheKeyPrefix, userObjectId, clientId, tenantId, scopesStr]
|
|
1092
|
-
.join(separator)
|
|
1093
|
-
.replace(/" "/g, "_");
|
|
1094
|
-
return [2 /*return*/, key];
|
|
1095
|
-
}
|
|
1096
|
-
});
|
|
1097
|
-
});
|
|
1098
|
-
};
|
|
1099
|
-
/**
|
|
1100
|
-
* Check whether the token is about to expire (within 5 minutes)
|
|
1101
|
-
* @returns Boolean value indicate whether the token is about to expire
|
|
1102
|
-
*/
|
|
1103
|
-
TeamsUserCredential.prototype.isAccessTokenNearExpired = function (token) {
|
|
1104
|
-
var expireDate = new Date(token.expiresOnTimestamp);
|
|
1105
|
-
if (expireDate.getTime() - Date.now() > tokenRefreshTimeSpanInMillisecond) {
|
|
1106
|
-
return false;
|
|
1107
|
-
}
|
|
1108
|
-
return true;
|
|
1109
|
-
};
|
|
1110
|
-
TeamsUserCredential.prototype.generateAuthServerError = function (err) {
|
|
1111
|
-
var _a, _b;
|
|
1112
|
-
var errorMessage = err.message;
|
|
1113
|
-
if ((_b = (_a = err.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.type) {
|
|
1114
|
-
errorMessage = err.response.data.detail;
|
|
1115
|
-
if (err.response.data.type === "AadUiRequiredException") {
|
|
1116
|
-
var fullErrorMsg_1 = "Failed to get access token from authentication server, please login first: " +
|
|
1117
|
-
errorMessage;
|
|
1118
|
-
internalLogger.warn(fullErrorMsg_1);
|
|
1119
|
-
return new ErrorWithCode(fullErrorMsg_1, ErrorCode.UiRequiredError);
|
|
1120
|
-
}
|
|
1121
|
-
else {
|
|
1122
|
-
var fullErrorMsg_2 = "Failed to get access token from authentication server: " + errorMessage;
|
|
1123
|
-
internalLogger.error(fullErrorMsg_2);
|
|
1124
|
-
return new ErrorWithCode(fullErrorMsg_2, ErrorCode.ServiceError);
|
|
1125
|
-
}
|
|
1126
|
-
}
|
|
1127
|
-
var fullErrorMsg = "Failed to get access token with error: " + errorMessage;
|
|
1128
|
-
return new ErrorWithCode(fullErrorMsg, ErrorCode.InternalError);
|
|
1129
|
-
};
|
|
1130
|
-
TeamsUserCredential.prototype.sleep = function (ms) {
|
|
1131
|
-
return new Promise(function (resolve) { return setTimeout(resolve, ms); });
|
|
1132
|
-
};
|
|
1133
989
|
return TeamsUserCredential;
|
|
1134
990
|
}());
|
|
1135
991
|
|