@azure/msal-node-extensions 1.0.0-alpha.8 → 1.0.0-beta.0

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 (118) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +208 -42
  3. package/bin/arm64/dpapi.node +0 -0
  4. package/bin/ia32/dpapi.node +0 -0
  5. package/bin/x64/dpapi.node +0 -0
  6. package/dist/{dpapi-addon/Dpapi.d.ts → Dpapi.d.ts} +4 -4
  7. package/dist/Dpapi.esm.js +24 -0
  8. package/dist/Dpapi.esm.js.map +1 -0
  9. package/dist/Dpapi.js +24 -0
  10. package/dist/Dpapi.js.map +1 -0
  11. package/dist/broker/NativeBrokerPlugin.d.ts +20 -0
  12. package/dist/broker/NativeBrokerPlugin.esm.js +408 -0
  13. package/dist/broker/NativeBrokerPlugin.esm.js.map +1 -0
  14. package/dist/broker/NativeBrokerPlugin.js +410 -0
  15. package/dist/broker/NativeBrokerPlugin.js.map +1 -0
  16. package/dist/error/NativeAuthError.d.ts +6 -0
  17. package/dist/error/NativeAuthError.esm.js +20 -0
  18. package/dist/error/NativeAuthError.esm.js.map +1 -0
  19. package/dist/error/NativeAuthError.js +22 -0
  20. package/dist/error/NativeAuthError.js.map +1 -0
  21. package/dist/error/PersistenceError.d.ts +21 -0
  22. package/{src/error/PersistenceError.ts → dist/error/PersistenceError.esm.js} +88 -70
  23. package/dist/error/PersistenceError.esm.js.map +1 -0
  24. package/dist/error/PersistenceError.js +91 -0
  25. package/dist/error/PersistenceError.js.map +1 -0
  26. package/dist/index.d.ts +4 -0
  27. package/dist/index.esm.js +12 -0
  28. package/dist/index.esm.js.map +1 -0
  29. package/dist/index.js +24 -6
  30. package/dist/index.js.map +1 -0
  31. package/dist/lock/CrossPlatformLock.esm.js +94 -0
  32. package/dist/lock/CrossPlatformLock.esm.js.map +1 -0
  33. package/dist/lock/CrossPlatformLock.js +96 -0
  34. package/dist/lock/CrossPlatformLock.js.map +1 -0
  35. package/dist/lock/CrossPlatformLockOptions.d.ts +1 -1
  36. package/dist/packageMetadata.d.ts +2 -0
  37. package/dist/packageMetadata.esm.js +8 -0
  38. package/dist/packageMetadata.esm.js.map +1 -0
  39. package/dist/packageMetadata.js +11 -0
  40. package/dist/packageMetadata.js.map +1 -0
  41. package/{src/persistence/BasePersistence.ts → dist/persistence/BasePersistence.esm.js} +35 -40
  42. package/dist/persistence/BasePersistence.esm.js.map +1 -0
  43. package/dist/persistence/BasePersistence.js +38 -0
  44. package/dist/persistence/BasePersistence.js.map +1 -0
  45. package/dist/persistence/DataProtectionScope.d.ts +5 -4
  46. package/{src/persistence/DataProtectionScope.ts → dist/persistence/DataProtectionScope.esm.js} +23 -19
  47. package/dist/persistence/DataProtectionScope.esm.js.map +1 -0
  48. package/dist/persistence/DataProtectionScope.js +26 -0
  49. package/dist/persistence/DataProtectionScope.js.map +1 -0
  50. package/dist/persistence/FilePersistence.d.ts +1 -0
  51. package/dist/persistence/FilePersistence.esm.js +173 -0
  52. package/dist/persistence/FilePersistence.esm.js.map +1 -0
  53. package/dist/persistence/FilePersistence.js +175 -0
  54. package/dist/persistence/FilePersistence.js.map +1 -0
  55. package/dist/persistence/FilePersistenceWithDataProtection.esm.js +84 -0
  56. package/dist/persistence/FilePersistenceWithDataProtection.esm.js.map +1 -0
  57. package/dist/persistence/FilePersistenceWithDataProtection.js +86 -0
  58. package/dist/persistence/FilePersistenceWithDataProtection.js.map +1 -0
  59. package/dist/persistence/IPersistenceConfiguration.d.ts +10 -0
  60. package/dist/persistence/KeychainPersistence.d.ts +2 -2
  61. package/dist/persistence/KeychainPersistence.esm.js +90 -0
  62. package/dist/persistence/KeychainPersistence.esm.js.map +1 -0
  63. package/dist/persistence/KeychainPersistence.js +92 -0
  64. package/dist/persistence/KeychainPersistence.js.map +1 -0
  65. package/dist/persistence/LibSecretPersistence.d.ts +2 -2
  66. package/dist/persistence/LibSecretPersistence.esm.js +91 -0
  67. package/dist/persistence/LibSecretPersistence.esm.js.map +1 -0
  68. package/dist/persistence/LibSecretPersistence.js +93 -0
  69. package/dist/persistence/LibSecretPersistence.js.map +1 -0
  70. package/dist/persistence/PersistenceCachePlugin.d.ts +1 -1
  71. package/{src/persistence/PersistenceCachePlugin.ts → dist/persistence/PersistenceCachePlugin.esm.js} +97 -104
  72. package/dist/persistence/PersistenceCachePlugin.esm.js.map +1 -0
  73. package/dist/persistence/PersistenceCachePlugin.js +101 -0
  74. package/dist/persistence/PersistenceCachePlugin.js.map +1 -0
  75. package/dist/persistence/PersistenceCreator.d.ts +5 -0
  76. package/dist/persistence/PersistenceCreator.esm.js +63 -0
  77. package/dist/persistence/PersistenceCreator.esm.js.map +1 -0
  78. package/dist/persistence/PersistenceCreator.js +65 -0
  79. package/dist/persistence/PersistenceCreator.js.map +1 -0
  80. package/dist/utils/Constants.d.ts +29 -0
  81. package/dist/utils/Constants.esm.js +62 -0
  82. package/dist/utils/Constants.esm.js.map +1 -0
  83. package/dist/utils/Constants.js +66 -0
  84. package/dist/utils/Constants.js.map +1 -0
  85. package/dist/utils/Environment.d.ts +16 -0
  86. package/dist/utils/Environment.esm.js +94 -0
  87. package/dist/utils/Environment.esm.js.map +1 -0
  88. package/dist/utils/Environment.js +96 -0
  89. package/dist/utils/Environment.js.map +1 -0
  90. package/dist/utils/TypeGuards.d.ts +5 -0
  91. package/dist/utils/TypeGuards.esm.js +16 -0
  92. package/dist/utils/TypeGuards.esm.js.map +1 -0
  93. package/dist/utils/TypeGuards.js +18 -0
  94. package/dist/utils/TypeGuards.js.map +1 -0
  95. package/package.json +70 -52
  96. package/CHANGELOG.json +0 -146
  97. package/binding.gyp +0 -29
  98. package/changelog.md +0 -68
  99. package/dist/msal-node-extensions.cjs.development.js +0 -687
  100. package/dist/msal-node-extensions.cjs.development.js.map +0 -1
  101. package/dist/msal-node-extensions.cjs.production.min.js +0 -2
  102. package/dist/msal-node-extensions.cjs.production.min.js.map +0 -1
  103. package/dist/msal-node-extensions.esm.js +0 -692
  104. package/dist/msal-node-extensions.esm.js.map +0 -1
  105. package/src/dpapi-addon/Dpapi.ts +0 -12
  106. package/src/dpapi-addon/dpapi_addon.h +0 -6
  107. package/src/dpapi-addon/dpapi_not_supported.cpp +0 -19
  108. package/src/dpapi-addon/dpapi_win.cpp +0 -114
  109. package/src/dpapi-addon/main.cpp +0 -32
  110. package/src/index.ts +0 -13
  111. package/src/lock/CrossPlatformLock.ts +0 -89
  112. package/src/lock/CrossPlatformLockOptions.ts +0 -15
  113. package/src/persistence/FilePersistence.ts +0 -140
  114. package/src/persistence/FilePersistenceWithDataProtection.ts +0 -92
  115. package/src/persistence/IPersistence.ts +0 -17
  116. package/src/persistence/KeychainPersistence.ts +0 -86
  117. package/src/persistence/LibSecretPersistence.ts +0 -87
  118. package/src/utils/Constants.ts +0 -35
