@xh/hoist 72.1.0 → 72.2.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.
- package/CHANGELOG.md +18 -0
- package/admin/tabs/activity/clienterrors/ClientErrorsModel.ts +23 -4
- package/admin/tabs/userData/roles/details/RoleDetailsModel.ts +2 -1
- package/admin/tabs/userData/roles/recategorize/RecategorizeDialogModel.ts +1 -1
- package/build/types/cmp/tab/TabContainerModel.d.ts +5 -5
- package/build/types/core/HoistProps.d.ts +1 -0
- package/build/types/core/XH.d.ts +5 -5
- package/build/types/core/types/Interfaces.d.ts +9 -0
- package/build/types/desktop/cmp/appOption/AutoRefreshAppOption.d.ts +1 -0
- package/build/types/desktop/cmp/appOption/ThemeAppOption.d.ts +1 -0
- package/build/types/kit/blueprint/Wrappers.d.ts +1 -1
- package/build/types/kit/swiper/index.d.ts +4 -4
- package/build/types/security/BaseOAuthClient.d.ts +19 -21
- package/build/types/security/Token.d.ts +0 -1
- package/build/types/security/Types.d.ts +12 -0
- package/build/types/security/authzero/AuthZeroClient.d.ts +3 -4
- package/build/types/security/msal/MsalClient.d.ts +3 -4
- package/cmp/tab/TabContainerModel.ts +5 -5
- package/core/HoistProps.ts +1 -0
- package/core/XH.ts +13 -5
- package/core/exception/Exception.ts +19 -12
- package/core/types/Interfaces.ts +11 -0
- package/desktop/appcontainer/ExceptionDialog.ts +1 -1
- package/desktop/cmp/dash/canvas/DashCanvas.ts +2 -1
- package/package.json +1 -1
- package/security/BaseOAuthClient.ts +41 -36
- package/security/Token.ts +0 -2
- package/security/Types.ts +22 -0
- package/security/authzero/AuthZeroClient.ts +6 -8
- package/security/msal/MsalClient.ts +6 -8
- package/tsconfig.tsbuildinfo +1 -1
- package/utils/react/LayoutPropUtils.ts +2 -1
|
@@ -9,16 +9,17 @@ import {HoistBase, managed, XH} from '@xh/hoist/core';
|
|
|
9
9
|
import {Icon} from '@xh/hoist/icon';
|
|
10
10
|
import {action, makeObservable} from '@xh/hoist/mobx';
|
|
11
11
|
import {never, wait} from '@xh/hoist/promise';
|
|
12
|
-
import {Token
|
|
12
|
+
import {Token} from '@xh/hoist/security/Token';
|
|
13
|
+
import {AccessTokenSpec, TokenMap} from './Types';
|
|
13
14
|
import {Timer} from '@xh/hoist/utils/async';
|
|
14
15
|
import {MINUTES, olderThan, ONE_MINUTE, SECONDS} from '@xh/hoist/utils/datetime';
|
|
15
16
|
import {isJSON, throwIf} from '@xh/hoist/utils/js';
|
|
16
|
-
import {find, forEach, isEmpty, isObject, keys, pickBy, union} from 'lodash';
|
|
17
|
+
import {find, forEach, isEmpty, isObject, keys, map, pickBy, union} from 'lodash';
|
|
17
18
|
import ShortUniqueId from 'short-unique-id';
|
|
18
19
|
|
|
19
20
|
export type LoginMethod = 'REDIRECT' | 'POPUP';
|
|
20
21
|
|
|
21
|
-
export interface BaseOAuthClientConfig<S> {
|
|
22
|
+
export interface BaseOAuthClientConfig<S extends AccessTokenSpec> {
|
|
22
23
|
/** Client ID (GUID) of your app registered with your Oauth provider. */
|
|
23
24
|
clientId: string;
|
|
24
25
|
|
|
@@ -45,26 +46,17 @@ export interface BaseOAuthClientConfig<S> {
|
|
|
45
46
|
* Governs an optional refresh timer that will work to keep the tokens fresh.
|
|
46
47
|
*
|
|
47
48
|
* A typical refresh will use the underlying provider cache, and should not result in
|
|
48
|
-
* network activity. However, if any token
|
|
49
|
+
* network activity. However, if any token would expire before the next autoRefresh,
|
|
49
50
|
* this client will force a call to the underlying provider to get the token.
|
|
50
51
|
*
|
|
51
52
|
* In order to allow aging tokens to be replaced in a timely manner, this value should be
|
|
52
53
|
* significantly shorter than both the minimum token lifetime that will be
|
|
53
|
-
* returned by the underlying API
|
|
54
|
+
* returned by the underlying API.
|
|
54
55
|
*
|
|
55
56
|
* Default is -1, disabling this behavior.
|
|
56
57
|
*/
|
|
57
58
|
autoRefreshSecs?: number;
|
|
58
59
|
|
|
59
|
-
/**
|
|
60
|
-
* During auto-refresh, if the remaining lifetime for any token is below this threshold,
|
|
61
|
-
* force the provider to skip the local cache and go directly to the underlying provider for
|
|
62
|
-
* new tokens and refresh tokens.
|
|
63
|
-
*
|
|
64
|
-
* Default is -1, disabling this behavior.
|
|
65
|
-
*/
|
|
66
|
-
autoRefreshSkipCacheSecs?: number;
|
|
67
|
-
|
|
68
60
|
/**
|
|
69
61
|
* Scopes to request - if any - beyond the core `['openid', 'email']` scopes, which
|
|
70
62
|
* this client will always request.
|
|
@@ -89,13 +81,15 @@ export interface BaseOAuthClientConfig<S> {
|
|
|
89
81
|
* suitable concrete implementation to power a client-side OauthService. See `MsalClient` and
|
|
90
82
|
* `AuthZeroClient`
|
|
91
83
|
*
|
|
92
|
-
* Initialize such a service and this client within
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
* flow as necessary.
|
|
84
|
+
* Initialize such a service and this client within an app's primary {@link HoistAuthModel} to use
|
|
85
|
+
* the tokens it acquires to authenticate with the Hoist server. (Note this requires a suitable
|
|
86
|
+
* server-side `AuthenticationService` implementation to validate the token and actually resolve
|
|
87
|
+
* the user.) On init, the client impl will initiate a pop-up or redirect flow as necessary.
|
|
97
88
|
*/
|
|
98
|
-
export abstract class BaseOAuthClient<
|
|
89
|
+
export abstract class BaseOAuthClient<
|
|
90
|
+
C extends BaseOAuthClientConfig<S>,
|
|
91
|
+
S extends AccessTokenSpec
|
|
92
|
+
> extends HoistBase {
|
|
99
93
|
/** Config loaded from UI server + init method. */
|
|
100
94
|
protected config: C;
|
|
101
95
|
|
|
@@ -121,7 +115,6 @@ export abstract class BaseOAuthClient<C extends BaseOAuthClientConfig<S>, S> ext
|
|
|
121
115
|
redirectUrl: 'APP_BASE_URL',
|
|
122
116
|
postLogoutRedirectUrl: 'APP_BASE_URL',
|
|
123
117
|
autoRefreshSecs: -1,
|
|
124
|
-
autoRefreshSkipCacheSecs: -1,
|
|
125
118
|
...config
|
|
126
119
|
};
|
|
127
120
|
throwIf(!config.clientId, 'Missing OAuth clientId. Please review your configuration.');
|
|
@@ -175,10 +168,10 @@ export abstract class BaseOAuthClient<C extends BaseOAuthClientConfig<S>, S> ext
|
|
|
175
168
|
}
|
|
176
169
|
|
|
177
170
|
/**
|
|
178
|
-
* Get all
|
|
171
|
+
* Get all configured tokens.
|
|
179
172
|
*/
|
|
180
|
-
async getAllTokensAsync(): Promise<TokenMap> {
|
|
181
|
-
return this.fetchAllTokensAsync(
|
|
173
|
+
async getAllTokensAsync(opts?: {eagerOnly?: boolean; useCache?: boolean}): Promise<TokenMap> {
|
|
174
|
+
return this.fetchAllTokensAsync(opts);
|
|
182
175
|
}
|
|
183
176
|
|
|
184
177
|
/**
|
|
@@ -314,12 +307,27 @@ export abstract class BaseOAuthClient<C extends BaseOAuthClientConfig<S>, S> ext
|
|
|
314
307
|
await never();
|
|
315
308
|
}
|
|
316
309
|
|
|
317
|
-
protected async fetchAllTokensAsync(
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
310
|
+
protected async fetchAllTokensAsync(opts?: {
|
|
311
|
+
eagerOnly?: boolean;
|
|
312
|
+
useCache?: boolean;
|
|
313
|
+
}): Promise<TokenMap> {
|
|
314
|
+
const eagerOnly = opts?.eagerOnly ?? false,
|
|
315
|
+
useCache = opts?.useCache ?? true,
|
|
316
|
+
accessSpecs = eagerOnly
|
|
317
|
+
? pickBy(this.accessSpecs, spec => spec.fetchMode === 'eager')
|
|
318
|
+
: this.accessSpecs,
|
|
319
|
+
ret: TokenMap = {};
|
|
320
|
+
|
|
321
|
+
await Promise.allSettled(
|
|
322
|
+
map(accessSpecs, async (spec, key) => {
|
|
323
|
+
try {
|
|
324
|
+
ret[key] = await this.fetchAccessTokenAsync(spec, useCache);
|
|
325
|
+
} catch (e) {
|
|
326
|
+
XH.handleException(e, {logOnServer: true, showAlert: false});
|
|
327
|
+
}
|
|
328
|
+
})
|
|
329
|
+
);
|
|
330
|
+
|
|
323
331
|
// Do this after getting any access tokens --which can also populate the idToken cache!
|
|
324
332
|
ret.id = await this.fetchIdTokenSafeAsync(useCache);
|
|
325
333
|
|
|
@@ -361,22 +369,19 @@ export abstract class BaseOAuthClient<C extends BaseOAuthClientConfig<S>, S> ext
|
|
|
361
369
|
private async onTimerAsync(): Promise<void> {
|
|
362
370
|
const {config, lastRefreshAttempt} = this,
|
|
363
371
|
refreshSecs = config.autoRefreshSecs * SECONDS,
|
|
364
|
-
skipCacheSecs =
|
|
372
|
+
skipCacheSecs = refreshSecs + 5 * SECONDS;
|
|
365
373
|
|
|
366
374
|
if (olderThan(lastRefreshAttempt, refreshSecs)) {
|
|
367
375
|
this.lastRefreshAttempt = Date.now();
|
|
368
376
|
try {
|
|
369
377
|
this.logDebug('Refreshing all tokens:');
|
|
370
378
|
let tokens = await this.fetchAllTokensAsync(),
|
|
371
|
-
aging = pickBy(
|
|
372
|
-
tokens,
|
|
373
|
-
v => skipCacheSecs > 0 && v.expiresWithin(skipCacheSecs)
|
|
374
|
-
);
|
|
379
|
+
aging = pickBy(tokens, v => v.expiresWithin(skipCacheSecs));
|
|
375
380
|
if (!isEmpty(aging)) {
|
|
376
381
|
this.logDebug(
|
|
377
382
|
`Tokens [${keys(aging).join(', ')}] have < ${skipCacheSecs}s remaining, reloading without cache.`
|
|
378
383
|
);
|
|
379
|
-
tokens = await this.fetchAllTokensAsync(false);
|
|
384
|
+
tokens = await this.fetchAllTokensAsync({useCache: false});
|
|
380
385
|
}
|
|
381
386
|
this.logTokensDebug(tokens);
|
|
382
387
|
} catch (e) {
|
package/security/Token.ts
CHANGED
|
@@ -11,8 +11,6 @@ import {jwtDecode} from 'jwt-decode';
|
|
|
11
11
|
import {getRelativeTimestamp} from '@xh/hoist/cmp/relativetimestamp';
|
|
12
12
|
import {isNil} from 'lodash';
|
|
13
13
|
|
|
14
|
-
export type TokenMap = Record<string, Token>;
|
|
15
|
-
|
|
16
14
|
export class Token {
|
|
17
15
|
readonly value: string;
|
|
18
16
|
readonly decoded: PlainObject;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file belongs to Hoist, an application development toolkit
|
|
3
|
+
* developed by Extremely Heavy Industries (www.xh.io | info@xh.io)
|
|
4
|
+
*
|
|
5
|
+
* Copyright © 2025 Extremely Heavy Industries Inc.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {Token} from './Token';
|
|
9
|
+
|
|
10
|
+
export type TokenMap = Record<string, Token>;
|
|
11
|
+
|
|
12
|
+
export interface AccessTokenSpec {
|
|
13
|
+
/**
|
|
14
|
+
* Mode governing when the access token should be requested from provider.
|
|
15
|
+
* eager (default) - initiate lookup on initialization, but do not block on failure.
|
|
16
|
+
* lazy - lookup when requested by client.
|
|
17
|
+
*/
|
|
18
|
+
fetchMode: 'eager' | 'lazy';
|
|
19
|
+
|
|
20
|
+
/** Scopes for the desired access token.*/
|
|
21
|
+
scopes: string[];
|
|
22
|
+
}
|
|
@@ -8,7 +8,8 @@ import type {Auth0ClientOptions} from '@auth0/auth0-spa-js';
|
|
|
8
8
|
import {Auth0Client} from '@auth0/auth0-spa-js';
|
|
9
9
|
import {XH} from '@xh/hoist/core';
|
|
10
10
|
import {wait} from '@xh/hoist/promise';
|
|
11
|
-
import {Token
|
|
11
|
+
import {Token} from '@xh/hoist/security/Token';
|
|
12
|
+
import {AccessTokenSpec, TokenMap} from '../Types';
|
|
12
13
|
import {SECONDS} from '@xh/hoist/utils/datetime';
|
|
13
14
|
import {mergeDeep, throwIf} from '@xh/hoist/utils/js';
|
|
14
15
|
import {flatMap, union} from 'lodash';
|
|
@@ -40,10 +41,7 @@ export interface AuthZeroClientConfig extends BaseOAuthClientConfig<AuthZeroToke
|
|
|
40
41
|
authZeroClientOptions?: Partial<Auth0ClientOptions>;
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
export interface AuthZeroTokenSpec {
|
|
44
|
-
/** Scopes for the desired access token.*/
|
|
45
|
-
scopes: string[];
|
|
46
|
-
|
|
44
|
+
export interface AuthZeroTokenSpec extends AccessTokenSpec {
|
|
47
45
|
/**
|
|
48
46
|
* Audience (i.e. API) identifier for AccessToken. Must be registered with Auth0.
|
|
49
47
|
* Note that this is required to ensure that issued token is a JWT and not an opaque string.
|
|
@@ -75,7 +73,7 @@ export class AuthZeroClient extends BaseOAuthClient<AuthZeroClientConfig, AuthZe
|
|
|
75
73
|
const {appState} = await client.handleRedirectCallback();
|
|
76
74
|
this.restoreRedirectState(appState);
|
|
77
75
|
await this.noteUserAuthenticatedAsync();
|
|
78
|
-
return this.fetchAllTokensAsync();
|
|
76
|
+
return this.fetchAllTokensAsync({eagerOnly: true});
|
|
79
77
|
}
|
|
80
78
|
|
|
81
79
|
// 1) If we are logged in, try to just reload tokens silently. This is the happy path on
|
|
@@ -83,7 +81,7 @@ export class AuthZeroClient extends BaseOAuthClient<AuthZeroClientConfig, AuthZe
|
|
|
83
81
|
if (await client.isAuthenticated()) {
|
|
84
82
|
try {
|
|
85
83
|
this.logDebug('Attempting silent token load.');
|
|
86
|
-
return await this.fetchAllTokensAsync();
|
|
84
|
+
return await this.fetchAllTokensAsync({eagerOnly: true});
|
|
87
85
|
} catch (e) {
|
|
88
86
|
this.logDebug('Failed to load tokens on init, fall back to login', e.message ?? e);
|
|
89
87
|
}
|
|
@@ -94,7 +92,7 @@ export class AuthZeroClient extends BaseOAuthClient<AuthZeroClientConfig, AuthZe
|
|
|
94
92
|
await this.loginAsync();
|
|
95
93
|
|
|
96
94
|
// 3) return tokens
|
|
97
|
-
return this.fetchAllTokensAsync();
|
|
95
|
+
return this.fetchAllTokensAsync({eagerOnly: true});
|
|
98
96
|
}
|
|
99
97
|
|
|
100
98
|
protected override async doLoginRedirectAsync(): Promise<void> {
|
|
@@ -13,7 +13,8 @@ import {
|
|
|
13
13
|
SilentRequest
|
|
14
14
|
} from '@azure/msal-browser';
|
|
15
15
|
import {XH} from '@xh/hoist/core';
|
|
16
|
-
import {Token
|
|
16
|
+
import {Token} from '@xh/hoist/security/Token';
|
|
17
|
+
import {AccessTokenSpec, TokenMap} from '../Types';
|
|
17
18
|
import {logDebug, logError, logInfo, logWarn, mergeDeep, throwIf} from '@xh/hoist/utils/js';
|
|
18
19
|
import {flatMap, union, uniq} from 'lodash';
|
|
19
20
|
import {BaseOAuthClient, BaseOAuthClientConfig} from '../BaseOAuthClient';
|
|
@@ -65,10 +66,7 @@ export interface MsalClientConfig extends BaseOAuthClientConfig<MsalTokenSpec> {
|
|
|
65
66
|
msalClientOptions?: Partial<msal.Configuration>;
|
|
66
67
|
}
|
|
67
68
|
|
|
68
|
-
export interface MsalTokenSpec {
|
|
69
|
-
/** Scopes for the desired access token. */
|
|
70
|
-
scopes: string[];
|
|
71
|
-
|
|
69
|
+
export interface MsalTokenSpec extends AccessTokenSpec {
|
|
72
70
|
/**
|
|
73
71
|
* Scopes to be added to the scopes requested during interactive and SSO logins.
|
|
74
72
|
* See the `scopes` property on `PopupRequest`, `RedirectRequest`, and `SSORequest`
|
|
@@ -127,7 +125,7 @@ export class MsalClient extends BaseOAuthClient<MsalClientConfig, MsalTokenSpec>
|
|
|
127
125
|
this.logDebug('Completing Redirect login');
|
|
128
126
|
this.noteUserAuthenticated(redirectResp.account);
|
|
129
127
|
this.restoreRedirectState(redirectResp.state);
|
|
130
|
-
return this.fetchAllTokensAsync();
|
|
128
|
+
return this.fetchAllTokensAsync({eagerOnly: true});
|
|
131
129
|
}
|
|
132
130
|
|
|
133
131
|
// 1) If we are logged in, try to just reload tokens silently. This is the happy path on
|
|
@@ -142,7 +140,7 @@ export class MsalClient extends BaseOAuthClient<MsalClientConfig, MsalTokenSpec>
|
|
|
142
140
|
try {
|
|
143
141
|
this.initialTokenLoad = true;
|
|
144
142
|
this.logDebug('Attempting silent token load.');
|
|
145
|
-
return await this.fetchAllTokensAsync();
|
|
143
|
+
return await this.fetchAllTokensAsync({eagerOnly: true});
|
|
146
144
|
} catch (e) {
|
|
147
145
|
this.account = null;
|
|
148
146
|
this.logDebug('Failed to load tokens on init, fall back to login', e.message ?? e);
|
|
@@ -171,7 +169,7 @@ export class MsalClient extends BaseOAuthClient<MsalClientConfig, MsalTokenSpec>
|
|
|
171
169
|
}
|
|
172
170
|
|
|
173
171
|
// 3) Return tokens
|
|
174
|
-
return this.fetchAllTokensAsync();
|
|
172
|
+
return this.fetchAllTokensAsync({eagerOnly: true});
|
|
175
173
|
}
|
|
176
174
|
|
|
177
175
|
protected override async doLoginPopupAsync(): Promise<void> {
|