-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Caches user auth in secure data store
Introduces a persistent cache using the secure data store to store accounts and tokens, which avoids making users log in every time they start the app. Fixes [AB#10633343](https://msazure.visualstudio.com/AzureBatch/_workitems/edit/10633343/)
- Loading branch information
Showing
8 changed files
with
174 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import MSALCachePlugin from "./msal-cache-plugin"; | ||
|
||
describe("MSALCachePlugin", () => { | ||
let plugin, cacheContextSpy; | ||
const storeSpy = jasmine.createSpyObj("DataStore", { | ||
setItem: jasmine.anything(), | ||
getItem: "deserializedValue" | ||
}); | ||
const cacheSpy = jasmine.createSpyObj("Cache", { | ||
serialize: "serializedValue", | ||
deserialize: jasmine.anything() | ||
}); | ||
beforeEach(() => { | ||
plugin = new MSALCachePlugin(storeSpy); | ||
cacheContextSpy = { | ||
tokenCache: cacheSpy, | ||
cacheHasChanged: false | ||
}; | ||
}) | ||
it("should get an item before the cache is called", async () => { | ||
expect(storeSpy.getItem).not.toHaveBeenCalled(); | ||
await plugin.beforeCacheAccess(cacheContextSpy); | ||
expect(storeSpy.getItem).toHaveBeenCalled(); | ||
expect(cacheSpy.deserialize).toHaveBeenCalledWith("deserializedValue"); | ||
}); | ||
it("should store an item after the cache is called", async () => { | ||
expect(storeSpy.setItem).not.toHaveBeenCalled(); | ||
|
||
await plugin.afterCacheAccess(cacheContextSpy); | ||
expect(storeSpy.setItem).not.toHaveBeenCalled(); | ||
|
||
cacheContextSpy.cacheHasChanged = true; | ||
await plugin.afterCacheAccess(cacheContextSpy); | ||
expect(storeSpy.setItem).toHaveBeenCalledWith( | ||
jasmine.anything(), | ||
"serializedValue" | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ICachePlugin, TokenCacheContext } from "@azure/msal-node"; | ||
import { SecureDataStore } from "../secure-data-store"; | ||
|
||
const CACHE_KEY = "msal_auth_cache"; | ||
|
||
export default class MSALCachePlugin implements ICachePlugin { | ||
constructor(private store: SecureDataStore) {} | ||
|
||
public async beforeCacheAccess(context: TokenCacheContext): Promise<void> { | ||
context.tokenCache.deserialize(await this.store.getItem(CACHE_KEY)); | ||
} | ||
|
||
public async afterCacheAccess(context: TokenCacheContext): Promise<void> { | ||
if (context.cacheHasChanged) { | ||
await this.store.setItem(CACHE_KEY, context.tokenCache.serialize()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters