@multiplatform.one/keycloak 7.6.3 → 7.7.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/dist/cjs/index.cjs +346 -53
- package/dist/cjs/index.native.cjs +390 -20
- package/dist/esm/index.js +345 -44
- package/dist/esm/index.native.js +397 -12
- package/package.json +9 -10
- package/src/betterAuth/accountSelection.spec.ts +131 -0
- package/src/betterAuth/accountSelection.ts +99 -0
- package/src/betterAuth/expoAuthFlow.native.ts +18 -6
- package/src/frappeToken.ts +14 -9
- package/types/betterAuth/accountSelection.d.ts +65 -0
- package/types/betterAuth/accountSelection.d.ts.map +1 -0
- package/types/betterAuth/expoAuthFlow.native.d.ts +7 -1
- package/types/betterAuth/expoAuthFlow.native.d.ts.map +1 -1
- package/types/frappeToken.d.ts.map +1 -1
package/dist/esm/index.js
CHANGED
|
@@ -1,5 +1,133 @@
|
|
|
1
|
+
// ../platform/src/platform/platformBase.ts
|
|
2
|
+
import {
|
|
3
|
+
isChrome,
|
|
4
|
+
isClient,
|
|
5
|
+
isServer,
|
|
6
|
+
isTouchable,
|
|
7
|
+
isWeb,
|
|
8
|
+
isWebTouchable,
|
|
9
|
+
isWindowDefined
|
|
10
|
+
} from "@tamagui/constants";
|
|
11
|
+
var platformOrder = [
|
|
12
|
+
"Ios",
|
|
13
|
+
"Android",
|
|
14
|
+
// Gnome outranks Native: the GTK target reports isNative (it is not a
|
|
15
|
+
// browser) but "gnome" is the precise answer callers want.
|
|
16
|
+
"Gnome",
|
|
17
|
+
"Native",
|
|
18
|
+
// Tauri outranks Desktop/Web the same way: the webview host reports isWeb
|
|
19
|
+
// and isDesktop, but "tauri" is the precise answer.
|
|
20
|
+
"Tauri",
|
|
21
|
+
"Desktop",
|
|
22
|
+
"ChromeExtension",
|
|
23
|
+
"FirefoxExtension",
|
|
24
|
+
"WebExtension",
|
|
25
|
+
"Next",
|
|
26
|
+
"Expo",
|
|
27
|
+
"Chrome",
|
|
28
|
+
"Firefox",
|
|
29
|
+
"Web",
|
|
30
|
+
"Browser",
|
|
31
|
+
"Client",
|
|
32
|
+
"Server",
|
|
33
|
+
"Iframe",
|
|
34
|
+
"Storybook"
|
|
35
|
+
];
|
|
36
|
+
var platformBase = {
|
|
37
|
+
isAndroid: false,
|
|
38
|
+
isBrowser: false,
|
|
39
|
+
isChrome,
|
|
40
|
+
isChromeExtension: false,
|
|
41
|
+
isClient,
|
|
42
|
+
isDesktop: false,
|
|
43
|
+
isExpo: false,
|
|
44
|
+
isFirefox: false,
|
|
45
|
+
isFirefoxExtension: false,
|
|
46
|
+
isGnome: false,
|
|
47
|
+
isIframe: false,
|
|
48
|
+
isIos: false,
|
|
49
|
+
isNative: false,
|
|
50
|
+
isNext: false,
|
|
51
|
+
isServer,
|
|
52
|
+
isStorybook: isWindowDefined && typeof window.__STORYBOOK_ADDONS_PREVIEW === "object",
|
|
53
|
+
isTauri: false,
|
|
54
|
+
isTouchable,
|
|
55
|
+
isWeb,
|
|
56
|
+
isWebExtension: false,
|
|
57
|
+
isWebTouchable,
|
|
58
|
+
isWindowDefined,
|
|
59
|
+
preciseName: "unknown",
|
|
60
|
+
broadName: "unknown"
|
|
61
|
+
};
|
|
62
|
+
function getPreciseName(platform2) {
|
|
63
|
+
const flags = platform2;
|
|
64
|
+
for (const name of platformOrder) {
|
|
65
|
+
if (flags[`is${name}`]) {
|
|
66
|
+
return `${name[0].toLowerCase()}${name.slice(1)}`;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return "unknown";
|
|
70
|
+
}
|
|
71
|
+
function getBroadName(platform2) {
|
|
72
|
+
const flags = platform2;
|
|
73
|
+
for (const name of platformOrder.slice().reverse()) {
|
|
74
|
+
if (flags[`is${name}`]) {
|
|
75
|
+
return `${name[0].toLowerCase()}${name.slice(1)}`;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return "unknown";
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ../platform/src/platform/index.ts
|
|
82
|
+
var isIframe = (() => {
|
|
83
|
+
if (!platformBase.isWindowDefined) return false;
|
|
84
|
+
try {
|
|
85
|
+
return window.self !== window.top;
|
|
86
|
+
} catch {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
})();
|
|
90
|
+
var isWebExtension = !!(typeof chrome !== "undefined" && !!chrome?.runtime?.id || typeof browser !== "undefined" && !!browser?.runtime?.id);
|
|
91
|
+
var platform = {
|
|
92
|
+
...platformBase,
|
|
93
|
+
isChromeExtension: typeof chrome !== "undefined" && !!chrome?.runtime?.id,
|
|
94
|
+
isFirefoxExtension: typeof browser !== "undefined" && !!browser?.runtime?.id,
|
|
95
|
+
isIframe,
|
|
96
|
+
isWebExtension,
|
|
97
|
+
isBrowser: !!(platformBase.isWeb && platformBase.isWindowDefined && !isWebExtension),
|
|
98
|
+
isNext: platformBase.isWeb && (!platformBase.isWindowDefined || typeof window.__NEXT_DATA__ === "object"),
|
|
99
|
+
isFirefox: platformBase.isWindowDefined && !!(window?.navigator?.userAgent?.toLowerCase().indexOf("firefox") > -1)
|
|
100
|
+
};
|
|
101
|
+
platform.preciseName = getPreciseName(platform);
|
|
102
|
+
platform.broadName = getBroadName(platform);
|
|
103
|
+
|
|
104
|
+
// ../platform/src/index.ts
|
|
105
|
+
var {
|
|
106
|
+
isAndroid,
|
|
107
|
+
isBrowser,
|
|
108
|
+
isChrome: isChrome2,
|
|
109
|
+
isChromeExtension,
|
|
110
|
+
isClient: isClient2,
|
|
111
|
+
isExpo,
|
|
112
|
+
isFirefox,
|
|
113
|
+
isFirefoxExtension,
|
|
114
|
+
isGnome,
|
|
115
|
+
isIframe: isIframe2,
|
|
116
|
+
isIos,
|
|
117
|
+
isNative,
|
|
118
|
+
isNext,
|
|
119
|
+
isServer: isServer2,
|
|
120
|
+
isStorybook,
|
|
121
|
+
isTauri,
|
|
122
|
+
isDesktop,
|
|
123
|
+
isTouchable: isTouchable2,
|
|
124
|
+
isWeb: isWeb2,
|
|
125
|
+
isWebExtension: isWebExtension2,
|
|
126
|
+
isWebTouchable: isWebTouchable2,
|
|
127
|
+
isWindowDefined: isWindowDefined2
|
|
128
|
+
} = platform;
|
|
129
|
+
|
|
1
130
|
// src/Authenticated.tsx
|
|
2
|
-
import { isIframe as isIframe3, isServer as isServer3 } from "@multiplatform.one/platform";
|
|
3
131
|
import { useEffect } from "react";
|
|
4
132
|
import { Text } from "react-native";
|
|
5
133
|
|
|
@@ -17,26 +145,95 @@ function useAuthConfig() {
|
|
|
17
145
|
}
|
|
18
146
|
|
|
19
147
|
// src/hooks/useTokensFromQuery/index.ts
|
|
20
|
-
import { isIframe } from "@multiplatform.one/platform";
|
|
21
148
|
function useTokensFromQuery() {
|
|
22
149
|
const { iframeSso } = useAuthConfig();
|
|
23
|
-
if (iframeSso || !
|
|
150
|
+
if (iframeSso || !isIframe2) return false;
|
|
24
151
|
const query = new URLSearchParams(
|
|
25
152
|
typeof window === "undefined" ? "" : window?.location?.search || ""
|
|
26
153
|
);
|
|
27
154
|
return typeof query.get("idToken") === "string" || typeof query.get("token") === "string" || typeof query.get("refreshToken") === "string";
|
|
28
155
|
}
|
|
29
156
|
|
|
157
|
+
// ../store/src/createStore.ts
|
|
158
|
+
import { Store as Store2 } from "@tanstack/store";
|
|
159
|
+
|
|
160
|
+
// ../store/src/persist.ts
|
|
161
|
+
import { Store } from "@tanstack/store";
|
|
162
|
+
function createPersistStore(initialState, persistOptions) {
|
|
163
|
+
const { key, storage: storage2, serialize = JSON.stringify, deserialize = JSON.parse } = persistOptions;
|
|
164
|
+
let hydrated = false;
|
|
165
|
+
const store = new Store(initialState);
|
|
166
|
+
store.subscribe(() => {
|
|
167
|
+
if (hydrated) {
|
|
168
|
+
const serialized = serialize(store.state);
|
|
169
|
+
void Promise.resolve(storage2.setItem(key, serialized));
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
const rehydrate = async () => {
|
|
173
|
+
const raw = await Promise.resolve(storage2.getItem(key));
|
|
174
|
+
if (raw !== null) {
|
|
175
|
+
const deserialized = deserialize(raw);
|
|
176
|
+
store.setState(() => deserialized);
|
|
177
|
+
}
|
|
178
|
+
hydrated = true;
|
|
179
|
+
};
|
|
180
|
+
const clearPersistedState = async () => {
|
|
181
|
+
await Promise.resolve(storage2.removeItem(key));
|
|
182
|
+
};
|
|
183
|
+
void rehydrate();
|
|
184
|
+
const persistStore = store;
|
|
185
|
+
persistStore.rehydrate = rehydrate;
|
|
186
|
+
persistStore.clearPersistedState = clearPersistedState;
|
|
187
|
+
return persistStore;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// ../store/src/storage/index.ts
|
|
191
|
+
var noopStorage = {
|
|
192
|
+
getItem: () => null,
|
|
193
|
+
setItem: () => {
|
|
194
|
+
},
|
|
195
|
+
removeItem: () => {
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
function getStorage() {
|
|
199
|
+
if (typeof globalThis !== "undefined" && "localStorage" in globalThis && typeof globalThis.localStorage?.getItem === "function") {
|
|
200
|
+
return {
|
|
201
|
+
getItem: (name) => globalThis.localStorage.getItem(name) ?? null,
|
|
202
|
+
setItem: (name, value) => globalThis.localStorage.setItem(name, value),
|
|
203
|
+
removeItem: (name) => globalThis.localStorage.removeItem(name)
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
return noopStorage;
|
|
207
|
+
}
|
|
208
|
+
var storage = getStorage();
|
|
209
|
+
|
|
210
|
+
// ../store/src/createStore.ts
|
|
211
|
+
function createStore(initialState, options) {
|
|
212
|
+
if (options?.persist) {
|
|
213
|
+
return createPersistStore(initialState, {
|
|
214
|
+
key: options.name,
|
|
215
|
+
storage: options.storage ?? storage
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
return new Store2(initialState);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// ../store/src/index.ts
|
|
222
|
+
import { Store as Store3, batch, createStore as createStore2 } from "@tanstack/store";
|
|
223
|
+
import { useStore } from "@tanstack/react-store";
|
|
224
|
+
import { useStore as tanstackUseStore } from "@tanstack/react-store";
|
|
225
|
+
function useStore2(store, selector) {
|
|
226
|
+
return tanstackUseStore(store, selector ?? ((s) => s));
|
|
227
|
+
}
|
|
228
|
+
|
|
30
229
|
// src/state.ts
|
|
31
|
-
|
|
32
|
-
import { isBrowser, isIframe as isIframe2, isServer } from "@multiplatform.one/platform";
|
|
33
|
-
var persist = isIframe2 || !isBrowser && !isServer;
|
|
230
|
+
var persist = isIframe2 || !isBrowser && !isServer2;
|
|
34
231
|
var authStore = createStore(
|
|
35
232
|
{ idToken: "", refreshToken: "", token: "" },
|
|
36
233
|
{ name: "auth", persist }
|
|
37
234
|
);
|
|
38
235
|
function useAuthStore() {
|
|
39
|
-
const state =
|
|
236
|
+
const state = useStore2(authStore);
|
|
40
237
|
return {
|
|
41
238
|
...state,
|
|
42
239
|
setIdToken: (idToken) => authStore.setState((prev) => ({ ...prev, idToken })),
|
|
@@ -99,23 +296,48 @@ function useTokensFromStore() {
|
|
|
99
296
|
}
|
|
100
297
|
|
|
101
298
|
// src/keycloak/index.ts
|
|
102
|
-
import { isBrowser as isBrowser3, isServer as isServer2, isStorybook, isWeb as isWeb2 } from "@multiplatform.one/platform";
|
|
103
299
|
import { useContext as useContext3 } from "react";
|
|
104
300
|
|
|
105
301
|
// src/betterAuth/client.ts
|
|
106
302
|
import { createAuthClient } from "better-auth/react";
|
|
107
303
|
import { genericOAuthClient } from "better-auth/client/plugins";
|
|
108
|
-
import { isWeb } from "@multiplatform.one/platform";
|
|
109
304
|
var _client;
|
|
110
305
|
function getAuthClient(baseURL) {
|
|
111
306
|
if (_client) return _client;
|
|
112
307
|
_client = createAuthClient({
|
|
113
|
-
baseURL: baseURL || (
|
|
308
|
+
baseURL: baseURL || (isWeb2 && typeof window !== "undefined" ? window.location.origin : ""),
|
|
114
309
|
plugins: [genericOAuthClient()]
|
|
115
310
|
});
|
|
116
311
|
return _client;
|
|
117
312
|
}
|
|
118
313
|
|
|
314
|
+
// src/betterAuth/accountSelection.ts
|
|
315
|
+
var validationErrorCode = "VALIDATION_ERROR";
|
|
316
|
+
var known;
|
|
317
|
+
function currentSelection(userId) {
|
|
318
|
+
return userId ? { useAccountCookie: true, userId } : { useAccountCookie: true };
|
|
319
|
+
}
|
|
320
|
+
function legacySelection(providerId, userId) {
|
|
321
|
+
return userId ? { providerId, userId } : { providerId };
|
|
322
|
+
}
|
|
323
|
+
function isBodyRefusal(res) {
|
|
324
|
+
return res.status === 400 && res.body?.code === validationErrorCode;
|
|
325
|
+
}
|
|
326
|
+
async function requestAccessToken(providerId, send, userId) {
|
|
327
|
+
const current = () => currentSelection(userId);
|
|
328
|
+
const legacy = () => legacySelection(providerId, userId);
|
|
329
|
+
if (known === "legacy") return send(legacy());
|
|
330
|
+
if (known === "current") return send(current());
|
|
331
|
+
const first = await send(current());
|
|
332
|
+
if (!isBodyRefusal(first)) {
|
|
333
|
+
if (first.status !== 400) known = "current";
|
|
334
|
+
return first;
|
|
335
|
+
}
|
|
336
|
+
const second = await send(legacy());
|
|
337
|
+
if (!isBodyRefusal(second)) known = "legacy";
|
|
338
|
+
return second;
|
|
339
|
+
}
|
|
340
|
+
|
|
119
341
|
// src/frappeToken.ts
|
|
120
342
|
var cached;
|
|
121
343
|
var pending;
|
|
@@ -153,17 +375,21 @@ async function getKeycloakBearerToken() {
|
|
|
153
375
|
}
|
|
154
376
|
async function fetchTokenOnce() {
|
|
155
377
|
try {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
378
|
+
let payload = null;
|
|
379
|
+
const res = await requestAccessToken("keycloak", async (selection) => {
|
|
380
|
+
const response = await fetch(`${window.location.origin}/api/auth/get-access-token`, {
|
|
381
|
+
method: "POST",
|
|
382
|
+
credentials: "include",
|
|
383
|
+
headers: { "Content-Type": "application/json" },
|
|
384
|
+
body: JSON.stringify(selection)
|
|
385
|
+
});
|
|
386
|
+
payload = await response.json().catch(() => null);
|
|
387
|
+
return { status: response.status, body: payload };
|
|
161
388
|
});
|
|
162
|
-
if (
|
|
163
|
-
|
|
164
|
-
return { ok: false, kind: classifyAccessTokenResponse(res.status, body) };
|
|
389
|
+
if (res.status < 200 || res.status >= 300) {
|
|
390
|
+
return { ok: false, kind: classifyAccessTokenResponse(res.status, res.body) };
|
|
165
391
|
}
|
|
166
|
-
const data =
|
|
392
|
+
const data = payload;
|
|
167
393
|
if (!data?.accessToken) {
|
|
168
394
|
return { ok: false, kind: "signed-out" };
|
|
169
395
|
}
|
|
@@ -227,11 +453,10 @@ function watchKeycloakBearerTokenRevalidate() {
|
|
|
227
453
|
}
|
|
228
454
|
|
|
229
455
|
// src/session/index.ts
|
|
230
|
-
import { isBrowser as isBrowser2 } from "@multiplatform.one/platform";
|
|
231
456
|
var useSession = (_options) => {
|
|
232
457
|
return { session: null, status: "unauthenticated" };
|
|
233
458
|
};
|
|
234
|
-
if (
|
|
459
|
+
if (isBrowser) {
|
|
235
460
|
useSession = (_options) => {
|
|
236
461
|
const client = getAuthClient();
|
|
237
462
|
const { data, isPending, error: _error } = client.useSession();
|
|
@@ -408,7 +633,7 @@ var Keycloak = class extends BaseKeycloak {
|
|
|
408
633
|
this._clear();
|
|
409
634
|
if (this._keycloakClient) {
|
|
410
635
|
await this._keycloakClient.login(options);
|
|
411
|
-
} else if (
|
|
636
|
+
} else if (isBrowser && !isServer2) {
|
|
412
637
|
const client = getAuthClient();
|
|
413
638
|
await client.signIn.social({
|
|
414
639
|
provider: "keycloak",
|
|
@@ -436,7 +661,7 @@ var Keycloak = class extends BaseKeycloak {
|
|
|
436
661
|
}
|
|
437
662
|
window.location.href = `${origin}/api/auth/sign-out/sso?callbackURL=${encodeURIComponent(callbackURL)}`;
|
|
438
663
|
return;
|
|
439
|
-
} else if (
|
|
664
|
+
} else if (isBrowser && !isServer2) {
|
|
440
665
|
const client = getAuthClient();
|
|
441
666
|
await client.signOut({
|
|
442
667
|
fetchOptions: {
|
|
@@ -485,7 +710,7 @@ function Authenticated({
|
|
|
485
710
|
const keycloak = useKeycloak();
|
|
486
711
|
const tokensFromQuery = useTokensFromQuery();
|
|
487
712
|
useEffect(() => {
|
|
488
|
-
if (!keycloak || !authConfig.iframeSso &&
|
|
713
|
+
if (!keycloak || !authConfig.iframeSso && isIframe2 || isServer2 || keycloak.authenticated || tokensFromQuery) {
|
|
489
714
|
return;
|
|
490
715
|
}
|
|
491
716
|
keycloak?.login({
|
|
@@ -506,8 +731,86 @@ function withAuthenticated(Component, options = {}) {
|
|
|
506
731
|
return (props) => /* @__PURE__ */ jsx(Authenticated, { ...options, children: /* @__PURE__ */ jsx(Component, { ...props }) });
|
|
507
732
|
}
|
|
508
733
|
|
|
734
|
+
// ../logger/src/base.ts
|
|
735
|
+
import log from "loglevel";
|
|
736
|
+
var defaultLoggerConfig = {
|
|
737
|
+
name: "default",
|
|
738
|
+
type: "pretty",
|
|
739
|
+
level: true ? 1 /* Debug */ : 2 /* Info */
|
|
740
|
+
};
|
|
741
|
+
var BaseLogger = class {
|
|
742
|
+
constructor(config = {}) {
|
|
743
|
+
const { metadata, name, level } = typeof config === "string" ? { metadata: void 0, name: config, level: void 0 } : config;
|
|
744
|
+
this.metadata = metadata || {};
|
|
745
|
+
this.name = name || "default";
|
|
746
|
+
this.logger = name ? log.getLogger(name) : log;
|
|
747
|
+
if (level !== void 0) this.logger.setLevel(level);
|
|
748
|
+
this.transport = this.createTransport?.();
|
|
749
|
+
}
|
|
750
|
+
trace(...args) {
|
|
751
|
+
this.logger.trace(...args);
|
|
752
|
+
this.sendToTransport(0 /* Trace */, args);
|
|
753
|
+
}
|
|
754
|
+
debug(...args) {
|
|
755
|
+
this.logger.debug(...args);
|
|
756
|
+
this.sendToTransport(1 /* Debug */, args);
|
|
757
|
+
}
|
|
758
|
+
info(...args) {
|
|
759
|
+
this.logger.info(...args);
|
|
760
|
+
this.sendToTransport(2 /* Info */, args);
|
|
761
|
+
}
|
|
762
|
+
warn(...args) {
|
|
763
|
+
this.logger.warn(...args);
|
|
764
|
+
this.sendToTransport(3 /* Warn */, args);
|
|
765
|
+
}
|
|
766
|
+
error(...args) {
|
|
767
|
+
this.logger.error(...args);
|
|
768
|
+
this.sendToTransport(4 /* Error */, args);
|
|
769
|
+
}
|
|
770
|
+
log(...args) {
|
|
771
|
+
this.logger.debug(...args);
|
|
772
|
+
this.sendToTransport(1 /* Debug */, args);
|
|
773
|
+
}
|
|
774
|
+
sendToTransport(level, args) {
|
|
775
|
+
if (this.transport) {
|
|
776
|
+
this.transport.send({
|
|
777
|
+
level,
|
|
778
|
+
message: args[0],
|
|
779
|
+
args: args.slice(1),
|
|
780
|
+
platform: platform.preciseName,
|
|
781
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
782
|
+
});
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
getSubLogger(input) {
|
|
786
|
+
const subConfig = typeof input === "string" ? {
|
|
787
|
+
name: input,
|
|
788
|
+
metadata: this.metadata,
|
|
789
|
+
level: this.logger.getLevel()
|
|
790
|
+
} : {
|
|
791
|
+
...input,
|
|
792
|
+
metadata: { ...this.metadata, ...input.metadata }
|
|
793
|
+
};
|
|
794
|
+
return new this.constructor(subConfig);
|
|
795
|
+
}
|
|
796
|
+
};
|
|
797
|
+
|
|
798
|
+
// ../logger/src/index.ts
|
|
799
|
+
var Logger = class extends BaseLogger {
|
|
800
|
+
constructor(config = {}) {
|
|
801
|
+
const metadata = {
|
|
802
|
+
platform: platform.preciseName
|
|
803
|
+
};
|
|
804
|
+
super({
|
|
805
|
+
name: metadata.platform,
|
|
806
|
+
...typeof config === "string" ? { name: config } : config,
|
|
807
|
+
metadata
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
var logger = new Logger();
|
|
812
|
+
|
|
509
813
|
// src/provider/AfterAuth.tsx
|
|
510
|
-
import { logger } from "@multiplatform.one/logger";
|
|
511
814
|
import { useEffect as useEffect2 } from "react";
|
|
512
815
|
import { Fragment as Fragment2, jsx as jsx2 } from "react/jsx-runtime";
|
|
513
816
|
function AfterAuth({ children, loadingComponent: _loadingComponent }) {
|
|
@@ -564,8 +867,6 @@ function AfterAuth({ children, loadingComponent: _loadingComponent }) {
|
|
|
564
867
|
|
|
565
868
|
// src/provider/authProvider/index.tsx
|
|
566
869
|
import KeycloakClient from "@multiplatform.one/keycloak-js";
|
|
567
|
-
import { isIframe as isIframe4, isWindowDefined } from "@multiplatform.one/platform";
|
|
568
|
-
import { logger as logger2 } from "@multiplatform.one/logger";
|
|
569
870
|
import { useCallback, useEffect as useEffect3, useMemo, useState } from "react";
|
|
570
871
|
|
|
571
872
|
// src/Loading.tsx
|
|
@@ -602,7 +903,7 @@ function AuthProvider({
|
|
|
602
903
|
loadingComponent
|
|
603
904
|
}) {
|
|
604
905
|
const { debug, messageHandlerKeys, iframeSso } = useAuthConfig();
|
|
605
|
-
const query = new URLSearchParams(
|
|
906
|
+
const query = new URLSearchParams(isWindowDefined2 ? window?.location?.search || "" : "");
|
|
606
907
|
const authStore2 = useAuthStore();
|
|
607
908
|
const [keycloak, setKeycloak] = useState();
|
|
608
909
|
const idTokenQuery = query.get("idToken");
|
|
@@ -614,20 +915,20 @@ function AuthProvider({
|
|
|
614
915
|
const [loadedMessagePosted, setLoadedMessagePosted] = useState(false);
|
|
615
916
|
const initialRefreshTokenQuery = useMemo(
|
|
616
917
|
() => validOrRefreshableToken(
|
|
617
|
-
!iframeSso &&
|
|
918
|
+
!iframeSso && isIframe2 || typeof refreshTokenQuery === "string" ? refreshTokenQuery || true : void 0
|
|
618
919
|
),
|
|
619
920
|
[refreshTokenQuery, iframeSso]
|
|
620
921
|
);
|
|
621
922
|
const initialIdTokenQuery = useMemo(
|
|
622
923
|
() => validOrRefreshableToken(
|
|
623
|
-
!iframeSso &&
|
|
924
|
+
!iframeSso && isIframe2 || typeof idTokenQuery === "string" ? idTokenQuery || true : void 0,
|
|
624
925
|
initialRefreshTokenQuery
|
|
625
926
|
),
|
|
626
927
|
[idTokenQuery, initialRefreshTokenQuery, iframeSso]
|
|
627
928
|
);
|
|
628
929
|
const initialTokenQuery = useMemo(
|
|
629
930
|
() => validOrRefreshableToken(
|
|
630
|
-
|
|
931
|
+
isIframe2 ? typeof tokenQuery === "string" ? tokenQuery || true : void 0 : void 0,
|
|
631
932
|
initialRefreshTokenQuery
|
|
632
933
|
),
|
|
633
934
|
[tokenQuery, initialRefreshTokenQuery, iframeSso]
|
|
@@ -638,7 +939,7 @@ function AuthProvider({
|
|
|
638
939
|
idToken: initialIdTokenQuery
|
|
639
940
|
});
|
|
640
941
|
const authSource = useMemo(() => {
|
|
641
|
-
if (!iframeSso &&
|
|
942
|
+
if (!iframeSso && isIframe2) {
|
|
642
943
|
if (initialTokenQuery) return "query" /* Query */;
|
|
643
944
|
return "store" /* Store */;
|
|
644
945
|
}
|
|
@@ -691,7 +992,7 @@ function AuthProvider({
|
|
|
691
992
|
if (disabled || authSource !== "query" /* Query */ || tokens.token === true || tokens.refreshToken === true || tokens.idToken === true || loadedMessagePosted) {
|
|
692
993
|
return;
|
|
693
994
|
}
|
|
694
|
-
if (debug)
|
|
995
|
+
if (debug) logger.debug("post message", JSON.stringify({ type: "LOADED" }));
|
|
695
996
|
(messageHandlerKeys || []).forEach((key) => {
|
|
696
997
|
window?.webkit?.messageHandlers?.[key]?.postMessage(JSON.stringify({ type: "LOADED" }));
|
|
697
998
|
});
|
|
@@ -712,11 +1013,11 @@ function AuthProvider({
|
|
|
712
1013
|
} catch {
|
|
713
1014
|
}
|
|
714
1015
|
}
|
|
715
|
-
if (debug)
|
|
1016
|
+
if (debug) logger.debug("received message", JSON.stringify(data));
|
|
716
1017
|
const message = data;
|
|
717
1018
|
if (!message.type) return;
|
|
718
1019
|
if (message.type.toUpperCase() === "REFRESH_TOKEN" && message.payload) {
|
|
719
|
-
if (debug)
|
|
1020
|
+
if (debug) logger.debug("setting refresh token", message.payload);
|
|
720
1021
|
setTokens((tokens2) => ({
|
|
721
1022
|
...tokens2,
|
|
722
1023
|
refreshToken: validOrRefreshableToken(message.payload)
|
|
@@ -726,7 +1027,7 @@ function AuthProvider({
|
|
|
726
1027
|
}
|
|
727
1028
|
}
|
|
728
1029
|
if (debug) {
|
|
729
|
-
|
|
1030
|
+
logger.debug(
|
|
730
1031
|
"listening for message",
|
|
731
1032
|
JSON.stringify({
|
|
732
1033
|
type: "REFRESH_TOKEN",
|
|
@@ -750,11 +1051,11 @@ function AuthProvider({
|
|
|
750
1051
|
} catch {
|
|
751
1052
|
}
|
|
752
1053
|
}
|
|
753
|
-
if (debug)
|
|
1054
|
+
if (debug) logger.debug("received message", JSON.stringify(data));
|
|
754
1055
|
const message = data;
|
|
755
1056
|
if (!message.type) return;
|
|
756
1057
|
if (message.type.toUpperCase() === "TOKEN" && message.payload) {
|
|
757
|
-
if (debug)
|
|
1058
|
+
if (debug) logger.debug("setting token", message.payload);
|
|
758
1059
|
setTokens((tokens2) => ({
|
|
759
1060
|
...tokens2,
|
|
760
1061
|
token: validOrRefreshableToken(message.payload, tokens2.refreshToken)
|
|
@@ -764,7 +1065,7 @@ function AuthProvider({
|
|
|
764
1065
|
}
|
|
765
1066
|
}
|
|
766
1067
|
if (debug) {
|
|
767
|
-
|
|
1068
|
+
logger.debug(
|
|
768
1069
|
"listening for message",
|
|
769
1070
|
JSON.stringify({
|
|
770
1071
|
type: "TOKEN",
|
|
@@ -788,11 +1089,11 @@ function AuthProvider({
|
|
|
788
1089
|
} catch {
|
|
789
1090
|
}
|
|
790
1091
|
}
|
|
791
|
-
if (debug)
|
|
1092
|
+
if (debug) logger.debug("received message", JSON.stringify(data));
|
|
792
1093
|
const message = data;
|
|
793
1094
|
if (!message.type) return;
|
|
794
1095
|
if (message.type.toUpperCase() === "ID_TOKEN" && message.payload) {
|
|
795
|
-
if (debug)
|
|
1096
|
+
if (debug) logger.debug("setting id token", message.payload);
|
|
796
1097
|
setTokens((tokens2) => ({
|
|
797
1098
|
...tokens2,
|
|
798
1099
|
idToken: validOrRefreshableToken(message.payload, tokens2.refreshToken)
|
|
@@ -802,7 +1103,7 @@ function AuthProvider({
|
|
|
802
1103
|
}
|
|
803
1104
|
}
|
|
804
1105
|
if (debug) {
|
|
805
|
-
|
|
1106
|
+
logger.debug(
|
|
806
1107
|
"listening for message",
|
|
807
1108
|
JSON.stringify({
|
|
808
1109
|
type: "ID_TOKEN",
|
|
@@ -859,7 +1160,7 @@ function AuthProvider({
|
|
|
859
1160
|
initOptions2.refreshToken = tokens.refreshToken;
|
|
860
1161
|
}
|
|
861
1162
|
}
|
|
862
|
-
if (!iframeSso &&
|
|
1163
|
+
if (!iframeSso && isIframe2 || tokens.token && !tokens.refreshToken) {
|
|
863
1164
|
initOptions2.checkLoginIframe = false;
|
|
864
1165
|
initOptions2.flow = "implicit";
|
|
865
1166
|
initOptions2.onLoad = void 0;
|
|
@@ -900,7 +1201,7 @@ function AuthProvider({
|
|
|
900
1201
|
try {
|
|
901
1202
|
await keycloakClient.updateToken(5);
|
|
902
1203
|
} catch (err) {
|
|
903
|
-
|
|
1204
|
+
logger.warn(err);
|
|
904
1205
|
setKeycloak(new Keycloak(keycloakConfig, keycloakClient));
|
|
905
1206
|
}
|
|
906
1207
|
};
|