Skip to content

Commit 73ebd27

Browse files
authored
Merge pull request #1652 from AzureAD/removefile
Remove file read and write from node lib
2 parents db845ff + 98d4b65 commit 73ebd27

File tree

5 files changed

+97
-92
lines changed

5 files changed

+97
-92
lines changed

lib/msal-node/src/cache/CacheContext.ts

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,15 @@
55

66
import {
77
JsonCache,
8-
Deserializer,
9-
Serializer,
10-
StringUtils
8+
Deserializer
119
} from '@azure/msal-common';
12-
import { CacheManager } from '../cache/CacheManager';
1310
import { Storage } from '../cache/Storage';
1411

1512
/**
1613
* class managing sync between the persistent cache blob in the disk and the in memory cache of the node
1714
*/
1815
export class CacheContext {
1916

20-
private cachePath: string;
2117
private defaultSerializedCache: JsonCache = {
2218
Account: {},
2319
IdToken: {},
@@ -26,54 +22,25 @@ export class CacheContext {
2622
AppMetadata: {}
2723
};
2824

29-
constructor() {
30-
this.cachePath = "";
31-
}
32-
33-
/**
34-
* sets the cache path provided by the user
35-
* @param path
36-
*/
37-
setCachePath(path: string) {
38-
this.cachePath = path;
39-
}
25+
constructor() {}
4026

4127
/**
4228
* Update the library cache
4329
* @param storage
4430
*/
45-
async setCurrentCache(storage: Storage) {
46-
const cache = await this.syncCache(storage);
47-
storage.setCache(Deserializer.deserializeAllCache(cache));
31+
setCurrentCache(storage: Storage, cacheObject: JsonCache) {
32+
const cacheWithOverlayedDefaults = this.overlayDefaults(cacheObject);
33+
storage.setCache(Deserializer.deserializeAllCache(cacheWithOverlayedDefaults));
4834
}
4935

50-
/**
51-
* read the cache from storage and merge it with the current cache
52-
* TODO: Make sure this operation is atomic - file lock that prevents anyone from changing it
53-
* @param storage
54-
*/
55-
async syncCache(storage: Storage): Promise<JsonCache> {
56-
if (!StringUtils.isEmpty(this.cachePath)) {
57-
const currentCache = Serializer.serializeAllCache(storage.getCache());
58-
const tCache = Deserializer.deserializeJSONBlob(await CacheManager.readFromFile(this.cachePath));
59-
return this.mergeCache(tCache, currentCache);
60-
}
61-
62-
return this.defaultSerializedCache;
63-
}
6436

65-
/**
66-
* Merges two cache entities
67-
* @param currentCache
68-
* @param persistentCache
69-
*/
70-
mergeCache(persistentCache: JsonCache, currentCache: JsonCache): JsonCache {
37+
overlayDefaults (passedInCache: JsonCache): JsonCache {
7138
return {
72-
Account: { ...persistentCache.Account, ...currentCache.Account },
73-
IdToken: { ...persistentCache.IdToken, ...currentCache.IdToken },
74-
AccessToken: { ...persistentCache.AccessToken, ...currentCache.AccessToken },
75-
RefreshToken: { ...persistentCache.RefreshToken, ...currentCache.RefreshToken },
76-
AppMetadata: { ...persistentCache.AppMetadata, ...currentCache.AppMetadata}
39+
Account: { ...this.defaultSerializedCache.Account, ...passedInCache.Account },
40+
IdToken: { ...this.defaultSerializedCache.IdToken, ...passedInCache.IdToken },
41+
AccessToken: { ...this.defaultSerializedCache.AccessToken, ...passedInCache.AccessToken },
42+
RefreshToken: { ...this.defaultSerializedCache.RefreshToken, ...passedInCache.RefreshToken },
43+
AppMetadata: { ...this.defaultSerializedCache.AppMetadata, ...passedInCache.AppMetadata}
7744
};
7845
}
7946

lib/msal-node/src/cache/CacheManager.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

lib/msal-node/src/client/ClientApplication.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import {
1111
RefreshTokenClient,
1212
RefreshTokenRequest,
1313
AuthenticationResult,
14-
Serializer,
14+
JsonCache,
15+
Serializer
1516
} from '@azure/msal-common';
1617
import { Configuration, buildAppConfiguration } from '../config/Configuration';
1718
import { CryptoProvider } from '../crypto/CryptoProvider';
1819
import { Storage } from '../cache/Storage';
1920
import { version } from '../../package.json';
2021
import { Constants } from './../utils/Constants';
2122
import { CacheContext } from '../cache/CacheContext';
22-
import { CacheManager } from '../cache/CacheManager'
2323

2424
export abstract class ClientApplication {
2525

@@ -126,24 +126,11 @@ export abstract class ClientApplication {
126126
};
127127
}
128128

129-
/**
130-
* read JSON formatted cache from disk
131-
* TODO: File should be locked for this operation
132-
*/
133-
async readCacheFromDisk(cachePath: string): Promise<void> {
134-
this.cacheContext.setCachePath(cachePath);
135-
this.cacheContext.setCurrentCache(this.storage);
129+
initializeCache(cacheObject: JsonCache) {
130+
this.cacheContext.setCurrentCache(this.storage, cacheObject)
136131
}
137132

138-
/**
139-
* write the JSON formatted cache to disk
140-
* TODO: File should be locked for this operation
141-
* @param jsonCache
142-
*/
143-
async writeCacheToDisk(cachePath: string): Promise<void> {
144-
await this.readCacheFromDisk(cachePath);
145-
const inMemCache = this.storage.getCache();
146-
const cacheBlob = Serializer.serializeJSONBlob(Serializer.serializeAllCache(inMemCache));
147-
CacheManager.writeToFile(cachePath, cacheBlob);
133+
readCache(): JsonCache {
134+
return Serializer.serializeAllCache(this.storage.getCache());
148135
}
149136
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"Account": {
3+
"uid.utid-login.microsoftonline.com-microsoft": {
4+
"username": "John Doe",
5+
"local_account_id": "object1234",
6+
"realm": "microsoft",
7+
"environment": "login.microsoftonline.com",
8+
"home_account_id": "uid.utid",
9+
"authority_type": "MSSTS",
10+
"client_info": "base64encodedjson"
11+
}
12+
},
13+
"RefreshToken": {
14+
"uid.utid-login.microsoftonline.com-refreshtoken-mock_client_id--": {
15+
"environment": "login.microsoftonline.com",
16+
"credential_type": "RefreshToken",
17+
"secret": "a refresh token",
18+
"client_id": "mock_client_id",
19+
"home_account_id": "uid.utid"
20+
},
21+
"uid.utid-login.microsoftonline.com-refreshtoken-1--": {
22+
"environment": "login.microsoftonline.com",
23+
"credential_type": "RefreshToken",
24+
"secret": "a refresh token",
25+
"client_id": "mock_client_id",
26+
"home_account_id": "uid.utid",
27+
"familyId": "1"
28+
}
29+
},
30+
"AccessToken": {
31+
"uid.utid-login.microsoftonline.com-accesstoken-mock_client_id-microsoft-scope1 scope2 scope3": {
32+
"environment": "login.microsoftonline.com",
33+
"credential_type": "AccessToken",
34+
"secret": "an access token",
35+
"realm": "microsoft",
36+
"target": "scope1 scope2 scope3",
37+
"client_id": "mock_client_id",
38+
"cached_at": "1000",
39+
"home_account_id": "uid.utid",
40+
"extended_expires_on": "4600",
41+
"expires_on": "4600"
42+
},
43+
"uid.utid-login.microsoftonline.com-accesstoken-mock_client_id-microsoft-scope4 scope5": {
44+
"environment": "login.microsoftonline.com",
45+
"credential_type": "AccessToken",
46+
"secret": "an access token",
47+
"realm": "microsoft",
48+
"target": "scope4 scope5",
49+
"client_id": "mock_client_id",
50+
"cached_at": "1000",
51+
"home_account_id": "uid.utid",
52+
"extended_expires_on": "4600",
53+
"expires_on": "4600"
54+
}
55+
},
56+
"IdToken": {
57+
"uid.utid-login.microsoftonline.com-idtoken-mock_client_id-microsoft-": {
58+
"realm": "microsoft",
59+
"environment": "login.microsoftonline.com",
60+
"credential_type": "IdToken",
61+
"secret": "header.eyJvaWQiOiAib2JqZWN0MTIzNCIsICJwcmVmZXJyZWRfdXNlcm5hbWUiOiAiSm9obiBEb2UiLCAic3ViIjogInN1YiJ9.signature",
62+
"client_id": "mock_client_id",
63+
"home_account_id": "uid.utid"
64+
}
65+
},
66+
"AppMetadata": {
67+
"appmetadata-login.microsoftonline.com-mock_client_id": {
68+
"environment": "login.microsoftonline.com",
69+
"family_id": "1",
70+
"client_id": "mock_client_id"
71+
}
72+
}
73+
}

samples/msal-node-auth-code/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
const express = require("express");
66
const msal = require('@azure/msal-node');
7+
const myLocalCache = require("./data/cache");
8+
const fs = require("fs");
79

810
const SERVER_PORT = process.env.PORT || 3000;
911

@@ -21,7 +23,7 @@ const publicClientConfig = {
2123
},
2224
};
2325
const pca = new msal.PublicClientApplication(publicClientConfig);
24-
pca.readCacheFromDisk("/Users/sameeragajjarapu/Documents/cache.json");
26+
pca.initializeCache(myLocalCache);
2527

2628
// Create Express App and Routes
2729
const app = express();
@@ -51,11 +53,12 @@ app.get('/redirect', (req, res) => {
5153

5254
pca.acquireTokenByCode(tokenRequest).then((response) => {
5355
console.log("\nResponse: \n:", response);
54-
// console.log(pca.getCache());
5556
res.send(200);
56-
pca.writeCacheToDisk("/Users/sameeragajjarapu/Documents/cache.json");
57+
// uncomment this to show writing of cache, dont commit real tokens.
58+
// fs.writeFileSync("./data/cache.json", JSON.stringify(pca.readCache()), null, 4);
5759
}).catch((error) => {
58-
res.send(500);
60+
console.log(error);
61+
res.status(500).send(error);
5962
});
6063
});
6164

0 commit comments

Comments
 (0)