@azure/msal-node 2.0.1 → 2.1.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/README.md +3 -1
- package/dist/cache/NodeStorage.cjs +1 -1
- package/dist/cache/NodeStorage.cjs.map +1 -1
- package/dist/cache/NodeStorage.d.ts +1 -1
- package/dist/cache/NodeStorage.d.ts.map +1 -1
- package/dist/cache/NodeStorage.mjs +1 -1
- package/dist/cache/NodeStorage.mjs.map +1 -1
- package/dist/cache/TokenCache.cjs +5 -9
- package/dist/cache/TokenCache.cjs.map +1 -1
- package/dist/cache/TokenCache.d.ts +3 -3
- package/dist/cache/TokenCache.d.ts.map +1 -1
- package/dist/cache/TokenCache.mjs +6 -10
- package/dist/cache/TokenCache.mjs.map +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.cjs +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.cjs.map +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.d.ts +2 -2
- package/dist/cache/distributed/DistributedCachePlugin.d.ts.map +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.mjs +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.mjs.map +1 -1
- package/dist/cache/serializer/Deserializer.cjs +2 -4
- package/dist/cache/serializer/Deserializer.cjs.map +1 -1
- package/dist/cache/serializer/Deserializer.d.ts +1 -1
- package/dist/cache/serializer/Deserializer.d.ts.map +1 -1
- package/dist/cache/serializer/Deserializer.mjs +3 -5
- package/dist/cache/serializer/Deserializer.mjs.map +1 -1
- package/dist/cache/serializer/Serializer.cjs +1 -1
- package/dist/cache/serializer/Serializer.cjs.map +1 -1
- package/dist/cache/serializer/Serializer.d.ts +1 -1
- package/dist/cache/serializer/Serializer.d.ts.map +1 -1
- package/dist/cache/serializer/Serializer.mjs +1 -1
- package/dist/cache/serializer/Serializer.mjs.map +1 -1
- package/dist/client/ClientApplication.cjs +2 -2
- package/dist/client/ClientApplication.cjs.map +1 -1
- package/dist/client/ClientApplication.d.ts +10 -10
- package/dist/client/ClientApplication.d.ts.map +1 -1
- package/dist/client/ClientApplication.mjs +2 -2
- package/dist/client/ClientApplication.mjs.map +1 -1
- package/dist/client/ClientAssertion.cjs +3 -3
- package/dist/client/ClientAssertion.cjs.map +1 -1
- package/dist/client/ClientAssertion.d.ts +1 -1
- package/dist/client/ClientAssertion.d.ts.map +1 -1
- package/dist/client/ClientAssertion.mjs +3 -3
- package/dist/client/ClientAssertion.mjs.map +1 -1
- package/dist/client/ClientCredentialClient.cjs +26 -4
- package/dist/client/ClientCredentialClient.cjs.map +1 -1
- package/dist/client/ClientCredentialClient.d.ts +1 -0
- package/dist/client/ClientCredentialClient.d.ts.map +1 -1
- package/dist/client/ClientCredentialClient.mjs +27 -5
- package/dist/client/ClientCredentialClient.mjs.map +1 -1
- package/dist/client/ConfidentialClientApplication.cjs +4 -5
- package/dist/client/ConfidentialClientApplication.cjs.map +1 -1
- package/dist/client/ConfidentialClientApplication.d.ts +5 -5
- package/dist/client/ConfidentialClientApplication.d.ts.map +1 -1
- package/dist/client/ConfidentialClientApplication.mjs +5 -6
- package/dist/client/ConfidentialClientApplication.mjs.map +1 -1
- package/dist/client/DeviceCodeClient.cjs +3 -3
- package/dist/client/DeviceCodeClient.cjs.map +1 -1
- package/dist/client/DeviceCodeClient.d.ts.map +1 -1
- package/dist/client/DeviceCodeClient.mjs +4 -4
- package/dist/client/DeviceCodeClient.mjs.map +1 -1
- package/dist/client/IConfidentialClientApplication.d.ts +8 -8
- package/dist/client/IConfidentialClientApplication.d.ts.map +1 -1
- package/dist/client/IPublicClientApplication.d.ts +9 -9
- package/dist/client/IPublicClientApplication.d.ts.map +1 -1
- package/dist/client/OnBehalfOfClient.cjs +7 -8
- package/dist/client/OnBehalfOfClient.cjs.map +1 -1
- package/dist/client/OnBehalfOfClient.d.ts.map +1 -1
- package/dist/client/OnBehalfOfClient.mjs +7 -8
- package/dist/client/OnBehalfOfClient.mjs.map +1 -1
- package/dist/client/PublicClientApplication.cjs +36 -29
- package/dist/client/PublicClientApplication.cjs.map +1 -1
- package/dist/client/PublicClientApplication.d.ts +7 -7
- package/dist/client/PublicClientApplication.d.ts.map +1 -1
- package/dist/client/PublicClientApplication.mjs +40 -33
- package/dist/client/PublicClientApplication.mjs.map +1 -1
- package/dist/client/UsernamePasswordClient.cjs +2 -2
- package/dist/client/UsernamePasswordClient.cjs.map +1 -1
- package/dist/client/UsernamePasswordClient.d.ts.map +1 -1
- package/dist/client/UsernamePasswordClient.mjs +2 -2
- package/dist/client/UsernamePasswordClient.mjs.map +1 -1
- package/dist/config/Configuration.cjs +2 -2
- package/dist/config/Configuration.cjs.map +1 -1
- package/dist/config/Configuration.d.ts +5 -4
- package/dist/config/Configuration.d.ts.map +1 -1
- package/dist/config/Configuration.mjs +2 -2
- package/dist/config/Configuration.mjs.map +1 -1
- package/dist/crypto/CryptoProvider.cjs +1 -1
- package/dist/crypto/CryptoProvider.cjs.map +1 -1
- package/dist/crypto/CryptoProvider.mjs +1 -1
- package/dist/crypto/CryptoProvider.mjs.map +1 -1
- package/dist/crypto/GuidGenerator.cjs +1 -1
- package/dist/crypto/GuidGenerator.cjs.map +1 -1
- package/dist/crypto/GuidGenerator.mjs +1 -1
- package/dist/crypto/GuidGenerator.mjs.map +1 -1
- package/dist/crypto/HashUtils.cjs +1 -1
- package/dist/crypto/HashUtils.cjs.map +1 -1
- package/dist/crypto/HashUtils.mjs +1 -1
- package/dist/crypto/HashUtils.mjs.map +1 -1
- package/dist/crypto/PkceGenerator.cjs +1 -1
- package/dist/crypto/PkceGenerator.cjs.map +1 -1
- package/dist/crypto/PkceGenerator.mjs +1 -1
- package/dist/crypto/PkceGenerator.mjs.map +1 -1
- package/dist/error/NodeAuthError.cjs +1 -1
- package/dist/error/NodeAuthError.cjs.map +1 -1
- package/dist/error/NodeAuthError.mjs +1 -1
- package/dist/error/NodeAuthError.mjs.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +32 -31
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/internals.cjs +1 -1
- package/dist/internals.cjs.map +1 -1
- package/dist/internals.d.ts +2 -2
- package/dist/internals.d.ts.map +1 -1
- package/dist/internals.mjs +1 -1
- package/dist/internals.mjs.map +1 -1
- package/dist/network/HttpClient.cjs +10 -9
- package/dist/network/HttpClient.cjs.map +1 -1
- package/dist/network/HttpClient.d.ts.map +1 -1
- package/dist/network/HttpClient.mjs +3 -2
- package/dist/network/HttpClient.mjs.map +1 -1
- package/dist/network/LoopbackClient.cjs +2 -2
- package/dist/network/LoopbackClient.cjs.map +1 -1
- package/dist/network/LoopbackClient.d.ts +1 -1
- package/dist/network/LoopbackClient.d.ts.map +1 -1
- package/dist/network/LoopbackClient.mjs +5 -5
- package/dist/network/LoopbackClient.mjs.map +1 -1
- package/dist/packageMetadata.cjs +2 -2
- package/dist/packageMetadata.cjs.map +1 -1
- package/dist/packageMetadata.d.ts +1 -1
- package/dist/packageMetadata.mjs +2 -2
- package/dist/packageMetadata.mjs.map +1 -1
- package/dist/request/InteractiveRequest.d.ts +2 -2
- package/dist/request/InteractiveRequest.d.ts.map +1 -1
- package/dist/utils/Constants.cjs +1 -11
- package/dist/utils/Constants.cjs.map +1 -1
- package/dist/utils/Constants.d.ts +0 -10
- package/dist/utils/Constants.d.ts.map +1 -1
- package/dist/utils/Constants.mjs +2 -11
- package/dist/utils/Constants.mjs.map +1 -1
- package/dist/utils/EncodingUtils.cjs +1 -1
- package/dist/utils/EncodingUtils.cjs.map +1 -1
- package/dist/utils/EncodingUtils.mjs +1 -1
- package/dist/utils/EncodingUtils.mjs.map +1 -1
- package/dist/utils/NetworkUtils.cjs +1 -1
- package/dist/utils/NetworkUtils.cjs.map +1 -1
- package/dist/utils/NetworkUtils.mjs +1 -1
- package/dist/utils/NetworkUtils.mjs.map +1 -1
- package/package.json +9 -6
- package/src/cache/ITokenCache.ts +24 -0
- package/src/cache/NodeStorage.ts +533 -0
- package/src/cache/TokenCache.ts +358 -0
- package/src/cache/distributed/DistributedCachePlugin.ts +56 -0
- package/src/cache/distributed/ICacheClient.ts +23 -0
- package/src/cache/distributed/IPartitionManager.ts +35 -0
- package/src/cache/serializer/Deserializer.ts +218 -0
- package/src/cache/serializer/Serializer.ts +173 -0
- package/src/cache/serializer/SerializerTypes.ts +121 -0
- package/src/client/ClientApplication.ts +595 -0
- package/src/client/ClientAssertion.ts +160 -0
- package/src/client/ClientCredentialClient.ts +348 -0
- package/src/client/ConfidentialClientApplication.ts +261 -0
- package/src/client/DeviceCodeClient.ts +360 -0
- package/src/client/IConfidentialClientApplication.ts +72 -0
- package/src/client/IPublicClientApplication.ts +72 -0
- package/src/client/OnBehalfOfClient.ts +361 -0
- package/src/client/PublicClientApplication.ts +283 -0
- package/src/client/UsernamePasswordClient.ts +180 -0
- package/src/config/Configuration.ts +201 -0
- package/src/crypto/CryptoProvider.ts +98 -0
- package/src/crypto/GuidGenerator.ts +28 -0
- package/src/crypto/HashUtils.ts +17 -0
- package/src/crypto/PkceGenerator.ts +63 -0
- package/src/error/NodeAuthError.ts +117 -0
- package/src/index.ts +121 -0
- package/src/internals.ts +12 -0
- package/src/network/HttpClient.ts +425 -0
- package/src/network/ILoopbackClient.ts +19 -0
- package/src/network/LoopbackClient.ts +126 -0
- package/src/packageMetadata.ts +3 -0
- package/src/request/AuthorizationCodeRequest.ts +39 -0
- package/src/request/AuthorizationUrlRequest.ts +50 -0
- package/src/request/ClientCredentialRequest.ts +29 -0
- package/src/request/DeviceCodeRequest.ts +34 -0
- package/src/request/InteractiveRequest.ts +39 -0
- package/src/request/OnBehalfOfRequest.ts +30 -0
- package/src/request/RefreshTokenRequest.ts +34 -0
- package/src/request/SignOutRequest.ts +11 -0
- package/src/request/SilentFlowRequest.ts +32 -0
- package/src/request/UsernamePasswordRequest.ts +36 -0
- package/src/utils/Constants.ts +103 -0
- package/src/utils/EncodingUtils.ts +50 -0
- package/src/utils/NetworkUtils.ts +49 -0
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { NodeStorage } from "./NodeStorage.js";
|
|
7
|
+
import {
|
|
8
|
+
AccountEntity,
|
|
9
|
+
AccountInfo,
|
|
10
|
+
Logger,
|
|
11
|
+
ISerializableTokenCache,
|
|
12
|
+
ICachePlugin,
|
|
13
|
+
TokenCacheContext,
|
|
14
|
+
} from "@azure/msal-common";
|
|
15
|
+
import {
|
|
16
|
+
InMemoryCache,
|
|
17
|
+
JsonCache,
|
|
18
|
+
SerializedAccountEntity,
|
|
19
|
+
SerializedAccessTokenEntity,
|
|
20
|
+
SerializedRefreshTokenEntity,
|
|
21
|
+
SerializedIdTokenEntity,
|
|
22
|
+
SerializedAppMetadataEntity,
|
|
23
|
+
CacheKVStore,
|
|
24
|
+
} from "./serializer/SerializerTypes.js";
|
|
25
|
+
import { Deserializer } from "./serializer/Deserializer.js";
|
|
26
|
+
import { Serializer } from "./serializer/Serializer.js";
|
|
27
|
+
import { ITokenCache } from "./ITokenCache.js";
|
|
28
|
+
|
|
29
|
+
const defaultSerializedCache: JsonCache = {
|
|
30
|
+
Account: {},
|
|
31
|
+
IdToken: {},
|
|
32
|
+
AccessToken: {},
|
|
33
|
+
RefreshToken: {},
|
|
34
|
+
AppMetadata: {},
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* In-memory token cache manager
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
export class TokenCache implements ISerializableTokenCache, ITokenCache {
|
|
42
|
+
private storage: NodeStorage;
|
|
43
|
+
private cacheHasChanged: boolean;
|
|
44
|
+
private cacheSnapshot: string;
|
|
45
|
+
private readonly persistence: ICachePlugin;
|
|
46
|
+
private logger: Logger;
|
|
47
|
+
|
|
48
|
+
constructor(
|
|
49
|
+
storage: NodeStorage,
|
|
50
|
+
logger: Logger,
|
|
51
|
+
cachePlugin?: ICachePlugin
|
|
52
|
+
) {
|
|
53
|
+
this.cacheHasChanged = false;
|
|
54
|
+
this.storage = storage;
|
|
55
|
+
this.storage.registerChangeEmitter(this.handleChangeEvent.bind(this));
|
|
56
|
+
if (cachePlugin) {
|
|
57
|
+
this.persistence = cachePlugin;
|
|
58
|
+
}
|
|
59
|
+
this.logger = logger;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Set to true if cache state has changed since last time serialize or writeToPersistence was called
|
|
64
|
+
*/
|
|
65
|
+
hasChanged(): boolean {
|
|
66
|
+
return this.cacheHasChanged;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Serializes in memory cache to JSON
|
|
71
|
+
*/
|
|
72
|
+
serialize(): string {
|
|
73
|
+
this.logger.trace("Serializing in-memory cache");
|
|
74
|
+
let finalState = Serializer.serializeAllCache(
|
|
75
|
+
this.storage.getInMemoryCache() as InMemoryCache
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
// if cacheSnapshot not null or empty, merge
|
|
79
|
+
if (this.cacheSnapshot) {
|
|
80
|
+
this.logger.trace("Reading cache snapshot from disk");
|
|
81
|
+
finalState = this.mergeState(
|
|
82
|
+
JSON.parse(this.cacheSnapshot),
|
|
83
|
+
finalState
|
|
84
|
+
);
|
|
85
|
+
} else {
|
|
86
|
+
this.logger.trace("No cache snapshot to merge");
|
|
87
|
+
}
|
|
88
|
+
this.cacheHasChanged = false;
|
|
89
|
+
|
|
90
|
+
return JSON.stringify(finalState);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Deserializes JSON to in-memory cache. JSON should be in MSAL cache schema format
|
|
95
|
+
* @param cache - blob formatted cache
|
|
96
|
+
*/
|
|
97
|
+
deserialize(cache: string): void {
|
|
98
|
+
this.logger.trace("Deserializing JSON to in-memory cache");
|
|
99
|
+
this.cacheSnapshot = cache;
|
|
100
|
+
|
|
101
|
+
if (this.cacheSnapshot) {
|
|
102
|
+
this.logger.trace("Reading cache snapshot from disk");
|
|
103
|
+
const deserializedCache = Deserializer.deserializeAllCache(
|
|
104
|
+
this.overlayDefaults(JSON.parse(this.cacheSnapshot))
|
|
105
|
+
);
|
|
106
|
+
this.storage.setInMemoryCache(deserializedCache);
|
|
107
|
+
} else {
|
|
108
|
+
this.logger.trace("No cache snapshot to deserialize");
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Fetches the cache key-value map
|
|
114
|
+
*/
|
|
115
|
+
getKVStore(): CacheKVStore {
|
|
116
|
+
return this.storage.getCache();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* API that retrieves all accounts currently in cache to the user
|
|
121
|
+
*/
|
|
122
|
+
async getAllAccounts(): Promise<AccountInfo[]> {
|
|
123
|
+
this.logger.trace("getAllAccounts called");
|
|
124
|
+
let cacheContext;
|
|
125
|
+
try {
|
|
126
|
+
if (this.persistence) {
|
|
127
|
+
cacheContext = new TokenCacheContext(this, false);
|
|
128
|
+
await this.persistence.beforeCacheAccess(cacheContext);
|
|
129
|
+
}
|
|
130
|
+
return this.storage.getAllAccounts();
|
|
131
|
+
} finally {
|
|
132
|
+
if (this.persistence && cacheContext) {
|
|
133
|
+
await this.persistence.afterCacheAccess(cacheContext);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Returns the signed in account matching homeAccountId.
|
|
140
|
+
* (the account object is created at the time of successful login)
|
|
141
|
+
* or null when no matching account is found
|
|
142
|
+
* @param homeAccountId - unique identifier for an account (uid.utid)
|
|
143
|
+
*/
|
|
144
|
+
async getAccountByHomeId(
|
|
145
|
+
homeAccountId: string
|
|
146
|
+
): Promise<AccountInfo | null> {
|
|
147
|
+
const allAccounts = await this.getAllAccounts();
|
|
148
|
+
if (homeAccountId && allAccounts && allAccounts.length) {
|
|
149
|
+
return (
|
|
150
|
+
allAccounts.filter(
|
|
151
|
+
(accountObj) => accountObj.homeAccountId === homeAccountId
|
|
152
|
+
)[0] || null
|
|
153
|
+
);
|
|
154
|
+
} else {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Returns the signed in account matching localAccountId.
|
|
161
|
+
* (the account object is created at the time of successful login)
|
|
162
|
+
* or null when no matching account is found
|
|
163
|
+
* @param localAccountId - unique identifier of an account (sub/obj when homeAccountId cannot be populated)
|
|
164
|
+
*/
|
|
165
|
+
async getAccountByLocalId(
|
|
166
|
+
localAccountId: string
|
|
167
|
+
): Promise<AccountInfo | null> {
|
|
168
|
+
const allAccounts = await this.getAllAccounts();
|
|
169
|
+
if (localAccountId && allAccounts && allAccounts.length) {
|
|
170
|
+
return (
|
|
171
|
+
allAccounts.filter(
|
|
172
|
+
(accountObj) => accountObj.localAccountId === localAccountId
|
|
173
|
+
)[0] || null
|
|
174
|
+
);
|
|
175
|
+
} else {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* API to remove a specific account and the relevant data from cache
|
|
182
|
+
* @param account - AccountInfo passed by the user
|
|
183
|
+
*/
|
|
184
|
+
async removeAccount(account: AccountInfo): Promise<void> {
|
|
185
|
+
this.logger.trace("removeAccount called");
|
|
186
|
+
let cacheContext;
|
|
187
|
+
try {
|
|
188
|
+
if (this.persistence) {
|
|
189
|
+
cacheContext = new TokenCacheContext(this, true);
|
|
190
|
+
await this.persistence.beforeCacheAccess(cacheContext);
|
|
191
|
+
}
|
|
192
|
+
await this.storage.removeAccount(
|
|
193
|
+
AccountEntity.generateAccountCacheKey(account)
|
|
194
|
+
);
|
|
195
|
+
} finally {
|
|
196
|
+
if (this.persistence && cacheContext) {
|
|
197
|
+
await this.persistence.afterCacheAccess(cacheContext);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Called when the cache has changed state.
|
|
204
|
+
*/
|
|
205
|
+
private handleChangeEvent() {
|
|
206
|
+
this.cacheHasChanged = true;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Merge in memory cache with the cache snapshot.
|
|
211
|
+
* @param oldState - cache before changes
|
|
212
|
+
* @param currentState - current cache state in the library
|
|
213
|
+
*/
|
|
214
|
+
private mergeState(
|
|
215
|
+
oldState: JsonCache,
|
|
216
|
+
currentState: JsonCache
|
|
217
|
+
): JsonCache {
|
|
218
|
+
this.logger.trace("Merging in-memory cache with cache snapshot");
|
|
219
|
+
const stateAfterRemoval = this.mergeRemovals(oldState, currentState);
|
|
220
|
+
return this.mergeUpdates(stateAfterRemoval, currentState);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Deep update of oldState based on newState values
|
|
225
|
+
* @param oldState - cache before changes
|
|
226
|
+
* @param newState - updated cache
|
|
227
|
+
*/
|
|
228
|
+
private mergeUpdates(oldState: object, newState: object): JsonCache {
|
|
229
|
+
Object.keys(newState).forEach((newKey: string) => {
|
|
230
|
+
const newValue = newState[newKey];
|
|
231
|
+
|
|
232
|
+
// if oldState does not contain value but newValue does, add it
|
|
233
|
+
if (!oldState.hasOwnProperty(newKey)) {
|
|
234
|
+
if (newValue !== null) {
|
|
235
|
+
oldState[newKey] = newValue;
|
|
236
|
+
}
|
|
237
|
+
} else {
|
|
238
|
+
// both oldState and newState contain the key, do deep update
|
|
239
|
+
const newValueNotNull = newValue !== null;
|
|
240
|
+
const newValueIsObject = typeof newValue === "object";
|
|
241
|
+
const newValueIsNotArray = !Array.isArray(newValue);
|
|
242
|
+
const oldStateNotUndefinedOrNull =
|
|
243
|
+
typeof oldState[newKey] !== "undefined" &&
|
|
244
|
+
oldState[newKey] !== null;
|
|
245
|
+
|
|
246
|
+
if (
|
|
247
|
+
newValueNotNull &&
|
|
248
|
+
newValueIsObject &&
|
|
249
|
+
newValueIsNotArray &&
|
|
250
|
+
oldStateNotUndefinedOrNull
|
|
251
|
+
) {
|
|
252
|
+
this.mergeUpdates(oldState[newKey], newValue);
|
|
253
|
+
} else {
|
|
254
|
+
oldState[newKey] = newValue;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
return oldState as JsonCache;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Removes entities in oldState that the were removed from newState. If there are any unknown values in root of
|
|
264
|
+
* oldState that are not recognized, they are left untouched.
|
|
265
|
+
* @param oldState - cache before changes
|
|
266
|
+
* @param newState - updated cache
|
|
267
|
+
*/
|
|
268
|
+
private mergeRemovals(oldState: JsonCache, newState: JsonCache): JsonCache {
|
|
269
|
+
this.logger.trace("Remove updated entries in cache");
|
|
270
|
+
const accounts = oldState.Account
|
|
271
|
+
? this.mergeRemovalsDict<SerializedAccountEntity>(
|
|
272
|
+
oldState.Account,
|
|
273
|
+
newState.Account
|
|
274
|
+
)
|
|
275
|
+
: oldState.Account;
|
|
276
|
+
const accessTokens = oldState.AccessToken
|
|
277
|
+
? this.mergeRemovalsDict<SerializedAccessTokenEntity>(
|
|
278
|
+
oldState.AccessToken,
|
|
279
|
+
newState.AccessToken
|
|
280
|
+
)
|
|
281
|
+
: oldState.AccessToken;
|
|
282
|
+
const refreshTokens = oldState.RefreshToken
|
|
283
|
+
? this.mergeRemovalsDict<SerializedRefreshTokenEntity>(
|
|
284
|
+
oldState.RefreshToken,
|
|
285
|
+
newState.RefreshToken
|
|
286
|
+
)
|
|
287
|
+
: oldState.RefreshToken;
|
|
288
|
+
const idTokens = oldState.IdToken
|
|
289
|
+
? this.mergeRemovalsDict<SerializedIdTokenEntity>(
|
|
290
|
+
oldState.IdToken,
|
|
291
|
+
newState.IdToken
|
|
292
|
+
)
|
|
293
|
+
: oldState.IdToken;
|
|
294
|
+
const appMetadata = oldState.AppMetadata
|
|
295
|
+
? this.mergeRemovalsDict<SerializedAppMetadataEntity>(
|
|
296
|
+
oldState.AppMetadata,
|
|
297
|
+
newState.AppMetadata
|
|
298
|
+
)
|
|
299
|
+
: oldState.AppMetadata;
|
|
300
|
+
|
|
301
|
+
return {
|
|
302
|
+
...oldState,
|
|
303
|
+
Account: accounts,
|
|
304
|
+
AccessToken: accessTokens,
|
|
305
|
+
RefreshToken: refreshTokens,
|
|
306
|
+
IdToken: idTokens,
|
|
307
|
+
AppMetadata: appMetadata,
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Helper to merge new cache with the old one
|
|
313
|
+
* @param oldState - cache before changes
|
|
314
|
+
* @param newState - updated cache
|
|
315
|
+
*/
|
|
316
|
+
private mergeRemovalsDict<T>(
|
|
317
|
+
oldState: Record<string, T>,
|
|
318
|
+
newState?: Record<string, T>
|
|
319
|
+
): Record<string, T> {
|
|
320
|
+
const finalState = { ...oldState };
|
|
321
|
+
Object.keys(oldState).forEach((oldKey) => {
|
|
322
|
+
if (!newState || !newState.hasOwnProperty(oldKey)) {
|
|
323
|
+
delete finalState[oldKey];
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
return finalState;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Helper to overlay as a part of cache merge
|
|
331
|
+
* @param passedInCache - cache read from the blob
|
|
332
|
+
*/
|
|
333
|
+
private overlayDefaults(passedInCache: JsonCache): JsonCache {
|
|
334
|
+
this.logger.trace("Overlaying input cache with the default cache");
|
|
335
|
+
return {
|
|
336
|
+
Account: {
|
|
337
|
+
...defaultSerializedCache.Account,
|
|
338
|
+
...passedInCache.Account,
|
|
339
|
+
},
|
|
340
|
+
IdToken: {
|
|
341
|
+
...defaultSerializedCache.IdToken,
|
|
342
|
+
...passedInCache.IdToken,
|
|
343
|
+
},
|
|
344
|
+
AccessToken: {
|
|
345
|
+
...defaultSerializedCache.AccessToken,
|
|
346
|
+
...passedInCache.AccessToken,
|
|
347
|
+
},
|
|
348
|
+
RefreshToken: {
|
|
349
|
+
...defaultSerializedCache.RefreshToken,
|
|
350
|
+
...passedInCache.RefreshToken,
|
|
351
|
+
},
|
|
352
|
+
AppMetadata: {
|
|
353
|
+
...defaultSerializedCache.AppMetadata,
|
|
354
|
+
...passedInCache.AppMetadata,
|
|
355
|
+
},
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
AccountEntity,
|
|
8
|
+
ICachePlugin,
|
|
9
|
+
TokenCacheContext,
|
|
10
|
+
} from "@azure/msal-common";
|
|
11
|
+
import { TokenCache } from "../TokenCache.js";
|
|
12
|
+
import { IPartitionManager } from "./IPartitionManager.js";
|
|
13
|
+
import { ICacheClient } from "./ICacheClient.js";
|
|
14
|
+
|
|
15
|
+
export class DistributedCachePlugin implements ICachePlugin {
|
|
16
|
+
private client: ICacheClient;
|
|
17
|
+
private partitionManager: IPartitionManager;
|
|
18
|
+
|
|
19
|
+
constructor(client: ICacheClient, partitionManager: IPartitionManager) {
|
|
20
|
+
this.client = client;
|
|
21
|
+
this.partitionManager = partitionManager;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public async beforeCacheAccess(
|
|
25
|
+
cacheContext: TokenCacheContext
|
|
26
|
+
): Promise<void> {
|
|
27
|
+
const partitionKey = await this.partitionManager.getKey();
|
|
28
|
+
const cacheData = await this.client.get(partitionKey);
|
|
29
|
+
cacheContext.tokenCache.deserialize(cacheData);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public async afterCacheAccess(
|
|
33
|
+
cacheContext: TokenCacheContext
|
|
34
|
+
): Promise<void> {
|
|
35
|
+
if (cacheContext.cacheHasChanged) {
|
|
36
|
+
const kvStore = (
|
|
37
|
+
cacheContext.tokenCache as TokenCache
|
|
38
|
+
).getKVStore();
|
|
39
|
+
const accountEntities = Object.values(kvStore).filter((value) =>
|
|
40
|
+
AccountEntity.isAccountEntity(value as object)
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
if (accountEntities.length > 0) {
|
|
44
|
+
const accountEntity = accountEntities[0] as AccountEntity;
|
|
45
|
+
const partitionKey = await this.partitionManager.extractKey(
|
|
46
|
+
accountEntity
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
await this.client.set(
|
|
50
|
+
partitionKey,
|
|
51
|
+
cacheContext.tokenCache.serialize()
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export interface ICacheClient {
|
|
7
|
+
/**
|
|
8
|
+
* Retrieve the value from the cache
|
|
9
|
+
*
|
|
10
|
+
* @param key string
|
|
11
|
+
* @returns Promise<string>
|
|
12
|
+
*/
|
|
13
|
+
get(key: string): Promise<string>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Save the required value using the provided key to cache
|
|
17
|
+
*
|
|
18
|
+
* @param key string
|
|
19
|
+
* @param value string
|
|
20
|
+
* @returns Promise<string>
|
|
21
|
+
*/
|
|
22
|
+
set(key: string, value: string): Promise<string>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { AccountEntity } from "@azure/msal-common";
|
|
7
|
+
|
|
8
|
+
export interface IPartitionManager {
|
|
9
|
+
/**
|
|
10
|
+
* This function should return the correct key from which to read
|
|
11
|
+
* the specific user's information from cache.
|
|
12
|
+
*
|
|
13
|
+
* Example: Your application may be partitioning the user's cache
|
|
14
|
+
* information for each user using the homeAccountId and thus
|
|
15
|
+
* this function would return the homeAccountId for
|
|
16
|
+
* the user in question
|
|
17
|
+
*
|
|
18
|
+
* @returns Promise<string>
|
|
19
|
+
*/
|
|
20
|
+
getKey(): Promise<string>;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* This function should return the correct key being used to save each
|
|
24
|
+
* user's cache information to cache - given an AccountEntity
|
|
25
|
+
*
|
|
26
|
+
* Example: Your application may be partitioning the user's cache
|
|
27
|
+
* information for each user using the homeAccountId thus
|
|
28
|
+
* this function would return the homeAccountId from
|
|
29
|
+
* the provided AccountEntity
|
|
30
|
+
*
|
|
31
|
+
* @param accountEntity: AccountEntity
|
|
32
|
+
* @returns Promise<string>
|
|
33
|
+
*/
|
|
34
|
+
extractKey(accountEntity: AccountEntity): Promise<string>;
|
|
35
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
AccountCache,
|
|
8
|
+
IdTokenCache,
|
|
9
|
+
AccessTokenCache,
|
|
10
|
+
RefreshTokenCache,
|
|
11
|
+
AppMetadataCache,
|
|
12
|
+
AccountEntity,
|
|
13
|
+
IdTokenEntity,
|
|
14
|
+
AccessTokenEntity,
|
|
15
|
+
RefreshTokenEntity,
|
|
16
|
+
AppMetadataEntity,
|
|
17
|
+
CacheManager,
|
|
18
|
+
} from "@azure/msal-common";
|
|
19
|
+
import {
|
|
20
|
+
JsonCache,
|
|
21
|
+
InMemoryCache,
|
|
22
|
+
SerializedAccountEntity,
|
|
23
|
+
SerializedIdTokenEntity,
|
|
24
|
+
SerializedAccessTokenEntity,
|
|
25
|
+
SerializedRefreshTokenEntity,
|
|
26
|
+
SerializedAppMetadataEntity,
|
|
27
|
+
} from "./SerializerTypes.js";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* This class deserializes cache entities read from the file into in memory object types defined internally
|
|
31
|
+
*/
|
|
32
|
+
export class Deserializer {
|
|
33
|
+
/**
|
|
34
|
+
* Parse the JSON blob in memory and deserialize the content
|
|
35
|
+
* @param cachedJson
|
|
36
|
+
*/
|
|
37
|
+
static deserializeJSONBlob(jsonFile: string): JsonCache {
|
|
38
|
+
const deserializedCache = !jsonFile ? {} : JSON.parse(jsonFile);
|
|
39
|
+
return deserializedCache;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Deserializes accounts to AccountEntity objects
|
|
44
|
+
* @param accounts
|
|
45
|
+
*/
|
|
46
|
+
static deserializeAccounts(
|
|
47
|
+
accounts: Record<string, SerializedAccountEntity>
|
|
48
|
+
): AccountCache {
|
|
49
|
+
const accountObjects: AccountCache = {};
|
|
50
|
+
if (accounts) {
|
|
51
|
+
Object.keys(accounts).map(function (key) {
|
|
52
|
+
const serializedAcc = accounts[key];
|
|
53
|
+
const mappedAcc = {
|
|
54
|
+
homeAccountId: serializedAcc.home_account_id,
|
|
55
|
+
environment: serializedAcc.environment,
|
|
56
|
+
realm: serializedAcc.realm,
|
|
57
|
+
localAccountId: serializedAcc.local_account_id,
|
|
58
|
+
username: serializedAcc.username,
|
|
59
|
+
authorityType: serializedAcc.authority_type,
|
|
60
|
+
name: serializedAcc.name,
|
|
61
|
+
clientInfo: serializedAcc.client_info,
|
|
62
|
+
lastModificationTime: serializedAcc.last_modification_time,
|
|
63
|
+
lastModificationApp: serializedAcc.last_modification_app,
|
|
64
|
+
};
|
|
65
|
+
const account: AccountEntity = new AccountEntity();
|
|
66
|
+
CacheManager.toObject(account, mappedAcc);
|
|
67
|
+
accountObjects[key] = account;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return accountObjects;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Deserializes id tokens to IdTokenEntity objects
|
|
76
|
+
* @param idTokens
|
|
77
|
+
*/
|
|
78
|
+
static deserializeIdTokens(
|
|
79
|
+
idTokens: Record<string, SerializedIdTokenEntity>
|
|
80
|
+
): IdTokenCache {
|
|
81
|
+
const idObjects: IdTokenCache = {};
|
|
82
|
+
if (idTokens) {
|
|
83
|
+
Object.keys(idTokens).map(function (key) {
|
|
84
|
+
const serializedIdT = idTokens[key];
|
|
85
|
+
const mappedIdT = {
|
|
86
|
+
homeAccountId: serializedIdT.home_account_id,
|
|
87
|
+
environment: serializedIdT.environment,
|
|
88
|
+
credentialType: serializedIdT.credential_type,
|
|
89
|
+
clientId: serializedIdT.client_id,
|
|
90
|
+
secret: serializedIdT.secret,
|
|
91
|
+
realm: serializedIdT.realm,
|
|
92
|
+
};
|
|
93
|
+
const idToken: IdTokenEntity = new IdTokenEntity();
|
|
94
|
+
CacheManager.toObject(idToken, mappedIdT);
|
|
95
|
+
idObjects[key] = idToken;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
return idObjects;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Deserializes access tokens to AccessTokenEntity objects
|
|
103
|
+
* @param accessTokens
|
|
104
|
+
*/
|
|
105
|
+
static deserializeAccessTokens(
|
|
106
|
+
accessTokens: Record<string, SerializedAccessTokenEntity>
|
|
107
|
+
): AccessTokenCache {
|
|
108
|
+
const atObjects: AccessTokenCache = {};
|
|
109
|
+
if (accessTokens) {
|
|
110
|
+
Object.keys(accessTokens).map(function (key) {
|
|
111
|
+
const serializedAT = accessTokens[key];
|
|
112
|
+
const mappedAT = {
|
|
113
|
+
homeAccountId: serializedAT.home_account_id,
|
|
114
|
+
environment: serializedAT.environment,
|
|
115
|
+
credentialType: serializedAT.credential_type,
|
|
116
|
+
clientId: serializedAT.client_id,
|
|
117
|
+
secret: serializedAT.secret,
|
|
118
|
+
realm: serializedAT.realm,
|
|
119
|
+
target: serializedAT.target,
|
|
120
|
+
cachedAt: serializedAT.cached_at,
|
|
121
|
+
expiresOn: serializedAT.expires_on,
|
|
122
|
+
extendedExpiresOn: serializedAT.extended_expires_on,
|
|
123
|
+
refreshOn: serializedAT.refresh_on,
|
|
124
|
+
keyId: serializedAT.key_id,
|
|
125
|
+
tokenType: serializedAT.token_type,
|
|
126
|
+
requestedClaims: serializedAT.requestedClaims,
|
|
127
|
+
requestedClaimsHash: serializedAT.requestedClaimsHash,
|
|
128
|
+
userAssertionHash: serializedAT.userAssertionHash,
|
|
129
|
+
};
|
|
130
|
+
const accessToken: AccessTokenEntity = new AccessTokenEntity();
|
|
131
|
+
CacheManager.toObject(accessToken, mappedAT);
|
|
132
|
+
atObjects[key] = accessToken;
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return atObjects;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Deserializes refresh tokens to RefreshTokenEntity objects
|
|
141
|
+
* @param refreshTokens
|
|
142
|
+
*/
|
|
143
|
+
static deserializeRefreshTokens(
|
|
144
|
+
refreshTokens: Record<string, SerializedRefreshTokenEntity>
|
|
145
|
+
): RefreshTokenCache {
|
|
146
|
+
const rtObjects: RefreshTokenCache = {};
|
|
147
|
+
if (refreshTokens) {
|
|
148
|
+
Object.keys(refreshTokens).map(function (key) {
|
|
149
|
+
const serializedRT = refreshTokens[key];
|
|
150
|
+
const mappedRT = {
|
|
151
|
+
homeAccountId: serializedRT.home_account_id,
|
|
152
|
+
environment: serializedRT.environment,
|
|
153
|
+
credentialType: serializedRT.credential_type,
|
|
154
|
+
clientId: serializedRT.client_id,
|
|
155
|
+
secret: serializedRT.secret,
|
|
156
|
+
familyId: serializedRT.family_id,
|
|
157
|
+
target: serializedRT.target,
|
|
158
|
+
realm: serializedRT.realm,
|
|
159
|
+
};
|
|
160
|
+
const refreshToken: RefreshTokenEntity =
|
|
161
|
+
new RefreshTokenEntity();
|
|
162
|
+
CacheManager.toObject(refreshToken, mappedRT);
|
|
163
|
+
rtObjects[key] = refreshToken;
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return rtObjects;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Deserializes appMetadata to AppMetaData objects
|
|
172
|
+
* @param appMetadata
|
|
173
|
+
*/
|
|
174
|
+
static deserializeAppMetadata(
|
|
175
|
+
appMetadata: Record<string, SerializedAppMetadataEntity>
|
|
176
|
+
): AppMetadataCache {
|
|
177
|
+
const appMetadataObjects: AppMetadataCache = {};
|
|
178
|
+
if (appMetadata) {
|
|
179
|
+
Object.keys(appMetadata).map(function (key) {
|
|
180
|
+
const serializedAmdt = appMetadata[key];
|
|
181
|
+
const mappedAmd = {
|
|
182
|
+
clientId: serializedAmdt.client_id,
|
|
183
|
+
environment: serializedAmdt.environment,
|
|
184
|
+
familyId: serializedAmdt.family_id,
|
|
185
|
+
};
|
|
186
|
+
const amd: AppMetadataEntity = new AppMetadataEntity();
|
|
187
|
+
CacheManager.toObject(amd, mappedAmd);
|
|
188
|
+
appMetadataObjects[key] = amd;
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return appMetadataObjects;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Deserialize an inMemory Cache
|
|
197
|
+
* @param jsonCache
|
|
198
|
+
*/
|
|
199
|
+
static deserializeAllCache(jsonCache: JsonCache): InMemoryCache {
|
|
200
|
+
return {
|
|
201
|
+
accounts: jsonCache.Account
|
|
202
|
+
? this.deserializeAccounts(jsonCache.Account)
|
|
203
|
+
: {},
|
|
204
|
+
idTokens: jsonCache.IdToken
|
|
205
|
+
? this.deserializeIdTokens(jsonCache.IdToken)
|
|
206
|
+
: {},
|
|
207
|
+
accessTokens: jsonCache.AccessToken
|
|
208
|
+
? this.deserializeAccessTokens(jsonCache.AccessToken)
|
|
209
|
+
: {},
|
|
210
|
+
refreshTokens: jsonCache.RefreshToken
|
|
211
|
+
? this.deserializeRefreshTokens(jsonCache.RefreshToken)
|
|
212
|
+
: {},
|
|
213
|
+
appMetadata: jsonCache.AppMetadata
|
|
214
|
+
? this.deserializeAppMetadata(jsonCache.AppMetadata)
|
|
215
|
+
: {},
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
}
|