-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathObsidianTokenCachePlugin.ts
More file actions
62 lines (49 loc) · 2.3 KB
/
ObsidianTokenCachePlugin.ts
File metadata and controls
62 lines (49 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { TokenCacheContext, ICachePlugin } from "@azure/msal-common";
const { safeStorage } = require('@electron/remote')
export const MSAL_ACCESS_TOKEN_LOCALSTORAGE_KEY_PREFIX = 'msal-tokencache-'
export class ObsidianTokenCachePlugin implements ICachePlugin {
public displayName: string
private MSAL_ACCESS_TOKEN_LOCALSTORAGE_KEY: string
public acquired: boolean
constructor(displayName: string) {
this.displayName = displayName
this.MSAL_ACCESS_TOKEN_LOCALSTORAGE_KEY = MSAL_ACCESS_TOKEN_LOCALSTORAGE_KEY_PREFIX + this.displayName
this.acquired = false
}
/**
* Reads from local storage and decrypts. We don't care about efficiency here, otherwise
* we could cache values in memory. But then we would have to prevent race conditions between
* successive method calls, and this seems excessive for our use case.
*/
public async beforeCacheAccess(cacheContext: TokenCacheContext): Promise<void> {
//console.info("Executing before cache access");
const encryptedCache: string = localStorage.getItem(this.MSAL_ACCESS_TOKEN_LOCALSTORAGE_KEY)
const cache = (encryptedCache !== null)
? safeStorage.decryptString(Buffer.from(encryptedCache, 'latin1'))
: ""
cacheContext.tokenCache.deserialize(cache);
}
/**
* Encrypts and writes to local storage.
*/
public async afterCacheAccess(cacheContext: TokenCacheContext): Promise<void> {
//console.info("Executing after cache access");
if (cacheContext.cacheHasChanged) {
const serializedAccounts = cacheContext.tokenCache.serialize()
localStorage.setItem(
this.MSAL_ACCESS_TOKEN_LOCALSTORAGE_KEY,
safeStorage.encryptString(serializedAccounts).toString('latin1')
)
}
}
/** Delete the token cache from local storage.
*/
public async deleteFromCache(): Promise<void> {
localStorage.removeItem(this.MSAL_ACCESS_TOKEN_LOCALSTORAGE_KEY)
this.acquired = false
}
/** Has this cache been properly initialized? */
public isInitialized(): boolean {
return (localStorage.getItem(this.MSAL_ACCESS_TOKEN_LOCALSTORAGE_KEY) !== null)
}
}