@@ -1,692 +0,0 @@
1
- import { promises } from 'fs';
2
- import { pid } from 'process';
3
- import { dirname } from 'path';
4
- import { Logger, LogLevel } from '@azure/msal-common';
5
- import { setPassword, getPassword, deletePassword } from 'keytar';
6
-
7
- /*
8
- * Copyright (c) Microsoft Corporation. All rights reserved.
9
- * Licensed under the MIT License.
10
- */
11
- const Constants = {
12
- /**
13
- * An existing file was the target of an operation that required that the target not exist
14
- */
15
- EEXIST_ERROR: "EEXIST",
16
-
17
- /**
18
- * No such file or directory: Commonly raised by fs operations to indicate that a component
19
- * of the specified pathname does not exist. No entity (file or directory) could be found
20
- * by the given path
21
- */
22
- ENOENT_ERROR: "ENOENT",
23
-
24
- /**
25
- * Operation not permitted. An attempt was made to perform an operation that requires
26
- * elevated privileges.
27
- */
28
- EPERM_ERROR: "EPERM",
29
-
30
- /**
31
- * Default service name for using MSAL Keytar
32
- */
33
- DEFAULT_SERVICE_NAME: "msal-node-extensions",
34
-
35
- /**
36
- * Test data used to verify underlying persistence mechanism
37
- */
38
- PERSISTENCE_TEST_DATA: "Dummy data to verify underlying persistence mechanism"
39
- };
40
-
41
- /*
42
- * Copyright (c) Microsoft Corporation. All rights reserved.
43
- * Licensed under the MIT License.
44
- */
45
-
46
- /**
47
- * Error thrown when trying to write MSAL cache to persistence.
48
- */
49
- class PersistenceError extends Error {
50
- constructor(errorCode, errorMessage) {
51
- const errorString = errorMessage ? `${errorCode}: ${errorMessage}` : errorCode;
52
- super(errorString);
53
- Object.setPrototypeOf(this, PersistenceError.prototype);
54
- this.errorCode = errorCode;
55
- this.errorMessage = errorMessage;
56
- this.name = "PersistenceError";
57
- }
58
- /**
59
- * Error thrown when trying to access the file system.
60
- */
61
-
62
-
63
- static createFileSystemError(errorCode, errorMessage) {
64
- return new PersistenceError(errorCode, errorMessage);
65
- }
66
- /**
67
- * Error thrown when trying to write, load, or delete data from secret service on linux.
68
- * Libsecret is used to access secret service.
69
- */
70
-
71
-
72
- static createLibSecretError(errorMessage) {
73
- return new PersistenceError("GnomeKeyringError", errorMessage);
74
- }
75
- /**
76
- * Error thrown when trying to write, load, or delete data from keychain on macOs.
77
- */
78
-
79
-
80
- static createKeychainPersistenceError(errorMessage) {
81
- return new PersistenceError("KeychainError", errorMessage);
82
- }
83
- /**
84
- * Error thrown when trying to encrypt or decrypt data using DPAPI on Windows.
85
- */
86
-
87
-
88
- static createFilePersistenceWithDPAPIError(errorMessage) {
89
- return new PersistenceError("DPAPIEncryptedFileError", errorMessage);
90
- }
91
- /**
92
- * Error thrown when using the cross platform lock.
93
- */
94
-
95
-
96
- static createCrossPlatformLockError(errorMessage) {
97
- return new PersistenceError("CrossPlatformLockError", errorMessage);
98
- }
99
- /**
100
- * Throw cache persistence error
101
- *
102
- * @param errorMessage string
103
- * @returns PersistenceError
104
- */
105
-
106
-
107
- static createCachePersistenceError(errorMessage) {
108
- return new PersistenceError("CachePersistenceError", errorMessage);
109
- }
110
-
111
- }
112
-
113
- /*
114
- * Copyright (c) Microsoft Corporation. All rights reserved.
115
- * Licensed under the MIT License.
116
- */
117
- /**
118
- * Cross-process lock that works on all platforms.
119
- */
120
-
121
- class CrossPlatformLock {
122
- constructor(lockFilePath, logger, lockOptions) {
123
- this.lockFilePath = lockFilePath;
124
- this.retryNumber = lockOptions ? lockOptions.retryNumber : 500;
125
- this.retryDelay = lockOptions ? lockOptions.retryDelay : 100;
126
- this.logger = logger;
127
- }
128
- /**
129
- * Locks cache from read or writes by creating file with same path and name as
130
- * cache file but with .lockfile extension. If another process has already created
131
- * the lockfile, will back off and retry based on configuration settings set by CrossPlatformLockOptions
132
- */
133
-
134
-
135
- async lock() {
136
- for (let tryCount = 0; tryCount < this.retryNumber; tryCount++) {
137
- try {
138
- this.logger.info(`Pid ${pid} trying to acquire lock`);
139
- this.lockFileHandle = await promises.open(this.lockFilePath, "wx+");
140
- this.logger.info(`Pid ${pid} acquired lock`);
141
- await this.lockFileHandle.write(pid.toString());
142
- return;
143
- } catch (err) {
144
- if (err.code === Constants.EEXIST_ERROR || err.code === Constants.EPERM_ERROR) {
145
- this.logger.info(err);
146
- await this.sleep(this.retryDelay);
147
- } else {
148
- this.logger.error(`${pid} was not able to acquire lock. Ran into error: ${err.message}`);
149
- throw PersistenceError.createCrossPlatformLockError(err.message);
150
- }
151
- }
152
- }
153
-
154
- this.logger.error(`${pid} was not able to acquire lock. Exceeded amount of retries set in the options`);
155
- throw PersistenceError.createCrossPlatformLockError("Not able to acquire lock. Exceeded amount of retries set in options");
156
- }
157
- /**
158
- * unlocks cache file by deleting .lockfile.
159
- */
160
-
161
-
162
- async unlock() {
163
- try {
164
- if (this.lockFileHandle) {
165
- // if we have a file handle to the .lockfile, delete lock file
166
- await promises.unlink(this.lockFilePath);
167
- await this.lockFileHandle.close();
168
- this.logger.info("lockfile deleted");
169
- } else {
170
- this.logger.warning("lockfile handle does not exist, so lockfile could not be deleted");
171
- }
172
- } catch (err) {
173
- if (err.code === Constants.ENOENT_ERROR) {
174
- this.logger.info("Tried to unlock but lockfile does not exist");
175
- } else {
176
- this.logger.error(`${pid} was not able to release lock. Ran into error: ${err.message}`);
177
- throw PersistenceError.createCrossPlatformLockError(err.message);
178
- }
179
- }
180
- }
181
-
182
- sleep(ms) {
183
- return new Promise(resolve => {
184
- setTimeout(resolve, ms);
185
- });
186
- }
187
-
188
- }
189
-
190
- /*
191
- * Copyright (c) Microsoft Corporation. All rights reserved.
192
- * Licensed under the MIT License.
193
- */
194
- /**
195
- * MSAL cache plugin which enables callers to write the MSAL cache to disk on Windows,
196
- * macOs, and Linux.
197
- *
198
- * - Persistence can be one of:
199
- * - FilePersistence: Writes and reads from an unencrypted file. Can be used on Windows,
200
- * macOs, or Linux.
201
- * - FilePersistenceWithDataProtection: Used on Windows, writes and reads from file encrypted
202
- * with windows dpapi-addon.
203
- * - KeychainPersistence: Used on macOs, writes and reads from keychain.
204
- * - LibSecretPersistence: Used on linux, writes and reads from secret service API. Requires
205
- * libsecret be installed.
206
- */
207
-
208
- class PersistenceCachePlugin {
209
- constructor(persistence, lockOptions) {
210
- this.persistence = persistence; // initialize logger
211
-
212
- this.logger = persistence.getLogger(); // create file lock
213
-
214
- this.lockFilePath = `${this.persistence.getFilePath()}.lockfile`;
215
- this.crossPlatformLock = new CrossPlatformLock(this.lockFilePath, this.logger, lockOptions); // initialize default values
216
-
217
- this.lastSync = 0;
218
- this.currentCache = null;
219
- }
220
- /**
221
- * Reads from storage and saves an in-memory copy. If persistence has not been updated
222
- * since last time data was read, in memory copy is used.
223
- *
224
- * If cacheContext.cacheHasChanged === true, then file lock is created and not deleted until
225
- * afterCacheAccess() is called, to prevent the cache file from changing in between
226
- * beforeCacheAccess() and afterCacheAccess().
227
- */
228
-
229
-
230
- async beforeCacheAccess(cacheContext) {
231
- this.logger.info("Executing before cache access");
232
- const reloadNecessary = await this.persistence.reloadNecessary(this.lastSync);
233
-
234
- if (!reloadNecessary && this.currentCache !== null) {
235
- if (cacheContext.cacheHasChanged) {
236
- this.logger.verbose("Cache context has changed");
237
- await this.crossPlatformLock.lock();
238
- }
239
-
240
- return;
241
- }
242
-
243
- try {
244
- this.logger.info(`Reload necessary. Last sync time: ${this.lastSync}`);
245
- await this.crossPlatformLock.lock();
246
- this.currentCache = await this.persistence.load();
247
- this.lastSync = new Date().getTime();
248
- cacheContext.tokenCache.deserialize(this.currentCache);
249
- this.logger.info(`Last sync time updated to: ${this.lastSync}`);
250
- } finally {
251
- if (!cacheContext.cacheHasChanged) {
252
- await this.crossPlatformLock.unlock();
253
- this.logger.info(`Pid ${pid} released lock`);
254
- } else {
255
- this.logger.info(`Pid ${pid} beforeCacheAccess did not release lock`);
256
- }
257
- }
258
- }
259
- /**
260
- * Writes to storage if MSAL in memory copy of cache has been changed.
261
- */
262
-
263
-
264
- async afterCacheAccess(cacheContext) {
265
- this.logger.info("Executing after cache access");
266
-
267
- try {
268
- if (cacheContext.cacheHasChanged) {
269
- this.logger.info("Msal in-memory cache has changed. Writing changes to persistence");
270
- this.currentCache = cacheContext.tokenCache.serialize();
271
- await this.persistence.save(this.currentCache);
272
- } else {
273
- this.logger.info("Msal in-memory cache has not changed. Did not write to persistence");
274
- }
275
- } finally {
276
- await this.crossPlatformLock.unlock();
277
- this.logger.info(`Pid ${pid} afterCacheAccess released lock`);
278
- }
279
- }
280
-
281
- }
282
-
283
- /*
284
- * Copyright (c) Microsoft Corporation. All rights reserved.
285
- * Licensed under the MIT License.
286
- */
287
- class BasePersistence {
288
- async verifyPersistence() {
289
- // We are using a different location for the test to avoid overriding the functional cache
290
- const persistenceValidator = await this.createForPersistenceValidation();
291
-
292
- try {
293
- await persistenceValidator.save(Constants.PERSISTENCE_TEST_DATA);
294
- const retrievedDummyData = await persistenceValidator.load();
295
-
296
- if (!retrievedDummyData) {
297
- throw PersistenceError.createCachePersistenceError("Persistence check failed. Data was written but it could not be read. " + "Possible cause: on Linux, LibSecret is installed but D-Bus isn't running because it cannot be started over SSH.");
298
- }
299
-
300
- if (retrievedDummyData !== Constants.PERSISTENCE_TEST_DATA) {
301
- throw PersistenceError.createCachePersistenceError(`Persistence check failed. Data written ${Constants.PERSISTENCE_TEST_DATA} is different from data read ${retrievedDummyData}`);
302
- }
303
-
304
- await persistenceValidator.delete();
305
- return true;
306
- } catch (e) {
307
- throw PersistenceError.createCachePersistenceError(`Verifing persistence failed with the error: ${e}`);
308
- }
309
- }
310
-
311
- }
312
-
313
- /*
314
- * Copyright (c) Microsoft Corporation. All rights reserved.
315
- * Licensed under the MIT License.
316
- */
317
- /**
318
- * Reads and writes data to file specified by file location. File contents are not
319
- * encrypted.
320
- *
321
- * If file or directory has not been created, it FilePersistence.create() will create
322
- * file and any directories in the path recursively.
323
- */
324
-
325
- class FilePersistence extends BasePersistence {
326
- static async create(fileLocation, loggerOptions) {
327
- const filePersistence = new FilePersistence();
328
- filePersistence.filePath = fileLocation;
329
- filePersistence.logger = new Logger(loggerOptions || FilePersistence.createDefaultLoggerOptions());
330
- await filePersistence.createCacheFile();
331
- return filePersistence;
332
- }
333
-
334
- async save(contents) {
335
- try {
336
- await promises.writeFile(this.getFilePath(), contents, "utf-8");
337
- } catch (err) {
338
- throw PersistenceError.createFileSystemError(err.code, err.message);
339
- }
340
- }
341
-
342
- async saveBuffer(contents) {
343
- try {
344
- await promises.writeFile(this.getFilePath(), contents);
345
- } catch (err) {
346
- throw PersistenceError.createFileSystemError(err.code, err.message);
347
- }
348
- }
349
-
350
- async load() {
351
- try {
352
- return await promises.readFile(this.getFilePath(), "utf-8");
353
- } catch (err) {
354
- throw PersistenceError.createFileSystemError(err.code, err.message);
355
- }
356
- }
357
-
358
- async loadBuffer() {
359
- try {
360
- return await promises.readFile(this.getFilePath());
361
- } catch (err) {
362
- throw PersistenceError.createFileSystemError(err.code, err.message);
363
- }
364
- }
365
-
366
- async delete() {
367
- try {
368
- await promises.unlink(this.getFilePath());
369
- return true;
370
- } catch (err) {
371
- if (err.code === Constants.ENOENT_ERROR) {
372
- // file does not exist, so it was not deleted
373
- this.logger.warning("Cache file does not exist, so it could not be deleted");
374
- return false;
375
- }
376
-
377
- throw PersistenceError.createFileSystemError(err.code, err.message);
378
- }
379
- }
380
-
381
- getFilePath() {
382
- return this.filePath;
383
- }
384
-
385
- async reloadNecessary(lastSync) {
386
- return lastSync < (await this.timeLastModified());
387
- }
388
-
389
- getLogger() {
390
- return this.logger;
391
- }
392
-
393
- createForPersistenceValidation() {
394
- const testCacheFileLocation = `${dirname(this.filePath)}/test.cache`;
395
- return FilePersistence.create(testCacheFileLocation);
396
- }
397
-
398
- static createDefaultLoggerOptions() {
399
- return {
400
- loggerCallback: () => {// allow users to not set loggerCallback
401
- },
402
- piiLoggingEnabled: false,
403
- logLevel: LogLevel.Info
404
- };
405
- }
406
-
407
- async timeLastModified() {
408
- try {
409
- const stats = await promises.stat(this.filePath);
410
- return stats.mtime.getTime();
411
- } catch (err) {
412
- if (err.code === Constants.ENOENT_ERROR) {
413
- // file does not exist, so it's never been modified
414
- this.logger.verbose("Cache file does not exist");
415
- return 0;
416
- }
417
-
418
- throw PersistenceError.createFileSystemError(err.code, err.message);
419
- }
420
- }
421
-
422
- async createCacheFile() {
423
- await this.createFileDirectory(); // File is created only if it does not exist
424
-
425
- const fileHandle = await promises.open(this.filePath, "a");
426
- await fileHandle.close();
427
- this.logger.info(`File created at ${this.filePath}`);
428
- }
429
-
430
- async createFileDirectory() {
431
- try {
432
- await promises.mkdir(dirname(this.filePath), {
433
- recursive: true
434
- });
435
- } catch (err) {
436
- if (err.code === Constants.EEXIST_ERROR) {
437
- this.logger.info(`Directory ${dirname(this.filePath)} already exists`);
438
- } else {
439
- throw PersistenceError.createFileSystemError(err.code, err.message);
440
- }
441
- }
442
- }
443
-
444
- }
445
-
446
- /*
447
- * Copyright (c) Microsoft Corporation. All rights reserved.
448
- * Licensed under the MIT License.
449
- */
450
-
451
- /* eslint-disable-next-line @typescript-eslint/no-var-requires, no-var */
452
- var Dpapi = /*#__PURE__*/require("bindings")("dpapi");
453
-
454
- /*
455
- * Copyright (c) Microsoft Corporation. All rights reserved.
456
- * Licensed under the MIT License.
457
- */
458
-
459
- /**
460
- * Specifies the scope of the data protection - either the current user or the local
461
- * machine.
462
- *
463
- * You do not need a key to protect or unprotect the data.
464
- * If you set the Scope to CurrentUser, only applications running on your credentials can
465
- * unprotect the data; however, that means that any application running on your credentials
466
- * can access the protected data. If you set the Scope to LocalMachine, any full-trust
467
- * application on the computer can unprotect, access, and modify the data.
468
- *
469
- */
470
- var DataProtectionScope;
471
-
472
- (function (DataProtectionScope) {
473
- DataProtectionScope["CurrentUser"] = "CurrentUser";
474
- DataProtectionScope["LocalMachine"] = "LocalMachine";
475
- })(DataProtectionScope || (DataProtectionScope = {}));
476
-
477
- /*
478
- * Copyright (c) Microsoft Corporation. All rights reserved.
479
- * Licensed under the MIT License.
480
- */
481
- /**
482
- * Uses CryptProtectData and CryptUnprotectData on Windows to encrypt and decrypt file contents.
483
- *
484
- * scope: Scope of the data protection. Either local user or the current machine
485
- * optionalEntropy: Password or other additional entropy used to encrypt the data
486
- */
487
-
488
- class FilePersistenceWithDataProtection extends BasePersistence {
489
- constructor(scope, optionalEntropy) {
490
- super();
491
- this.scope = scope;
492
- this.optionalEntropy = optionalEntropy ? Buffer.from(optionalEntropy, "utf-8") : null;
493
- }
494
-
495
- static async create(fileLocation, scope, optionalEntropy, loggerOptions) {
496
- const persistence = new FilePersistenceWithDataProtection(scope, optionalEntropy);
497
- persistence.filePersistence = await FilePersistence.create(fileLocation, loggerOptions);
498
- return persistence;
499
- }
500
-
501
- async save(contents) {
502
- try {
503
- const encryptedContents = Dpapi.protectData(Buffer.from(contents, "utf-8"), this.optionalEntropy, this.scope.toString());
504
- await this.filePersistence.saveBuffer(encryptedContents);
505
- } catch (err) {
506
- throw PersistenceError.createFilePersistenceWithDPAPIError(err.message);
507
- }
508
- }
509
-
510
- async load() {
511
- try {
512
- const encryptedContents = await this.filePersistence.loadBuffer();
513
-
514
- if (typeof encryptedContents === "undefined" || !encryptedContents || 0 === encryptedContents.length) {
515
- this.filePersistence.getLogger().info("Encrypted contents loaded from file were null or empty");
516
- return null;
517
- }
518
-
519
- return Dpapi.unprotectData(encryptedContents, this.optionalEntropy, this.scope.toString()).toString();
520
- } catch (err) {
521
- throw PersistenceError.createFilePersistenceWithDPAPIError(err.message);
522
- }
523
- }
524
-
525
- async delete() {
526
- return this.filePersistence.delete();
527
- }
528
-
529
- async reloadNecessary(lastSync) {
530
- return this.filePersistence.reloadNecessary(lastSync);
531
- }
532
-
533
- getFilePath() {
534
- return this.filePersistence.getFilePath();
535
- }
536
-
537
- getLogger() {
538
- return this.filePersistence.getLogger();
539
- }
540
-
541
- createForPersistenceValidation() {
542
- const testCacheFileLocation = `${dirname(this.filePersistence.getFilePath())}/test.cache`;
543
- return FilePersistenceWithDataProtection.create(testCacheFileLocation, DataProtectionScope.CurrentUser);
544
- }
545
-
546
- }
547
-
548
- /*
549
- * Copyright (c) Microsoft Corporation. All rights reserved.
550
- * Licensed under the MIT License.
551
- */
552
- /**
553
- * Uses reads and writes passwords to macOS keychain
554
- *
555
- * serviceName: Identifier used as key for whatever value is stored
556
- * accountName: Account under which password should be stored
557
- */
558
-
559
- class KeychainPersistence extends BasePersistence {
560
- constructor(serviceName, accountName) {
561
- super();
562
- this.serviceName = serviceName;
563
- this.accountName = accountName;
564
- }
565
-
566
- static async create(fileLocation, serviceName, accountName, loggerOptions) {
567
- const persistence = new KeychainPersistence(serviceName, accountName);
568
- persistence.filePersistence = await FilePersistence.create(fileLocation, loggerOptions);
569
- return persistence;
570
- }
571
-
572
- async save(contents) {
573
- try {
574
- await setPassword(this.serviceName, this.accountName, contents);
575
- } catch (err) {
576
- throw PersistenceError.createKeychainPersistenceError(err.message);
577
- } // Write dummy data to update file mtime
578
-
579
-
580
- await this.filePersistence.save("{}");
581
- }
582
-
583
- async load() {
584
- try {
585
- return await getPassword(this.serviceName, this.accountName);
586
- } catch (err) {
587
- throw PersistenceError.createKeychainPersistenceError(err.message);
588
- }
589
- }
590
-
591
- async delete() {
592
- try {
593
- await this.filePersistence.delete();
594
- return await deletePassword(this.serviceName, this.accountName);
595
- } catch (err) {
596
- throw PersistenceError.createKeychainPersistenceError(err.message);
597
- }
598
- }
599
-
600
- async reloadNecessary(lastSync) {
601
- return this.filePersistence.reloadNecessary(lastSync);
602
- }
603
-
604
- getFilePath() {
605
- return this.filePersistence.getFilePath();
606
- }
607
-
608
- getLogger() {
609
- return this.filePersistence.getLogger();
610
- }
611
-
612
- createForPersistenceValidation() {
613
- const testCacheFileLocation = `${dirname(this.filePersistence.getFilePath())}/test.cache`;
614
- return KeychainPersistence.create(testCacheFileLocation, "persistenceValidationServiceName", "persistencValidationAccountName");
615
- }
616
-
617
- }
618
-
619
- /*
620
- * Copyright (c) Microsoft Corporation. All rights reserved.
621
- * Licensed under the MIT License.
622
- */
623
- /**
624
- * Uses reads and writes passwords to Secret Service API/libsecret. Requires libsecret
625
- * to be installed.
626
- *
627
- * serviceName: Identifier used as key for whatever value is stored
628
- * accountName: Account under which password should be stored
629
- */
630
-
631
- class LibSecretPersistence extends BasePersistence {
632
- constructor(serviceName, accountName) {
633
- super();
634
- this.serviceName = serviceName;
635
- this.accountName = accountName;
636
- }
637
-
638
- static async create(fileLocation, serviceName, accountName, loggerOptions) {
639
- const persistence = new LibSecretPersistence(serviceName, accountName);
640
- persistence.filePersistence = await FilePersistence.create(fileLocation, loggerOptions);
641
- return persistence;
642
- }
643
-
644
- async save(contents) {
645
- try {
646
- await setPassword(this.serviceName, this.accountName, contents);
647
- } catch (err) {
648
- throw PersistenceError.createLibSecretError(err.message);
649
- } // Write dummy data to update file mtime
650
-
651
-
652
- await this.filePersistence.save("{}");
653
- }
654
-
655
- async load() {
656
- try {
657
- return await getPassword(this.serviceName, this.accountName);
658
- } catch (err) {
659
- throw PersistenceError.createLibSecretError(err.message);
660
- }
661
- }
662
-
663
- async delete() {
664
- try {
665
- await this.filePersistence.delete();
666
- return await deletePassword(this.serviceName, this.accountName);
667
- } catch (err) {
668
- throw PersistenceError.createLibSecretError(err.message);
669
- }
670
- }
671
-
672
- async reloadNecessary(lastSync) {
673
- return this.filePersistence.reloadNecessary(lastSync);
674
- }
675
-
676
- getFilePath() {
677
- return this.filePersistence.getFilePath();
678
- }
679
-
680
- getLogger() {
681
- return this.filePersistence.getLogger();
682
- }
683
-
684
- createForPersistenceValidation() {
685
- const testCacheFileLocation = `${dirname(this.filePersistence.getFilePath())}/test.cache`;
686
- return LibSecretPersistence.create(testCacheFileLocation, "persistenceValidationServiceName", "persistencValidationAccountName");
687
- }
688
-
689
- }
690
-
691
- export { DataProtectionScope, FilePersistence, FilePersistenceWithDataProtection, KeychainPersistence, LibSecretPersistence, PersistenceCachePlugin };
692
- //# sourceMappingURL=msal-node-extensions.esm.js.map