@azure/msal-node-extensions 1.0.0-alpha.3 → 1.0.0-alpha.30
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 +156 -5
- package/binding.gyp +11 -1
- package/dist/{dpapi-addon/Dpapi.d.ts → Dpapi.d.ts} +0 -0
- package/dist/error/PersistenceError.d.ts +28 -0
- package/dist/index.d.ts +3 -0
- package/dist/msal-node-extensions.cjs.development.js +603 -859
- package/dist/msal-node-extensions.cjs.development.js.map +1 -1
- package/dist/msal-node-extensions.cjs.production.min.js +1 -1
- package/dist/msal-node-extensions.cjs.production.min.js.map +1 -1
- package/dist/msal-node-extensions.esm.js +613 -874
- package/dist/msal-node-extensions.esm.js.map +1 -1
- package/dist/packageMetadata.d.ts +2 -0
- package/dist/persistence/BasePersistence.d.ts +5 -0
- package/dist/persistence/FilePersistence.d.ts +4 -2
- package/dist/persistence/FilePersistenceWithDataProtection.d.ts +3 -1
- package/dist/persistence/IPersistence.d.ts +3 -1
- package/dist/persistence/IPersistenceConfiguration.d.ts +10 -0
- package/dist/persistence/KeychainPersistence.d.ts +3 -1
- package/dist/persistence/LibSecretPersistence.d.ts +3 -1
- package/dist/persistence/PersistenceCachePlugin.d.ts +11 -7
- package/dist/persistence/PersistenceCreator.d.ts +5 -0
- package/dist/utils/Constants.d.ts +26 -0
- package/dist/utils/Environment.d.ts +16 -0
- package/package.json +27 -15
- package/src/{dpapi-addon/Dpapi.ts → Dpapi.ts} +12 -12
- package/src/dpapi-addon/dpapi_addon.h +2 -1
- package/src/dpapi-addon/dpapi_not_supported.cpp +4 -10
- package/src/dpapi-addon/dpapi_win.cpp +25 -33
- package/src/dpapi-addon/main.cpp +14 -16
- package/src/error/PersistenceError.ts +101 -61
- package/src/index.ts +16 -8
- package/src/lock/CrossPlatformLock.ts +89 -89
- package/src/lock/CrossPlatformLockOptions.ts +15 -15
- package/src/packageMetadata.ts +3 -0
- package/src/persistence/BasePersistence.ts +43 -0
- package/src/persistence/FilePersistence.ts +140 -134
- package/src/persistence/FilePersistenceWithDataProtection.ts +92 -84
- package/src/persistence/IPersistence.ts +4 -2
- package/src/persistence/IPersistenceConfiguration.ts +17 -0
- package/src/persistence/KeychainPersistence.ts +89 -78
- package/src/persistence/LibSecretPersistence.ts +90 -79
- package/src/persistence/PersistenceCachePlugin.ts +40 -30
- package/src/persistence/PersistenceCreator.ts +78 -0
- package/src/utils/Constants.ts +62 -30
- package/src/utils/Environment.ts +101 -0
- package/CHANGELOG.json +0 -50
- package/changelog.md +0 -28
|
@@ -1,78 +1,89 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
-
* Licensed under the MIT License.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { setPassword, getPassword, deletePassword } from "keytar";
|
|
7
|
-
import { FilePersistence } from "./FilePersistence";
|
|
8
|
-
import { IPersistence } from "./IPersistence";
|
|
9
|
-
import { PersistenceError } from "../error/PersistenceError";
|
|
10
|
-
import { Logger, LoggerOptions } from "@azure/msal-common";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
private
|
|
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
|
-
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { setPassword, getPassword, deletePassword } from "keytar";
|
|
7
|
+
import { FilePersistence } from "./FilePersistence";
|
|
8
|
+
import { IPersistence } from "./IPersistence";
|
|
9
|
+
import { PersistenceError } from "../error/PersistenceError";
|
|
10
|
+
import { Logger, LoggerOptions } from "@azure/msal-common";
|
|
11
|
+
import { dirname } from "path";
|
|
12
|
+
import { BasePersistence } from "./BasePersistence";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Uses reads and writes passwords to macOS keychain
|
|
16
|
+
*
|
|
17
|
+
* serviceName: Identifier used as key for whatever value is stored
|
|
18
|
+
* accountName: Account under which password should be stored
|
|
19
|
+
*/
|
|
20
|
+
export class KeychainPersistence extends BasePersistence implements IPersistence {
|
|
21
|
+
|
|
22
|
+
protected readonly serviceName;
|
|
23
|
+
protected readonly accountName;
|
|
24
|
+
private filePersistence: FilePersistence;
|
|
25
|
+
|
|
26
|
+
private constructor(serviceName: string, accountName: string) {
|
|
27
|
+
super();
|
|
28
|
+
this.serviceName = serviceName;
|
|
29
|
+
this.accountName = accountName;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public static async create(
|
|
33
|
+
fileLocation: string,
|
|
34
|
+
serviceName: string,
|
|
35
|
+
accountName: string,
|
|
36
|
+
loggerOptions?: LoggerOptions): Promise<KeychainPersistence> {
|
|
37
|
+
|
|
38
|
+
const persistence = new KeychainPersistence(serviceName, accountName);
|
|
39
|
+
persistence.filePersistence = await FilePersistence.create(fileLocation, loggerOptions);
|
|
40
|
+
return persistence;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public async save(contents: string): Promise<void> {
|
|
44
|
+
try {
|
|
45
|
+
await setPassword(this.serviceName, this.accountName, contents);
|
|
46
|
+
} catch (err) {
|
|
47
|
+
throw PersistenceError.createKeychainPersistenceError(err.message);
|
|
48
|
+
}
|
|
49
|
+
// Write dummy data to update file mtime
|
|
50
|
+
await this.filePersistence.save("{}");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public async load(): Promise<string | null> {
|
|
54
|
+
try{
|
|
55
|
+
return await getPassword(this.serviceName, this.accountName);
|
|
56
|
+
} catch(err){
|
|
57
|
+
throw PersistenceError.createKeychainPersistenceError(err.message);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public async delete(): Promise<boolean> {
|
|
62
|
+
try {
|
|
63
|
+
await this.filePersistence.delete();
|
|
64
|
+
return await deletePassword(this.serviceName, this.accountName);
|
|
65
|
+
} catch (err) {
|
|
66
|
+
throw PersistenceError.createKeychainPersistenceError(err.message);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public async reloadNecessary(lastSync: number): Promise<boolean> {
|
|
71
|
+
return this.filePersistence.reloadNecessary(lastSync);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
public getFilePath(): string {
|
|
75
|
+
return this.filePersistence.getFilePath();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public getLogger(): Logger {
|
|
79
|
+
return this.filePersistence.getLogger();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public createForPersistenceValidation(): Promise<KeychainPersistence> {
|
|
83
|
+
const testCacheFileLocation = `${dirname(this.filePersistence.getFilePath())}/test.cache`;
|
|
84
|
+
return KeychainPersistence.create(
|
|
85
|
+
testCacheFileLocation,
|
|
86
|
+
"persistenceValidationServiceName", "persistencValidationAccountName"
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -1,79 +1,90 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
-
* Licensed under the MIT License.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { setPassword, getPassword, deletePassword } from "keytar";
|
|
7
|
-
import { FilePersistence } from "./FilePersistence";
|
|
8
|
-
import { IPersistence } from "./IPersistence";
|
|
9
|
-
import { PersistenceError } from "../error/PersistenceError";
|
|
10
|
-
import { Logger, LoggerOptions } from "@azure/msal-common";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
private
|
|
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
|
-
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { setPassword, getPassword, deletePassword } from "keytar";
|
|
7
|
+
import { FilePersistence } from "./FilePersistence";
|
|
8
|
+
import { IPersistence } from "./IPersistence";
|
|
9
|
+
import { PersistenceError } from "../error/PersistenceError";
|
|
10
|
+
import { Logger, LoggerOptions } from "@azure/msal-common";
|
|
11
|
+
import { dirname } from "path";
|
|
12
|
+
import { BasePersistence } from "./BasePersistence";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Uses reads and writes passwords to Secret Service API/libsecret. Requires libsecret
|
|
16
|
+
* to be installed.
|
|
17
|
+
*
|
|
18
|
+
* serviceName: Identifier used as key for whatever value is stored
|
|
19
|
+
* accountName: Account under which password should be stored
|
|
20
|
+
*/
|
|
21
|
+
export class LibSecretPersistence extends BasePersistence implements IPersistence {
|
|
22
|
+
|
|
23
|
+
protected readonly serviceName;
|
|
24
|
+
protected readonly accountName;
|
|
25
|
+
private filePersistence: FilePersistence;
|
|
26
|
+
|
|
27
|
+
private constructor(serviceName: string, accountName: string) {
|
|
28
|
+
super();
|
|
29
|
+
this.serviceName = serviceName;
|
|
30
|
+
this.accountName = accountName;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public static async create(
|
|
34
|
+
fileLocation: string,
|
|
35
|
+
serviceName: string,
|
|
36
|
+
accountName: string,
|
|
37
|
+
loggerOptions?: LoggerOptions): Promise<LibSecretPersistence> {
|
|
38
|
+
|
|
39
|
+
const persistence = new LibSecretPersistence(serviceName, accountName);
|
|
40
|
+
persistence.filePersistence = await FilePersistence.create(fileLocation, loggerOptions);
|
|
41
|
+
return persistence;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public async save(contents: string): Promise<void> {
|
|
45
|
+
try {
|
|
46
|
+
await setPassword(this.serviceName, this.accountName, contents);
|
|
47
|
+
} catch (err) {
|
|
48
|
+
throw PersistenceError.createLibSecretError(err.message);
|
|
49
|
+
}
|
|
50
|
+
// Write dummy data to update file mtime
|
|
51
|
+
await this.filePersistence.save("{}");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public async load(): Promise<string | null> {
|
|
55
|
+
try {
|
|
56
|
+
return await getPassword(this.serviceName, this.accountName);
|
|
57
|
+
} catch (err) {
|
|
58
|
+
throw PersistenceError.createLibSecretError(err.message);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public async delete(): Promise<boolean> {
|
|
63
|
+
try {
|
|
64
|
+
await this.filePersistence.delete();
|
|
65
|
+
return await deletePassword(this.serviceName, this.accountName);
|
|
66
|
+
} catch (err) {
|
|
67
|
+
throw PersistenceError.createLibSecretError(err.message);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public async reloadNecessary(lastSync: number): Promise<boolean> {
|
|
72
|
+
return this.filePersistence.reloadNecessary(lastSync);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public getFilePath(): string {
|
|
76
|
+
return this.filePersistence.getFilePath();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public getLogger(): Logger {
|
|
80
|
+
return this.filePersistence.getLogger();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public createForPersistenceValidation(): Promise<LibSecretPersistence> {
|
|
84
|
+
const testCacheFileLocation = `${dirname(this.filePersistence.getFilePath())}/test.cache`;
|
|
85
|
+
return LibSecretPersistence.create(
|
|
86
|
+
testCacheFileLocation,
|
|
87
|
+
"persistenceValidationServiceName", "persistencValidationAccountName"
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import { IPersistence } from "
|
|
6
|
+
import { IPersistence } from "./IPersistence";
|
|
7
7
|
import { CrossPlatformLock } from "../lock/CrossPlatformLock";
|
|
8
8
|
import { CrossPlatformLockOptions } from "../lock/CrossPlatformLockOptions";
|
|
9
9
|
import { pid } from "process";
|
|
10
|
-
import { Logger } from "@azure/msal-common";
|
|
10
|
+
import { TokenCacheContext, ICachePlugin, Logger } from "@azure/msal-common";
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* MSAL cache plugin which enables callers to write the MSAL cache to disk on Windows,
|
|
@@ -22,7 +22,7 @@ import { Logger } from "@azure/msal-common";
|
|
|
22
22
|
* - LibSecretPersistence: Used on linux, writes and reads from secret service API. Requires
|
|
23
23
|
* libsecret be installed.
|
|
24
24
|
*/
|
|
25
|
-
export class PersistenceCachePlugin {
|
|
25
|
+
export class PersistenceCachePlugin implements ICachePlugin {
|
|
26
26
|
|
|
27
27
|
public persistence: IPersistence;
|
|
28
28
|
public lastSync: number;
|
|
@@ -49,48 +49,58 @@ export class PersistenceCachePlugin {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
|
-
* Reads from storage and
|
|
52
|
+
* Reads from storage and saves an in-memory copy. If persistence has not been updated
|
|
53
53
|
* since last time data was read, in memory copy is used.
|
|
54
|
+
*
|
|
55
|
+
* If cacheContext.cacheHasChanged === true, then file lock is created and not deleted until
|
|
56
|
+
* afterCacheAccess() is called, to prevent the cache file from changing in between
|
|
57
|
+
* beforeCacheAccess() and afterCacheAccess().
|
|
54
58
|
*/
|
|
55
|
-
public async
|
|
56
|
-
this.logger.info("
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
59
|
+
public async beforeCacheAccess(cacheContext: TokenCacheContext): Promise<void> {
|
|
60
|
+
this.logger.info("Executing before cache access");
|
|
61
|
+
const reloadNecessary = await this.persistence.reloadNecessary(this.lastSync);
|
|
62
|
+
if (!reloadNecessary && this.currentCache !== null) {
|
|
63
|
+
if (cacheContext.cacheHasChanged) {
|
|
64
|
+
this.logger.verbose("Cache context has changed");
|
|
60
65
|
await this.crossPlatformLock.lock();
|
|
66
|
+
}
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
this.logger.info(`Reload necessary. Last sync time: ${this.lastSync}`);
|
|
71
|
+
await this.crossPlatformLock.lock();
|
|
72
|
+
|
|
73
|
+
this.currentCache = await this.persistence.load();
|
|
74
|
+
this.lastSync = new Date().getTime();
|
|
75
|
+
cacheContext.tokenCache.deserialize(this.currentCache);
|
|
61
76
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
} finally {
|
|
77
|
+
this.logger.info(`Last sync time updated to: ${this.lastSync}`);
|
|
78
|
+
} finally {
|
|
79
|
+
if (!cacheContext.cacheHasChanged) {
|
|
66
80
|
await this.crossPlatformLock.unlock();
|
|
67
|
-
this.logger.info(`Pid ${pid}
|
|
81
|
+
this.logger.info(`Pid ${pid} released lock`);
|
|
82
|
+
} else {
|
|
83
|
+
this.logger.info(`Pid ${pid} beforeCacheAccess did not release lock`);
|
|
68
84
|
}
|
|
69
85
|
}
|
|
70
|
-
return this.currentCache;
|
|
71
86
|
}
|
|
72
87
|
|
|
73
88
|
/**
|
|
74
|
-
* Writes to storage
|
|
75
|
-
* reads and latest state from persistence, sends state via callback, and updates in memory copy.
|
|
89
|
+
* Writes to storage if MSAL in memory copy of cache has been changed.
|
|
76
90
|
*/
|
|
77
|
-
public async
|
|
91
|
+
public async afterCacheAccess(cacheContext: TokenCacheContext): Promise<void> {
|
|
92
|
+
this.logger.info("Executing after cache access");
|
|
78
93
|
try {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
this.
|
|
85
|
-
this.lastSync = new Date().getTime();
|
|
86
|
-
this.logger.info(`Last sync time updated to: ${this.lastSync}`);
|
|
94
|
+
if (cacheContext.cacheHasChanged) {
|
|
95
|
+
this.logger.info("Msal in-memory cache has changed. Writing changes to persistence");
|
|
96
|
+
this.currentCache = cacheContext.tokenCache.serialize();
|
|
97
|
+
await this.persistence.save(this.currentCache);
|
|
98
|
+
} else {
|
|
99
|
+
this.logger.info("Msal in-memory cache has not changed. Did not write to persistence");
|
|
87
100
|
}
|
|
88
|
-
|
|
89
|
-
this.currentCache = await callback(this.currentCache);
|
|
90
|
-
await this.persistence.save(this.currentCache);
|
|
91
101
|
} finally {
|
|
92
102
|
await this.crossPlatformLock.unlock();
|
|
93
|
-
this.logger.info(`Pid ${pid}
|
|
103
|
+
this.logger.info(`Pid ${pid} afterCacheAccess released lock`);
|
|
94
104
|
}
|
|
95
105
|
}
|
|
96
106
|
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { FilePersistenceWithDataProtection } from "./FilePersistenceWithDataProtection";
|
|
7
|
+
import { LibSecretPersistence } from "./LibSecretPersistence";
|
|
8
|
+
import { KeychainPersistence } from "./KeychainPersistence";
|
|
9
|
+
import { DataProtectionScope } from "./DataProtectionScope";
|
|
10
|
+
import { Environment } from "../utils/Environment";
|
|
11
|
+
import { IPersistence } from "./IPersistence";
|
|
12
|
+
import { FilePersistence } from "./FilePersistence";
|
|
13
|
+
import { PersistenceError } from "../error/PersistenceError";
|
|
14
|
+
import { IPersistenceConfiguration } from "./IPersistenceConfiguration";
|
|
15
|
+
|
|
16
|
+
export class PersistenceCreator {
|
|
17
|
+
static async createPersistence(config: IPersistenceConfiguration): Promise<IPersistence> {
|
|
18
|
+
let peristence: IPersistence;
|
|
19
|
+
|
|
20
|
+
// On Windows, uses a DPAPI encrypted file
|
|
21
|
+
if (Environment.isWindowsPlatform()) {
|
|
22
|
+
if (!config.cachePath || !config.dataProtectionScope) {
|
|
23
|
+
throw PersistenceError.createPersistenceNotValidatedError(
|
|
24
|
+
"Cache path and/or data protection scope not provided for the FilePersistenceWithDataProtection cache plugin");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
peristence = await FilePersistenceWithDataProtection.create(config.cachePath, DataProtectionScope.CurrentUser, undefined, config.loggerOptions);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// On Mac, uses keychain.
|
|
31
|
+
else if (Environment.isMacPlatform()) {
|
|
32
|
+
if (!config.cachePath || !config.serviceName || !config.accountName) {
|
|
33
|
+
throw PersistenceError.createPersistenceNotValidatedError(
|
|
34
|
+
"Cache path, service name and/or account name not provided for the KeychainPersistence cache plugin");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
peristence = await KeychainPersistence.create(config.cachePath, config.serviceName, config.accountName, config.loggerOptions);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// On Linux, uses libsecret to store to secret service. Libsecret has to be installed.
|
|
41
|
+
else if (Environment.isLinuxPlatform()) {
|
|
42
|
+
if (!config.cachePath || !config.serviceName || !config.accountName) {
|
|
43
|
+
throw PersistenceError.createPersistenceNotValidatedError(
|
|
44
|
+
"Cache path, service name and/or account name not provided for the LibSecretPersistence cache plugin");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
peristence = await LibSecretPersistence.create(config.cachePath, config.serviceName, config.accountName, config.loggerOptions);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
else {
|
|
51
|
+
throw PersistenceError.createNotSupportedError(
|
|
52
|
+
"The current environment is not supported by msal-node-extensions yet.");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Initially suppress the error thrown during persistence verification to allow us to fallback to plain text
|
|
56
|
+
const isPersistenceVerified = await peristence.verifyPersistence().catch(() => false);
|
|
57
|
+
|
|
58
|
+
if (!isPersistenceVerified) {
|
|
59
|
+
if (Environment.isLinuxPlatform() && config.usePlaintextFileOnLinux) {
|
|
60
|
+
if (!config.cachePath) {
|
|
61
|
+
throw PersistenceError.createPersistenceNotValidatedError(
|
|
62
|
+
"Cache path not provided for the FilePersistence cache plugin");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
peristence = await FilePersistence.create(config.cachePath, config.loggerOptions);
|
|
66
|
+
|
|
67
|
+
const isFilePersistenceVerified = await peristence.verifyPersistence();
|
|
68
|
+
if (isFilePersistenceVerified) {
|
|
69
|
+
return peristence;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
throw PersistenceError.createPersistenceNotVerifiedError("Persistence could not be verified");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return peristence;
|
|
77
|
+
}
|
|
78
|
+
}
|
package/src/utils/Constants.ts
CHANGED
|
@@ -1,30 +1,62 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
-
* Licensed under the MIT License.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
export const Constants = {
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* An existing file was the target of an operation that required that the target not exist
|
|
10
|
-
*/
|
|
11
|
-
EEXIST_ERROR: "EEXIST",
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* No such file or directory: Commonly raised by fs operations to indicate that a component
|
|
15
|
-
* of the specified pathname does not exist. No entity (file or directory) could be found
|
|
16
|
-
* by the given path
|
|
17
|
-
*/
|
|
18
|
-
ENOENT_ERROR: "ENOENT",
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Operation not permitted. An attempt was made to perform an operation that requires
|
|
22
|
-
* elevated privileges.
|
|
23
|
-
*/
|
|
24
|
-
EPERM_ERROR: "EPERM",
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Default service name for using MSAL Keytar
|
|
28
|
-
*/
|
|
29
|
-
DEFAULT_SERVICE_NAME: "msal-node-extensions",
|
|
30
|
-
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export const Constants = {
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* An existing file was the target of an operation that required that the target not exist
|
|
10
|
+
*/
|
|
11
|
+
EEXIST_ERROR: "EEXIST",
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* No such file or directory: Commonly raised by fs operations to indicate that a component
|
|
15
|
+
* of the specified pathname does not exist. No entity (file or directory) could be found
|
|
16
|
+
* by the given path
|
|
17
|
+
*/
|
|
18
|
+
ENOENT_ERROR: "ENOENT",
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Operation not permitted. An attempt was made to perform an operation that requires
|
|
22
|
+
* elevated privileges.
|
|
23
|
+
*/
|
|
24
|
+
EPERM_ERROR: "EPERM",
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Default service name for using MSAL Keytar
|
|
28
|
+
*/
|
|
29
|
+
DEFAULT_SERVICE_NAME: "msal-node-extensions",
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Test data used to verify underlying persistence mechanism
|
|
33
|
+
*/
|
|
34
|
+
PERSISTENCE_TEST_DATA: "Dummy data to verify underlying persistence mechanism",
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* This is the value of a the guid if the process is being ran by the root user
|
|
38
|
+
*/
|
|
39
|
+
LINUX_ROOT_USER_GUID: 0,
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* List of environment variables
|
|
43
|
+
*/
|
|
44
|
+
ENVIRONMENT: {
|
|
45
|
+
HOME: "HOME",
|
|
46
|
+
LOGNAME: "LOGNAME",
|
|
47
|
+
USER: "USER",
|
|
48
|
+
LNAME: "LNAME",
|
|
49
|
+
USERNAME: "USERNAME",
|
|
50
|
+
PLATFORM: "platform",
|
|
51
|
+
LOCAL_APPLICATION_DATA: "LOCALAPPDATA"
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
// Name of the default cache file
|
|
55
|
+
DEFAULT_CACHE_FILE_NAME: "cache.json"
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export enum Platform {
|
|
59
|
+
WINDOWS = "win32",
|
|
60
|
+
LINUX = "linux",
|
|
61
|
+
MACOS = "darwin"
|
|
62
|
+
}
|