@azure/msal-node-extensions 1.0.0-alpha.12 → 1.0.0-alpha.13

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.
@@ -1,73 +1,73 @@
1
- /*
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the MIT License.
4
- */
5
-
6
- import { FilePersistenceWithDataProtection } from "../persistence/FilePersistenceWithDataProtection";
7
- import { LibSecretPersistence } from "../persistence/LibSecretPersistence";
8
- import { KeychainPersistence } from "../persistence/KeychainPersistence";
9
- import { DataProtectionScope } from "../persistence/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 "../persistence/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(`Cache path and/or data protection scope not provided for the FilePersistenceWithDataProtection cache plugin`);
24
- }
25
-
26
- peristence = await FilePersistenceWithDataProtection.create(config.cachePath, DataProtectionScope.CurrentUser);
27
- }
28
-
29
- // On Mac, uses keychain.
30
- else if (Environment.isMacPlatform()) {
31
- if (!config.cachePath || !config.serviceName || !config.accountName) {
32
- throw PersistenceError.createPersistenceNotValidatedError(`Cache path, service name and/or account name not provided for the KeychainPersistence cache plugin`);
33
- }
34
-
35
- peristence = await KeychainPersistence.create(config.cachePath, config.serviceName, config.accountName);
36
- }
37
-
38
- // On Linux, uses libsecret to store to secret service. Libsecret has to be installed.
39
- else if (Environment.isLinuxPlatform()) {
40
- if (!config.cachePath || !config.serviceName || !config.accountName) {
41
- throw PersistenceError.createPersistenceNotValidatedError(`Cache path, service name and/or account name not provided for the LibSecretPersistence cache plugin`);
42
- }
43
-
44
- peristence = await LibSecretPersistence.create(config.cachePath, config.serviceName, config.accountName);
45
- }
46
-
47
- else {
48
- throw PersistenceError.createNotSupportedError("The current environment is not supported by msal-node-extensions yet.");
49
- }
50
-
51
- // Initially suppress the error thrown during persistence verification to allow us to fallback to plain text
52
- const isPersistenceVerified = await peristence.verifyPersistence().catch(() => false);
53
-
54
- if (!isPersistenceVerified) {
55
- if (Environment.isLinuxPlatform() && config.usePlaintextFileOnLinux) {
56
- if (!config.cachePath) {
57
- throw PersistenceError.createPersistenceNotValidatedError(`Cache path not provided for the FilePersistence cache plugin`);
58
- }
59
-
60
- peristence = await FilePersistence.create(config.cachePath);
61
-
62
- const isFilePersistenceVerified = await peristence.verifyPersistence();
63
- if (isFilePersistenceVerified) {
64
- return peristence;
65
- }
66
- }
67
-
68
- throw PersistenceError.createPersistenceNotVerifiedError("Persistence could not be verified");
69
- }
70
-
71
- return peristence;
72
- }
73
- }
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { FilePersistenceWithDataProtection } from "../persistence/FilePersistenceWithDataProtection";
7
+ import { LibSecretPersistence } from "../persistence/LibSecretPersistence";
8
+ import { KeychainPersistence } from "../persistence/KeychainPersistence";
9
+ import { DataProtectionScope } from "../persistence/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 "../persistence/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(`Cache path and/or data protection scope not provided for the FilePersistenceWithDataProtection cache plugin`);
24
+ }
25
+
26
+ peristence = await FilePersistenceWithDataProtection.create(config.cachePath, DataProtectionScope.CurrentUser);
27
+ }
28
+
29
+ // On Mac, uses keychain.
30
+ else if (Environment.isMacPlatform()) {
31
+ if (!config.cachePath || !config.serviceName || !config.accountName) {
32
+ throw PersistenceError.createPersistenceNotValidatedError(`Cache path, service name and/or account name not provided for the KeychainPersistence cache plugin`);
33
+ }
34
+
35
+ peristence = await KeychainPersistence.create(config.cachePath, config.serviceName, config.accountName);
36
+ }
37
+
38
+ // On Linux, uses libsecret to store to secret service. Libsecret has to be installed.
39
+ else if (Environment.isLinuxPlatform()) {
40
+ if (!config.cachePath || !config.serviceName || !config.accountName) {
41
+ throw PersistenceError.createPersistenceNotValidatedError(`Cache path, service name and/or account name not provided for the LibSecretPersistence cache plugin`);
42
+ }
43
+
44
+ peristence = await LibSecretPersistence.create(config.cachePath, config.serviceName, config.accountName);
45
+ }
46
+
47
+ else {
48
+ throw PersistenceError.createNotSupportedError("The current environment is not supported by msal-node-extensions yet.");
49
+ }
50
+
51
+ // Initially suppress the error thrown during persistence verification to allow us to fallback to plain text
52
+ const isPersistenceVerified = await peristence.verifyPersistence().catch(() => false);
53
+
54
+ if (!isPersistenceVerified) {
55
+ if (Environment.isLinuxPlatform() && config.usePlaintextFileOnLinux) {
56
+ if (!config.cachePath) {
57
+ throw PersistenceError.createPersistenceNotValidatedError(`Cache path not provided for the FilePersistence cache plugin`);
58
+ }
59
+
60
+ peristence = await FilePersistence.create(config.cachePath);
61
+
62
+ const isFilePersistenceVerified = await peristence.verifyPersistence();
63
+ if (isFilePersistenceVerified) {
64
+ return peristence;
65
+ }
66
+ }
67
+
68
+ throw PersistenceError.createPersistenceNotVerifiedError("Persistence could not be verified");
69
+ }
70
+
71
+ return peristence;
72
+ }
73
+ }
@@ -1,62 +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
-
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
- };
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
+ };
@@ -1,99 +1,99 @@
1
- /*
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the MIT License.
4
- */
5
-
6
- import path from "path";
7
- import { Constants, Platform } from "./Constants";
8
- import { PersistenceError } from "../error/PersistenceError";
9
- import { StringUtils } from "@azure/msal-common";
10
-
11
- export class Environment {
12
- static get homeEnvVar() {
13
- return this.getEnvironmentVariable(Constants.ENVIRONMENT.HOME);
14
- }
15
-
16
- static get lognameEnvVar() {
17
- return this.getEnvironmentVariable(Constants.ENVIRONMENT.LOGNAME);
18
- }
19
-
20
- static get userEnvVar() {
21
- return this.getEnvironmentVariable(Constants.ENVIRONMENT.USER);
22
- }
23
-
24
- static get lnameEnvVar() {
25
- return this.getEnvironmentVariable(Constants.ENVIRONMENT.LNAME);
26
- }
27
-
28
- static get usernameEnvVar() {
29
- return this.getEnvironmentVariable(Constants.ENVIRONMENT.USERNAME);
30
- }
31
-
32
- static getEnvironmentVariable(name: string): string {
33
- return process.env[name];
34
- }
35
-
36
- static getEnvironmentPlatform(): string {
37
- return process.platform;
38
- }
39
-
40
- static isWindowsPlatform(): boolean {
41
- return this.getEnvironmentPlatform() === Platform.WINDOWS;
42
- }
43
-
44
- static isLinuxPlatform(): boolean {
45
- return this.getEnvironmentPlatform() === Platform.LINUX;
46
- }
47
-
48
- static isMacPlatform(): boolean {
49
- return this.getEnvironmentPlatform() === Platform.MACOS;
50
- }
51
-
52
- static isLinuxRootUser(): boolean {
53
- return process.getuid() == Constants.LINUX_ROOT_USER_GUID;
54
- }
55
-
56
- static getUserRootDirectory(): string {
57
- return !this.isWindowsPlatform ?
58
- this.getUserHomeDirOnUnix() :
59
- this.getUserHomeDirOnWindows()
60
- }
61
-
62
- static getUserHomeDirOnWindows(): string {
63
- return this.getEnvironmentVariable(Constants.ENVIRONMENT.LOCAL_APPLICATION_DATA);
64
- }
65
-
66
- static getUserHomeDirOnUnix(): string | null {
67
- if (this.isWindowsPlatform()) {
68
- throw PersistenceError.createNotSupportedError("Getting the user home directory for unix is not supported in windows")
69
- }
70
-
71
- if (!StringUtils.isEmpty(this.homeEnvVar)) {
72
- return this.homeEnvVar;
73
- }
74
-
75
- let username = null;
76
- if (!StringUtils.isEmpty(this.lognameEnvVar)) {
77
- username = this.lognameEnvVar;
78
- } else if (!StringUtils.isEmpty(this.userEnvVar)) {
79
- username = this.userEnvVar;
80
- } else if (!StringUtils.isEmpty(this.lnameEnvVar)) {
81
- username = this.lnameEnvVar;
82
- } else if (!StringUtils.isEmpty(this.usernameEnvVar)) {
83
- username = this.usernameEnvVar;
84
- }
85
-
86
- if (this.isMacPlatform()) {
87
- return !StringUtils.isEmpty(username) ? path.join("/Users", username) : null;
88
- } else if (this.isLinuxPlatform()) {
89
- if (this.isLinuxRootUser()) {
90
- return "/root";
91
- } else {
92
- return !StringUtils.isEmpty(username) ? path.join("/home", username) : null;
93
- }
94
- } else {
95
- throw PersistenceError.createNotSupportedError("Getting the user home directory for unix is not supported in windows")
96
- }
97
-
98
- }
99
- }
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import path from "path";
7
+ import { Constants, Platform } from "./Constants";
8
+ import { PersistenceError } from "../error/PersistenceError";
9
+ import { StringUtils } from "@azure/msal-common";
10
+
11
+ export class Environment {
12
+ static get homeEnvVar() {
13
+ return this.getEnvironmentVariable(Constants.ENVIRONMENT.HOME);
14
+ }
15
+
16
+ static get lognameEnvVar() {
17
+ return this.getEnvironmentVariable(Constants.ENVIRONMENT.LOGNAME);
18
+ }
19
+
20
+ static get userEnvVar() {
21
+ return this.getEnvironmentVariable(Constants.ENVIRONMENT.USER);
22
+ }
23
+
24
+ static get lnameEnvVar() {
25
+ return this.getEnvironmentVariable(Constants.ENVIRONMENT.LNAME);
26
+ }
27
+
28
+ static get usernameEnvVar() {
29
+ return this.getEnvironmentVariable(Constants.ENVIRONMENT.USERNAME);
30
+ }
31
+
32
+ static getEnvironmentVariable(name: string): string {
33
+ return process.env[name];
34
+ }
35
+
36
+ static getEnvironmentPlatform(): string {
37
+ return process.platform;
38
+ }
39
+
40
+ static isWindowsPlatform(): boolean {
41
+ return this.getEnvironmentPlatform() === Platform.WINDOWS;
42
+ }
43
+
44
+ static isLinuxPlatform(): boolean {
45
+ return this.getEnvironmentPlatform() === Platform.LINUX;
46
+ }
47
+
48
+ static isMacPlatform(): boolean {
49
+ return this.getEnvironmentPlatform() === Platform.MACOS;
50
+ }
51
+
52
+ static isLinuxRootUser(): boolean {
53
+ return process.getuid() == Constants.LINUX_ROOT_USER_GUID;
54
+ }
55
+
56
+ static getUserRootDirectory(): string {
57
+ return !this.isWindowsPlatform ?
58
+ this.getUserHomeDirOnUnix() :
59
+ this.getUserHomeDirOnWindows()
60
+ }
61
+
62
+ static getUserHomeDirOnWindows(): string {
63
+ return this.getEnvironmentVariable(Constants.ENVIRONMENT.LOCAL_APPLICATION_DATA);
64
+ }
65
+
66
+ static getUserHomeDirOnUnix(): string | null {
67
+ if (this.isWindowsPlatform()) {
68
+ throw PersistenceError.createNotSupportedError("Getting the user home directory for unix is not supported in windows")
69
+ }
70
+
71
+ if (!StringUtils.isEmpty(this.homeEnvVar)) {
72
+ return this.homeEnvVar;
73
+ }
74
+
75
+ let username = null;
76
+ if (!StringUtils.isEmpty(this.lognameEnvVar)) {
77
+ username = this.lognameEnvVar;
78
+ } else if (!StringUtils.isEmpty(this.userEnvVar)) {
79
+ username = this.userEnvVar;
80
+ } else if (!StringUtils.isEmpty(this.lnameEnvVar)) {
81
+ username = this.lnameEnvVar;
82
+ } else if (!StringUtils.isEmpty(this.usernameEnvVar)) {
83
+ username = this.usernameEnvVar;
84
+ }
85
+
86
+ if (this.isMacPlatform()) {
87
+ return !StringUtils.isEmpty(username) ? path.join("/Users", username) : null;
88
+ } else if (this.isLinuxPlatform()) {
89
+ if (this.isLinuxRootUser()) {
90
+ return "/root";
91
+ } else {
92
+ return !StringUtils.isEmpty(username) ? path.join("/home", username) : null;
93
+ }
94
+ } else {
95
+ throw PersistenceError.createNotSupportedError("Getting the user home directory for unix is not supported in windows")
96
+ }
97
+
98
+ }
99
+ }