@equinor/fusion-framework-module-msal 10.0.2 → 11.0.0-next.1

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 (82) hide show
  1. package/CHANGELOG.md +175 -0
  2. package/README.md +17 -368
  3. package/dist/esm/MsalConfigurator.js +176 -88
  4. package/dist/esm/MsalConfigurator.js.map +1 -1
  5. package/dist/esm/__tests__/MsalConfigurator.test.js +75 -0
  6. package/dist/esm/__tests__/MsalConfigurator.test.js.map +1 -1
  7. package/dist/esm/__tests__/create-proxy-provider.test.js +53 -0
  8. package/dist/esm/__tests__/create-proxy-provider.test.js.map +1 -0
  9. package/dist/esm/__tests__/mock/create-mock-user-from-token.test.js +39 -0
  10. package/dist/esm/__tests__/mock/create-mock-user-from-token.test.js.map +1 -0
  11. package/dist/esm/__tests__/mock/msal-mock.test.js +451 -0
  12. package/dist/esm/__tests__/mock/msal-mock.test.js.map +1 -0
  13. package/dist/esm/index.js +5 -0
  14. package/dist/esm/index.js.map +1 -1
  15. package/dist/esm/mock/MsalMockClient.js +484 -0
  16. package/dist/esm/mock/MsalMockClient.js.map +1 -0
  17. package/dist/esm/mock/MsalMockConfigurator.js +239 -0
  18. package/dist/esm/mock/MsalMockConfigurator.js.map +1 -0
  19. package/dist/esm/mock/create-mock-token.js +60 -0
  20. package/dist/esm/mock/create-mock-token.js.map +1 -0
  21. package/dist/esm/mock/create-mock-user-from-token.js +40 -0
  22. package/dist/esm/mock/create-mock-user-from-token.js.map +1 -0
  23. package/dist/esm/mock/create-msal-mock-client.js +20 -0
  24. package/dist/esm/mock/create-msal-mock-client.js.map +1 -0
  25. package/dist/esm/mock/decode-jwt-segment.js +22 -0
  26. package/dist/esm/mock/decode-jwt-segment.js.map +1 -0
  27. package/dist/esm/mock/index.js +31 -0
  28. package/dist/esm/mock/index.js.map +1 -0
  29. package/dist/esm/mock/module.js +38 -0
  30. package/dist/esm/mock/module.js.map +1 -0
  31. package/dist/esm/msal-config-schema.js +37 -0
  32. package/dist/esm/msal-config-schema.js.map +1 -0
  33. package/dist/esm/telemetry-config-schema.js +16 -0
  34. package/dist/esm/telemetry-config-schema.js.map +1 -0
  35. package/dist/esm/version.js +1 -1
  36. package/dist/esm/version.js.map +1 -1
  37. package/dist/esm/versioning/resolve-version.js +0 -1
  38. package/dist/esm/versioning/resolve-version.js.map +1 -1
  39. package/dist/tsconfig.tsbuildinfo +1 -1
  40. package/dist/types/MsalConfigurator.d.ts +96 -20
  41. package/dist/types/__tests__/create-proxy-provider.test.d.ts +1 -0
  42. package/dist/types/__tests__/mock/create-mock-user-from-token.test.d.ts +1 -0
  43. package/dist/types/__tests__/mock/msal-mock.test.d.ts +1 -0
  44. package/dist/types/index.d.ts +5 -0
  45. package/dist/types/mock/MsalMockClient.d.ts +282 -0
  46. package/dist/types/mock/MsalMockConfigurator.d.ts +186 -0
  47. package/dist/types/mock/create-mock-token.d.ts +54 -0
  48. package/dist/types/mock/create-mock-user-from-token.d.ts +25 -0
  49. package/dist/types/mock/create-msal-mock-client.d.ts +14 -0
  50. package/dist/types/mock/decode-jwt-segment.d.ts +13 -0
  51. package/dist/types/mock/index.d.ts +30 -0
  52. package/dist/types/mock/module.d.ts +35 -0
  53. package/dist/types/msal-config-schema.d.ts +64 -0
  54. package/dist/types/telemetry-config-schema.d.ts +8 -0
  55. package/dist/types/version.d.ts +1 -1
  56. package/docs/api-reference.md +85 -0
  57. package/docs/auth-code-flow.md +86 -0
  58. package/docs/migration-v2-to-v4.md +115 -0
  59. package/docs/testing.md +191 -0
  60. package/docs/troubleshooting.md +17 -0
  61. package/docs/version-management.md +67 -0
  62. package/package.json +12 -5
  63. package/src/MsalConfigurator.ts +202 -115
  64. package/src/__tests__/MsalConfigurator.test.ts +106 -0
  65. package/src/__tests__/create-proxy-provider.test.ts +77 -0
  66. package/src/__tests__/mock/create-mock-user-from-token.test.ts +46 -0
  67. package/src/__tests__/mock/msal-mock.test.ts +613 -0
  68. package/src/index.ts +6 -0
  69. package/src/mock/MsalMockClient.ts +618 -0
  70. package/src/mock/MsalMockConfigurator.ts +305 -0
  71. package/src/mock/create-mock-token.ts +92 -0
  72. package/src/mock/create-mock-user-from-token.ts +46 -0
  73. package/src/mock/create-msal-mock-client.ts +25 -0
  74. package/src/mock/decode-jwt-segment.ts +22 -0
  75. package/src/mock/index.ts +30 -0
  76. package/src/mock/module.ts +54 -0
  77. package/src/msal-config-schema.ts +81 -0
  78. package/src/telemetry-config-schema.ts +25 -0
  79. package/src/version.ts +1 -1
  80. package/src/versioning/resolve-version.ts +0 -1
  81. package/vitest.config.ts +1 -1
  82. package/.changeset/msal-auth-provider-fix.md +0 -7
