@equinor/fusion-framework-module-msal-node 0.1.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 (62) hide show
  1. package/CHANGELOG.md +99 -0
  2. package/LICENSE +21 -0
  3. package/README.md +125 -0
  4. package/dist/esm/AuthConfigurator.interface.js +2 -0
  5. package/dist/esm/AuthConfigurator.interface.js.map +1 -0
  6. package/dist/esm/AuthConfigurator.js +112 -0
  7. package/dist/esm/AuthConfigurator.js.map +1 -0
  8. package/dist/esm/AuthProvider.interface.js +2 -0
  9. package/dist/esm/AuthProvider.interface.js.map +1 -0
  10. package/dist/esm/AuthProvider.js +109 -0
  11. package/dist/esm/AuthProvider.js.map +1 -0
  12. package/dist/esm/AuthProviderInteractive.js +88 -0
  13. package/dist/esm/AuthProviderInteractive.js.map +1 -0
  14. package/dist/esm/AuthTokenProvider.js +50 -0
  15. package/dist/esm/AuthTokenProvider.js.map +1 -0
  16. package/dist/esm/create-auth-cache.js +67 -0
  17. package/dist/esm/create-auth-cache.js.map +1 -0
  18. package/dist/esm/create-auth-client.js +35 -0
  19. package/dist/esm/create-auth-client.js.map +1 -0
  20. package/dist/esm/create-auth-server.js +81 -0
  21. package/dist/esm/create-auth-server.js.map +1 -0
  22. package/dist/esm/enable-module.js +31 -0
  23. package/dist/esm/enable-module.js.map +1 -0
  24. package/dist/esm/error.js +64 -0
  25. package/dist/esm/error.js.map +1 -0
  26. package/dist/esm/index.js +4 -0
  27. package/dist/esm/index.js.map +1 -0
  28. package/dist/esm/module.js +26 -0
  29. package/dist/esm/module.js.map +1 -0
  30. package/dist/esm/version.js +3 -0
  31. package/dist/esm/version.js.map +1 -0
  32. package/dist/tsconfig.tsbuildinfo +1 -0
  33. package/dist/types/AuthConfigurator.d.ts +55 -0
  34. package/dist/types/AuthConfigurator.interface.d.ts +153 -0
  35. package/dist/types/AuthProvider.d.ts +81 -0
  36. package/dist/types/AuthProvider.interface.d.ts +55 -0
  37. package/dist/types/AuthProviderInteractive.d.ts +73 -0
  38. package/dist/types/AuthTokenProvider.d.ts +43 -0
  39. package/dist/types/create-auth-cache.d.ts +32 -0
  40. package/dist/types/create-auth-client.d.ts +24 -0
  41. package/dist/types/create-auth-server.d.ts +34 -0
  42. package/dist/types/enable-module.d.ts +24 -0
  43. package/dist/types/error.d.ts +59 -0
  44. package/dist/types/index.d.ts +5 -0
  45. package/dist/types/module.d.ts +24 -0
  46. package/dist/types/version.d.ts +1 -0
  47. package/package.json +46 -0
  48. package/src/AuthConfigurator.interface.ts +163 -0
  49. package/src/AuthConfigurator.ts +131 -0
  50. package/src/AuthProvider.interface.ts +53 -0
  51. package/src/AuthProvider.ts +119 -0
  52. package/src/AuthProviderInteractive.ts +117 -0
  53. package/src/AuthTokenProvider.ts +56 -0
  54. package/src/create-auth-cache.ts +85 -0
  55. package/src/create-auth-client.ts +40 -0
  56. package/src/create-auth-server.ts +93 -0
  57. package/src/enable-module.ts +35 -0
  58. package/src/error.ts +66 -0
  59. package/src/index.ts +9 -0
  60. package/src/module.ts +52 -0
  61. package/src/version.ts +2 -0
  62. package/tsconfig.json +15 -0
