@azure/msal-node-extensions 1.0.0-alpha.2 → 1.0.0-alpha.20

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.
Files changed (45) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +193 -42
  3. package/binding.gyp +29 -29
  4. package/dist/error/PersistenceError.d.ts +32 -4
  5. package/dist/index.d.ts +3 -0
  6. package/dist/msal-node-extensions.cjs.development.js +615 -849
  7. package/dist/msal-node-extensions.cjs.development.js.map +1 -1
  8. package/dist/msal-node-extensions.cjs.production.min.js +1 -1
  9. package/dist/msal-node-extensions.cjs.production.min.js.map +1 -1
  10. package/dist/msal-node-extensions.esm.js +625 -864
  11. package/dist/msal-node-extensions.esm.js.map +1 -1
  12. package/dist/persistence/BasePersistence.d.ts +5 -0
  13. package/dist/persistence/FilePersistence.d.ts +4 -2
  14. package/dist/persistence/FilePersistenceWithDataProtection.d.ts +3 -1
  15. package/dist/persistence/IPersistence.d.ts +3 -1
  16. package/dist/persistence/IPersistenceConfiguration.d.ts +8 -0
  17. package/dist/persistence/KeychainPersistence.d.ts +3 -1
  18. package/dist/persistence/LibSecretPersistence.d.ts +3 -1
  19. package/dist/persistence/PersistenceCachePlugin.d.ts +11 -7
  20. package/dist/persistence/PersistenceCreator.d.ts +5 -0
  21. package/dist/utils/Constants.d.ts +31 -0
  22. package/dist/utils/Environment.d.ts +16 -0
  23. package/package.json +27 -14
  24. package/src/dpapi-addon/Dpapi.ts +17 -12
  25. package/src/dpapi-addon/dpapi_addon.h +6 -6
  26. package/src/dpapi-addon/dpapi_not_supported.cpp +19 -19
  27. package/src/dpapi-addon/dpapi_win.cpp +114 -114
  28. package/src/dpapi-addon/main.cpp +32 -32
  29. package/src/error/PersistenceError.ts +101 -65
  30. package/src/index.ts +16 -8
  31. package/src/lock/CrossPlatformLock.ts +89 -82
  32. package/src/lock/CrossPlatformLockOptions.ts +15 -15
  33. package/src/persistence/BasePersistence.ts +41 -0
  34. package/src/persistence/DataProtectionScope.ts +20 -20
  35. package/src/persistence/FilePersistence.ts +140 -134
  36. package/src/persistence/FilePersistenceWithDataProtection.ts +92 -84
  37. package/src/persistence/IPersistence.ts +17 -15
  38. package/src/persistence/IPersistenceConfiguration.ts +14 -0
  39. package/src/persistence/KeychainPersistence.ts +86 -78
  40. package/src/persistence/LibSecretPersistence.ts +87 -79
  41. package/src/persistence/PersistenceCachePlugin.ts +106 -96
  42. package/src/persistence/PersistenceCreator.ts +73 -0
  43. package/src/utils/Constants.ts +62 -24
  44. package/src/utils/Environment.ts +99 -0
  45. package/changelog.md +0 -14