@@ -0,0 +1,191 @@
1
+ # MSAL — test double
2
+
3
+ Authenticate in-process instead of against Entra ID.
4
+
5
+ ```typescript
6
+ import { enableMsalMock } from '@equinor/fusion-framework-module-msal/mock';
7
+
8
+ enableMsalMock(configurator);
9
+ ```
10
+
11
+ Import path: `@equinor/fusion-framework-module-msal/mock`. The entry point has **no test-runner dependency**.
12
+
13
+ ## What is substituted
14
+
15
+ > [!IMPORTANT]
16
+ > Only `IMsalClient` — the object that would contact Entra ID. The real `MsalConfigurator`, the real `MsalProvider` and the real schema validation all still run.
17
+
18
+ That distinction is the point. Scope resolution, silent-first token acquisition, account handling, proxy providers and telemetry stay on the production code path, so a test observes real provider behaviour:
19
+
20
+ ```typescript
21
+ const fusion = await mockFramework((configurator) => {
22
+ // the client is configured with *what it talks to*, exactly as in production
23
+ configurator.msal.setClientConfig({ auth: { clientId: 'my-app', tenantId: 'my-tenant' } });
24
+ });
25
+
26
+ const token = await fusion.modules.auth.acquireAccessToken();
27
+ // scope is 'my-app/.default' — resolved by the real provider, not by the test double
28
+ ```
29
+
30
+ A double that replaced the provider would have skipped that logic and reported whatever it was told to.
31
+
32
+ ## Defaults
33
+
34
+ A user named `Test User` is signed in. Tokens are real JWTs, minted in-process with a fixed issue time, so they are identical across runs and machines and can be compared or snapshotted directly.
35
+
36
+ When no client configuration is declared, a stand-in one is used, so an application boots under test without credentials it does not have.
37
+
38
+ ## Choosing the signed-in user
39
+
40
+ `MsalMockClient` takes the same `MsalClientConfig` the real `MsalClient` takes — a client configuration has no notion of who is signed in, so the user is declared separately and signed in on the client as it is built:
41
+
42
+ ```typescript
43
+ import { enableMsalMock } from '@equinor/fusion-framework-module-msal/mock';
44
+
45
+ enableMsalMock(configurator, (builder) => {
46
+ builder.setAccount({ name: 'Ada Lovelace', username: 'ada@equinor.com' });
47
+ });
48
+ ```
49
+
50
+ Pass `null` when nobody is signed in:
51
+
52
+ ```typescript
53
+ enableMsalMock(configurator, (builder) => {
54
+ builder.setAccount(null);
55
+ });
56
+ ```
57
+
58
+ `setAccount` also takes an ordinary config-builder callback, resolved while the configuration is assembled and handed the same arguments every other builder callback receives:
59
+
60
+ ```typescript
61
+ enableMsalMock(configurator, (builder) => {
62
+ builder.setAccount(async ({ hasModule }) => ({
63
+ name: hasModule('app') ? 'App User' : 'Portal User',
64
+ }));
65
+ });
66
+ ```
67
+
68
+ The user is in place **before** `MsalProvider.initialize()` runs, so the provider's own start-up path acts on it. Combined with `setRequiresAuth(true)`, a test observes the real automatic login rather than a state assigned after the fact.
69
+
70
+ `setClient` replaces the client, but not the rule: the declared user is signed in on whichever client the module authenticates through, so a mock client supplied that way receives it too.
71
+
72
+ ## Returning an exact token
73
+
74
+ Most tests only care who is signed in and let the mock fabricate a token from that user's fields. When a backend mock validates the token itself — specific claims, an audience, or a signature — it needs to see the exact token it expects instead:
75
+
76
+ ```typescript
77
+ enableMsalMock(configurator, (builder) => {
78
+ builder.setToken(token);
79
+ });
80
+ ```
81
+
82
+ `setToken` also signs in the user the token's claims describe, via `createMockUserFromToken` — so `acquireAccessToken` returns this token, and the account APIs agree with it. Pass `true` as the second argument to keep a separately declared account instead:
83
+
84
+ ```typescript
85
+ enableMsalMock(configurator, (builder) => {
86
+ builder.setAccount({ name: 'Ada Lovelace' }).setToken(token, true);
87
+ });
88
+ ```
89
+
90
+ | Option | Default | Purpose |
91
+ | --- | --- | --- |
92
+ | `name` | `Test User` | Display name |
93
+ | `username` | `test.user@equinor.com` | UPN / email |
94
+ | `userId` | `fusion-mock-user` | Object ID |
95
+ | `tenantId` | the client's configured tenant | Tenant |
96
+ | `scopes` | `fusion-mock-scope` | Granted when a request specifies none |
97
+ | `account` | derived | A preconfigured `AccountInfo` to use outright |
98
+ | `signedOut` | `false` | Start without a signed-in user |
99
+
100
+ The client tokens are issued for comes from the client configuration (`setClientConfig`), not from the user.
101
+
102
+ ### Why the user is signed in at construction
103
+
104
+ The account is put in the client's cache as the client is built — before the provider exists. That reproduces the production shape of a returning user with a live session: the provider finds an account already there and takes the branch it takes in the browser.
105
+
106
+ Assigning the account **after** `MsalProvider.initialize()` — from an `onInitialized` hook, say — looks equivalent but is not. `initialize()` exchanges an auth code, calls `handleRedirect()` and, when `requiresAuth` is set, performs an automatic login. A late assignment silently overwrites all of that, so a test asserting on the sign-in journey would be observing its own assignment rather than the framework.
107
+
108
+ ## Changing the user between tests
109
+
110
+ When a suite shares one framework instance but needs a different user per test, set the active account directly:
111
+
112
+ ```typescript
113
+ beforeEach(() => {
114
+ fusion.modules.auth.client.setActiveAccount(account);
115
+ });
116
+ ```
117
+
118
+ The mock keeps a real account cache, so this behaves the way MSAL does: `getActiveAccount`, `getAllAccounts` and `getAccount(filter)` all agree afterwards. Unlike real MSAL, an account that was never issued by a sign-in is accepted and added to the cache, which is what makes the one-liner above possible.
119
+
120
+ Signing out (`logout`, `logoutPopup`, `logoutRedirect`) removes the account from the cache rather than merely deactivating it, as MSAL does.
121
+
122
+ ## Running inside a host application
123
+
124
+ When the module is hoisted onto a host application's provider — an app inside a portal — no client is built. The app authenticates through the host's client, exactly as in production.
125
+
126
+ A user declared with `setAccount` is still honoured: it is signed in on the **host's** client, because that is the client the app authenticates through. The alternative would be for `setAccount` to silently do nothing precisely when an app is being tested inside a portal.
127
+
128
+ The session is shared, so this changes who the host sees signed in too — as it does in production. If the host does not authenticate through a mock client, `setAccount` throws rather than failing quietly.
129
+
130
+ ## Testing signed-out behaviour
131
+
132
+ Both `null` and `signedOut: true` start without a session. Silent flows then resolve empty so the provider follows its unauthenticated path, while an explicit login still succeeds — which lets a test drive the sign-in journey rather than only its end state.
133
+
134
+ They differ in what the login resolves to. `null` forgets the identity, so a login produces the default user:
135
+
136
+ ```typescript
137
+ builder.setAccount(null);
138
+ ```
139
+
140
+ `signedOut: true` keeps it, so a login produces the user the test named — which is what to reach for when the assertion is about *who* signed in:
141
+
142
+ ```typescript
143
+ builder.setAccount({ name: 'Ada Lovelace', signedOut: true });
144
+ ```
145
+
146
+ ## Mocking an individual call
147
+
148
+ > [!IMPORTANT]
149
+ > That is your test runner's job. This module ships **no mocking API**.
150
+
151
+ The mock client is a plain class with ordinary methods, so `vi.spyOn`, `bun:test`'s `spyOn` and Node's `t.mock.method` all work on it directly — with their own call assertions, argument matchers and reset semantics, which a framework-specific API would not give you.
152
+
153
+ The provider exposes the client it authenticates through, so a spy has a stable target:
154
+
155
+ ```typescript
156
+ vi.spyOn(fusion.modules.auth.client, 'acquireToken').mockResolvedValue(result);
157
+
158
+ afterEach(() => vi.restoreAllMocks());
159
+ ```
160
+
161
+ ## Minting a token directly
162
+
163
+ For code that only needs a token — an HTTP interceptor test, say — skip the client:
164
+
165
+ ```typescript
166
+ import { createMockToken } from '@equinor/fusion-framework-module-msal/mock';
167
+
168
+ const token = createMockToken({ oid: 'fusion-mock-user' });
169
+ ```
170
+
171
+ To use a generated token as the signed-in user for `ffc app dev --mock`, `ffc app serve --mock`,
172
+ or a Vite SPA, see
173
+ [Generate a mock user and update `.env`](../../../vite-plugins/spa/README.md#generate-a-mock-user-and-update-env).
174
+ That workflow documents persistent mock-auth configuration, identity claims, and custom `scp`
175
+ token scopes.
176
+
177
+ ## Exports
178
+
179
+ | Export | Purpose |
180
+ | --- | --- |
181
+ | `enableMsalMock(configurator, configure?)` | Register the module with an in-process client |
182
+ | `msalMockModule` | The module itself, for manual registration |
183
+ | `MsalMockConfigurator` | The real configurator, backed by an in-process client |
184
+ | `MsalMockClient(config)` | The in-process client, taking the same `MsalClientConfig` as `MsalClient` |
185
+ | `createMsalMockClient(config, user?)` | Convenience alias for `new MsalMockClient(config)` |
186
+ | `createMockToken(claims?)` | Mint a deterministic JWT |
187
+
188
+ ## Related
189
+
190
+ - [Module README](../README.md) — production configuration
191
+ - [`@equinor/fusion-framework/mock`](../../../framework/docs/testing.md) — mock every framework boundary at once
@@ -0,0 +1,17 @@
1
+ # Troubleshooting
2
+
3
+ ## Common Issues
4
+
5
+ | Issue | Solution |
6
+ |-------|----------|
7
+ | **Authentication Loop** | Ensure redirect URIs match your application's routing |
8
+ | **Token Acquisition Fails** | Check that required scopes are properly configured |
9
+ | **Module Not Found** | Ensure the module is properly configured and framework is initialized |
10
+ | **Multiple MSAL Instances** | Remove duplicate configurations from child modules |
11
+ | **Redirect Returns Void** | For redirect flows, use `handleRedirect()` after navigation completes |
12
+ | **Token Empty/Undefined** | Verify user is authenticated and scopes are correct |
13
+
14
+ ## Getting Help
15
+
16
+ - 📖 [MSAL Cookbook](https://github.com/equinor/fusion-framework/tree/main/cookbooks/app-react-msal) - Complete working examples
17
+ - 🐛 [Report Issues](https://github.com/equinor/fusion/issues) - Bug reports and feature requests
@@ -0,0 +1,67 @@
1
+ # Version Management
2
+
3
+ The MSAL module includes built-in version checking to ensure compatibility between different MSAL library versions.
4
+
5
+ ## Version Resolution
6
+
7
+ ```typescript
8
+ import { resolveVersion, VersionError } from '@equinor/fusion-framework-module-msal/versioning';
9
+
10
+ // Resolve and validate a version
11
+ const result = resolveVersion('2.0.0');
12
+ console.log(result.isLatest); // false
13
+ console.log(result.satisfiesLatest); // true
14
+ console.log(result.enumVersion); // MsalModuleVersion.V2
15
+ ```
16
+
17
+ ## Version Checking Behavior
18
+
19
+ - **Major Version Incompatibility**: Throws `VersionError` if requested major version is greater than latest
20
+ - **Minor Version Mismatch**: Logs warning but allows execution
21
+ - **Patch Differences**: Ignored for compatibility
22
+ - **Invalid Versions**: Throws `VersionError` with descriptive message
23
+
24
+ ## API Reference
25
+
26
+ ### `resolveVersion(version: string | SemVer): ResolvedVersion`
27
+
28
+ Resolves and validates a version string against the latest available MSAL version.
29
+
30
+ **Parameters:**
31
+ - `version` - Version string or SemVer object to resolve
32
+
33
+ **Returns:** `ResolvedVersion` object containing:
34
+ - `wantedVersion: SemVer` - The parsed requested version
35
+ - `latestVersion: SemVer` - The latest available version
36
+ - `isLatest: boolean` - Whether the version is exactly the latest
37
+ - `satisfiesLatest: boolean` - Whether the major version matches latest
38
+ - `enumVersion: MsalModuleVersion` - Corresponding enum version
39
+
40
+ **Throws:** `VersionError` for invalid or incompatible versions
41
+
42
+ ### `VersionError`
43
+
44
+ Error class for version-related issues with the following types:
45
+ - `InvalidVersion` - Requested version is not a valid semver
46
+ - `InvalidLatestVersion` - Latest version parsing failed (build issue)
47
+ - `MajorIncompatibility` - Major version is greater than latest
48
+ - `MinorMismatch` - Minor version differs (warning only)
49
+ - `PatchDifference` - Patch version differs (info only)
50
+ - `IncompatibleVersion` - General incompatibility
51
+
52
+ ## Error Handling
53
+
54
+ ```typescript
55
+ import { resolveVersion, VersionError } from '@equinor/fusion-framework-module-msal/versioning';
56
+
57
+ try {
58
+ const result = resolveVersion('3.0.0'); // Assuming latest is 2.x
59
+ } catch (error) {
60
+ if (error instanceof VersionError) {
61
+ console.error('Version error:', error.message);
62
+ console.error('Requested:', error.requestedVersion);
63
+ console.error('Latest:', error.latestVersion);
64
+ console.error('Type:', error.type);
65
+ }
66
+ }
67
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@equinor/fusion-framework-module-msal",
3
- "version": "10.0.2",
3
+ "version": "11.0.0-next.1",
4
4
  "description": "Microsoft Authentication Library (MSAL) integration module for Fusion Framework",
5
5
  "main": "dist/esm/index.js",
6
6
  "types": "dist/types/index.d.ts",
@@ -16,6 +16,10 @@
16
16
  "./v4": {
17
17
  "import": "./dist/esm/v4/index.js",
18
18
  "types": "./dist/types/v4/index.d.ts"
19
+ },
20
+ "./mock": {
21
+ "import": "./dist/esm/mock/index.js",
22
+ "types": "./dist/types/mock/index.d.ts"
19
23
  }
20
24
  },
21
25
  "typesVersions": {
@@ -28,6 +32,9 @@
28
32
  ],
29
33
  "v4": [
30
34
  "dist/types/v4/index.d.ts"
35
+ ],
36
+ "mock": [
37
+ "dist/types/mock/index.d.ts"
31
38
  ]
32
39
  }
33
40
  },
@@ -50,16 +57,16 @@
50
57
  "semver": "^7.7.4",
51
58
  "typescript": "^7.0.2",
52
59
  "zod": "^4.4.3",
53
- "@equinor/fusion-framework-module": "^6.1.1",
54
- "@equinor/fusion-framework-module-telemetry": "^7.0.1"
60
+ "@equinor/fusion-framework-module": "^6.1.3-next.0",
61
+ "@equinor/fusion-framework-module-telemetry": "^8.0.0-next.1"
55
62
  },
56
63
  "peerDependencies": {
57
64
  "@types/semver": "^7.0.0",
58
65
  "semver": "^7.0.0",
59
66
  "typescript": ">=5.0.0",
60
67
  "zod": "^4.0.0",
61
- "@equinor/fusion-framework-module": "^6.1.1",
62
- "@equinor/fusion-framework-module-telemetry": "^7.0.1"
68
+ "@equinor/fusion-framework-module": "^6.1.3-next.0",
69
+ "@equinor/fusion-framework-module-telemetry": "^8.0.0-next.1"
63
70
  },
64
71
  "peerDependenciesMeta": {
65
72
  "@equinor/fusion-framework-module-telemetry": {