@azure/msal-common 4.2.1 → 4.5.1

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.
Files changed (190) hide show
  1. package/CHANGELOG.json +212 -1
  2. package/LICENSE +21 -21
  3. package/README.md +61 -58
  4. package/changelog.md +402 -344
  5. package/dist/_virtual/_tslib.js +90 -0
  6. package/dist/_virtual/_tslib.js.map +1 -0
  7. package/dist/account/AuthToken.js +43 -0
  8. package/dist/account/AuthToken.js.map +1 -0
  9. package/dist/account/CcsCredential.d.ts +9 -0
  10. package/dist/account/CcsCredential.d.ts.map +1 -0
  11. package/dist/account/CcsCredential.js +14 -0
  12. package/dist/account/CcsCredential.js.map +1 -0
  13. package/dist/account/ClientInfo.d.ts +6 -1
  14. package/dist/account/ClientInfo.d.ts.map +1 -1
  15. package/dist/account/ClientInfo.js +44 -0
  16. package/dist/account/ClientInfo.js.map +1 -0
  17. package/dist/account/TokenClaims.d.ts +1 -0
  18. package/dist/account/TokenClaims.d.ts.map +1 -1
  19. package/dist/authority/Authority.d.ts +25 -0
  20. package/dist/authority/Authority.d.ts.map +1 -1
  21. package/dist/authority/Authority.js +574 -0
  22. package/dist/authority/Authority.js.map +1 -0
  23. package/dist/authority/AuthorityFactory.js +69 -0
  24. package/dist/authority/AuthorityFactory.js.map +1 -0
  25. package/dist/authority/AuthorityOptions.d.ts +2 -0
  26. package/dist/authority/AuthorityOptions.d.ts.map +1 -1
  27. package/dist/authority/AuthorityType.js +17 -0
  28. package/dist/authority/AuthorityType.js.map +1 -0
  29. package/dist/authority/AzureRegion.d.ts +2 -0
  30. package/dist/authority/AzureRegion.d.ts.map +1 -0
  31. package/dist/authority/AzureRegionConfiguration.d.ts +6 -0
  32. package/dist/authority/AzureRegionConfiguration.d.ts.map +1 -0
  33. package/dist/authority/CloudInstanceDiscoveryResponse.js +13 -0
  34. package/dist/authority/CloudInstanceDiscoveryResponse.js.map +1 -0
  35. package/dist/authority/OpenIdConfigResponse.js +15 -0
  36. package/dist/authority/OpenIdConfigResponse.js.map +1 -0
  37. package/dist/authority/ProtocolMode.js +17 -0
  38. package/dist/authority/ProtocolMode.js.map +1 -0
  39. package/dist/authority/RegionDiscovery.d.ts +31 -0
  40. package/dist/authority/RegionDiscovery.d.ts.map +1 -0
  41. package/dist/authority/RegionDiscovery.js +119 -0
  42. package/dist/authority/RegionDiscovery.js.map +1 -0
  43. package/dist/authority/RegionDiscoveryMetadata.d.ts +7 -0
  44. package/dist/authority/RegionDiscoveryMetadata.d.ts.map +1 -0
  45. package/dist/cache/CacheManager.js +713 -0
  46. package/dist/cache/CacheManager.js.map +1 -0
  47. package/dist/cache/entities/AccessTokenEntity.js +111 -0
  48. package/dist/cache/entities/AccessTokenEntity.js.map +1 -0
  49. package/dist/cache/entities/AccountEntity.d.ts +5 -5
  50. package/dist/cache/entities/AccountEntity.d.ts.map +1 -1
  51. package/dist/cache/entities/AccountEntity.js +242 -0
  52. package/dist/cache/entities/AccountEntity.js.map +1 -0
  53. package/dist/cache/entities/AppMetadataEntity.js +74 -0
  54. package/dist/cache/entities/AppMetadataEntity.js.map +1 -0
  55. package/dist/cache/entities/AuthorityMetadataEntity.js +81 -0
  56. package/dist/cache/entities/AuthorityMetadataEntity.js.map +1 -0
  57. package/dist/cache/entities/CacheRecord.js +19 -0
  58. package/dist/cache/entities/CacheRecord.js.map +1 -0
  59. package/dist/cache/entities/CredentialEntity.js +142 -0
  60. package/dist/cache/entities/CredentialEntity.js.map +1 -0
  61. package/dist/cache/entities/IdTokenEntity.js +71 -0
  62. package/dist/cache/entities/IdTokenEntity.js.map +1 -0
  63. package/dist/cache/entities/RefreshTokenEntity.js +73 -0
  64. package/dist/cache/entities/RefreshTokenEntity.js.map +1 -0
  65. package/dist/cache/entities/ServerTelemetryEntity.js +35 -0
  66. package/dist/cache/entities/ServerTelemetryEntity.js.map +1 -0
  67. package/dist/cache/entities/ThrottlingEntity.js +32 -0
  68. package/dist/cache/entities/ThrottlingEntity.js.map +1 -0
  69. package/dist/cache/persistence/TokenCacheContext.js +39 -0
  70. package/dist/cache/persistence/TokenCacheContext.js.map +1 -0
  71. package/dist/client/AuthorizationCodeClient.d.ts.map +1 -1
  72. package/dist/client/AuthorizationCodeClient.js +388 -0
  73. package/dist/client/AuthorizationCodeClient.js.map +1 -0
  74. package/dist/client/BaseClient.d.ts +2 -1
  75. package/dist/client/BaseClient.d.ts.map +1 -1
  76. package/dist/client/BaseClient.js +101 -0
  77. package/dist/client/BaseClient.js.map +1 -0
  78. package/dist/client/ClientCredentialClient.d.ts.map +1 -1
  79. package/dist/client/ClientCredentialClient.js +169 -0
  80. package/dist/client/ClientCredentialClient.js.map +1 -0
  81. package/dist/client/DeviceCodeClient.d.ts +6 -0
  82. package/dist/client/DeviceCodeClient.d.ts.map +1 -1
  83. package/dist/client/DeviceCodeClient.js +212 -0
  84. package/dist/client/DeviceCodeClient.js.map +1 -0
  85. package/dist/client/OnBehalfOfClient.d.ts.map +1 -1
  86. package/dist/client/OnBehalfOfClient.js +209 -0
  87. package/dist/client/OnBehalfOfClient.js.map +1 -0
  88. package/dist/client/RefreshTokenClient.d.ts.map +1 -1
  89. package/dist/client/RefreshTokenClient.js +212 -0
  90. package/dist/client/RefreshTokenClient.js.map +1 -0
  91. package/dist/client/SilentFlowClient.d.ts.map +1 -1
  92. package/dist/client/SilentFlowClient.js +132 -0
  93. package/dist/client/SilentFlowClient.js.map +1 -0
  94. package/dist/client/UsernamePasswordClient.d.ts.map +1 -1
  95. package/dist/client/UsernamePasswordClient.js +112 -0
  96. package/dist/client/UsernamePasswordClient.js.map +1 -0
  97. package/dist/config/ClientConfiguration.d.ts +3 -0
  98. package/dist/config/ClientConfiguration.d.ts.map +1 -1
  99. package/dist/config/ClientConfiguration.js +91 -0
  100. package/dist/config/ClientConfiguration.js.map +1 -0
  101. package/dist/crypto/ICrypto.js +53 -0
  102. package/dist/crypto/ICrypto.js.map +1 -0
  103. package/dist/crypto/PopTokenGenerator.js +72 -0
  104. package/dist/crypto/PopTokenGenerator.js.map +1 -0
  105. package/dist/error/AuthError.js +46 -0
  106. package/dist/error/AuthError.js.map +1 -0
  107. package/dist/error/ClientAuthError.d.ts +13 -6
  108. package/dist/error/ClientAuthError.d.ts.map +1 -1
  109. package/dist/error/ClientAuthError.js +456 -0
  110. package/dist/error/ClientAuthError.js.map +1 -0
  111. package/dist/error/ClientConfigurationError.d.ts +1 -6
  112. package/dist/error/ClientConfigurationError.d.ts.map +1 -1
  113. package/dist/error/ClientConfigurationError.js +210 -0
  114. package/dist/error/ClientConfigurationError.js.map +1 -0
  115. package/dist/error/InteractionRequiredAuthError.js +48 -0
  116. package/dist/error/InteractionRequiredAuthError.js.map +1 -0
  117. package/dist/error/ServerError.js +25 -0
  118. package/dist/error/ServerError.js.map +1 -0
  119. package/dist/index.cjs.js +7045 -0
  120. package/dist/index.cjs.js.map +1 -0
  121. package/dist/index.d.ts +4 -0
  122. package/dist/index.d.ts.map +1 -1
  123. package/dist/index.js +43 -6479
  124. package/dist/index.js.map +1 -0
  125. package/dist/logger/Logger.d.ts +12 -3
  126. package/dist/logger/Logger.d.ts.map +1 -1
  127. package/dist/logger/Logger.js +183 -0
  128. package/dist/logger/Logger.js.map +1 -0
  129. package/dist/network/INetworkModule.d.ts +1 -1
  130. package/dist/network/INetworkModule.d.ts.map +1 -1
  131. package/dist/network/INetworkModule.js +21 -0
  132. package/dist/network/INetworkModule.js.map +1 -0
  133. package/dist/network/NetworkManager.js +56 -0
  134. package/dist/network/NetworkManager.js.map +1 -0
  135. package/dist/network/ThrottlingUtils.d.ts.map +1 -1
  136. package/dist/network/ThrottlingUtils.js +95 -0
  137. package/dist/network/ThrottlingUtils.js.map +1 -0
  138. package/dist/packageMetadata.d.ts +1 -1
  139. package/dist/packageMetadata.js +8 -0
  140. package/dist/packageMetadata.js.map +1 -0
  141. package/dist/request/CommonAuthorizationCodeRequest.d.ts +3 -0
  142. package/dist/request/CommonAuthorizationCodeRequest.d.ts.map +1 -1
  143. package/dist/request/CommonAuthorizationUrlRequest.d.ts +1 -0
  144. package/dist/request/CommonAuthorizationUrlRequest.d.ts.map +1 -1
  145. package/dist/request/CommonClientCredentialRequest.d.ts +7 -4
  146. package/dist/request/CommonClientCredentialRequest.d.ts.map +1 -1
  147. package/dist/request/CommonRefreshTokenRequest.d.ts +2 -0
  148. package/dist/request/CommonRefreshTokenRequest.d.ts.map +1 -1
  149. package/dist/request/RequestParameterBuilder.d.ts +11 -0
  150. package/dist/request/RequestParameterBuilder.d.ts.map +1 -1
  151. package/dist/request/RequestParameterBuilder.js +340 -0
  152. package/dist/request/RequestParameterBuilder.js.map +1 -0
  153. package/dist/request/RequestValidator.d.ts.map +1 -1
  154. package/dist/request/RequestValidator.js +92 -0
  155. package/dist/request/RequestValidator.js.map +1 -0
  156. package/dist/request/ScopeSet.js +192 -0
  157. package/dist/request/ScopeSet.js.map +1 -0
  158. package/dist/response/AuthenticationResult.d.ts +1 -1
  159. package/dist/response/AuthenticationResult.d.ts.map +1 -1
  160. package/dist/response/AuthorizationCodePayload.d.ts +1 -0
  161. package/dist/response/AuthorizationCodePayload.d.ts.map +1 -1
  162. package/dist/response/IMDSBadResponse.d.ts +5 -0
  163. package/dist/response/IMDSBadResponse.d.ts.map +1 -0
  164. package/dist/response/ResponseHandler.d.ts.map +1 -1
  165. package/dist/response/ResponseHandler.js +281 -0
  166. package/dist/response/ResponseHandler.js.map +1 -0
  167. package/dist/telemetry/server/ServerTelemetryManager.d.ts +23 -1
  168. package/dist/telemetry/server/ServerTelemetryManager.d.ts.map +1 -1
  169. package/dist/telemetry/server/ServerTelemetryManager.js +168 -0
  170. package/dist/telemetry/server/ServerTelemetryManager.js.map +1 -0
  171. package/dist/url/UrlString.d.ts +10 -0
  172. package/dist/url/UrlString.d.ts.map +1 -1
  173. package/dist/url/UrlString.js +223 -0
  174. package/dist/url/UrlString.js.map +1 -0
  175. package/dist/utils/Constants.d.ts +47 -4
  176. package/dist/utils/Constants.d.ts.map +1 -1
  177. package/dist/utils/Constants.js +362 -0
  178. package/dist/utils/Constants.js.map +1 -0
  179. package/dist/utils/ProtocolUtils.js +77 -0
  180. package/dist/utils/ProtocolUtils.js.map +1 -0
  181. package/dist/utils/StringUtils.d.ts +5 -0
  182. package/dist/utils/StringUtils.d.ts.map +1 -1
  183. package/dist/utils/StringUtils.js +128 -0
  184. package/dist/utils/StringUtils.js.map +1 -0
  185. package/dist/utils/TimeUtils.d.ts +13 -0
  186. package/dist/utils/TimeUtils.d.ts.map +1 -1
  187. package/dist/utils/TimeUtils.js +53 -0
  188. package/dist/utils/TimeUtils.js.map +1 -0
  189. package/package.json +10 -19
  190. package/dist/index.es.js +0 -6472
