@multiplatform.one/keycloak 0.7.1 → 0.7.3
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/lib/@types/coreJsPure.d.d.mts +2 -0
- package/lib/@types/coreJsPure.d.d.ts +2 -0
- package/lib/@types/coreJsPure.d.js +1 -0
- package/lib/@types/coreJsPure.d.mjs +0 -0
- package/lib/@types/expoKeycloakAuth.d.d.mts +28 -0
- package/lib/@types/expoKeycloakAuth.d.d.ts +28 -0
- package/lib/@types/expoKeycloakAuth.d.js +1 -0
- package/lib/@types/expoKeycloakAuth.d.mjs +0 -0
- package/lib/Authenticated.d.mts +36 -0
- package/lib/Authenticated.d.ts +36 -0
- package/lib/Authenticated.js +19 -0
- package/lib/Authenticated.mjs +20 -0
- package/lib/authConfig.d.mts +37 -0
- package/lib/authConfig.d.ts +37 -0
- package/lib/authConfig.js +8 -0
- package/lib/authConfig.mjs +9 -0
- package/lib/chunk-2GUOEPFY.mjs +0 -0
- package/lib/chunk-3ZPHNEJL.js +11 -0
- package/lib/chunk-64PPU7Q3.mjs +13 -0
- package/lib/chunk-6NDWOV55.js +1 -0
- package/lib/chunk-7MTPH6XK.mjs +26 -0
- package/lib/chunk-A7UBOIAL.js +13 -0
- package/lib/chunk-AD6VPMID.mjs +20 -0
- package/lib/chunk-AOKVCTMY.mjs +76 -0
- package/lib/chunk-AYKQVWMR.mjs +0 -0
- package/lib/chunk-BJ2OPIMW.js +78 -0
- package/lib/chunk-BTCP7NB3.js +443 -0
- package/lib/chunk-C2FNSVJO.js +17 -0
- package/lib/chunk-DKRUDENY.mjs +265 -0
- package/lib/chunk-E2X33LSD.js +460 -0
- package/lib/chunk-FXGWERIA.js +161 -0
- package/lib/chunk-HDNHCVXT.js +33 -0
- package/lib/chunk-KLLABHIC.js +13 -0
- package/lib/chunk-KMAXXKJT.js +22 -0
- package/lib/chunk-L5HMOY4M.mjs +18 -0
- package/lib/chunk-NU2SJ3ST.mjs +84 -0
- package/lib/chunk-NWRANDZ6.js +98 -0
- package/lib/chunk-NXTVYIBR.mjs +11 -0
- package/lib/chunk-OYWAYMFN.js +17 -0
- package/lib/chunk-S237YNYQ.mjs +10 -0
- package/lib/chunk-SK4YK6KE.mjs +50 -0
- package/lib/chunk-UBCNCE73.js +56 -0
- package/lib/chunk-UBVYEZI3.js +1 -0
- package/lib/chunk-UOKDJT5L.mjs +16 -0
- package/lib/chunk-VLUTEDDM.mjs +41 -0
- package/lib/chunk-Y4XMF3R6.mjs +13 -0
- package/lib/chunk-YGNNHTFE.js +12 -0
- package/lib/chunk-ZVG3OFME.mjs +52 -0
- package/lib/hooks/index.d.mts +9 -0
- package/lib/hooks/index.d.ts +9 -0
- package/lib/hooks/index.js +23 -0
- package/lib/hooks/index.mjs +34 -0
- package/lib/hooks/useAuthConfig.d.mts +29 -0
- package/lib/hooks/useAuthConfig.d.ts +29 -0
- package/lib/hooks/useAuthConfig.js +8 -0
- package/lib/hooks/useAuthConfig.mjs +8 -0
- package/lib/hooks/useCurrentRouteName/index.d.mts +26 -0
- package/lib/hooks/useCurrentRouteName/index.d.ts +26 -0
- package/lib/hooks/useCurrentRouteName/index.js +7 -0
- package/lib/hooks/useCurrentRouteName/index.mjs +7 -0
- package/lib/hooks/useCurrentRouteName/index.native.d.mts +26 -0
- package/lib/hooks/useCurrentRouteName/index.native.d.ts +26 -0
- package/lib/hooks/useCurrentRouteName/index.native.js +14 -0
- package/lib/hooks/useCurrentRouteName/index.native.mjs +14 -0
- package/lib/hooks/useKeycloak/index.d.mts +70 -0
- package/lib/hooks/useKeycloak/index.d.ts +70 -0
- package/lib/hooks/useKeycloak/index.js +11 -0
- package/lib/hooks/useKeycloak/index.mjs +12 -0
- package/lib/hooks/useKeycloak/useKeycloak.d.mts +2 -0
- package/lib/hooks/useKeycloak/useKeycloak.d.ts +2 -0
- package/lib/hooks/useKeycloak/useKeycloak.js +10 -0
- package/lib/hooks/useKeycloak/useKeycloak.mjs +11 -0
- package/lib/hooks/useKeycloak/useKeycloak.native.d.mts +44 -0
- package/lib/hooks/useKeycloak/useKeycloak.native.d.ts +44 -0
- package/lib/hooks/useKeycloak/useKeycloak.native.js +80 -0
- package/lib/hooks/useKeycloak/useKeycloak.native.mjs +69 -0
- package/lib/hooks/useLogin.d.mts +26 -0
- package/lib/hooks/useLogin.d.ts +26 -0
- package/lib/hooks/useLogin.js +11 -0
- package/lib/hooks/useLogin.mjs +11 -0
- package/lib/hooks/useTokensFromQuery/index.d.mts +26 -0
- package/lib/hooks/useTokensFromQuery/index.d.ts +26 -0
- package/lib/hooks/useTokensFromQuery/index.js +7 -0
- package/lib/hooks/useTokensFromQuery/index.mjs +7 -0
- package/lib/hooks/useTokensFromQuery/index.native.d.mts +26 -0
- package/lib/hooks/useTokensFromQuery/index.native.d.ts +26 -0
- package/lib/hooks/useTokensFromQuery/index.native.js +10 -0
- package/lib/hooks/useTokensFromQuery/index.native.mjs +7 -0
- package/lib/hooks/useTokensFromState.d.mts +26 -0
- package/lib/hooks/useTokensFromState.d.ts +26 -0
- package/lib/hooks/useTokensFromState.js +9 -0
- package/lib/hooks/useTokensFromState.mjs +9 -0
- package/lib/index.d.mts +13 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.js +15 -15
- package/lib/index.mjs +56 -0
- package/lib/provider/afterAuth.d.mts +32 -0
- package/lib/provider/afterAuth.d.ts +32 -0
- package/lib/provider/afterAuth.js +18 -0
- package/lib/provider/afterAuth.mjs +18 -0
- package/lib/provider/index.d.mts +42 -0
- package/lib/provider/index.d.ts +42 -0
- package/lib/provider/index.js +22 -0
- package/lib/provider/index.mjs +25 -0
- package/lib/provider/keycloakProvider.d.mts +45 -0
- package/lib/provider/keycloakProvider.d.ts +45 -0
- package/lib/provider/keycloakProvider.js +20 -0
- package/lib/provider/keycloakProvider.mjs +21 -0
- package/lib/provider/keycloakProvider.native.d.mts +32 -0
- package/lib/provider/keycloakProvider.native.d.ts +32 -0
- package/lib/provider/keycloakProvider.native.js +87 -0
- package/lib/provider/keycloakProvider.native.mjs +30 -0
- package/lib/state/index.d.mts +34 -0
- package/lib/state/index.d.ts +34 -0
- package/lib/state/index.js +7 -0
- package/lib/state/index.mjs +7 -0
- package/lib/types.d.mts +40 -0
- package/lib/types.d.ts +40 -0
- package/lib/types.js +8 -0
- package/lib/types.mjs +7 -0
- package/lib/util.d.mts +27 -0
- package/lib/util.d.ts +27 -0
- package/lib/util.js +8 -0
- package/lib/util.mjs +9 -0
- package/package.json +30 -30
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AfterAuth
|
|
3
|
+
} from "./chunk-SK4YK6KE.mjs";
|
|
4
|
+
import {
|
|
5
|
+
useAuthConfig
|
|
6
|
+
} from "./chunk-Y4XMF3R6.mjs";
|
|
7
|
+
import {
|
|
8
|
+
useAuthState
|
|
9
|
+
} from "./chunk-AD6VPMID.mjs";
|
|
10
|
+
import {
|
|
11
|
+
validToken
|
|
12
|
+
} from "./chunk-7MTPH6XK.mjs";
|
|
13
|
+
|
|
14
|
+
// src/provider/keycloakProvider.tsx
|
|
15
|
+
import Keycloak from "@bitspur/keycloak-js";
|
|
16
|
+
import React, { useMemo, useEffect, useState, useCallback } from "react";
|
|
17
|
+
import { MultiPlatform } from "multiplatform.one";
|
|
18
|
+
import { ReactKeycloakProvider } from "@bitspur/react-keycloak-web";
|
|
19
|
+
import { SSRKeycloakProvider, SSRCookies } from "@bitspur/react-keycloak-ssr";
|
|
20
|
+
import { useRouter } from "next/router";
|
|
21
|
+
var logger = console;
|
|
22
|
+
function KeycloakProvider({
|
|
23
|
+
children,
|
|
24
|
+
cookies,
|
|
25
|
+
keycloakConfig,
|
|
26
|
+
keycloakInitOptions,
|
|
27
|
+
loadingComponent,
|
|
28
|
+
onEvent,
|
|
29
|
+
onTokens
|
|
30
|
+
}) {
|
|
31
|
+
const { debug, ensureFreshness, persist, messageHandlerKeys, ssr } = useAuthConfig();
|
|
32
|
+
const authState = useAuthState();
|
|
33
|
+
const LoadingComponent = loadingComponent || (() => /* @__PURE__ */ React.createElement(React.Fragment, null, debug ? "authenticating" : null));
|
|
34
|
+
const { query } = MultiPlatform.isNext ? useRouter() : { query: {} };
|
|
35
|
+
const [refreshToken, setRefreshToken] = useState(
|
|
36
|
+
validToken(
|
|
37
|
+
ensureFreshness && ("refreshToken" in query && (query.refreshToken?.toString() || true) || persist && authState.refreshToken) || false
|
|
38
|
+
)
|
|
39
|
+
);
|
|
40
|
+
const [idToken, setIdToken] = useState(
|
|
41
|
+
validToken(
|
|
42
|
+
"idToken" in query && (query.idToken?.toString() || true) || persist && authState.idToken || false,
|
|
43
|
+
refreshToken
|
|
44
|
+
)
|
|
45
|
+
);
|
|
46
|
+
const [token, setToken] = useState(
|
|
47
|
+
validToken(
|
|
48
|
+
"token" in query && (query.token?.toString() || true) || persist && authState.token || false,
|
|
49
|
+
refreshToken
|
|
50
|
+
)
|
|
51
|
+
);
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (token !== true && refreshToken !== true && idToken !== true)
|
|
54
|
+
return;
|
|
55
|
+
if (debug)
|
|
56
|
+
logger.debug("post message", JSON.stringify({ type: "LOADED" }));
|
|
57
|
+
(messageHandlerKeys || []).forEach((key) => {
|
|
58
|
+
window?.webkit?.messageHandlers?.[key]?.postMessage(JSON.stringify({ type: "LOADED" }));
|
|
59
|
+
});
|
|
60
|
+
window?.ReactNativeWebView?.postMessage(JSON.stringify({ type: "LOADED" }));
|
|
61
|
+
window?.parent?.postMessage(JSON.stringify({ type: "LOADED" }));
|
|
62
|
+
}, []);
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
if (refreshToken !== true)
|
|
65
|
+
return;
|
|
66
|
+
const messageCallback = (e) => {
|
|
67
|
+
let data = e?.data || null;
|
|
68
|
+
if (typeof data === "string") {
|
|
69
|
+
try {
|
|
70
|
+
data = JSON.parse(data);
|
|
71
|
+
} catch (err) {
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (debug)
|
|
75
|
+
logger.debug("received message", JSON.stringify(data));
|
|
76
|
+
const message = data;
|
|
77
|
+
if (!message.type)
|
|
78
|
+
return;
|
|
79
|
+
if (message.type.toUpperCase() === "REFRESH_TOKEN" && message.payload) {
|
|
80
|
+
if (debug)
|
|
81
|
+
logger.debug("setting refresh token", message.payload);
|
|
82
|
+
setRefreshToken(validToken(message.payload));
|
|
83
|
+
window.removeEventListener("message", messageCallback);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
if (debug) {
|
|
87
|
+
logger.debug(
|
|
88
|
+
"listening for message",
|
|
89
|
+
JSON.stringify({
|
|
90
|
+
type: "REFRESH_TOKEN",
|
|
91
|
+
payload: "<some_refresh_token>"
|
|
92
|
+
})
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
window.addEventListener("message", messageCallback);
|
|
96
|
+
return () => {
|
|
97
|
+
window.removeEventListener("message", messageCallback);
|
|
98
|
+
};
|
|
99
|
+
}, []);
|
|
100
|
+
useEffect(() => {
|
|
101
|
+
if (token !== true)
|
|
102
|
+
return;
|
|
103
|
+
const messageCallback = (e) => {
|
|
104
|
+
let data = e?.data || null;
|
|
105
|
+
if (typeof data === "string") {
|
|
106
|
+
try {
|
|
107
|
+
data = JSON.parse(data);
|
|
108
|
+
} catch (err) {
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (debug)
|
|
112
|
+
logger.debug("received message", JSON.stringify(data));
|
|
113
|
+
const message = data;
|
|
114
|
+
if (!message.type)
|
|
115
|
+
return;
|
|
116
|
+
if (message.type.toUpperCase() === "TOKEN" && message.payload) {
|
|
117
|
+
if (debug)
|
|
118
|
+
logger.debug("setting token", message.payload);
|
|
119
|
+
setToken(validToken(message.payload, refreshToken));
|
|
120
|
+
window.removeEventListener("message", messageCallback);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
if (debug) {
|
|
124
|
+
logger.debug(
|
|
125
|
+
"listening for message",
|
|
126
|
+
JSON.stringify({
|
|
127
|
+
type: "TOKEN",
|
|
128
|
+
payload: "<some_token>"
|
|
129
|
+
})
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
window.addEventListener("message", messageCallback);
|
|
133
|
+
return () => {
|
|
134
|
+
window.removeEventListener("message", messageCallback);
|
|
135
|
+
};
|
|
136
|
+
}, []);
|
|
137
|
+
useEffect(() => {
|
|
138
|
+
if (idToken !== true)
|
|
139
|
+
return;
|
|
140
|
+
const messageCallback = (e) => {
|
|
141
|
+
let data = e?.data || null;
|
|
142
|
+
if (typeof data === "string") {
|
|
143
|
+
try {
|
|
144
|
+
data = JSON.parse(data);
|
|
145
|
+
} catch (err) {
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (debug)
|
|
149
|
+
logger.debug("received message", JSON.stringify(data));
|
|
150
|
+
const message = data;
|
|
151
|
+
if (!message.type)
|
|
152
|
+
return;
|
|
153
|
+
if (message.type.toUpperCase() === "ID_TOKEN" && message.payload) {
|
|
154
|
+
if (debug)
|
|
155
|
+
logger.debug("setting id token", message.payload);
|
|
156
|
+
setIdToken(validToken(message.payload, refreshToken));
|
|
157
|
+
window.removeEventListener("message", messageCallback);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
if (debug) {
|
|
161
|
+
logger.debug(
|
|
162
|
+
"listening for message",
|
|
163
|
+
JSON.stringify({
|
|
164
|
+
type: "ID_TOKEN",
|
|
165
|
+
payload: "<some_id_token>"
|
|
166
|
+
})
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
window.addEventListener("message", messageCallback);
|
|
170
|
+
return () => {
|
|
171
|
+
window.removeEventListener("message", messageCallback);
|
|
172
|
+
};
|
|
173
|
+
}, []);
|
|
174
|
+
const handleEvent = useCallback((eventType, error) => {
|
|
175
|
+
if (error) {
|
|
176
|
+
if (typeof error === "string")
|
|
177
|
+
throw new Error(error);
|
|
178
|
+
const err = new Error(error.error);
|
|
179
|
+
err.errorDescription = error.error_description;
|
|
180
|
+
throw err;
|
|
181
|
+
}
|
|
182
|
+
if (onEvent)
|
|
183
|
+
onEvent(eventType, error);
|
|
184
|
+
}, []);
|
|
185
|
+
const keycloak = useMemo(
|
|
186
|
+
() => typeof window !== "undefined" && new Keycloak({
|
|
187
|
+
url: keycloakConfig.url,
|
|
188
|
+
realm: keycloakConfig.realm,
|
|
189
|
+
clientId: keycloakConfig.clientId
|
|
190
|
+
}),
|
|
191
|
+
[keycloakInitOptions, token, refreshToken, idToken]
|
|
192
|
+
);
|
|
193
|
+
const initOptions = useMemo(() => {
|
|
194
|
+
const initOptions2 = {
|
|
195
|
+
...defaultKeycloakInitOptions,
|
|
196
|
+
...keycloakInitOptions
|
|
197
|
+
};
|
|
198
|
+
if (debug)
|
|
199
|
+
initOptions2.enableLogging = true;
|
|
200
|
+
if (token && typeof token === "string") {
|
|
201
|
+
initOptions2.token = token;
|
|
202
|
+
initOptions2.timeSkew = 0;
|
|
203
|
+
if (idToken && typeof idToken === "string")
|
|
204
|
+
initOptions2.idToken = idToken;
|
|
205
|
+
if (refreshToken && typeof refreshToken === "string")
|
|
206
|
+
initOptions2.refreshToken = refreshToken;
|
|
207
|
+
}
|
|
208
|
+
if (typeof window !== "undefined" && window.self !== window.top || token && !refreshToken) {
|
|
209
|
+
initOptions2.checkLoginIframe = false;
|
|
210
|
+
initOptions2.flow = "implicit";
|
|
211
|
+
initOptions2.onLoad = void 0;
|
|
212
|
+
}
|
|
213
|
+
return initOptions2;
|
|
214
|
+
}, [keycloakInitOptions, token, refreshToken, idToken]);
|
|
215
|
+
if (token === true || idToken === true || refreshToken === true)
|
|
216
|
+
return /* @__PURE__ */ React.createElement(LoadingComponent, null);
|
|
217
|
+
if (cookies && ssr) {
|
|
218
|
+
return (
|
|
219
|
+
// @ts-ignore
|
|
220
|
+
/* @__PURE__ */ React.createElement(
|
|
221
|
+
SSRKeycloakProvider,
|
|
222
|
+
{
|
|
223
|
+
LoadingComponent: /* @__PURE__ */ React.createElement(LoadingComponent, null),
|
|
224
|
+
autoRefreshToken: initOptions.checkLoginIframe,
|
|
225
|
+
initOptions,
|
|
226
|
+
onEvent: handleEvent,
|
|
227
|
+
onTokens,
|
|
228
|
+
persistor: SSRCookies(cookies),
|
|
229
|
+
keycloakConfig: {
|
|
230
|
+
clientId: keycloakConfig.clientId,
|
|
231
|
+
realm: keycloakConfig.realm,
|
|
232
|
+
url: keycloakConfig.url
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
/* @__PURE__ */ React.createElement(AfterAuth, null, children)
|
|
236
|
+
)
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
if (!keycloak)
|
|
240
|
+
return /* @__PURE__ */ React.createElement(LoadingComponent, null);
|
|
241
|
+
return /* @__PURE__ */ React.createElement(
|
|
242
|
+
ReactKeycloakProvider,
|
|
243
|
+
{
|
|
244
|
+
LoadingComponent: /* @__PURE__ */ React.createElement(LoadingComponent, null),
|
|
245
|
+
authClient: keycloak,
|
|
246
|
+
autoRefreshToken: initOptions.checkLoginIframe,
|
|
247
|
+
initOptions,
|
|
248
|
+
onEvent: handleEvent,
|
|
249
|
+
onTokens
|
|
250
|
+
},
|
|
251
|
+
/* @__PURE__ */ React.createElement(AfterAuth, null, children)
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
var defaultKeycloakInitOptions = {
|
|
255
|
+
checkLoginIframe: true,
|
|
256
|
+
checkLoginIframeInterval: 5,
|
|
257
|
+
enableLogging: false,
|
|
258
|
+
onLoad: "check-sso",
|
|
259
|
+
pkceMethod: "S256"
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
export {
|
|
263
|
+
KeycloakProvider,
|
|
264
|
+
defaultKeycloakInitOptions
|
|
265
|
+
};
|