@microsoft/teamsfx 0.4.1-alpha.fcc60ca0.0 → 0.4.2-alpha.7b2fe9ea.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 +135 -218
- package/dist/index.esm2017.js.map +1 -1
- package/dist/index.esm2017.mjs.map +1 -1
- package/dist/index.esm5.js +184 -305
- package/dist/index.esm5.js.map +1 -1
- package/dist/index.node.cjs.js.map +1 -1
- package/package.json +6 -5
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,58 @@ 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
|
-
|
|
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, resultJson, failedToParseResult;
|
|
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
|
+
resultJson = {};
|
|
719
|
+
try {
|
|
720
|
+
resultJson = JSON.parse(result);
|
|
721
|
+
}
|
|
722
|
+
catch (error) {
|
|
723
|
+
failedToParseResult = "Failed to parse result to Json.";
|
|
724
|
+
internalLogger.verbose(failedToParseResult);
|
|
706
725
|
resolve();
|
|
707
|
-
return [
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
726
|
+
return [2 /*return*/];
|
|
727
|
+
}
|
|
728
|
+
// If sessionStorage exists in result, set the values in current session storage.
|
|
729
|
+
if (resultJson.sessionStorage) {
|
|
730
|
+
this.setSessionStorage(resultJson.sessionStorage);
|
|
731
|
+
}
|
|
732
|
+
resolve();
|
|
733
|
+
return [2 /*return*/];
|
|
734
|
+
});
|
|
735
|
+
}); },
|
|
736
|
+
failureCallback: function (reason) {
|
|
737
|
+
var errorMsg = "Consent failed for the scope " + scopesStr + " with error: " + reason;
|
|
738
|
+
internalLogger.error(errorMsg);
|
|
739
|
+
reject(new ErrorWithCode(errorMsg, ErrorCode.ConsentFailed));
|
|
740
|
+
},
|
|
741
|
+
});
|
|
721
742
|
});
|
|
722
|
-
});
|
|
723
|
-
|
|
743
|
+
})];
|
|
744
|
+
}
|
|
724
745
|
});
|
|
725
746
|
});
|
|
726
747
|
};
|
|
@@ -748,7 +769,6 @@ var TeamsUserCredential = /** @class */ (function () {
|
|
|
748
769
|
*
|
|
749
770
|
* @throws {@link ErrorCode|InternalError} when failed to get access token with unknown error.
|
|
750
771
|
* @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
772
|
* @throws {@link ErrorCode|InvalidParameter} when scopes is not a valid string or string array.
|
|
753
773
|
* @throws {@link ErrorCode|RuntimeNotSupported} when runtime is nodeJS.
|
|
754
774
|
*
|
|
@@ -761,7 +781,7 @@ var TeamsUserCredential = /** @class */ (function () {
|
|
|
761
781
|
*/
|
|
762
782
|
TeamsUserCredential.prototype.getToken = function (scopes, options) {
|
|
763
783
|
return __awaiter(this, void 0, void 0, function () {
|
|
764
|
-
var ssoToken, scopeStr,
|
|
784
|
+
var ssoToken, scopeStr, tokenResponse, scopesArray, domain, account, scopesRequestForAcquireTokenSilent, error_1, acquireTokenSilentFailedMessage, scopesRequestForSsoSilent, error_2, ssoSilentFailedMessage, errorMsg, accessToken;
|
|
765
785
|
return __generator(this, function (_a) {
|
|
766
786
|
switch (_a.label) {
|
|
767
787
|
case 0:
|
|
@@ -775,25 +795,59 @@ var TeamsUserCredential = /** @class */ (function () {
|
|
|
775
795
|
return [2 /*return*/, ssoToken];
|
|
776
796
|
case 2:
|
|
777
797
|
internalLogger.info("Get access token with scopes: " + scopeStr);
|
|
778
|
-
return [
|
|
798
|
+
if (!!this.initialized) return [3 /*break*/, 4];
|
|
799
|
+
return [4 /*yield*/, this.init()];
|
|
779
800
|
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)];
|
|
801
|
+
_a.sent();
|
|
802
|
+
_a.label = 4;
|
|
795
803
|
case 4:
|
|
796
|
-
|
|
804
|
+
tokenResponse = void 0;
|
|
805
|
+
scopesArray = typeof scopes === "string" ? scopes.split(" ") : scopes;
|
|
806
|
+
domain = window.location.origin;
|
|
807
|
+
_a.label = 5;
|
|
808
|
+
case 5:
|
|
809
|
+
_a.trys.push([5, 7, , 8]);
|
|
810
|
+
account = this.msalInstance.getAccountByUsername(this.loginHint);
|
|
811
|
+
scopesRequestForAcquireTokenSilent = {
|
|
812
|
+
scopes: scopesArray,
|
|
813
|
+
account: account !== null && account !== void 0 ? account : undefined,
|
|
814
|
+
redirectUri: domain + "/blank-auth-end.html",
|
|
815
|
+
};
|
|
816
|
+
return [4 /*yield*/, this.msalInstance.acquireTokenSilent(scopesRequestForAcquireTokenSilent)];
|
|
817
|
+
case 6:
|
|
818
|
+
tokenResponse = _a.sent();
|
|
819
|
+
return [3 /*break*/, 8];
|
|
820
|
+
case 7:
|
|
821
|
+
error_1 = _a.sent();
|
|
822
|
+
acquireTokenSilentFailedMessage = "Failed to call acquireTokenSilent. Reason: " + (error_1 === null || error_1 === void 0 ? void 0 : error_1.message) + ". ";
|
|
823
|
+
internalLogger.verbose(acquireTokenSilentFailedMessage);
|
|
824
|
+
return [3 /*break*/, 8];
|
|
825
|
+
case 8:
|
|
826
|
+
if (!!tokenResponse) return [3 /*break*/, 12];
|
|
827
|
+
_a.label = 9;
|
|
828
|
+
case 9:
|
|
829
|
+
_a.trys.push([9, 11, , 12]);
|
|
830
|
+
scopesRequestForSsoSilent = {
|
|
831
|
+
scopes: scopesArray,
|
|
832
|
+
loginHint: this.loginHint,
|
|
833
|
+
redirectUri: domain + "/blank-auth-end.html",
|
|
834
|
+
};
|
|
835
|
+
return [4 /*yield*/, this.msalInstance.ssoSilent(scopesRequestForSsoSilent)];
|
|
836
|
+
case 10:
|
|
837
|
+
tokenResponse = _a.sent();
|
|
838
|
+
return [3 /*break*/, 12];
|
|
839
|
+
case 11:
|
|
840
|
+
error_2 = _a.sent();
|
|
841
|
+
ssoSilentFailedMessage = "Failed to call ssoSilent. Reason: " + (error_2 === null || error_2 === void 0 ? void 0 : error_2.message) + ". ";
|
|
842
|
+
internalLogger.verbose(ssoSilentFailedMessage);
|
|
843
|
+
return [3 /*break*/, 12];
|
|
844
|
+
case 12:
|
|
845
|
+
if (!tokenResponse) {
|
|
846
|
+
errorMsg = "Failed to get access token cache silently, please login first: you need login first before get access token.";
|
|
847
|
+
internalLogger.error(errorMsg);
|
|
848
|
+
throw new ErrorWithCode(errorMsg, ErrorCode.UiRequiredError);
|
|
849
|
+
}
|
|
850
|
+
accessToken = parseAccessTokenFromAuthCodeTokenResponse(tokenResponse);
|
|
797
851
|
return [2 /*return*/, accessToken];
|
|
798
852
|
}
|
|
799
853
|
});
|
|
@@ -830,92 +884,29 @@ var TeamsUserCredential = /** @class */ (function () {
|
|
|
830
884
|
});
|
|
831
885
|
});
|
|
832
886
|
};
|
|
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) {
|
|
887
|
+
TeamsUserCredential.prototype.init = function () {
|
|
889
888
|
return __awaiter(this, void 0, void 0, function () {
|
|
890
|
-
var
|
|
889
|
+
var ssoToken, info, msalConfig;
|
|
891
890
|
return __generator(this, function (_a) {
|
|
892
891
|
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()];
|
|
892
|
+
case 0: return [4 /*yield*/, this.getSSOToken()];
|
|
897
893
|
case 1:
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
894
|
+
ssoToken = _a.sent();
|
|
895
|
+
info = getTenantIdAndLoginHintFromSsoToken(ssoToken.token);
|
|
896
|
+
this.loginHint = info.loginHint;
|
|
897
|
+
this.tid = info.tid;
|
|
898
|
+
msalConfig = {
|
|
899
|
+
auth: {
|
|
900
|
+
clientId: this.config.clientId,
|
|
901
|
+
authority: "https://login.microsoftonline.com/" + this.tid,
|
|
902
|
+
},
|
|
903
|
+
cache: {
|
|
904
|
+
cacheLocation: "sessionStorage",
|
|
905
|
+
},
|
|
909
906
|
};
|
|
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*/];
|
|
907
|
+
this.msalInstance = new PublicClientApplication(msalConfig);
|
|
908
|
+
this.initialized = true;
|
|
909
|
+
return [2 /*return*/];
|
|
919
910
|
}
|
|
920
911
|
});
|
|
921
912
|
});
|
|
@@ -989,16 +980,13 @@ var TeamsUserCredential = /** @class */ (function () {
|
|
|
989
980
|
internalLogger.error(ErrorMessage.AuthenticationConfigurationNotExists);
|
|
990
981
|
throw new ErrorWithCode(ErrorMessage.AuthenticationConfigurationNotExists, ErrorCode.InvalidConfiguration);
|
|
991
982
|
}
|
|
992
|
-
if (config.initiateLoginEndpoint && config.
|
|
983
|
+
if (config.initiateLoginEndpoint && config.clientId) {
|
|
993
984
|
return config;
|
|
994
985
|
}
|
|
995
986
|
var missingValues = [];
|
|
996
987
|
if (!config.initiateLoginEndpoint) {
|
|
997
988
|
missingValues.push("initiateLoginEndpoint");
|
|
998
989
|
}
|
|
999
|
-
if (!config.simpleAuthEndpoint) {
|
|
1000
|
-
missingValues.push("simpleAuthEndpoint");
|
|
1001
|
-
}
|
|
1002
990
|
if (!config.clientId) {
|
|
1003
991
|
missingValues.push("clientId");
|
|
1004
992
|
}
|
|
@@ -1006,129 +994,20 @@ var TeamsUserCredential = /** @class */ (function () {
|
|
|
1006
994
|
internalLogger.error(errorMsg);
|
|
1007
995
|
throw new ErrorWithCode(errorMsg, ErrorCode.InvalidConfiguration);
|
|
1008
996
|
};
|
|
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) {
|
|
997
|
+
TeamsUserCredential.prototype.setSessionStorage = function (sessonStorageValues) {
|
|
1060
998
|
try {
|
|
1061
|
-
var
|
|
1062
|
-
|
|
1063
|
-
|
|
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
|
-
}
|
|
999
|
+
var sessionStorageKeys = Object.keys(sessonStorageValues);
|
|
1000
|
+
sessionStorageKeys.forEach(function (key) {
|
|
1001
|
+
sessionStorage.setItem(key, sessonStorageValues[key]);
|
|
1096
1002
|
});
|
|
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
1003
|
}
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
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
|
-
}
|
|
1004
|
+
catch (error) {
|
|
1005
|
+
// Values in result.sessionStorage can not be set into session storage.
|
|
1006
|
+
// Throw error since this may block user.
|
|
1007
|
+
var errorMessage = "Failed to set values in session storage. Error: " + error.message;
|
|
1008
|
+
internalLogger.error(errorMessage);
|
|
1009
|
+
throw new ErrorWithCode(errorMessage, ErrorCode.InternalError);
|
|
1126
1010
|
}
|
|
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
1011
|
};
|
|
1133
1012
|
return TeamsUserCredential;
|
|
1134
1013
|
}());
|