@@ -0,0 +1,153 @@
1
+ import type { PublicClientApplication } from '@azure/msal-node';
2
+ import type { IAuthProvider } from './AuthProvider.interface.js';
3
+ /**
4
+ * Represents the configuration for authentication in "token only" mode.
5
+ *
6
+ * @property mode - Specifies the authentication mode as 'token_only'.
7
+ * @property accessToken - The authentication token to be used.
8
+ * @property parent - An optional reference to a parent authentication provider.
9
+ */
10
+ type AuthConfigTokenMode = {
11
+ mode: 'token_only';
12
+ accessToken: string;
13
+ client?: never;
14
+ server?: never;
15
+ parent?: IAuthProvider;
16
+ };
17
+ /**
18
+ * Configuration type for silent authentication mode.
19
+ *
20
+ * @property mode - Specifies the authentication mode as 'silent'.
21
+ * @property client - An instance of `PublicClientApplication` used for authentication.
22
+ * @property parent - An optional parent `IAuthProvider` instance for delegation.
23
+ */
24
+ type AuthConfigSilentMode = {
25
+ mode: 'silent';
26
+ client: PublicClientApplication;
27
+ server?: never;
28
+ accessToken?: never;
29
+ parent?: IAuthProvider;
30
+ };
31
+ /**
32
+ * Configuration type for the interactive authentication mode.
33
+ *
34
+ * @property mode - Specifies the authentication mode as 'interactive'.
35
+ * @property client - An instance of `PublicClientApplication` used for authentication.
36
+ * @property server - Configuration for the local server used during the interactive authentication process.
37
+ * @property server.port - The port number on which the local server will run.
38
+ * @property server.onOpen - An optional callback function that is invoked with the authentication URL when the server starts.
39
+ * @property parent - An optional parent `IAuthProvider` instance for delegation or chaining of authentication providers.
40
+ */
41
+ type AuthConfigInteractiveMode = {
42
+ mode: 'interactive';
43
+ client: PublicClientApplication;
44
+ server: {
45
+ port: number;
46
+ onOpen?: (url: string) => void;
47
+ };
48
+ accessToken?: never;
49
+ parent?: IAuthProvider;
50
+ };
51
+ /**
52
+ * Represents the configuration options for authentication.
53
+ *
54
+ * This type is a union of three different authentication modes:
55
+ * - `AuthConfigInteractiveMode`: Configuration for interactive authentication.
56
+ * - `AuthConfigSilentMode`: Configuration for silent authentication.
57
+ * - `AuthConfigTokenMode`: Configuration for token-based authentication.
58
+ */
59
+ export type AuthConfig = AuthConfigInteractiveMode | AuthConfigSilentMode | AuthConfigTokenMode;
60
+ /**
61
+ * Interface for configuring authentication settings for the MSAL Node module.
62
+ *
63
+ * This interface is intended for both consumers (users integrating authentication into their Fusion Framework Node.js applications)
64
+ * and future maintainers or developers extending or refactoring the module.
65
+ *
66
+ * Each method allows for fine-grained control over the authentication setup, supporting multiple authentication modes:
67
+ * - `token_only`: Use a pre-obtained access token (e.g., for CI/CD or automation).
68
+ * - `silent`: Use MSAL's silent authentication with a configured client (for background services or cached tokens).
69
+ * - `interactive`: Use MSAL's interactive authentication, typically for CLI tools or development, with a local server for browser-based login.
70
+ *
71
+ * Consumers should use the provided methods to configure the module according to their use case.
72
+ * Maintainers should ensure that new authentication flows or configuration options are exposed via this interface for consistency.
73
+ *
74
+ * @example
75
+ * // --- Interactive mode (browser login, local server) ---
76
+ * ```ts
77
+ * builder.setMode('interactive');
78
+ * builder.setClientConfig('your-tenant-id', 'your-client-id');
79
+ * builder.setServerPort(3000);
80
+ * builder.setServerOnOpen((url) => {
81
+ * console.log(`Please navigate to: ${url}`);
82
+ * });
83
+ * ```
84
+ *
85
+ * // --- Silent mode (background, cached/refresh tokens) ---
86
+ * ```ts
87
+ * builder.setMode('silent');
88
+ * builder.setClientConfig('your-tenant-id', 'your-client-id');
89
+ * ```
90
+ *
91
+ * // --- Token only mode (pre-obtained token, CI/CD) ---
92
+ * ```ts
93
+ * builder.setMode('token_only');
94
+ * builder.setAccessToken('your-access-token');
95
+ */
96
+ export interface IAuthConfigurator {
97
+ /**
98
+ * Sets the authentication mode for the module.
99
+ *
100
+ * @param mode - The authentication mode to use: 'token_only', 'silent', or 'interactive'.
101
+ *
102
+ * Consumers: Call this first to define the overall authentication strategy.
103
+ * Maintainers: Add new modes here if supporting additional auth flows.
104
+ */
105
+ setMode(mode: AuthConfig['mode']): void;
106
+ /**
107
+ * Sets the MSAL client instance for authentication.
108
+ *
109
+ * @param client - The MSAL PublicClientApplication instance to use for authentication.
110
+ *
111
+ * Consumers: Use this to provide a custom MSAL client if needed.
112
+ * Maintainers: Ensure compatibility with MSAL updates and custom client options.
113
+ */
114
+ setClient(client: AuthConfig['client']): void;
115
+ /**
116
+ * Configures the MSAL client using tenant and client IDs.
117
+ *
118
+ * @param tenantId - Azure AD tenant ID.
119
+ * @param clientId - Azure AD client/application ID.
120
+ *
121
+ * Consumers: Use this for quick setup without manually creating a client instance.
122
+ * Maintainers: Update this if the client configuration contract changes.
123
+ */
124
+ setClientConfig(tenantId: string, clientId: string): void;
125
+ /**
126
+ * Sets the port for the local server (used in interactive mode for auth callbacks).
127
+ *
128
+ * @param port - The port number for the local HTTP server.
129
+ *
130
+ * Consumers: Use this to avoid port conflicts or customize the callback endpoint.
131
+ * Maintainers: Ensure this is respected in server setup logic.
132
+ */
133
+ setServerPort(port: number): void;
134
+ /**
135
+ * Sets a callback to be invoked when the local server opens (interactive mode).
136
+ *
137
+ * @param onOpen - Callback receiving the server URL when ready, or undefined to disable.
138
+ *
139
+ * Consumers: Use this to display or log the login URL for users.
140
+ * Maintainers: Update this if the server startup flow changes.
141
+ */
142
+ setServerOnOpen(onOpen: ((url: string) => void) | undefined): void;
143
+ /**
144
+ * Sets a pre-obtained access token for token_only mode.
145
+ *
146
+ * @param token - The access token to use for authentication.
147
+ *
148
+ * Consumers: Use this for automation or CI/CD scenarios.
149
+ * Maintainers: Ensure this is securely handled and not mixed with other modes.
150
+ */
151
+ setAccessToken(token: string): void;
152
+ }
153
+ export {};
@@ -0,0 +1,81 @@
1
+ import type { AccountInfo, AuthenticationResult, PublicClientApplication } from '@azure/msal-node';
2
+ import type { IAuthProvider } from './AuthProvider.interface.js';
3
+ /**
4
+ * Implementation of the authentication provider for the Fusion MSAL Node module.
5
+ *
6
+ * Implements {@link IAuthProvider} and provides methods for managing authentication
7
+ * and token acquisition using the MSAL (Microsoft Authentication Library) for Node.js.
8
+ *
9
+ * This implementation assumes the user is already logged in and does not support
10
+ * triggering interactive login or logout flows. The `login` method will always throw, and `logout`
11
+ * only clears the token cache.
12
+ *
13
+ * @see AuthProviderInteractive For interactive login/logout support (user-driven authentication flows).
14
+ * @see AuthProviderTokenOnly For scenarios where a pre-obtained token is used (automation, CI/CD, etc).
15
+ *
16
+ * Developers extending this class can add support for additional authentication flows or modify token
17
+ * acquisition logic. Ensure that any changes remain consistent with the interface contract.
18
+ */
19
+ export declare class AuthProvider implements IAuthProvider {
20
+ protected _client: PublicClientApplication;
21
+ constructor(_client: PublicClientApplication);
22
+ /**
23
+ * Retrieves the first account from the list of all accounts available in the MSAL client.
24
+ *
25
+ * @returns A promise that resolves to the first `AccountInfo` object if available, or `null` if no accounts exist.
26
+ */
27
+ getAccount(): Promise<AccountInfo | null>;
28
+ /**
29
+ * Acquires an access token for the specified scopes.
30
+ *
31
+ * @param options - An object containing the options for acquiring the token.
32
+ * @param options.scopes - An array of strings representing the scopes for which the access token is requested.
33
+ * @returns A promise that resolves to the acquired access token as a string.
34
+ * @throws An error if the token acquisition process fails.
35
+ */
36
+ acquireAccessToken(options: {
37
+ scopes: string[];
38
+ }): Promise<string>;
39
+ /**
40
+ * Initiates the login process with the specified options.
41
+ *
42
+ * @param _options - An object containing the scopes required for authentication.
43
+ * @returns A promise that resolves to an `AuthenticationResult` upon successful login.
44
+ * @throws `AuthServerError` - Always throws this error as login is not supported in this implementation.
45
+ *
46
+ * @remarks
47
+ * This method is not supported and is intended to be overridden by `AuthProviderInteractive`.
48
+ */
49
+ login(_options: {
50
+ scopes: string[];
51
+ }): Promise<AuthenticationResult>;
52
+ /**
53
+ * Logs out the user by clearing the token cache and removing all accounts.
54
+ *
55
+ * This method retrieves all accounts from the token cache and removes them
56
+ * individually. Afterward, it clears the entire cache to ensure no residual
57
+ * authentication data remains.
58
+ *
59
+ * @returns A promise that resolves when the logout process is complete.
60
+ */
61
+ logout(): Promise<void>;
62
+ /**
63
+ * Acquires an authentication token for the specified scopes.
64
+ *
65
+ * This method first attempts to acquire a token silently using the accounts
66
+ * available in the token cache. If no accounts are found and interactive login
67
+ * is allowed, it initiates an interactive login flow. If interactive login is
68
+ * not allowed and no accounts are found, an error is thrown.
69
+ *
70
+ * @param scopes - An array of strings representing the scopes for which the token is requested.
71
+ * @param options - Optional parameters for token acquisition.
72
+ * @param options.interactive - A boolean indicating whether interactive login is allowed
73
+ * if no accounts are found in the cache. Defaults to `false`.
74
+ * @returns A promise that resolves to an `AuthenticationResult` containing the acquired token.
75
+ * @throws {@link NoAccountsError} If no accounts are found in the cache and interactive login is not allowed.
76
+ * @throws {@link SilentTokenAcquisitionError} If an error occurs during silent token acquisition.
77
+ */
78
+ acquireToken(options: {
79
+ scopes: string[];
80
+ }): Promise<AuthenticationResult>;
81
+ }
@@ -0,0 +1,55 @@
1
+ import type { AuthenticationResult } from '@azure/msal-node';
2
+ /**
3
+ * Interface for authentication providers in the Fusion MSAL Node module.
4
+ *
5
+ * Provides a unified API for authentication operations, including acquiring access tokens.
6
+ * The actual behavior of each method depends on the configured authentication mode (interactive, silent, or token-only).
7
+ *
8
+ * When accessing `appModules.msal.auth`, you will interact with this interface to:
9
+ * - Acquire Azure AD access tokens for specified scopes
10
+ *
11
+ * All methods are asynchronous and return Promises. See method documentation for details.
12
+ *
13
+ * @remarks
14
+ * This provider assumes the user is already logged in. To support login and logout operations, the module must be configured in interactive mode. In other modes, `login` and `logout` are present for compatibility but will be no-ops or throw if called.
15
+ *
16
+ * @see IAuthProviderInteractive for interactive login/logout support
17
+ */
18
+ export interface IAuthProvider {
19
+ /**
20
+ * This method is present for compatibility but will never trigger a user login flow unless interactive mode is configured.
21
+ *
22
+ * @param options - An object specifying the required scopes for authentication.
23
+ * @returns A Promise that will always reject or be a no-op, depending on implementation.
24
+ *
25
+ * @remarks
26
+ * This method is not supported and should not be used to initiate login unless interactive mode is enabled.
27
+ */
28
+ login(options: {
29
+ scopes: string[];
30
+ }): Promise<AuthenticationResult>;
31
+ /**
32
+ * This method is present for compatibility but will never trigger a user logout flow unless interactive mode is configured.
33
+ *
34
+ * @returns A Promise that will always resolve immediately or be a no-op, depending on implementation.
35
+ *
36
+ * @remarks
37
+ * This method is not supported and should not be used to initiate logout unless interactive mode is enabled.
38
+ */
39
+ logout(): Promise<void>;
40
+ /**
41
+ * Acquires an access token for the specified scopes.
42
+ *
43
+ * @param options - An object specifying the required scopes and an optional `interactive` flag.
44
+ * - `scopes`: The scopes for which the token is requested.
45
+ * - `interactive`: If true, may trigger an interactive login if silent acquisition fails (not supported unless interactive mode is enabled).
46
+ * @returns A Promise that resolves to a string representing the acquired access token.
47
+ *
48
+ * @remarks
49
+ * This is the primary method for obtaining tokens for API calls or resource access.
50
+ */
51
+ acquireAccessToken(options: {
52
+ scopes: string[];
53
+ interactive?: boolean;
54
+ }): Promise<string>;
55
+ }
@@ -0,0 +1,73 @@
1
+ import { type AuthenticationResult, type PublicClientApplication } from '@azure/msal-node';
2
+ import { AuthProvider } from './AuthProvider.js';
3
+ /**
4
+ * Options for configuring the interactive authentication provider.
5
+ *
6
+ * @property server - Configuration for the local server used to handle authentication callbacks.
7
+ * @property port - The port number on which the local server will listen for authentication responses.
8
+ * @property onOpen - Optional callback invoked with the authentication URL when the server is ready (e.g., to display or log the URL).
9
+ *
10
+ * Used when constructing an instance of {@link AuthProviderInteractive} to enable browser-based login flows.
11
+ */
12
+ type AuthProviderOptions = {
13
+ server: {
14
+ port: number;
15
+ onOpen?: (url: string) => void;
16
+ };
17
+ };
18
+ /**
19
+ * Implementation of an interactive authentication provider for the Fusion MSAL Node module.
20
+ *
21
+ * Extends {@link AuthProvider} to support user-driven authentication flows using the authorization code flow with PKCE.
22
+ * This class opens the user's default browser for authentication and handles the response via a local server.
23
+ *
24
+ * This implementation is intended for scenarios where interactive login is required, such as CLI tools or development utilities.
25
+ *
26
+ * Developers extending this provider can customize the interactive flow, server handling, or PKCE logic as needed.
27
+ * Ensure that any changes remain consistent with the expected interface and security best practices.
28
+ *
29
+ * @see AuthProvider for non-interactive (silent) authentication flows.
30
+ * @see AuthProviderTokenOnly for token-only scenarios.
31
+ */
32
+ export declare class AuthProviderInteractive extends AuthProvider {
33
+ #private;
34
+ constructor(client: PublicClientApplication, options: AuthProviderOptions);
35
+ /**
36
+ * Initiates the login process using the authorization code flow with PKCE.
37
+ *
38
+ * This method generates a PKCE code verifier and challenge to enhance security
39
+ * and prevent authorization code interception attacks. It constructs an
40
+ * authorization code URL, opens the default browser for user authentication,
41
+ * and starts a local server to handle the authentication response.
42
+ *
43
+ * @param scopes - An array of scopes that specify the permissions being requested.
44
+ * @returns A promise that resolves to an `AuthenticationResult` containing the
45
+ * authentication details upon successful login.
46
+ *
47
+ * @throws Will throw an error if the PKCE code generation, browser opening, or
48
+ * authentication server setup fails.
49
+ */
50
+ login(options: {
51
+ scopes: string[];
52
+ }): Promise<AuthenticationResult>;
53
+ /**
54
+ * Acquires an authentication token for the specified scopes.
55
+ *
56
+ * This method first attempts to acquire a token silently using the accounts
57
+ * available in the token cache. If no accounts are found and interactive login
58
+ * is allowed, it initiates an interactive login flow. If interactive login is
59
+ * not allowed and no accounts are found, an error is thrown.
60
+ *
61
+ * @param scopes - An array of strings representing the scopes for which the token is requested.
62
+ * @param options - Optional parameters for token acquisition.
63
+ * @param options.interactive - A boolean indicating whether interactive login is allowed
64
+ * if no accounts are found in the cache. Defaults to `false`.
65
+ * @returns A promise that resolves to an `AuthenticationResult` containing the acquired token.
66
+ * @throws {@link NoAccountsError} If no accounts are found in the cache and interactive login is not allowed.
67
+ * @throws {@link SilentTokenAcquisitionError} If an error occurs during silent token acquisition.
68
+ */
69
+ acquireToken(options: {
70
+ scopes: string[];
71
+ }): Promise<AuthenticationResult>;
72
+ }
73
+ export {};
@@ -0,0 +1,43 @@
1
+ import type { AuthenticationResult } from '@azure/msal-node';
2
+ import type { IAuthProvider } from './AuthProvider.interface.js';
3
+ /**
4
+ * Implementation of an authentication provider that supplies a static, pre-obtained access token.
5
+ *
6
+ * This class implements {@link IAuthProvider} and is intended for scenarios where authentication
7
+ * is handled externally and a token is provided directly (e.g., CI/CD pipelines, automation, or service accounts).
8
+ *
9
+ * Login and logout operations are not supported and will always throw errors if called.
10
+ *
11
+ * @see AuthProvider for silent authentication using cached accounts and MSAL flows.
12
+ * @see AuthProviderInteractive for interactive, user-driven authentication flows.
13
+ */
14
+ export declare class AuthTokenProvider implements IAuthProvider {
15
+ #private;
16
+ constructor(token: string);
17
+ /**
18
+ * Not supported in token-only mode. Always throws an error if called.
19
+ *
20
+ * This provider is designed for scenarios where authentication is handled externally
21
+ * and a static token is supplied. Login flows are not possible in this context.
22
+ *
23
+ * @throws Error Always throws to indicate login is not supported.
24
+ */
25
+ login(): Promise<AuthenticationResult>;
26
+ /**
27
+ * Not supported in token-only mode. Always throws an error if called.
28
+ *
29
+ * Since this provider does not manage user sessions or accounts, logout is not applicable.
30
+ *
31
+ * @throws Error Always throws to indicate logout is not supported.
32
+ */
33
+ logout(): Promise<void>;
34
+ /**
35
+ * Returns the pre-obtained access token supplied to the provider.
36
+ *
37
+ * This is the only supported operation for this provider. No token refresh or acquisition logic is performed.
38
+ *
39
+ * @returns The static access token as a string.
40
+ */
41
+ acquireAccessToken(): Promise<string>;
42
+ }
43
+ export default AuthTokenProvider;
@@ -0,0 +1,32 @@
1
+ import { PersistenceCachePlugin, type IPersistence } from '@azure/msal-node-extensions';
2
+ /**
3
+ * Creates a persistence cache for storing authentication data securely on disk.
4
+ *
5
+ * The cache is encrypted and scoped to the current user for security. It is uniquely identified
6
+ * by the provided tenant and client IDs, and is associated with the 'fusion-framework' service.
7
+ *
8
+ * @param tenantId - The Azure AD tenant ID used to identify the cache.
9
+ * @param clientId - The Azure AD client/application ID used to identify the cache.
10
+ * @returns A promise that resolves to the created persistence cache instance.
11
+ */
12
+ export declare const createPersistenceCache: (tenantId: string, clientId: string) => Promise<IPersistence>;
13
+ /**
14
+ * Clears the persistence cache for a specific tenant and client.
15
+ *
16
+ * Deletes the cache file and all associated authentication data for the given tenant and client IDs.
17
+ *
18
+ * @param tenantId - The Azure AD tenant ID.
19
+ * @param clientId - The Azure AD client/application ID.
20
+ * @returns A promise that resolves when the cache has been successfully cleared.
21
+ */
22
+ export declare const clearPersistenceCache: (tenantId: string, clientId: string) => Promise<void>;
23
+ /**
24
+ * Creates a `PersistenceCachePlugin` instance for use with MSAL, using the provided tenant and client IDs.
25
+ *
26
+ * This plugin enables MSAL to use the secure persistence cache for token storage.
27
+ *
28
+ * @param tenantId - The Azure AD tenant ID.
29
+ * @param clientId - The Azure AD client/application ID.
30
+ * @returns A promise that resolves to an instance of `PersistenceCachePlugin`.
31
+ */
32
+ export declare const createPersistenceCachePlugin: (tenantId: string, clientId: string) => Promise<PersistenceCachePlugin>;
@@ -0,0 +1,24 @@
1
+ import { PublicClientApplication } from '@azure/msal-node';
2
+ /**
3
+ * Creates and configures a new MSAL PublicClientApplication instance for Azure AD authentication.
4
+ *
5
+ * This function sets up the client with the specified tenant and client IDs, and attaches a secure
6
+ * persistence cache plugin for storing authentication tokens on disk. The resulting client can be used
7
+ * for both silent and interactive authentication flows, depending on how it is integrated.
8
+ *
9
+ * @param tenantId - The Azure Active Directory tenant ID (directory identifier).
10
+ * @param clientId - The client/application ID registered in Azure AD.
11
+ * @returns A Promise that resolves to a configured instance of `PublicClientApplication`.
12
+ *
13
+ * @remarks
14
+ * The returned client uses a secure, user-scoped cache for token storage. This is recommended for CLI tools,
15
+ * background services, and other Node.js applications that require persistent authentication.
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * const client = await createAuthClient('your-tenant-id', 'your-client-id');
20
+ * // Use the client with MSAL APIs for authentication flows
21
+ * ```
22
+ */
23
+ export declare const createAuthClient: (tenantId: string, clientId: string) => Promise<PublicClientApplication>;
24
+ export default createAuthClient;
@@ -0,0 +1,34 @@
1
+ import type { AuthenticationResult, PublicClientApplication } from '@azure/msal-node';
2
+ /**
3
+ * Creates a temporary HTTP server to handle the OAuth 2.0 authorization code flow for interactive authentication.
4
+ *
5
+ * This function is used in interactive authentication scenarios to listen for the authorization code
6
+ * returned by Azure AD after the user authenticates in the browser. It exchanges the code for an access token
7
+ * using the provided `PublicClientApplication` instance. The server automatically shuts down after a successful
8
+ * authentication, error, or timeout.
9
+ *
10
+ * @param client - The MSAL `PublicClientApplication` instance used to acquire tokens.
11
+ * @param scopes - An array of scopes for which the token is requested.
12
+ * @param options - Configuration for the authentication server.
13
+ * @param options.port - The port on which the server will listen for the authentication response.
14
+ * @param options.codeVerifier - The PKCE code verifier used for enhanced security (optional).
15
+ * @param options.timeout - Timeout in milliseconds before the server shuts down if no response is received (default: 5 minutes).
16
+ *
17
+ * @returns A promise that resolves with the `AuthenticationResult` upon successful authentication,
18
+ * or rejects with an error if authentication fails or times out.
19
+ *
20
+ * @throws {@link AuthServerError} If no authorization code is received or if token acquisition fails.
21
+ * @throws {@link AuthServerTimeoutError} If the server times out before receiving a response.
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * const result = await createAuthServer(client, ['user.read'], { port: 3000, codeVerifier });
26
+ * console.log(result.accessToken);
27
+ * ```
28
+ */
29
+ export declare const createAuthServer: (client: PublicClientApplication, scopes: string[], options: {
30
+ port: number;
31
+ codeVerifier?: string;
32
+ timeout?: number;
33
+ }) => Promise<AuthenticationResult>;
34
+ export default createAuthServer;
@@ -0,0 +1,24 @@
1
+ import type { IModuleConfigurator, IModulesConfigurator } from '@equinor/fusion-framework-module';
2
+ import { type MsalNodeModule } from './module';
3
+ /**
4
+ * Enables the MSAL Node module by registering its configuration with the provided modules configurator.
5
+ *
6
+ * This function should be called to add the MSAL Node authentication module to a Fusion Framework application.
7
+ * It accepts a configurator instance and a configuration function, which is used to define the module's behavior and settings.
8
+ *
9
+ * @param configurator - The modules configurator instance used to register the MSAL Node module.
10
+ * @param configure - A configuration function for customizing the module (e.g., setting client ID, tenant ID, mode).
11
+ *
12
+ * @see IAuthProvider for the authentication provider interface exposed by the module.
13
+ * @see IAuthConfigurator for the configuration builder interface used in the configure callback.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * enableModule(configurator, (builder) => {
18
+ * builder.setMode('interactive');
19
+ * builder.setClientConfig('your-tenant-id', 'your-client-id');
20
+ * });
21
+ * ```
22
+ */
23
+ export declare const enableModule: (configurator: IModulesConfigurator<any, any>, configure: IModuleConfigurator<MsalNodeModule>["configure"]) => void;
24
+ export default enableModule;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Error thrown when no accounts are available for an operation that requires one.
3
+ *
4
+ * Typically used when attempting to acquire a token or perform an action that requires
5
+ * a user account, but none are found in the MSAL cache.
6
+ *
7
+ * @param message - Description of the error.
8
+ * @param options - Optional error options, including a cause for error chaining.
9
+ */
10
+ export declare class NoAccountsError extends Error {
11
+ static readonly Name: string;
12
+ constructor(message: string, options?: {
13
+ cause?: unknown;
14
+ });
15
+ }
16
+ /**
17
+ * Error thrown when silent token acquisition fails.
18
+ *
19
+ * This error is used when MSAL cannot acquire a token silently, often due to missing
20
+ * credentials, expired tokens, or lack of a valid session.
21
+ *
22
+ * @param message - Description of the error.
23
+ * @param options - Optional error options, including a cause for error chaining.
24
+ */
25
+ export declare class SilentTokenAcquisitionError extends Error {
26
+ static readonly Name: string;
27
+ constructor(message: string, options?: {
28
+ cause?: unknown;
29
+ });
30
+ }
31
+ /**
32
+ * Error representing a failure or issue in the authentication server flow.
33
+ *
34
+ * Used to signal problems during the OAuth 2.0 authorization code flow, such as
35
+ * missing codes, invalid requests, or token exchange failures. Supports error chaining.
36
+ *
37
+ * @param message - Description of the error.
38
+ * @param options - Optional error options, including a cause for error chaining.
39
+ */
40
+ export declare class AuthServerError extends Error {
41
+ static readonly Name: string;
42
+ constructor(message: string, options?: {
43
+ cause?: Error;
44
+ });
45
+ }
46
+ /**
47
+ * Error thrown when the authentication server times out waiting for a response.
48
+ *
49
+ * Extends {@link AuthServerError} to provide additional context for timeout scenarios.
50
+ *
51
+ * @param message - Description of the error.
52
+ * @param options - Optional error options, including a cause for error chaining.
53
+ */
54
+ export declare class AuthServerTimeoutError extends AuthServerError {
55
+ static readonly Name: string;
56
+ constructor(message: string, options?: {
57
+ cause?: Error;
58
+ });
59
+ }
@@ -0,0 +1,5 @@
1
+ export { AuthProvider } from './AuthProvider.js';
2
+ export type { IAuthProvider } from './AuthProvider.interface.js';
3
+ export type { IAuthConfigurator, AuthConfig } from './AuthConfigurator.interface.js';
4
+ export { module as authModule, type MsalNodeModule } from './module.js';
5
+ export { enableModule as enableAuthModule } from './enable-module.js';
@@ -0,0 +1,24 @@
1
+ import type { Module } from '@equinor/fusion-framework-module';
2
+ import { AuthConfigurator } from './AuthConfigurator';
3
+ import type { IAuthProvider } from './AuthProvider.interface';
4
+ /**
5
+ * MSAL Node authentication module for the Fusion Framework.
6
+ *
7
+ * This module provides authentication capabilities for Node.js applications using Microsoft's MSAL library.
8
+ * It supports multiple authentication modes: token-only, interactive (browser-based), and silent (cached credentials).
9
+ *
10
+ * The module exposes a unified provider interface for acquiring tokens and managing authentication state.
11
+ *
12
+ * - In `token_only` mode, a static access token is used (see {@link AuthTokenProvider}).
13
+ * - In `interactive` mode, the user is prompted via a local server and browser (see {@link AuthProviderInteractive}).
14
+ * - In all other cases, silent authentication is attempted using cached credentials (see {@link AuthProvider}).
15
+ *
16
+ * @see AuthProvider
17
+ * @see AuthProviderInteractive
18
+ * @see AuthTokenProvider
19
+ * @see IAuthProvider
20
+ * @see AuthConfigurator
21
+ */
22
+ export type MsalNodeModule = Module<'auth', IAuthProvider, AuthConfigurator>;
23
+ export declare const module: MsalNodeModule;
24
+ export default module;
@@ -0,0 +1 @@
1
+ export declare const version = "0.1.0";
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@equinor/fusion-framework-module-msal-node",
3
+ "version": "0.1.0",
4
+ "description": "Fusion Framework module for secure Azure AD authentication in Node.js using MSAL. Supports interactive, silent, and token-only authentication modes with encrypted token storage.",
5
+ "main": "dist/esm/index.js",
6
+ "types": "dist/types/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/esm/index.js",
10
+ "types": "./dist/types/index.d.ts"
11
+ },
12
+ "./error": {
13
+ "import": "./dist/esm/error.js",
14
+ "types": "./dist/types/error.d.ts"
15
+ }
16
+ },
17
+ "keywords": [
18
+ "msal",
19
+ "msal-node",
20
+ "node",
21
+ "auth",
22
+ "authentication"
23
+ ],
24
+ "author": "",
25
+ "license": "ISC",
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/equinor/fusion-framework.git",
32
+ "directory": "packages/modules/msal-node"
33
+ },
34
+ "dependencies": {
35
+ "@azure/msal-node": "^3.5.1",
36
+ "@azure/msal-node-extensions": "^1.5.11",
37
+ "open": "^10.1.1",
38
+ "@equinor/fusion-framework-module": "^4.4.2"
39
+ },
40
+ "devDependencies": {
41
+ "typescript": "^5.8.2"
42
+ },
43
+ "scripts": {
44
+ "build": "tsc -b"
45
+ }
46
+ }