-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
PersistenceCreator.ts
114 lines (101 loc) · 4.09 KB
/
PersistenceCreator.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { FilePersistenceWithDataProtection } from "./FilePersistenceWithDataProtection";
import { LibSecretPersistence } from "./LibSecretPersistence";
import { KeychainPersistence } from "./KeychainPersistence";
import { DataProtectionScope } from "./DataProtectionScope";
import { Environment } from "../utils/Environment";
import { IPersistence } from "./IPersistence";
import { FilePersistence } from "./FilePersistence";
import { PersistenceError } from "../error/PersistenceError";
import { IPersistenceConfiguration } from "./IPersistenceConfiguration";
export class PersistenceCreator {
static async createPersistence(
config: IPersistenceConfiguration
): Promise<IPersistence> {
let peristence: IPersistence;
// On Windows, uses a DPAPI encrypted file
if (Environment.isWindowsPlatform()) {
if (!config.cachePath || !config.dataProtectionScope) {
throw PersistenceError.createPersistenceNotValidatedError(
"Cache path and/or data protection scope not provided for the FilePersistenceWithDataProtection cache plugin"
);
}
peristence = await FilePersistenceWithDataProtection.create(
config.cachePath,
DataProtectionScope.CurrentUser,
undefined,
config.loggerOptions
);
}
// On Mac, uses keychain.
else if (Environment.isMacPlatform()) {
if (
!config.cachePath ||
!config.serviceName ||
!config.accountName
) {
throw PersistenceError.createPersistenceNotValidatedError(
"Cache path, service name and/or account name not provided for the KeychainPersistence cache plugin"
);
}
peristence = await KeychainPersistence.create(
config.cachePath,
config.serviceName,
config.accountName,
config.loggerOptions
);
}
// On Linux, uses libsecret to store to secret service. Libsecret has to be installed.
else if (Environment.isLinuxPlatform()) {
if (
!config.cachePath ||
!config.serviceName ||
!config.accountName
) {
throw PersistenceError.createPersistenceNotValidatedError(
"Cache path, service name and/or account name not provided for the LibSecretPersistence cache plugin"
);
}
peristence = await LibSecretPersistence.create(
config.cachePath,
config.serviceName,
config.accountName,
config.loggerOptions
);
} else {
throw PersistenceError.createNotSupportedError(
"The current environment is not supported by msal-node-extensions yet."
);
}
await peristence.verifyPersistence().catch(async (e) => {
if (
Environment.isLinuxPlatform() &&
config.usePlaintextFileOnLinux
) {
if (!config.cachePath) {
throw PersistenceError.createPersistenceNotValidatedError(
"Cache path not provided for the FilePersistence cache plugin"
);
}
peristence = await FilePersistence.create(
config.cachePath,
config.loggerOptions
);
const isFilePersistenceVerified =
await peristence.verifyPersistence();
if (isFilePersistenceVerified) {
return peristence;
}
throw PersistenceError.createPersistenceNotVerifiedError(
"Persistence could not be verified"
);
} else {
throw e;
}
});
return peristence;
}
}