@equinor/fusion-framework-module-msal 11.0.0 → 11.0.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.
- package/dist/esm/version.js +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/version.d.ts +1 -1
- package/package.json +8 -5
- package/CHANGELOG.md +0 -1212
- package/docs/api-reference.md +0 -85
- package/docs/auth-code-flow.md +0 -86
- package/docs/migration-v2-to-v4.md +0 -115
- package/docs/testing.md +0 -191
- package/docs/troubleshooting.md +0 -17
- package/docs/version-management.md +0 -67
- package/src/MsalClient.interface.ts +0 -139
- package/src/MsalClient.ts +0 -326
- package/src/MsalConfigurator.ts +0 -486
- package/src/MsalProvider.interface.ts +0 -179
- package/src/MsalProvider.ts +0 -776
- package/src/MsalProxyProvider.interface.ts +0 -72
- package/src/__tests__/MsalConfigurator.test.ts +0 -222
- package/src/__tests__/MsalProvider.test.ts +0 -74
- package/src/__tests__/create-proxy-provider.test.ts +0 -77
- package/src/__tests__/mock/create-mock-user-from-token.test.ts +0 -46
- package/src/__tests__/mock/msal-mock.test.ts +0 -613
- package/src/__tests__/versioning/resolve-version.test.ts +0 -161
- package/src/create-client-log-callback.ts +0 -102
- package/src/create-proxy-provider.ts +0 -97
- package/src/index.ts +0 -48
- package/src/mock/MsalMockClient.ts +0 -618
- package/src/mock/MsalMockConfigurator.ts +0 -305
- package/src/mock/create-mock-token.ts +0 -92
- package/src/mock/create-mock-user-from-token.ts +0 -46
- package/src/mock/create-msal-mock-client.ts +0 -25
- package/src/mock/decode-jwt-segment.ts +0 -22
- package/src/mock/index.ts +0 -30
- package/src/mock/module.ts +0 -54
- package/src/module.ts +0 -142
- package/src/msal-config-schema.ts +0 -81
- package/src/static.ts +0 -38
- package/src/telemetry-config-schema.ts +0 -25
- package/src/types.ts +0 -16
- package/src/util/compare-origin.ts +0 -18
- package/src/util/normalize-uri.ts +0 -24
- package/src/util/redirect.ts +0 -19
- package/src/v2/IAuthClient.interface.ts +0 -114
- package/src/v2/Logger.ts +0 -204
- package/src/v2/MsalProvider.interface.ts +0 -102
- package/src/v2/create-proxy-client.ts +0 -195
- package/src/v2/create-proxy-provider.ts +0 -177
- package/src/v2/map-account-info.ts +0 -23
- package/src/v2/map-authentication-result.ts +0 -28
- package/src/v2/types.ts +0 -674
- package/src/v4/create-proxy-provider.ts +0 -75
- package/src/v4/index.ts +0 -13
- package/src/v4/types.ts +0 -727
- package/src/version.ts +0 -2
- package/src/versioning/VersionError.ts +0 -64
- package/src/versioning/index.ts +0 -29
- package/src/versioning/resolve-version.ts +0 -154
- package/src/versioning/types.ts +0 -60
- package/tsconfig.json +0 -18
- package/vitest.config.ts +0 -11
package/docs/api-reference.md
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
# MSAL API Reference
|
|
2
|
-
|
|
3
|
-
## `enableMSAL(configurator, configure?)`
|
|
4
|
-
|
|
5
|
-
Enables the MSAL module in your Fusion Framework application.
|
|
6
|
-
|
|
7
|
-
**Parameters:**
|
|
8
|
-
- `configurator`: `IModulesConfigurator` - The modules configurator instance
|
|
9
|
-
- `configure?`: `(builder: { setClientConfig, setRequiresAuth }) => void` - Optional configuration function
|
|
10
|
-
|
|
11
|
-
**Returns:** `void`
|
|
12
|
-
|
|
13
|
-
**Example:**
|
|
14
|
-
```typescript
|
|
15
|
-
enableMSAL(configurator, (builder) => {
|
|
16
|
-
builder.setClientConfig({ auth: { clientId: '...', tenantId: '...' } });
|
|
17
|
-
builder.setRequiresAuth(true);
|
|
18
|
-
});
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
## Type Definitions
|
|
22
|
-
|
|
23
|
-
### `LoginOptions`
|
|
24
|
-
|
|
25
|
-
```typescript
|
|
26
|
-
type LoginOptions = {
|
|
27
|
-
request: PopupRequest | RedirectRequest; // MSAL request object
|
|
28
|
-
behavior?: 'popup' | 'redirect'; // Auth method (default: 'redirect')
|
|
29
|
-
silent?: boolean; // Attempt silent auth first (default: true)
|
|
30
|
-
};
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
### `LogoutOptions`
|
|
34
|
-
|
|
35
|
-
```typescript
|
|
36
|
-
type LogoutOptions = {
|
|
37
|
-
redirectUri?: string; // Redirect after logout
|
|
38
|
-
account?: AccountInfo; // Account to logout (defaults to active)
|
|
39
|
-
};
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
### `AcquireTokenOptions`
|
|
43
|
-
|
|
44
|
-
```typescript
|
|
45
|
-
type AcquireTokenOptions = {
|
|
46
|
-
request: PopupRequest | RedirectRequest; // MSAL request with scopes
|
|
47
|
-
behavior?: 'popup' | 'redirect'; // Auth method (default: 'redirect')
|
|
48
|
-
silent?: boolean; // Attempt silent first (default: true if account available)
|
|
49
|
-
};
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
## `IMsalProvider`
|
|
53
|
-
|
|
54
|
-
The authentication provider interface available at `framework.auth`:
|
|
55
|
-
|
|
56
|
-
```typescript
|
|
57
|
-
interface IMsalProvider {
|
|
58
|
-
// The MSAL PublicClientApplication instance
|
|
59
|
-
readonly client: IMsalClient;
|
|
60
|
-
|
|
61
|
-
// Current user account information
|
|
62
|
-
readonly account: AccountInfo | null;
|
|
63
|
-
|
|
64
|
-
// Initialize the MSAL provider
|
|
65
|
-
initialize(): Promise<void>;
|
|
66
|
-
|
|
67
|
-
// Acquire an access token for the specified scopes
|
|
68
|
-
acquireAccessToken(options: AcquireTokenOptionsLegacy): Promise<string | undefined>;
|
|
69
|
-
|
|
70
|
-
// Acquire full authentication result
|
|
71
|
-
acquireToken(options: AcquireTokenOptionsLegacy): Promise<AcquireTokenResult>;
|
|
72
|
-
|
|
73
|
-
// Login user interactively
|
|
74
|
-
login(options: LoginOptions): Promise<LoginResult>;
|
|
75
|
-
|
|
76
|
-
// Logout user (returns boolean)
|
|
77
|
-
logout(options?: LogoutOptions): Promise<boolean>;
|
|
78
|
-
|
|
79
|
-
// Handle authentication redirect (returns AuthenticationResult | null)
|
|
80
|
-
handleRedirect(): Promise<AuthenticationResult | null>;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Note: defaultAccount and other deprecated v2 properties are available only
|
|
84
|
-
// when using a v2-compatible proxy via createProxyProvider()
|
|
85
|
-
```
|
package/docs/auth-code-flow.md
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
# Backend-Issued Auth Code Flow
|
|
2
|
-
|
|
3
|
-
Enable automatic sign-in using a backend-issued authorization code without interactive login prompts.
|
|
4
|
-
|
|
5
|
-
## Overview
|
|
6
|
-
|
|
7
|
-
When your backend authenticates a user and generates a short-lived SPA auth code, the MSAL module can exchange it for tokens during initialization, eliminating double-login issues and providing seamless authentication.
|
|
8
|
-
|
|
9
|
-
## Usage
|
|
10
|
-
|
|
11
|
-
```typescript
|
|
12
|
-
import { enableMSAL } from '@equinor/fusion-framework-module-msal';
|
|
13
|
-
|
|
14
|
-
enableMSAL(configurator, (builder) => {
|
|
15
|
-
builder.setClientConfig({
|
|
16
|
-
auth: {
|
|
17
|
-
clientId: 'your-client-id',
|
|
18
|
-
tenantId: 'your-tenant-id'
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
// Backend injects auth code as window.MSAL_AUTH_CODE during initial first page load
|
|
23
|
-
// This is the most secure approach - only available on first render, cleared after use
|
|
24
|
-
if (typeof window !== 'undefined' && window.MSAL_AUTH_CODE) {
|
|
25
|
-
builder.setAuthCode(window.MSAL_AUTH_CODE);
|
|
26
|
-
delete (window as any).MSAL_AUTH_CODE; // Clear after consuming
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
builder.setRequiresAuth(true);
|
|
30
|
-
});
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
## How It Works
|
|
34
|
-
|
|
35
|
-
1. Backend authenticates user and generates short-lived auth code
|
|
36
|
-
2. Frontend passes auth code to MSAL: `builder.setAuthCode(authCode)`
|
|
37
|
-
3. During `initialize()`: auth code exchanged for tokens (before `requiresAuth` check)
|
|
38
|
-
4. Tokens cached by MSAL → user automatically signed in
|
|
39
|
-
5. Falls back to standard MSAL flows on exchange failure
|
|
40
|
-
|
|
41
|
-
## API: `setAuthCode(authCode?: string)`
|
|
42
|
-
|
|
43
|
-
Sets backend-issued auth code for token exchange during initialization.
|
|
44
|
-
|
|
45
|
-
Pass `undefined` to clear/reset a previously configured auth code.
|
|
46
|
-
|
|
47
|
-
**Returns:** configurator instance (chainable)
|
|
48
|
-
|
|
49
|
-
**Behavior:**
|
|
50
|
-
- Exchange happens before `requiresAuth` check
|
|
51
|
-
- On success: user auto-authenticated, no login prompt
|
|
52
|
-
- On failure: falls back to standard MSAL login
|
|
53
|
-
- Auth code cleared after exchange (no reuse)
|
|
54
|
-
- `setAuthCode(undefined)` clears configured auth code
|
|
55
|
-
- `setAuthCode('')` is treated as absent auth code
|
|
56
|
-
- `setAuthCode(' ')` is trimmed and treated as absent auth code
|
|
57
|
-
- No auth-code exchange is attempted when auth code is absent/cleared
|
|
58
|
-
|
|
59
|
-
**Example:**
|
|
60
|
-
|
|
61
|
-
```typescript
|
|
62
|
-
// Best practice: Backend injects auth code on initial page load as window.MSAL_AUTH_CODE
|
|
63
|
-
if (typeof window !== 'undefined' && window.MSAL_AUTH_CODE) {
|
|
64
|
-
builder.setAuthCode(window.MSAL_AUTH_CODE);
|
|
65
|
-
delete (window as any).MSAL_AUTH_CODE; // Clear after consuming to prevent reuse
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// Clear/reset auth code when input is missing
|
|
69
|
-
builder.setAuthCode(undefined);
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
## Security
|
|
73
|
-
|
|
74
|
-
- ✅ Auth codes: single-use, short-lived (5-10 min)
|
|
75
|
-
- ✅ MSAL validates tokens from Microsoft authority
|
|
76
|
-
- ✅ Tokens stored securely, refresh tokens auto-managed
|
|
77
|
-
- ⚠️ Pass codes securely: HTTPS, HTTP-only cookies, or encrypted channels
|
|
78
|
-
|
|
79
|
-
## Troubleshooting
|
|
80
|
-
|
|
81
|
-
| Issue | Solution |
|
|
82
|
-
|-------|----------|
|
|
83
|
-
| Auth code not exchanged | Verify `setAuthCode()` called before init |
|
|
84
|
-
| Invalid auth code error | Confirm backend `WithSpaAuthCode` enabled, code is fresh |
|
|
85
|
-
| Still shows login prompt | Check auth code exchange completes before `requiresAuth` check |
|
|
86
|
-
| Exchange fails | Auth code may have expired; backend should generate fresh code per load |
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
# Migration Guide
|
|
2
|
-
|
|
3
|
-
## MSAL v2 to v4 Migration
|
|
4
|
-
|
|
5
|
-
This package has been upgraded from MSAL Browser v2 to v4, providing the latest security improvements and features from Microsoft.
|
|
6
|
-
|
|
7
|
-
### What Changed in v4
|
|
8
|
-
|
|
9
|
-
**New MSAL Browser v4 Features:**
|
|
10
|
-
- Enhanced security with improved token management
|
|
11
|
-
- Better performance and memory usage
|
|
12
|
-
- New authentication API structure with nested request objects
|
|
13
|
-
- Improved error handling and retry mechanisms
|
|
14
|
-
|
|
15
|
-
**Architecture Changes:**
|
|
16
|
-
- **Module Hoisting**: The module uses module hoisting, meaning sub-module instances proxy the parent module instance
|
|
17
|
-
- **Shared Authentication State**: Authentication state is shared across all module instances
|
|
18
|
-
- **Async Initialization**: New `initialize()` method must be called before using the provider
|
|
19
|
-
|
|
20
|
-
### Breaking Changes
|
|
21
|
-
|
|
22
|
-
1. **Auto-initialization via Framework**
|
|
23
|
-
```typescript
|
|
24
|
-
// The provider initializes automatically when framework loads
|
|
25
|
-
const framework = await initialize(configurator);
|
|
26
|
-
const auth = framework.auth; // Already initialized
|
|
27
|
-
|
|
28
|
-
// Manual initialization is only needed for standalone usage
|
|
29
|
-
const provider = new MsalProvider(config);
|
|
30
|
-
await provider.initialize();
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
2. **API Method Signature Updates**
|
|
34
|
-
- `logout()` now returns `Promise<boolean>` instead of `Promise<void>`
|
|
35
|
-
- `handleRedirect()` now returns `Promise<AuthenticationResult | null>` instead of `Promise<void>`
|
|
36
|
-
- Methods now expect nested request objects (v4 format)
|
|
37
|
-
|
|
38
|
-
3. **Account Property Changes**
|
|
39
|
-
- Use `account` property (returns `AccountInfo | null`) - v4 native
|
|
40
|
-
- `defaultAccount` is deprecated and only available via v2 proxy layer
|
|
41
|
-
- Migration: Replace `defaultAccount` with `account` throughout your code
|
|
42
|
-
|
|
43
|
-
### Migration Steps
|
|
44
|
-
|
|
45
|
-
1. **Update Token Acquisition** (Recommended)
|
|
46
|
-
```typescript
|
|
47
|
-
// Before (v2 format - still works via proxy)
|
|
48
|
-
const token = await framework.auth.acquireAccessToken({
|
|
49
|
-
scopes: ['api.read']
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
// After (v4 format - recommended)
|
|
53
|
-
const token = await framework.auth.acquireAccessToken({
|
|
54
|
-
request: { scopes: ['api.read'] }
|
|
55
|
-
});
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
2. **Update Logout Handling**
|
|
59
|
-
```typescript
|
|
60
|
-
// Before
|
|
61
|
-
await framework.auth.logout();
|
|
62
|
-
|
|
63
|
-
// After (check return value)
|
|
64
|
-
const success = await framework.auth.logout();
|
|
65
|
-
if (success) {
|
|
66
|
-
// Handle successful logout
|
|
67
|
-
}
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
3. **Update Redirect Handling**
|
|
71
|
-
```typescript
|
|
72
|
-
// Before
|
|
73
|
-
await framework.auth.handleRedirect();
|
|
74
|
-
|
|
75
|
-
// After (handle result)
|
|
76
|
-
const result = await framework.auth.handleRedirect();
|
|
77
|
-
if (result?.account) {
|
|
78
|
-
// User authenticated successfully
|
|
79
|
-
console.log('Logged in as:', result.account.username);
|
|
80
|
-
}
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
4. **Update Configuration** (if needed)
|
|
84
|
-
```typescript
|
|
85
|
-
// Ensure only the root module configures MSAL
|
|
86
|
-
enableMSAL(configurator, (builder) => {
|
|
87
|
-
builder.setClientConfig({
|
|
88
|
-
auth: {
|
|
89
|
-
clientId: 'your-client-id',
|
|
90
|
-
tenantId: 'your-tenant-id',
|
|
91
|
-
redirectUri: 'https://your-app.com/callback'
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
builder.setRequiresAuth(true);
|
|
95
|
-
});
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
5. **Remove Duplicate Configurations**: Remove MSAL configuration from child modules
|
|
99
|
-
|
|
100
|
-
### Backward Compatibility
|
|
101
|
-
|
|
102
|
-
The module includes a **v2 proxy layer** that automatically converts v2 API calls to v4 format. This means:
|
|
103
|
-
- ✅ Existing code continues to work without changes
|
|
104
|
-
- ✅ Legacy format `{ scopes: [] }` is still supported
|
|
105
|
-
- ✅ Deprecated v2 properties like `defaultAccount` are available via v2 proxy (with deprecation warnings)
|
|
106
|
-
- ⚠️ New v4 features require using v4 format
|
|
107
|
-
|
|
108
|
-
### Benefits of Migration
|
|
109
|
-
|
|
110
|
-
- **Better Security**: Latest MSAL v4 security improvements and token handling
|
|
111
|
-
- **Improved Performance**: Faster token acquisition, better caching, reduced memory usage
|
|
112
|
-
- **Enhanced Error Handling**: More robust error recovery and retry mechanisms
|
|
113
|
-
- **Future-Proof**: Access to latest Microsoft authentication features and updates
|
|
114
|
-
- **Shared State**: Improved authentication state management across app scopes via module hoisting
|
|
115
|
-
- **Better Developer Experience**: Cleaner API, better TypeScript support, comprehensive documentation
|
package/docs/testing.md
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
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
|
package/docs/troubleshooting.md
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
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
|
|
@@ -1,67 +0,0 @@
|
|
|
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
|
-
```
|