@@ -0,0 +1,713 @@
1
+ /*! @azure/msal-common v4.5.1 2021-08-02 */
2
+ 'use strict';
3
+ import { __extends } from '../_virtual/_tslib.js';
4
+ import { Constants, CredentialType, CacheSchemaType, THE_FAMILY_ID, APP_METADATA, AUTHORITY_METADATA_CONSTANTS, AuthenticationScheme } from '../utils/Constants.js';
5
+ import { CredentialEntity } from './entities/CredentialEntity.js';
6
+ import { ScopeSet } from '../request/ScopeSet.js';
7
+ import { AccountEntity } from './entities/AccountEntity.js';
8
+ import { AuthError } from '../error/AuthError.js';
9
+ import { ClientAuthError } from '../error/ClientAuthError.js';
10
+ import { AuthToken } from '../account/AuthToken.js';
11
+
12
+ /*
13
+ * Copyright (c) Microsoft Corporation. All rights reserved.
14
+ * Licensed under the MIT License.
15
+ */
16
+ /**
17
+ * Interface class which implement cache storage functions used by MSAL to perform validity checks, and store tokens.
18
+ */
19
+ var CacheManager = /** @class */ (function () {
20
+ function CacheManager(clientId, cryptoImpl) {
21
+ this.clientId = clientId;
22
+ this.cryptoImpl = cryptoImpl;
23
+ }
24
+ /**
25
+ * Returns all accounts in cache
26
+ */
27
+ CacheManager.prototype.getAllAccounts = function () {
28
+ var _this = this;
29
+ var currentAccounts = this.getAccountsFilteredBy();
30
+ var accountValues = Object.keys(currentAccounts).map(function (accountKey) { return currentAccounts[accountKey]; });
31
+ var numAccounts = accountValues.length;
32
+ if (numAccounts < 1) {
33
+ return [];
34
+ }
35
+ else {
36
+ var allAccounts = accountValues.map(function (value) {
37
+ var accountEntity = CacheManager.toObject(new AccountEntity(), value);
38
+ var accountInfo = accountEntity.getAccountInfo();
39
+ var idToken = _this.readIdTokenFromCache(_this.clientId, accountInfo);
40
+ if (idToken && !accountInfo.idTokenClaims) {
41
+ accountInfo.idTokenClaims = new AuthToken(idToken.secret, _this.cryptoImpl).claims;
42
+ }
43
+ return accountInfo;
44
+ });
45
+ return allAccounts;
46
+ }
47
+ };
48
+ /**
49
+ * saves a cache record
50
+ * @param cacheRecord
51
+ */
52
+ CacheManager.prototype.saveCacheRecord = function (cacheRecord) {
53
+ if (!cacheRecord) {
54
+ throw ClientAuthError.createNullOrUndefinedCacheRecord();
55
+ }
56
+ if (!!cacheRecord.account) {
57
+ this.setAccount(cacheRecord.account);
58
+ }
59
+ if (!!cacheRecord.idToken) {
60
+ this.setIdTokenCredential(cacheRecord.idToken);
61
+ }
62
+ if (!!cacheRecord.accessToken) {
63
+ this.saveAccessToken(cacheRecord.accessToken);
64
+ }
65
+ if (!!cacheRecord.refreshToken) {
66
+ this.setRefreshTokenCredential(cacheRecord.refreshToken);
67
+ }
68
+ if (!!cacheRecord.appMetadata) {
69
+ this.setAppMetadata(cacheRecord.appMetadata);
70
+ }
71
+ };
72
+ /**
73
+ * saves access token credential
74
+ * @param credential
75
+ */
76
+ CacheManager.prototype.saveAccessToken = function (credential) {
77
+ var _this = this;
78
+ var currentTokenCache = this.getCredentialsFilteredBy({
79
+ clientId: credential.clientId,
80
+ credentialType: credential.credentialType,
81
+ environment: credential.environment,
82
+ homeAccountId: credential.homeAccountId,
83
+ realm: credential.realm,
84
+ });
85
+ var currentScopes = ScopeSet.fromString(credential.target);
86
+ var currentAccessTokens = Object.keys(currentTokenCache.accessTokens).map(function (key) { return currentTokenCache.accessTokens[key]; });
87
+ if (currentAccessTokens) {
88
+ currentAccessTokens.forEach(function (tokenEntity) {
89
+ var tokenScopeSet = ScopeSet.fromString(tokenEntity.target);
90
+ if (tokenScopeSet.intersectingScopeSets(currentScopes)) {
91
+ _this.removeCredential(tokenEntity);
92
+ }
93
+ });
94
+ }
95
+ this.setAccessTokenCredential(credential);
96
+ };
97
+ /**
98
+ * retrieve accounts matching all provided filters; if no filter is set, get all accounts
99
+ * not checking for casing as keys are all generated in lower case, remember to convert to lower case if object properties are compared
100
+ * @param homeAccountId
101
+ * @param environment
102
+ * @param realm
103
+ */
104
+ CacheManager.prototype.getAccountsFilteredBy = function (accountFilter) {
105
+ return this.getAccountsFilteredByInternal(accountFilter ? accountFilter.homeAccountId : "", accountFilter ? accountFilter.environment : "", accountFilter ? accountFilter.realm : "");
106
+ };
107
+ /**
108
+ * retrieve accounts matching all provided filters; if no filter is set, get all accounts
109
+ * not checking for casing as keys are all generated in lower case, remember to convert to lower case if object properties are compared
110
+ * @param homeAccountId
111
+ * @param environment
112
+ * @param realm
113
+ */
114
+ CacheManager.prototype.getAccountsFilteredByInternal = function (homeAccountId, environment, realm) {
115
+ var _this = this;
116
+ var allCacheKeys = this.getKeys();
117
+ var matchingAccounts = {};
118
+ allCacheKeys.forEach(function (cacheKey) {
119
+ var entity = _this.getAccount(cacheKey);
120
+ if (!entity) {
121
+ return;
122
+ }
123
+ if (!!homeAccountId && !_this.matchHomeAccountId(entity, homeAccountId)) {
124
+ return;
125
+ }
126
+ if (!!environment && !_this.matchEnvironment(entity, environment)) {
127
+ return;
128
+ }
129
+ if (!!realm && !_this.matchRealm(entity, realm)) {
130
+ return;
131
+ }
132
+ matchingAccounts[cacheKey] = entity;
133
+ });
134
+ return matchingAccounts;
135
+ };
136
+ /**
137
+ * retrieve credentails matching all provided filters; if no filter is set, get all credentials
138
+ * @param homeAccountId
139
+ * @param environment
140
+ * @param credentialType
141
+ * @param clientId
142
+ * @param realm
143
+ * @param target
144
+ */
145
+ CacheManager.prototype.getCredentialsFilteredBy = function (filter) {
146
+ return this.getCredentialsFilteredByInternal(filter.homeAccountId, filter.environment, filter.credentialType, filter.clientId, filter.familyId, filter.realm, filter.target, filter.oboAssertion);
147
+ };
148
+ /**
149
+ * Support function to help match credentials
150
+ * @param homeAccountId
151
+ * @param environment
152
+ * @param credentialType
153
+ * @param clientId
154
+ * @param realm
155
+ * @param target
156
+ */
157
+ CacheManager.prototype.getCredentialsFilteredByInternal = function (homeAccountId, environment, credentialType, clientId, familyId, realm, target, oboAssertion) {
158
+ var _this = this;
159
+ var allCacheKeys = this.getKeys();
160
+ var matchingCredentials = {
161
+ idTokens: {},
162
+ accessTokens: {},
163
+ refreshTokens: {},
164
+ };
165
+ allCacheKeys.forEach(function (cacheKey) {
166
+ // don't parse any non-credential type cache entities
167
+ var credType = CredentialEntity.getCredentialType(cacheKey);
168
+ if (credType === Constants.NOT_DEFINED) {
169
+ return;
170
+ }
171
+ // Attempt retrieval
172
+ var entity = _this.getSpecificCredential(cacheKey, credType);
173
+ if (!entity) {
174
+ return;
175
+ }
176
+ if (!!oboAssertion && !_this.matchOboAssertion(entity, oboAssertion)) {
177
+ return;
178
+ }
179
+ if (!!homeAccountId && !_this.matchHomeAccountId(entity, homeAccountId)) {
180
+ return;
181
+ }
182
+ if (!!environment && !_this.matchEnvironment(entity, environment)) {
183
+ return;
184
+ }
185
+ if (!!realm && !_this.matchRealm(entity, realm)) {
186
+ return;
187
+ }
188
+ if (!!credentialType && !_this.matchCredentialType(entity, credentialType)) {
189
+ return;
190
+ }
191
+ if (!!clientId && !_this.matchClientId(entity, clientId)) {
192
+ return;
193
+ }
194
+ if (!!familyId && !_this.matchFamilyId(entity, familyId)) {
195
+ return;
196
+ }
197
+ /*
198
+ * idTokens do not have "target", target specific refreshTokens do exist for some types of authentication
199
+ * Resource specific refresh tokens case will be added when the support is deemed necessary
200
+ */
201
+ if (!!target && !_this.matchTarget(entity, target)) {
202
+ return;
203
+ }
204
+ switch (credType) {
205
+ case CredentialType.ID_TOKEN:
206
+ matchingCredentials.idTokens[cacheKey] = entity;
207
+ break;
208
+ case CredentialType.ACCESS_TOKEN:
209
+ case CredentialType.ACCESS_TOKEN_WITH_AUTH_SCHEME:
210
+ matchingCredentials.accessTokens[cacheKey] = entity;
211
+ break;
212
+ case CredentialType.REFRESH_TOKEN:
213
+ matchingCredentials.refreshTokens[cacheKey] = entity;
214
+ break;
215
+ }
216
+ });
217
+ return matchingCredentials;
218
+ };
219
+ /**
220
+ * retrieve appMetadata matching all provided filters; if no filter is set, get all appMetadata
221
+ * @param filter
222
+ */
223
+ CacheManager.prototype.getAppMetadataFilteredBy = function (filter) {
224
+ return this.getAppMetadataFilteredByInternal(filter.environment, filter.clientId);
225
+ };
226
+ /**
227
+ * Support function to help match appMetadata
228
+ * @param environment
229
+ * @param clientId
230
+ */
231
+ CacheManager.prototype.getAppMetadataFilteredByInternal = function (environment, clientId) {
232
+ var _this = this;
233
+ var allCacheKeys = this.getKeys();
234
+ var matchingAppMetadata = {};
235
+ allCacheKeys.forEach(function (cacheKey) {
236
+ // don't parse any non-appMetadata type cache entities
237
+ if (!_this.isAppMetadata(cacheKey)) {
238
+ return;
239
+ }
240
+ // Attempt retrieval
241
+ var entity = _this.getAppMetadata(cacheKey);
242
+ if (!entity) {
243
+ return;
244
+ }
245
+ if (!!environment && !_this.matchEnvironment(entity, environment)) {
246
+ return;
247
+ }
248
+ if (!!clientId && !_this.matchClientId(entity, clientId)) {
249
+ return;
250
+ }
251
+ matchingAppMetadata[cacheKey] = entity;
252
+ });
253
+ return matchingAppMetadata;
254
+ };
255
+ /**
256
+ * retrieve authorityMetadata that contains a matching alias
257
+ * @param filter
258
+ */
259
+ CacheManager.prototype.getAuthorityMetadataByAlias = function (host) {
260
+ var _this = this;
261
+ var allCacheKeys = this.getAuthorityMetadataKeys();
262
+ var matchedEntity = null;
263
+ allCacheKeys.forEach(function (cacheKey) {
264
+ // don't parse any non-authorityMetadata type cache entities
265
+ if (!_this.isAuthorityMetadata(cacheKey) || cacheKey.indexOf(_this.clientId) === -1) {
266
+ return;
267
+ }
268
+ // Attempt retrieval
269
+ var entity = _this.getAuthorityMetadata(cacheKey);
270
+ if (!entity) {
271
+ return;
272
+ }
273
+ if (entity.aliases.indexOf(host) === -1) {
274
+ return;
275
+ }
276
+ matchedEntity = entity;
277
+ });
278
+ return matchedEntity;
279
+ };
280
+ /**
281
+ * Removes all accounts and related tokens from cache.
282
+ */
283
+ CacheManager.prototype.removeAllAccounts = function () {
284
+ var _this = this;
285
+ var allCacheKeys = this.getKeys();
286
+ allCacheKeys.forEach(function (cacheKey) {
287
+ var entity = _this.getAccount(cacheKey);
288
+ if (!entity) {
289
+ return;
290
+ }
291
+ _this.removeAccount(cacheKey);
292
+ });
293
+ return true;
294
+ };
295
+ /**
296
+ * returns a boolean if the given account is removed
297
+ * @param account
298
+ */
299
+ CacheManager.prototype.removeAccount = function (accountKey) {
300
+ var account = this.getAccount(accountKey);
301
+ if (!account) {
302
+ throw ClientAuthError.createNoAccountFoundError();
303
+ }
304
+ return (this.removeAccountContext(account) && this.removeItem(accountKey, CacheSchemaType.ACCOUNT));
305
+ };
306
+ /**
307
+ * returns a boolean if the given account is removed
308
+ * @param account
309
+ */
310
+ CacheManager.prototype.removeAccountContext = function (account) {
311
+ var _this = this;
312
+ var allCacheKeys = this.getKeys();
313
+ var accountId = account.generateAccountId();
314
+ allCacheKeys.forEach(function (cacheKey) {
315
+ // don't parse any non-credential type cache entities
316
+ var credType = CredentialEntity.getCredentialType(cacheKey);
317
+ if (credType === Constants.NOT_DEFINED) {
318
+ return;
319
+ }
320
+ var cacheEntity = _this.getSpecificCredential(cacheKey, credType);
321
+ if (!!cacheEntity && accountId === cacheEntity.generateAccountId()) {
322
+ _this.removeCredential(cacheEntity);
323
+ }
324
+ });
325
+ return true;
326
+ };
327
+ /**
328
+ * returns a boolean if the given credential is removed
329
+ * @param credential
330
+ */
331
+ CacheManager.prototype.removeCredential = function (credential) {
332
+ var key = credential.generateCredentialKey();
333
+ return this.removeItem(key, CacheSchemaType.CREDENTIAL);
334
+ };
335
+ /**
336
+ * Removes all app metadata objects from cache.
337
+ */
338
+ CacheManager.prototype.removeAppMetadata = function () {
339
+ var _this = this;
340
+ var allCacheKeys = this.getKeys();
341
+ allCacheKeys.forEach(function (cacheKey) {
342
+ if (_this.isAppMetadata(cacheKey)) {
343
+ _this.removeItem(cacheKey, CacheSchemaType.APP_METADATA);
344
+ }
345
+ });
346
+ return true;
347
+ };
348
+ /**
349
+ * Retrieve the cached credentials into a cacherecord
350
+ * @param account
351
+ * @param clientId
352
+ * @param scopes
353
+ * @param environment
354
+ * @param authScheme
355
+ */
356
+ CacheManager.prototype.readCacheRecord = function (account, clientId, scopes, environment, authScheme) {
357
+ var cachedAccount = this.readAccountFromCache(account);
358
+ var cachedIdToken = this.readIdTokenFromCache(clientId, account);
359
+ var cachedAccessToken = this.readAccessTokenFromCache(clientId, account, scopes, authScheme);
360
+ var cachedRefreshToken = this.readRefreshTokenFromCache(clientId, account, false);
361
+ var cachedAppMetadata = this.readAppMetadataFromCache(environment, clientId);
362
+ if (cachedAccount && cachedIdToken) {
363
+ cachedAccount.idTokenClaims = new AuthToken(cachedIdToken.secret, this.cryptoImpl).claims;
364
+ }
365
+ return {
366
+ account: cachedAccount,
367
+ idToken: cachedIdToken,
368
+ accessToken: cachedAccessToken,
369
+ refreshToken: cachedRefreshToken,
370
+ appMetadata: cachedAppMetadata,
371
+ };
372
+ };
373
+ /**
374
+ * Retrieve AccountEntity from cache
375
+ * @param account
376
+ */
377
+ CacheManager.prototype.readAccountFromCache = function (account) {
378
+ var accountKey = AccountEntity.generateAccountCacheKey(account);
379
+ return this.getAccount(accountKey);
380
+ };
381
+ /**
382
+ * Retrieve IdTokenEntity from cache
383
+ * @param clientId
384
+ * @param account
385
+ * @param inputRealm
386
+ */
387
+ CacheManager.prototype.readIdTokenFromCache = function (clientId, account) {
388
+ var idTokenFilter = {
389
+ homeAccountId: account.homeAccountId,
390
+ environment: account.environment,
391
+ credentialType: CredentialType.ID_TOKEN,
392
+ clientId: clientId,
393
+ realm: account.tenantId,
394
+ };
395
+ var credentialCache = this.getCredentialsFilteredBy(idTokenFilter);
396
+ var idTokens = Object.keys(credentialCache.idTokens).map(function (key) { return credentialCache.idTokens[key]; });
397
+ var numIdTokens = idTokens.length;
398
+ if (numIdTokens < 1) {
399
+ return null;
400
+ }
401
+ else if (numIdTokens > 1) {
402
+ throw ClientAuthError.createMultipleMatchingTokensInCacheError();
403
+ }
404
+ return idTokens[0];
405
+ };
406
+ /**
407
+ * Retrieve AccessTokenEntity from cache
408
+ * @param clientId
409
+ * @param account
410
+ * @param scopes
411
+ * @param authScheme
412
+ */
413
+ CacheManager.prototype.readAccessTokenFromCache = function (clientId, account, scopes, authScheme) {
414
+ var credentialType = (authScheme === AuthenticationScheme.POP) ? CredentialType.ACCESS_TOKEN_WITH_AUTH_SCHEME : CredentialType.ACCESS_TOKEN;
415
+ var accessTokenFilter = {
416
+ homeAccountId: account.homeAccountId,
417
+ environment: account.environment,
418
+ credentialType: credentialType,
419
+ clientId: clientId,
420
+ realm: account.tenantId,
421
+ target: scopes.printScopesLowerCase(),
422
+ };
423
+ var credentialCache = this.getCredentialsFilteredBy(accessTokenFilter);
424
+ var accessTokens = Object.keys(credentialCache.accessTokens).map(function (key) { return credentialCache.accessTokens[key]; });
425
+ var numAccessTokens = accessTokens.length;
426
+ if (numAccessTokens < 1) {
427
+ return null;
428
+ }
429
+ else if (numAccessTokens > 1) {
430
+ throw ClientAuthError.createMultipleMatchingTokensInCacheError();
431
+ }
432
+ return accessTokens[0];
433
+ };
434
+ /**
435
+ * Helper to retrieve the appropriate refresh token from cache
436
+ * @param clientId
437
+ * @param account
438
+ * @param familyRT
439
+ */
440
+ CacheManager.prototype.readRefreshTokenFromCache = function (clientId, account, familyRT) {
441
+ var id = familyRT ? THE_FAMILY_ID : undefined;
442
+ var refreshTokenFilter = {
443
+ homeAccountId: account.homeAccountId,
444
+ environment: account.environment,
445
+ credentialType: CredentialType.REFRESH_TOKEN,
446
+ clientId: clientId,
447
+ familyId: id
448
+ };
449
+ var credentialCache = this.getCredentialsFilteredBy(refreshTokenFilter);
450
+ var refreshTokens = Object.keys(credentialCache.refreshTokens).map(function (key) { return credentialCache.refreshTokens[key]; });
451
+ var numRefreshTokens = refreshTokens.length;
452
+ if (numRefreshTokens < 1) {
453
+ return null;
454
+ }
455
+ // address the else case after remove functions address environment aliases
456
+ return refreshTokens[0];
457
+ };
458
+ /**
459
+ * Retrieve AppMetadataEntity from cache
460
+ */
461
+ CacheManager.prototype.readAppMetadataFromCache = function (environment, clientId) {
462
+ var appMetadataFilter = {
463
+ environment: environment,
464
+ clientId: clientId,
465
+ };
466
+ var appMetadata = this.getAppMetadataFilteredBy(appMetadataFilter);
467
+ var appMetadataEntries = Object.keys(appMetadata).map(function (key) { return appMetadata[key]; });
468
+ var numAppMetadata = appMetadataEntries.length;
469
+ if (numAppMetadata < 1) {
470
+ return null;
471
+ }
472
+ else if (numAppMetadata > 1) {
473
+ throw ClientAuthError.createMultipleMatchingAppMetadataInCacheError();
474
+ }
475
+ return appMetadataEntries[0];
476
+ };
477
+ /**
478
+ * Return the family_id value associated with FOCI
479
+ * @param environment
480
+ * @param clientId
481
+ */
482
+ CacheManager.prototype.isAppMetadataFOCI = function (environment, clientId) {
483
+ var appMetadata = this.readAppMetadataFromCache(environment, clientId);
484
+ return !!(appMetadata && appMetadata.familyId === THE_FAMILY_ID);
485
+ };
486
+ /**
487
+ * helper to match account ids
488
+ * @param value
489
+ * @param homeAccountId
490
+ */
491
+ CacheManager.prototype.matchHomeAccountId = function (entity, homeAccountId) {
492
+ return !!(entity.homeAccountId && homeAccountId === entity.homeAccountId);
493
+ };
494
+ /**
495
+ * helper to match assertion
496
+ * @param value
497
+ * @param oboAssertion
498
+ */
499
+ CacheManager.prototype.matchOboAssertion = function (entity, oboAssertion) {
500
+ return !!(entity.oboAssertion && oboAssertion === entity.oboAssertion);
501
+ };
502
+ /**
503
+ * helper to match environment
504
+ * @param value
505
+ * @param environment
506
+ */
507
+ CacheManager.prototype.matchEnvironment = function (entity, environment) {
508
+ var cloudMetadata = this.getAuthorityMetadataByAlias(environment);
509
+ if (cloudMetadata && cloudMetadata.aliases.indexOf(entity.environment) > -1) {
510
+ return true;
511
+ }
512
+ return false;
513
+ };
514
+ /**
515
+ * helper to match credential type
516
+ * @param entity
517
+ * @param credentialType
518
+ */
519
+ CacheManager.prototype.matchCredentialType = function (entity, credentialType) {
520
+ return (entity.credentialType && credentialType.toLowerCase() === entity.credentialType.toLowerCase());
521
+ };
522
+ /**
523
+ * helper to match client ids
524
+ * @param entity
525
+ * @param clientId
526
+ */
527
+ CacheManager.prototype.matchClientId = function (entity, clientId) {
528
+ return !!(entity.clientId && clientId === entity.clientId);
529
+ };
530
+ /**
531
+ * helper to match family ids
532
+ * @param entity
533
+ * @param familyId
534
+ */
535
+ CacheManager.prototype.matchFamilyId = function (entity, familyId) {
536
+ return !!(entity.familyId && familyId === entity.familyId);
537
+ };
538
+ /**
539
+ * helper to match realm
540
+ * @param entity
541
+ * @param realm
542
+ */
543
+ CacheManager.prototype.matchRealm = function (entity, realm) {
544
+ return !!(entity.realm && realm === entity.realm);
545
+ };
546
+ /**
547
+ * Returns true if the target scopes are a subset of the current entity's scopes, false otherwise.
548
+ * @param entity
549
+ * @param target
550
+ */
551
+ CacheManager.prototype.matchTarget = function (entity, target) {
552
+ var isNotAccessTokenCredential = (entity.credentialType !== CredentialType.ACCESS_TOKEN && entity.credentialType !== CredentialType.ACCESS_TOKEN_WITH_AUTH_SCHEME);
553
+ if (isNotAccessTokenCredential || !entity.target) {
554
+ return false;
555
+ }
556
+ var entityScopeSet = ScopeSet.fromString(entity.target);
557
+ var requestTargetScopeSet = ScopeSet.fromString(target);
558
+ if (!requestTargetScopeSet.containsOnlyOIDCScopes()) {
559
+ requestTargetScopeSet.removeOIDCScopes(); // ignore OIDC scopes
560
+ }
561
+ else {
562
+ requestTargetScopeSet.removeScope(Constants.OFFLINE_ACCESS_SCOPE);
563
+ }
564
+ return entityScopeSet.containsScopeSet(requestTargetScopeSet);
565
+ };
566
+ /**
567
+ * returns if a given cache entity is of the type appmetadata
568
+ * @param key
569
+ */
570
+ CacheManager.prototype.isAppMetadata = function (key) {
571
+ return key.indexOf(APP_METADATA) !== -1;
572
+ };
573
+ /**
574
+ * returns if a given cache entity is of the type authoritymetadata
575
+ * @param key
576
+ */
577
+ CacheManager.prototype.isAuthorityMetadata = function (key) {
578
+ return key.indexOf(AUTHORITY_METADATA_CONSTANTS.CACHE_KEY) !== -1;
579
+ };
580
+ /**
581
+ * returns cache key used for cloud instance metadata
582
+ */
583
+ CacheManager.prototype.generateAuthorityMetadataCacheKey = function (authority) {
584
+ return AUTHORITY_METADATA_CONSTANTS.CACHE_KEY + "-" + this.clientId + "-" + authority;
585
+ };
586
+ /**
587
+ * Returns the specific credential (IdToken/AccessToken/RefreshToken) from the cache
588
+ * @param key
589
+ * @param credType
590
+ */
591
+ CacheManager.prototype.getSpecificCredential = function (key, credType) {
592
+ switch (credType) {
593
+ case CredentialType.ID_TOKEN: {
594
+ return this.getIdTokenCredential(key);
595
+ }
596
+ case CredentialType.ACCESS_TOKEN:
597
+ case CredentialType.ACCESS_TOKEN_WITH_AUTH_SCHEME: {
598
+ return this.getAccessTokenCredential(key);
599
+ }
600
+ case CredentialType.REFRESH_TOKEN: {
601
+ return this.getRefreshTokenCredential(key);
602
+ }
603
+ default:
604
+ return null;
605
+ }
606
+ };
607
+ /**
608
+ * Helper to convert serialized data to object
609
+ * @param obj
610
+ * @param json
611
+ */
612
+ CacheManager.toObject = function (obj, json) {
613
+ for (var propertyName in json) {
614
+ obj[propertyName] = json[propertyName];
615
+ }
616
+ return obj;
617
+ };
618
+ return CacheManager;
619
+ }());
620
+ var DefaultStorageClass = /** @class */ (function (_super) {
621
+ __extends(DefaultStorageClass, _super);
622
+ function DefaultStorageClass() {
623
+ return _super !== null && _super.apply(this, arguments) || this;
624
+ }
625
+ DefaultStorageClass.prototype.setAccount = function () {
626
+ var notImplErr = "Storage interface - setAccount() has not been implemented for the cacheStorage interface.";
627
+ throw AuthError.createUnexpectedError(notImplErr);
628
+ };
629
+ DefaultStorageClass.prototype.getAccount = function () {
630
+ var notImplErr = "Storage interface - getAccount() has not been implemented for the cacheStorage interface.";
631
+ throw AuthError.createUnexpectedError(notImplErr);
632
+ };
633
+ DefaultStorageClass.prototype.setIdTokenCredential = function () {
634
+ var notImplErr = "Storage interface - setIdTokenCredential() has not been implemented for the cacheStorage interface.";
635
+ throw AuthError.createUnexpectedError(notImplErr);
636
+ };
637
+ DefaultStorageClass.prototype.getIdTokenCredential = function () {
638
+ var notImplErr = "Storage interface - getIdTokenCredential() has not been implemented for the cacheStorage interface.";
639
+ throw AuthError.createUnexpectedError(notImplErr);
640
+ };
641
+ DefaultStorageClass.prototype.setAccessTokenCredential = function () {
642
+ var notImplErr = "Storage interface - setAccessTokenCredential() has not been implemented for the cacheStorage interface.";
643
+ throw AuthError.createUnexpectedError(notImplErr);
644
+ };
645
+ DefaultStorageClass.prototype.getAccessTokenCredential = function () {
646
+ var notImplErr = "Storage interface - getAccessTokenCredential() has not been implemented for the cacheStorage interface.";
647
+ throw AuthError.createUnexpectedError(notImplErr);
648
+ };
649
+ DefaultStorageClass.prototype.setRefreshTokenCredential = function () {
650
+ var notImplErr = "Storage interface - setRefreshTokenCredential() has not been implemented for the cacheStorage interface.";
651
+ throw AuthError.createUnexpectedError(notImplErr);
652
+ };
653
+ DefaultStorageClass.prototype.getRefreshTokenCredential = function () {
654
+ var notImplErr = "Storage interface - getRefreshTokenCredential() has not been implemented for the cacheStorage interface.";
655
+ throw AuthError.createUnexpectedError(notImplErr);
656
+ };
657
+ DefaultStorageClass.prototype.setAppMetadata = function () {
658
+ var notImplErr = "Storage interface - setAppMetadata() has not been implemented for the cacheStorage interface.";
659
+ throw AuthError.createUnexpectedError(notImplErr);
660
+ };
661
+ DefaultStorageClass.prototype.getAppMetadata = function () {
662
+ var notImplErr = "Storage interface - getAppMetadata() has not been implemented for the cacheStorage interface.";
663
+ throw AuthError.createUnexpectedError(notImplErr);
664
+ };
665
+ DefaultStorageClass.prototype.setServerTelemetry = function () {
666
+ var notImplErr = "Storage interface - setServerTelemetry() has not been implemented for the cacheStorage interface.";
667
+ throw AuthError.createUnexpectedError(notImplErr);
668
+ };
669
+ DefaultStorageClass.prototype.getServerTelemetry = function () {
670
+ var notImplErr = "Storage interface - getServerTelemetry() has not been implemented for the cacheStorage interface.";
671
+ throw AuthError.createUnexpectedError(notImplErr);
672
+ };
673
+ DefaultStorageClass.prototype.setAuthorityMetadata = function () {
674
+ var notImplErr = "Storage interface - setAuthorityMetadata() has not been implemented for the cacheStorage interface.";
675
+ throw AuthError.createUnexpectedError(notImplErr);
676
+ };
677
+ DefaultStorageClass.prototype.getAuthorityMetadata = function () {
678
+ var notImplErr = "Storage interface - getAuthorityMetadata() has not been implemented for the cacheStorage interface.";
679
+ throw AuthError.createUnexpectedError(notImplErr);
680
+ };
681
+ DefaultStorageClass.prototype.getAuthorityMetadataKeys = function () {
682
+ var notImplErr = "Storage interface - getAuthorityMetadataKeys() has not been implemented for the cacheStorage interface.";
683
+ throw AuthError.createUnexpectedError(notImplErr);
684
+ };
685
+ DefaultStorageClass.prototype.setThrottlingCache = function () {
686
+ var notImplErr = "Storage interface - setThrottlingCache() has not been implemented for the cacheStorage interface.";
687
+ throw AuthError.createUnexpectedError(notImplErr);
688
+ };
689
+ DefaultStorageClass.prototype.getThrottlingCache = function () {
690
+ var notImplErr = "Storage interface - getThrottlingCache() has not been implemented for the cacheStorage interface.";
691
+ throw AuthError.createUnexpectedError(notImplErr);
692
+ };
693
+ DefaultStorageClass.prototype.removeItem = function () {
694
+ var notImplErr = "Storage interface - removeItem() has not been implemented for the cacheStorage interface.";
695
+ throw AuthError.createUnexpectedError(notImplErr);
696
+ };
697
+ DefaultStorageClass.prototype.containsKey = function () {
698
+ var notImplErr = "Storage interface - containsKey() has not been implemented for the cacheStorage interface.";
699
+ throw AuthError.createUnexpectedError(notImplErr);
700
+ };
701
+ DefaultStorageClass.prototype.getKeys = function () {
702
+ var notImplErr = "Storage interface - getKeys() has not been implemented for the cacheStorage interface.";
703
+ throw AuthError.createUnexpectedError(notImplErr);
704
+ };
705
+ DefaultStorageClass.prototype.clear = function () {
706
+ var notImplErr = "Storage interface - clear() has not been implemented for the cacheStorage interface.";
707
+ throw AuthError.createUnexpectedError(notImplErr);
708
+ };
709
+ return DefaultStorageClass;
710
+ }(CacheManager));
711
+
712
+ export { CacheManager, DefaultStorageClass };
713
+ //# sourceMappingURL=CacheManager.js.map