@@ -1,82 +1,89 @@
1
- /*
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the MIT License.
4
- */
5
-
6
- import { promises as fs } from "fs"
7
- import { pid } from "process";
8
- import { CrossPlatformLockOptions } from "./CrossPlatformLockOptions";
9
- import { Constants } from "../utils/Constants";
10
- import { PersistenceError } from "../error/PersistenceError";
11
- import { Logger } from "@azure/msal-common";
12
-
13
- /**
14
- * Cross-process lock that works on all platforms.
15
- */
16
- export class CrossPlatformLock {
17
-
18
- private readonly lockFilePath: string;
19
- private lockFileHandle: fs.FileHandle;
20
- private readonly retryNumber: number;
21
- private readonly retryDelay: number;
22
-
23
- private logger: Logger;
24
-
25
- constructor(lockFilePath: string, logger: Logger, lockOptions?: CrossPlatformLockOptions) {
26
- this.lockFilePath = lockFilePath;
27
- this.retryNumber = lockOptions ? lockOptions.retryNumber : 500;
28
- this.retryDelay = lockOptions ? lockOptions.retryDelay : 100;
29
- this.logger = logger;
30
- }
31
-
32
- /**
33
- * Locks cache from read or writes by creating file with same path and name as
34
- * cache file but with .lockfile extension. If another process has already created
35
- * the lockfile, will back off and retry based on configuration settings set by CrossPlatformLockOptions
36
- */
37
- public async lock(): Promise<void> {
38
- for (let tryCount = 0; tryCount < this.retryNumber; tryCount++) {
39
- try {
40
- this.logger.info(`Pid ${pid} trying to acquire lock`);
41
- this.lockFileHandle = await fs.open(this.lockFilePath, "wx+");
42
-
43
- this.logger.info(`Pid ${pid} acquired lock`);
44
- await this.lockFileHandle.write(pid.toString());
45
- return;
46
- } catch (err) {
47
- if (err.code == Constants.EEXIST_ERROR) {
48
- this.logger.info(err);
49
- await this.sleep(this.retryDelay);
50
- } else {
51
- throw PersistenceError.createCrossPlatformLockError(err.code, err.message);
52
- }
53
- }
54
- }
55
- throw PersistenceError.createCrossPlatformLockError(
56
- "Exceeded retry options",
57
- "Not able to acquire lock. Exceeded amount of retries set in options");
58
- }
59
-
60
- /**
61
- * unlocks cache file by deleting .lockfile.
62
- */
63
- public async unlock(): Promise<void> {
64
- try {
65
- // delete lock file
66
- await fs.unlink(this.lockFilePath);
67
- await this.lockFileHandle.close();
68
- } catch (err) {
69
- if (err.code == Constants.ENOENT_ERROR) {
70
- this.logger.warning("Tried to unlock but Lockfile does not exist");
71
- } else {
72
- throw PersistenceError.createCrossPlatformLockError(err.code, err.message);
73
- }
74
- }
75
- }
76
-
77
- private sleep(ms): Promise<void> {
78
- return new Promise((resolve) => {
79
- setTimeout(resolve, ms);
80
- });
81
- }
82
- }
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { promises as fs } from "fs";
7
+ import { pid } from "process";
8
+ import { CrossPlatformLockOptions } from "./CrossPlatformLockOptions";
9
+ import { Constants } from "../utils/Constants";
10
+ import { PersistenceError } from "../error/PersistenceError";
11
+ import { Logger } from "@azure/msal-common";
12
+
13
+ /**
14
+ * Cross-process lock that works on all platforms.
15
+ */
16
+ export class CrossPlatformLock {
17
+
18
+ private readonly lockFilePath: string;
19
+ private lockFileHandle: fs.FileHandle;
20
+ private readonly retryNumber: number;
21
+ private readonly retryDelay: number;
22
+
23
+ private logger: Logger;
24
+
25
+ constructor(lockFilePath: string, logger: Logger, lockOptions?: CrossPlatformLockOptions) {
26
+ this.lockFilePath = lockFilePath;
27
+ this.retryNumber = lockOptions ? lockOptions.retryNumber : 500;
28
+ this.retryDelay = lockOptions ? lockOptions.retryDelay : 100;
29
+ this.logger = logger;
30
+ }
31
+
32
+ /**
33
+ * Locks cache from read or writes by creating file with same path and name as
34
+ * cache file but with .lockfile extension. If another process has already created
35
+ * the lockfile, will back off and retry based on configuration settings set by CrossPlatformLockOptions
36
+ */
37
+ public async lock(): Promise<void> {
38
+ for (let tryCount = 0; tryCount < this.retryNumber; tryCount++) {
39
+ try {
40
+ this.logger.info(`Pid ${pid} trying to acquire lock`);
41
+ this.lockFileHandle = await fs.open(this.lockFilePath, "wx+");
42
+
43
+ this.logger.info(`Pid ${pid} acquired lock`);
44
+ await this.lockFileHandle.write(pid.toString());
45
+ return;
46
+ } catch (err) {
47
+ if (err.code === Constants.EEXIST_ERROR || err.code === Constants.EPERM_ERROR) {
48
+ this.logger.info(err);
49
+ await this.sleep(this.retryDelay);
50
+ } else {
51
+ this.logger.error(`${pid} was not able to acquire lock. Ran into error: ${err.message}`);
52
+ throw PersistenceError.createCrossPlatformLockError(err.message);
53
+ }
54
+ }
55
+ }
56
+ this.logger.error(`${pid} was not able to acquire lock. Exceeded amount of retries set in the options`);
57
+ throw PersistenceError.createCrossPlatformLockError(
58
+ "Not able to acquire lock. Exceeded amount of retries set in options");
59
+ }
60
+
61
+ /**
62
+ * unlocks cache file by deleting .lockfile.
63
+ */
64
+ public async unlock(): Promise<void> {
65
+ try {
66
+ if(this.lockFileHandle){
67
+ // if we have a file handle to the .lockfile, delete lock file
68
+ await fs.unlink(this.lockFilePath);
69
+ await this.lockFileHandle.close();
70
+ this.logger.info("lockfile deleted");
71
+ } else {
72
+ this.logger.warning("lockfile handle does not exist, so lockfile could not be deleted");
73
+ }
74
+ } catch (err) {
75
+ if (err.code === Constants.ENOENT_ERROR) {
76
+ this.logger.info("Tried to unlock but lockfile does not exist");
77
+ } else {
78
+ this.logger.error(`${pid} was not able to release lock. Ran into error: ${err.message}`);
79
+ throw PersistenceError.createCrossPlatformLockError(err.message);
80
+ }
81
+ }
82
+ }
83
+
84
+ private sleep(ms): Promise<void> {
85
+ return new Promise((resolve) => {
86
+ setTimeout(resolve, ms);
87
+ });
88
+ }
89
+ }
@@ -1,15 +1,15 @@
1
- /*
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the MIT License.
4
- */
5
-
6
- /**
7
- * Options for CrossPlatform lock.
8
- *
9
- * retryNumber: Numbers of times we should try to acquire a lock. Defaults to 500.
10
- * retryDelay: Time to wait before trying to retry a lock acquisition. Defaults to 100 ms.
11
- */
12
- export type CrossPlatformLockOptions = {
13
- retryNumber: number;
14
- retryDelay: number;
15
- }
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /**
7
+ * Options for CrossPlatform lock.
8
+ *
9
+ * retryNumber: Numbers of times we should try to acquire a lock. Defaults to 500.
10
+ * retryDelay: Time to wait before trying to retry a lock acquisition. Defaults to 100 ms.
11
+ */
12
+ export type CrossPlatformLockOptions = {
13
+ retryNumber: number;
14
+ retryDelay: number;
15
+ };
@@ -0,0 +1,41 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { PersistenceError } from "../error/PersistenceError";
7
+ import { Constants } from "../utils/Constants";
8
+ import { IPersistence } from "./IPersistence";
9
+
10
+ export abstract class BasePersistence {
11
+ public abstract createForPersistenceValidation(): Promise<IPersistence>;
12
+
13
+ public async verifyPersistence(): Promise<boolean> {
14
+ // We are using a different location for the test to avoid overriding the functional cache
15
+ const persistenceValidator = await this.createForPersistenceValidation();
16
+
17
+ try {
18
+ await persistenceValidator.save(Constants.PERSISTENCE_TEST_DATA);
19
+
20
+ const retrievedDummyData = await persistenceValidator.load();
21
+
22
+ if (!retrievedDummyData) {
23
+ throw PersistenceError.createCachePersistenceError(
24
+ "Persistence check failed. Data was written but it could not be read. " +
25
+ "Possible cause: on Linux, LibSecret is installed but D-Bus isn't running because it cannot be started over SSH."
26
+ );
27
+ }
28
+
29
+ if (retrievedDummyData !== Constants.PERSISTENCE_TEST_DATA) {
30
+ throw PersistenceError.createCachePersistenceError(
31
+ `Persistence check failed. Data written ${Constants.PERSISTENCE_TEST_DATA} is different from data read ${retrievedDummyData}`
32
+ );
33
+ }
34
+ await persistenceValidator.delete();
35
+ return true;
36
+ } catch (e) {
37
+ throw PersistenceError.createCachePersistenceError(`Verifing persistence failed with the error: ${e}`);
38
+ }
39
+ }
40
+
41
+ }
@@ -1,20 +1,20 @@
1
- /*
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the MIT License.
4
- */
5
-
6
- /**
7
- * Specifies the scope of the data protection - either the current user or the local
8
- * machine.
9
- *
10
- * You do not need a key to protect or unprotect the data.
11
- * If you set the Scope to CurrentUser, only applications running on your credentials can
12
- * unprotect the data; however, that means that any application running on your credentials
13
- * can access the protected data. If you set the Scope to LocalMachine, any full-trust
14
- * application on the computer can unprotect, access, and modify the data.
15
- *
16
- */
17
- export enum DataProtectionScope {
18
- CurrentUser = "CurrentUser",
19
- LocalMachine = "LocalMachine",
20
- }
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /**
7
+ * Specifies the scope of the data protection - either the current user or the local
8
+ * machine.
9
+ *
10
+ * You do not need a key to protect or unprotect the data.
11
+ * If you set the Scope to CurrentUser, only applications running on your credentials can
12
+ * unprotect the data; however, that means that any application running on your credentials
13
+ * can access the protected data. If you set the Scope to LocalMachine, any full-trust
14
+ * application on the computer can unprotect, access, and modify the data.
15
+ *
16
+ */
17
+ export enum DataProtectionScope {
18
+ CurrentUser = "CurrentUser",
19
+ LocalMachine = "LocalMachine",
20
+ }
@@ -1,134 +1,140 @@
1
- /*
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the MIT License.
4
- */
5
-
6
- import { promises as fs } from "fs"
7
- import { dirname } from "path";
8
- import { IPersistence } from "./IPersistence";
9
- import { Constants } from "../utils/Constants";
10
- import { PersistenceError } from "../error/PersistenceError";
11
- import { Logger, LoggerOptions, LogLevel } from "@azure/msal-common";
12
-
13
- /**
14
- * Reads and writes data to file specified by file location. File contents are not
15
- * encrypted.
16
- *
17
- * If file or directory has not been created, it FilePersistence.create() will create
18
- * file and any directories in the path recursively.
19
- */
20
- export class FilePersistence implements IPersistence {
21
-
22
- private filePath: string;
23
- private logger: Logger;
24
-
25
- public static async create(fileLocation: string, loggerOptions?: LoggerOptions): Promise<FilePersistence> {
26
- const filePersistence = new FilePersistence();
27
- filePersistence.filePath = fileLocation;
28
- filePersistence.logger = new Logger(loggerOptions || FilePersistence.createDefaultLoggerOptions());
29
- await filePersistence.createCacheFile();
30
- return filePersistence;
31
- }
32
-
33
- public async save(contents: string): Promise<void> {
34
- try {
35
- await fs.writeFile(this.getFilePath(), contents, "utf-8");
36
- } catch (err) {
37
- throw PersistenceError.createFileSystemError(err.code, err.message);
38
- }
39
- }
40
-
41
- public async saveBuffer(contents: Uint8Array): Promise<void> {
42
- try {
43
- await fs.writeFile(this.getFilePath(), contents);
44
- } catch (err) {
45
- throw PersistenceError.createFileSystemError(err.code, err.message);
46
- }
47
- }
48
-
49
- public async load(): Promise<string> {
50
- try {
51
- return await fs.readFile(this.getFilePath(), "utf-8");
52
- } catch (err) {
53
- throw PersistenceError.createFileSystemError(err.code, err.message);
54
- }
55
- };
56
-
57
- public async loadBuffer(): Promise<Uint8Array> {
58
- try {
59
- return await fs.readFile(this.getFilePath());
60
- } catch (err) {
61
- throw PersistenceError.createFileSystemError(err.code, err.message);
62
- }
63
- };
64
-
65
- public async delete(): Promise<boolean> {
66
- try {
67
- await fs.unlink(this.getFilePath());
68
- return true;
69
- } catch (err) {
70
- if (err.code == Constants.ENOENT_ERROR) {
71
- // file does not exist, so it was not deleted
72
- this.logger.warning("Cache file does not exist, so it could not be deleted");
73
- return false;
74
- }
75
- throw PersistenceError.createFileSystemError(err.code, err.message);
76
- }
77
- }
78
-
79
- public getFilePath(): string {
80
- return this.filePath;
81
- }
82
-
83
- public async reloadNecessary(lastSync: number): Promise<boolean> {
84
- return lastSync < await this.timeLastModified();
85
- }
86
-
87
- public getLogger(): Logger {
88
- return this.logger;
89
- }
90
-
91
- private static createDefaultLoggerOptions(): LoggerOptions {
92
- return {
93
- loggerCallback: () => {
94
- // allow users to not set loggerCallback
95
- },
96
- piiLoggingEnabled: false,
97
- logLevel: LogLevel.Info
98
- }
99
- }
100
-
101
- private async timeLastModified(): Promise<number> {
102
- try {
103
- const stats = await fs.stat(this.filePath);
104
- return stats.mtime.getTime();
105
- } catch (err) {
106
- if (err.code == Constants.ENOENT_ERROR) {
107
- // file does not exist, so it's never been modified
108
- this.logger.verbose("Cache file does not exist");
109
- return 0;
110
- }
111
- throw PersistenceError.createFileSystemError(err.code, err.message);
112
- }
113
- }
114
-
115
- private async createCacheFile(): Promise<void> {
116
- await this.createFileDirectory();
117
- // File is created only if it does not exist
118
- const fileHandle = await fs.open(this.filePath, "a");
119
- await fileHandle.close();
120
- this.logger.info(`File created at ${this.filePath}`);
121
- }
122
-
123
- private async createFileDirectory(): Promise<void> {
124
- try {
125
- await fs.mkdir(dirname(this.filePath), {recursive: true});
126
- } catch (err) {
127
- if (err.code == Constants.EEXIST_ERROR) {
128
- this.logger.info(`Directory ${dirname(this.filePath)} already exists`);
129
- } else {
130
- throw PersistenceError.createFileSystemError(err.code, err.message);
131
- }
132
- }
133
- }
134
- }
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { promises as fs } from "fs";
7
+ import { dirname } from "path";
8
+ import { IPersistence } from "./IPersistence";
9
+ import { Constants } from "../utils/Constants";
10
+ import { PersistenceError } from "../error/PersistenceError";
11
+ import { Logger, LoggerOptions, LogLevel } from "@azure/msal-common";
12
+ import { BasePersistence } from "./BasePersistence";
13
+
14
+ /**
15
+ * Reads and writes data to file specified by file location. File contents are not
16
+ * encrypted.
17
+ *
18
+ * If file or directory has not been created, it FilePersistence.create() will create
19
+ * file and any directories in the path recursively.
20
+ */
21
+ export class FilePersistence extends BasePersistence implements IPersistence {
22
+
23
+ private filePath: string;
24
+ private logger: Logger;
25
+
26
+ public static async create(fileLocation: string, loggerOptions?: LoggerOptions): Promise<FilePersistence> {
27
+ const filePersistence = new FilePersistence();
28
+ filePersistence.filePath = fileLocation;
29
+ filePersistence.logger = new Logger(loggerOptions || FilePersistence.createDefaultLoggerOptions());
30
+ await filePersistence.createCacheFile();
31
+ return filePersistence;
32
+ }
33
+
34
+ public async save(contents: string): Promise<void> {
35
+ try {
36
+ await fs.writeFile(this.getFilePath(), contents, "utf-8");
37
+ } catch (err) {
38
+ throw PersistenceError.createFileSystemError(err.code, err.message);
39
+ }
40
+ }
41
+
42
+ public async saveBuffer(contents: Uint8Array): Promise<void> {
43
+ try {
44
+ await fs.writeFile(this.getFilePath(), contents);
45
+ } catch (err) {
46
+ throw PersistenceError.createFileSystemError(err.code, err.message);
47
+ }
48
+ }
49
+
50
+ public async load(): Promise<string | null> {
51
+ try {
52
+ return await fs.readFile(this.getFilePath(), "utf-8");
53
+ } catch (err) {
54
+ throw PersistenceError.createFileSystemError(err.code, err.message);
55
+ }
56
+ }
57
+
58
+ public async loadBuffer(): Promise<Uint8Array> {
59
+ try {
60
+ return await fs.readFile(this.getFilePath());
61
+ } catch (err) {
62
+ throw PersistenceError.createFileSystemError(err.code, err.message);
63
+ }
64
+ }
65
+
66
+ public async delete(): Promise<boolean> {
67
+ try {
68
+ await fs.unlink(this.getFilePath());
69
+ return true;
70
+ } catch (err) {
71
+ if (err.code === Constants.ENOENT_ERROR) {
72
+ // file does not exist, so it was not deleted
73
+ this.logger.warning("Cache file does not exist, so it could not be deleted");
74
+ return false;
75
+ }
76
+ throw PersistenceError.createFileSystemError(err.code, err.message);
77
+ }
78
+ }
79
+
80
+ public getFilePath(): string {
81
+ return this.filePath;
82
+ }
83
+
84
+ public async reloadNecessary(lastSync: number): Promise<boolean> {
85
+ return lastSync < await this.timeLastModified();
86
+ }
87
+
88
+ public getLogger(): Logger {
89
+ return this.logger;
90
+ }
91
+
92
+ public createForPersistenceValidation(): Promise<FilePersistence> {
93
+ const testCacheFileLocation = `${dirname(this.filePath)}/test.cache`;
94
+ return FilePersistence.create(testCacheFileLocation);
95
+ }
96
+
97
+ private static createDefaultLoggerOptions(): LoggerOptions {
98
+ return {
99
+ loggerCallback: () => {
100
+ // allow users to not set loggerCallback
101
+ },
102
+ piiLoggingEnabled: false,
103
+ logLevel: LogLevel.Info
104
+ };
105
+ }
106
+
107
+ private async timeLastModified(): Promise<number> {
108
+ try {
109
+ const stats = await fs.stat(this.filePath);
110
+ return stats.mtime.getTime();
111
+ } catch (err) {
112
+ if (err.code === Constants.ENOENT_ERROR) {
113
+ // file does not exist, so it's never been modified
114
+ this.logger.verbose("Cache file does not exist");
115
+ return 0;
116
+ }
117
+ throw PersistenceError.createFileSystemError(err.code, err.message);
118
+ }
119
+ }
120
+
121
+ private async createCacheFile(): Promise<void> {
122
+ await this.createFileDirectory();
123
+ // File is created only if it does not exist
124
+ const fileHandle = await fs.open(this.filePath, "a");
125
+ await fileHandle.close();
126
+ this.logger.info(`File created at ${this.filePath}`);
127
+ }
128
+
129
+ private async createFileDirectory(): Promise<void> {
130
+ try {
131
+ await fs.mkdir(dirname(this.filePath), {recursive: true});
132
+ } catch (err) {
133
+ if (err.code === Constants.EEXIST_ERROR) {
134
+ this.logger.info(`Directory ${dirname(this.filePath)} already exists`);
135
+ } else {
136
+ throw PersistenceError.createFileSystemError(err.code, err.message);
137
+ }
138
+ }
139
+ }
140
+ }