@multiplatform.one/keycloak 0.2.16 → 0.3.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/authConfig.mjs +2 -2
- package/dist/esm/authConfig.mjs.map +1 -1
- package/dist/esm/provider/afterAuth.mjs +1 -1
- package/dist/esm/provider/afterAuth.mjs.map +2 -2
- package/dist/esm/provider/index.mjs +64 -8
- package/dist/esm/provider/index.mjs.map +2 -2
- package/dist/esm/provider/keycloakProvider.mjs +32 -10
- package/dist/esm/provider/keycloakProvider.mjs.map +2 -2
- package/dist/esm/provider/keycloakProvider.native.mjs +1 -1
- package/dist/esm/provider/keycloakProvider.native.mjs.map +1 -1
- package/dist/jsx/authConfig.mjs +2 -2
- package/dist/jsx/authConfig.mjs.map +1 -1
- package/dist/jsx/provider/afterAuth.mjs +1 -1
- package/dist/jsx/provider/afterAuth.mjs.map +2 -2
- package/dist/jsx/provider/index.mjs +45 -7
- package/dist/jsx/provider/index.mjs.map +2 -2
- package/dist/jsx/provider/keycloakProvider.mjs +30 -10
- package/dist/jsx/provider/keycloakProvider.mjs.map +2 -2
- package/dist/jsx/provider/keycloakProvider.native.mjs +1 -1
- package/dist/jsx/provider/keycloakProvider.native.mjs.map +1 -1
- package/dist/lib/authConfig.d.ts +1 -1
- package/dist/lib/authConfig.js +2 -2
- package/dist/lib/authConfig.js.map +1 -1
- package/dist/lib/provider/afterAuth.js +1 -1
- package/dist/lib/provider/afterAuth.js.map +2 -2
- package/dist/lib/provider/index.d.ts +11 -4
- package/dist/lib/provider/index.js +77 -7
- package/dist/lib/provider/index.js.map +2 -2
- package/dist/lib/provider/keycloakProvider.d.ts +3 -10
- package/dist/lib/provider/keycloakProvider.js +33 -11
- package/dist/lib/provider/keycloakProvider.js.map +2 -2
- package/dist/lib/provider/keycloakProvider.native.js +1 -1
- package/dist/lib/provider/keycloakProvider.native.js.map +1 -1
- package/dist/lib/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +4 -4
- package/src/authConfig.ts +3 -3
- package/src/provider/afterAuth.tsx +1 -1
- package/src/provider/index.tsx +65 -12
- package/src/provider/keycloakProvider.native.tsx +1 -1
- package/src/provider/keycloakProvider.tsx +28 -19
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/provider/index.tsx"],
|
|
4
|
-
"sourcesContent": ["/**\n * File: /auth/provider/index.tsx\n * Project: app\n * File Created: 08-11-2022 06:33:07\n * Author: Clay Risser\n * -----\n * Last Modified: 22-11-2022 18:00:13\n * Modified By: Clay Risser\n * -----\n * Risser Labs LLC (c) Copyright 2021 - 2022\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React, { useMemo } from 'react';\nimport type { AuthConfig } from '../authConfig';\nimport type { FC } from 'react';\nimport type {
|
|
5
|
-
"mappings": "AAwBA,SAAgB,eAAe;
|
|
4
|
+
"sourcesContent": ["/**\n * File: /auth/provider/index.tsx\n * Project: app\n * File Created: 08-11-2022 06:33:07\n * Author: Clay Risser\n * -----\n * Last Modified: 22-11-2022 18:00:13\n * Modified By: Clay Risser\n * -----\n * Risser Labs LLC (c) Copyright 2021 - 2022\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React, { useMemo } from 'react';\nimport type { AuthClientEvent, AuthClientError, AuthClientTokens } from '@bitspur/react-keycloak-core';\nimport type { AuthConfig } from '../authConfig';\nimport type { FC, ComponentType, ReactNode } from 'react';\nimport type { KeycloakConfig, KeycloakInitOptions } from '@bitspur/keycloak-js';\nimport { AuthConfigContext, defaultAuthConfig } from '../authConfig';\nimport { KeycloakProvider } from './keycloakProvider';\n// @ts-ignore\nimport { config } from 'app/config';\n\nexport interface AuthProviderProps\n extends Partial<Omit<AuthConfig, 'persist'>>,\n Omit<KeycloakConfig, 'url'>,\n KeycloakInitOptions {\n children: ReactNode;\n cookies?: unknown;\n disabled?: boolean;\n baseUrl?: string;\n loadingComponent?: ComponentType;\n onEvent?: (eventType: AuthClientEvent, error?: AuthClientError) => void;\n onTokens?: (tokens: AuthClientTokens) => void;\n}\n\nconst persist = config.get('KEYCLOAK_PERSIST') === '1';\n\nexport const AuthProvider: FC<AuthProviderProps> = ({\n // keycloak config\n realm,\n clientId,\n baseUrl,\n // auth config\n debug,\n ensureFreshness,\n loginRoute,\n messageHandlerKeys,\n ssr,\n // props\n children,\n cookies,\n disabled,\n loadingComponent,\n onEvent,\n onTokens,\n ...keycloakInitOptions\n}: AuthProviderProps) => {\n const authConfig = useMemo(\n () => ({\n ...defaultAuthConfig,\n debug,\n ensureFreshness,\n loginRoute,\n messageHandlerKeys,\n persist,\n ssr,\n }),\n [debug, ensureFreshness, loginRoute, messageHandlerKeys, persist, ssr],\n );\n\n if (disabled) {\n return <AuthConfigContext.Provider value={authConfig}>{children}</AuthConfigContext.Provider>;\n }\n\n return (\n <AuthConfigContext.Provider value={authConfig}>\n <KeycloakProvider\n cookies={cookies}\n keycloakConfig={{\n realm,\n clientId,\n url: baseUrl,\n }}\n keycloakInitOptions={keycloakInitOptions}\n loadingComponent={loadingComponent}\n onEvent={onEvent}\n onTokens={onTokens}\n >\n {children}\n </KeycloakProvider>\n </AuthConfigContext.Provider>\n );\n};\n\nexport * from './keycloakProvider';\n"],
|
|
5
|
+
"mappings": "AAwBA,SAAgB,eAAe;AAK/B,SAAS,mBAAmB,yBAAyB;AACrD,SAAS,wBAAwB;AAEjC,SAAS,cAAc;AAevB,MAAM,UAAU,OAAO,IAAI,kBAAkB,MAAM;AAE5C,MAAM,eAAsC,CAAC;AAAA;AAAA,EAElD;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,GAAG;AACL,MAAyB;AACvB,QAAM,aAAa;AAAA,IACjB,OAAO;AAAA,MACL,GAAG;AAAA,MACH;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,IACA,CAAC,OAAO,iBAAiB,YAAY,oBAAoB,SAAS,GAAG;AAAA,EACvE;AAEA,MAAI,UAAU;AACZ,WAAO,CAAC,kBAAkB,SAAS,OAAO,aAAa,SAAS,EAAxD,kBAAkB;AAAA,EAC5B;AAEA,SACE,CAAC,kBAAkB,SAAS,OAAO,YACjC,CAAC;AAAA,IACC,SAAS;AAAA,IACT,gBAAgB;AAAA,MACd;AAAA,MACA;AAAA,MACA,KAAK;AAAA,IACP;AAAA,IACA,qBAAqB;AAAA,IACrB,kBAAkB;AAAA,IAClB,SAAS;AAAA,IACT,UAAU;AAAA,IAET,SACH,EAbC,iBAcH,EAfC,kBAAkB;AAiBvB;AAEA,cAAc;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -11,25 +11,24 @@ const logger = console;
|
|
|
11
11
|
function KeycloakProvider({
|
|
12
12
|
children,
|
|
13
13
|
cookies,
|
|
14
|
-
debug,
|
|
15
14
|
keycloakConfig,
|
|
16
15
|
keycloakInitOptions,
|
|
17
16
|
loadingComponent,
|
|
18
17
|
onEvent,
|
|
19
18
|
onTokens
|
|
20
19
|
}) {
|
|
20
|
+
const { debug, ensureFreshness, persist, messageHandlerKeys, ssr } = useAuthConfig();
|
|
21
21
|
const authState = useAuthState();
|
|
22
22
|
const LoadingComponent = loadingComponent || (() => <>{debug ? "authenticating" : null}</>);
|
|
23
23
|
const { query } = MultiPlatform.isNext ? useRouter() : { query: {} };
|
|
24
|
-
const authConfig = useAuthConfig();
|
|
25
24
|
const [idToken, setIdToken] = useState(
|
|
26
|
-
"idToken" in query && (query.idToken?.toString() || true) ||
|
|
25
|
+
"idToken" in query && (query.idToken?.toString() || true) || persist && authState.idToken || false
|
|
27
26
|
);
|
|
28
27
|
const [token, setToken] = useState(
|
|
29
|
-
"token" in query && (query.token?.toString() || true) ||
|
|
28
|
+
"token" in query && (query.token?.toString() || true) || persist && authState.token || false
|
|
30
29
|
);
|
|
31
30
|
const [refreshToken, setRefreshToken] = useState(
|
|
32
|
-
|
|
31
|
+
ensureFreshness && ("refreshToken" in query && (query.refreshToken?.toString() || true) || persist && authState.refreshToken) || false
|
|
33
32
|
);
|
|
34
33
|
const explicitToken = "idToken" in query || "token" in query || "refreshToken" in query;
|
|
35
34
|
useEffect(() => {
|
|
@@ -37,12 +36,20 @@ function KeycloakProvider({
|
|
|
37
36
|
return;
|
|
38
37
|
if (debug)
|
|
39
38
|
logger.debug("post message", { type: "LOADED" });
|
|
40
|
-
(
|
|
39
|
+
(messageHandlerKeys || []).forEach((key) => {
|
|
41
40
|
window?.webkit?.messageHandlers?.[key]?.postMessage(JSON.stringify({ type: "LOADED" }));
|
|
42
41
|
});
|
|
43
42
|
window?.ReactNativeWebView?.postMessage(JSON.stringify({ type: "LOADED" }));
|
|
44
43
|
window?.parent?.postMessage(JSON.stringify({ type: "LOADED" }));
|
|
45
44
|
}, []);
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
if (typeof idToken === "string" && !isTokenValid(idToken))
|
|
47
|
+
setIdToken(false);
|
|
48
|
+
if (typeof refreshToken === "string" && !isTokenValid(refreshToken))
|
|
49
|
+
setRefreshToken(false);
|
|
50
|
+
if (typeof token === "string" && !isTokenValid(token))
|
|
51
|
+
setToken(false);
|
|
52
|
+
}, [idToken, token, refreshToken]);
|
|
46
53
|
useEffect(() => {
|
|
47
54
|
if (token !== true)
|
|
48
55
|
return;
|
|
@@ -155,7 +162,7 @@ function KeycloakProvider({
|
|
|
155
162
|
}, []);
|
|
156
163
|
const keycloak = useMemo(
|
|
157
164
|
() => typeof window !== "undefined" && new Keycloak({
|
|
158
|
-
url: keycloakConfig.
|
|
165
|
+
url: keycloakConfig.url,
|
|
159
166
|
realm: keycloakConfig.realm,
|
|
160
167
|
clientId: keycloakConfig.clientId
|
|
161
168
|
}),
|
|
@@ -183,11 +190,12 @@ function KeycloakProvider({
|
|
|
183
190
|
}, [keycloakInitOptions, token, refreshToken, idToken]);
|
|
184
191
|
if (token === true || idToken === true || refreshToken === true)
|
|
185
192
|
return <LoadingComponent />;
|
|
186
|
-
if (cookies &&
|
|
193
|
+
if (cookies && ssr) {
|
|
187
194
|
return (
|
|
188
195
|
// @ts-ignore
|
|
189
196
|
<SSRKeycloakProvider
|
|
190
197
|
LoadingComponent={<LoadingComponent />}
|
|
198
|
+
autoRefreshToken={false}
|
|
191
199
|
initOptions={initOptions}
|
|
192
200
|
onEvent={handleEvent}
|
|
193
201
|
onTokens={onTokens}
|
|
@@ -195,7 +203,7 @@ function KeycloakProvider({
|
|
|
195
203
|
keycloakConfig={{
|
|
196
204
|
clientId: keycloakConfig.clientId,
|
|
197
205
|
realm: keycloakConfig.realm,
|
|
198
|
-
url: keycloakConfig.
|
|
206
|
+
url: keycloakConfig.url
|
|
199
207
|
}}
|
|
200
208
|
><AfterAuth>{children}</AfterAuth></SSRKeycloakProvider>
|
|
201
209
|
);
|
|
@@ -205,11 +213,22 @@ function KeycloakProvider({
|
|
|
205
213
|
return <ReactKeycloakProvider
|
|
206
214
|
LoadingComponent={<LoadingComponent />}
|
|
207
215
|
authClient={keycloak}
|
|
216
|
+
autoRefreshToken={false}
|
|
208
217
|
initOptions={initOptions}
|
|
209
218
|
onEvent={handleEvent}
|
|
210
219
|
onTokens={onTokens}
|
|
211
220
|
><AfterAuth>{children}</AfterAuth></ReactKeycloakProvider>;
|
|
212
221
|
}
|
|
222
|
+
function isTokenValid(token) {
|
|
223
|
+
try {
|
|
224
|
+
const { exp } = JSON.parse(Buffer.from(token.split(".")?.[1] || "", "base64").toString() || "{}");
|
|
225
|
+
return Math.floor(Date.now() / 1e3) <= exp;
|
|
226
|
+
} catch (err) {
|
|
227
|
+
if (err instanceof SyntaxError)
|
|
228
|
+
return false;
|
|
229
|
+
throw err;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
213
232
|
const defaultKeycloakInitOptions = {
|
|
214
233
|
checkLoginIframe: true,
|
|
215
234
|
checkLoginIframeInterval: 5,
|
|
@@ -219,6 +238,7 @@ const defaultKeycloakInitOptions = {
|
|
|
219
238
|
};
|
|
220
239
|
export {
|
|
221
240
|
KeycloakProvider,
|
|
222
|
-
defaultKeycloakInitOptions
|
|
241
|
+
defaultKeycloakInitOptions,
|
|
242
|
+
isTokenValid
|
|
223
243
|
};
|
|
224
244
|
//# sourceMappingURL=keycloakProvider.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/provider/keycloakProvider.tsx"],
|
|
4
|
-
"sourcesContent": ["/**\n * File: /auth/provider/keycloakProvider.tsx\n * Project: app\n * File Created: 08-11-2022 03:12:43\n * Author: Clay Risser\n * -----\n * Last Modified: 22-11-2022 18:07:46\n * Modified By: Clay Risser\n * -----\n * Risser Labs LLC (c) Copyright 2021 - 2022\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport Keycloak from '@bitspur/keycloak-js';\nimport React, { useMemo, useEffect, useState, useCallback } from 'react';\nimport type { AuthClientEvent, AuthClientError, AuthClientTokens } from '@bitspur/react-keycloak-core';\nimport type { KeycloakInitOptions } from '@bitspur/keycloak-js';\nimport type { ReactNode, ComponentType } from 'react';\nimport { AfterAuth } from './afterAuth';\nimport { MultiPlatform } from 'multiplatform.one';\nimport { ReactKeycloakProvider } from '@bitspur/react-keycloak-web';\nimport { SSRKeycloakProvider, SSRCookies } from '@bitspur/react-keycloak-ssr';\nimport { useAuthConfig } from '../hooks/useAuthConfig';\nimport { useAuthState } from '../state';\nimport { useRouter } from 'next/router';\n\nexport interface KeycloakProviderProps {\n children: ReactNode;\n cookies?: unknown;\n
|
|
5
|
-
"mappings": "AAwBA,OAAO,cAAc;AACrB,SAAgB,SAAS,WAAW,UAAU,mBAAmB;AAIjE,SAAS,iBAAiB;AAC1B,SAAS,qBAAqB;AAC9B,SAAS,6BAA6B;AACtC,SAAS,qBAAqB,kBAAkB;AAChD,SAAS,qBAAqB;AAC9B,SAAS,oBAAoB;AAC7B,SAAS,iBAAiB;
|
|
4
|
+
"sourcesContent": ["/**\n * File: /auth/provider/keycloakProvider.tsx\n * Project: app\n * File Created: 08-11-2022 03:12:43\n * Author: Clay Risser\n * -----\n * Last Modified: 22-11-2022 18:07:46\n * Modified By: Clay Risser\n * -----\n * Risser Labs LLC (c) Copyright 2021 - 2022\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport Keycloak from '@bitspur/keycloak-js';\nimport React, { useMemo, useEffect, useState, useCallback } from 'react';\nimport type { AuthClientEvent, AuthClientError, AuthClientTokens } from '@bitspur/react-keycloak-core';\nimport type { KeycloakInitOptions, KeycloakConfig } from '@bitspur/keycloak-js';\nimport type { ReactNode, ComponentType } from 'react';\nimport { AfterAuth } from './afterAuth';\nimport { MultiPlatform } from 'multiplatform.one';\nimport { ReactKeycloakProvider } from '@bitspur/react-keycloak-web';\nimport { SSRKeycloakProvider, SSRCookies } from '@bitspur/react-keycloak-ssr';\nimport { useAuthConfig } from '../hooks/useAuthConfig';\nimport { useAuthState } from '../state';\nimport { useRouter } from 'next/router';\n\nexport interface KeycloakProviderProps {\n children: ReactNode;\n cookies?: unknown;\n keycloakConfig: KeycloakConfig;\n keycloakInitOptions?: KeycloakInitOptions;\n loadingComponent?: ComponentType;\n onEvent?: (eventType: AuthClientEvent, error?: AuthClientError) => void;\n onTokens?: (tokens: AuthClientTokens) => void;\n}\n\nconst logger = console;\n\nexport function KeycloakProvider({\n children,\n cookies,\n keycloakConfig,\n keycloakInitOptions,\n loadingComponent,\n onEvent,\n onTokens,\n}: KeycloakProviderProps) {\n const { debug, ensureFreshness, persist, messageHandlerKeys, ssr } = useAuthConfig();\n const authState = useAuthState();\n const LoadingComponent = loadingComponent || (() => <>{debug ? 'authenticating' : null}</>);\n const { query } = MultiPlatform.isNext ? useRouter() : { query: {} };\n const [idToken, setIdToken] = useState<string | boolean>(\n ('idToken' in query && (query.idToken?.toString() || true)) || (persist && authState.idToken) || false,\n );\n const [token, setToken] = useState<string | boolean>(\n ('token' in query && (query.token?.toString() || true)) || (persist && authState.token) || false,\n );\n const [refreshToken, setRefreshToken] = useState<string | boolean>(\n (ensureFreshness &&\n (('refreshToken' in query && (query.refreshToken?.toString() || true)) || (persist && authState.refreshToken))) ||\n false,\n );\n const explicitToken = 'idToken' in query || 'token' in query || 'refreshToken' in query;\n\n useEffect(() => {\n if (token !== true && refreshToken !== true && idToken !== true) return;\n if (debug) logger.debug('post message', { type: 'LOADED' });\n (messageHandlerKeys || []).forEach((key: string) => {\n window?.webkit?.messageHandlers?.[key]?.postMessage(JSON.stringify({ type: 'LOADED' }));\n });\n window?.ReactNativeWebView?.postMessage(JSON.stringify({ type: 'LOADED' }));\n window?.parent?.postMessage(JSON.stringify({ type: 'LOADED' }));\n }, []);\n\n useEffect(() => {\n if (typeof idToken === 'string' && !isTokenValid(idToken)) setIdToken(false);\n if (typeof refreshToken === 'string' && !isTokenValid(refreshToken)) setRefreshToken(false);\n if (typeof token === 'string' && !isTokenValid(token)) setToken(false);\n }, [idToken, token, refreshToken]);\n\n useEffect(() => {\n if (token !== true) return;\n const messageCallback = (e: MessageEvent<any>) => {\n let data = e?.data;\n if (typeof data === 'string') {\n try {\n data = JSON.parse(data);\n } catch (err) {}\n }\n if (debug) logger.debug('received message', data);\n const message: MessageSchema = data;\n if (!message.type) return;\n if (message.type.toUpperCase() === 'TOKEN' && message.payload) {\n if (debug) logger.debug('setting token', message.payload);\n setToken(message.payload);\n }\n };\n if (debug) {\n logger.debug('listening for message', {\n type: 'TOKEN',\n payload: '<some_token>',\n });\n }\n window.addEventListener('message', messageCallback);\n return () => {\n window.removeEventListener('message', messageCallback);\n };\n }, []);\n\n useEffect(() => {\n if (idToken !== true) return;\n const messageCallback = (e: MessageEvent<any>) => {\n let data = e?.data;\n if (typeof data === 'string') {\n try {\n data = JSON.parse(data);\n } catch (err) {}\n }\n if (debug) logger.debug('received message', data);\n const message: MessageSchema = data;\n if (!message.type) return;\n if (message.type.toUpperCase() === 'ID_TOKEN' && message.payload) {\n if (debug) logger.debug('setting id token', message.payload);\n setIdToken(message.payload);\n }\n };\n if (debug) {\n logger.debug('listening for message', {\n type: 'ID_TOKEN',\n payload: '<some_id_token>',\n });\n }\n window.addEventListener('message', messageCallback);\n return () => {\n window.removeEventListener('message', messageCallback);\n };\n }, []);\n\n useEffect(() => {\n if (refreshToken !== true) return;\n const messageCallback = (e: MessageEvent<any>) => {\n let data = e?.data;\n if (typeof data === 'string') {\n try {\n data = JSON.parse(data);\n } catch (err) {}\n }\n if (debug) logger.debug('received message', data);\n const message: MessageSchema = data;\n if (!message.type) return;\n if (message.type.toUpperCase() === 'REFRESH_TOKEN' && message.payload) {\n if (debug) logger.debug('setting refresh token', message.payload);\n setRefreshToken(message.payload);\n }\n };\n if (debug) {\n logger.debug('listening for message', {\n type: 'REFRESH_TOKEN',\n payload: '<some_refresh_token>',\n });\n }\n window.addEventListener('message', messageCallback);\n return () => {\n window.removeEventListener('message', messageCallback);\n };\n }, []);\n\n const handleEvent = useCallback((eventType: AuthClientEvent, error?: AuthClientError) => {\n if (error) {\n if (typeof error === 'string') throw new Error(error);\n const err: Error & { errorDescription?: string } = new Error(error.error);\n err.errorDescription = error.error_description;\n throw err;\n }\n if (onEvent) onEvent(eventType, error);\n }, []);\n\n const keycloak = useMemo(\n () =>\n typeof window !== 'undefined' &&\n new Keycloak({\n url: keycloakConfig.url,\n realm: keycloakConfig.realm,\n clientId: keycloakConfig.clientId,\n }),\n [],\n );\n\n const initOptions = useMemo(() => {\n const initOptions = {\n ...defaultKeycloakInitOptions,\n ...keycloakInitOptions,\n };\n if (debug) initOptions.enableLogging = true;\n if (token && typeof token === 'string') {\n initOptions.token = token;\n if (idToken && typeof idToken === 'string') initOptions.idToken = idToken;\n if (refreshToken && typeof refreshToken === 'string') initOptions.refreshToken = refreshToken;\n if (explicitToken) {\n initOptions.checkLoginIframe = false;\n initOptions.onLoad = undefined;\n }\n }\n return initOptions;\n }, [keycloakInitOptions, token, refreshToken, idToken]);\n\n if (token === true || idToken === true || refreshToken === true) return <LoadingComponent />;\n if (cookies && ssr) {\n return (\n // @ts-ignore\n <SSRKeycloakProvider\n LoadingComponent={<LoadingComponent />}\n // keycloak-js already handles refreshing tokens\n autoRefreshToken={false}\n initOptions={initOptions}\n onEvent={handleEvent}\n onTokens={onTokens}\n persistor={SSRCookies(cookies)}\n keycloakConfig={{\n clientId: keycloakConfig.clientId,\n realm: keycloakConfig.realm,\n url: keycloakConfig.url,\n }}\n >\n <AfterAuth>{children}</AfterAuth>\n </SSRKeycloakProvider>\n );\n }\n if (!keycloak) return <>{children}</>;\n return (\n <ReactKeycloakProvider\n LoadingComponent={<LoadingComponent />}\n authClient={keycloak}\n // keycloak-js already handles refreshing tokens\n autoRefreshToken={false}\n initOptions={initOptions}\n onEvent={handleEvent}\n onTokens={onTokens}\n >\n <AfterAuth>{children}</AfterAuth>\n </ReactKeycloakProvider>\n );\n}\n\nexport function isTokenValid(token: string) {\n try {\n const { exp } = JSON.parse(Buffer.from(token.split('.')?.[1] || '', 'base64').toString() || '{}');\n return Math.floor(Date.now() / 1000) <= exp;\n } catch (err) {\n if (err instanceof SyntaxError) return false;\n throw err;\n }\n}\n\nexport const defaultKeycloakInitOptions: KeycloakInitOptions = {\n checkLoginIframe: true,\n checkLoginIframeInterval: 5,\n enableLogging: false,\n onLoad: 'check-sso',\n pkceMethod: 'S256',\n};\n\nexport interface MessageSchema {\n type: string;\n payload?: string;\n}\n"],
|
|
5
|
+
"mappings": "AAwBA,OAAO,cAAc;AACrB,SAAgB,SAAS,WAAW,UAAU,mBAAmB;AAIjE,SAAS,iBAAiB;AAC1B,SAAS,qBAAqB;AAC9B,SAAS,6BAA6B;AACtC,SAAS,qBAAqB,kBAAkB;AAChD,SAAS,qBAAqB;AAC9B,SAAS,oBAAoB;AAC7B,SAAS,iBAAiB;AAY1B,MAAM,SAAS;AAER,SAAS,iBAAiB;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAA0B;AACxB,QAAM,EAAE,OAAO,iBAAiB,SAAS,oBAAoB,IAAI,IAAI,cAAc;AACnF,QAAM,YAAY,aAAa;AAC/B,QAAM,mBAAmB,qBAAqB,MAAM,GAAG,QAAQ,mBAAmB,KAAK;AACvF,QAAM,EAAE,MAAM,IAAI,cAAc,SAAS,UAAU,IAAI,EAAE,OAAO,CAAC,EAAE;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AAI,IAAI,KAAK,MAAM,OAAO,KAAK,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,IAAI,QAAQ,EAAE,SAAS,KAAK,IAAI;AAChG,WAAO,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI,KAAK;AAAA,EAC1C,SAAS,KAAP;AACA,QAAI,eAAe;AAAa,aAAO;AACvC,UAAM;AAAA,EACR;AACF;AAEO,MAAM,6BAAkD;AAAA,EAC7D,kBAAkB;AAAA,EAClB,0BAA0B;AAAA,EAC1B,eAAe;AAAA,EACf,QAAQ;AAAA,EACR,YAAY;AACd;",
|
|
6
6
|
"names": ["initOptions"]
|
|
7
7
|
}
|
|
@@ -3,7 +3,7 @@ import { KeycloakProvider as ExpoKeycloakProvider } from "expo-keycloak-auth";
|
|
|
3
3
|
const KeycloakProvider = ({ children, keycloakConfig }) => {
|
|
4
4
|
const clonedKeycloakConfig = {
|
|
5
5
|
...keycloakConfig,
|
|
6
|
-
url: keycloakConfig.
|
|
6
|
+
url: keycloakConfig.url
|
|
7
7
|
};
|
|
8
8
|
delete keycloakConfig.baseUrl;
|
|
9
9
|
return <ExpoKeycloakProvider {...clonedKeycloakConfig}><AfterAuth>{children}</AfterAuth></ExpoKeycloakProvider>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/provider/keycloakProvider.native.tsx"],
|
|
4
|
-
"sourcesContent": ["/**\n * File: /auth/provider/keycloakProvider.native.tsx\n * Project: app\n * File Created: 08-11-2022 03:12:37\n * Author: Clay Risser\n * -----\n * Last Modified: 22-11-2022 17:52:31\n * Modified By: Clay Risser\n * -----\n * Risser Labs LLC (c) Copyright 2021 - 2022\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React from 'react';\nimport type { FC } from 'react';\nimport type { KeycloakConfig } from '@bitspur/keycloak-js';\nimport type { KeycloakProviderProps } from './keycloakProvider';\nimport { AfterAuth } from './afterAuth';\nimport { KeycloakProvider as ExpoKeycloakProvider } from 'expo-keycloak-auth';\n\nexport const KeycloakProvider: FC<KeycloakProviderProps> = ({ children, keycloakConfig }: KeycloakProviderProps) => {\n const clonedKeycloakConfig = {\n ...(keycloakConfig as Omit<KeycloakConfig, 'baseUrl'>),\n url: keycloakConfig.
|
|
4
|
+
"sourcesContent": ["/**\n * File: /auth/provider/keycloakProvider.native.tsx\n * Project: app\n * File Created: 08-11-2022 03:12:37\n * Author: Clay Risser\n * -----\n * Last Modified: 22-11-2022 17:52:31\n * Modified By: Clay Risser\n * -----\n * Risser Labs LLC (c) Copyright 2021 - 2022\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React from 'react';\nimport type { FC } from 'react';\nimport type { KeycloakConfig } from '@bitspur/keycloak-js';\nimport type { KeycloakProviderProps } from './keycloakProvider';\nimport { AfterAuth } from './afterAuth';\nimport { KeycloakProvider as ExpoKeycloakProvider } from 'expo-keycloak-auth';\n\nexport const KeycloakProvider: FC<KeycloakProviderProps> = ({ children, keycloakConfig }: KeycloakProviderProps) => {\n const clonedKeycloakConfig = {\n ...(keycloakConfig as Omit<KeycloakConfig, 'baseUrl'>),\n url: keycloakConfig.url,\n };\n // @ts-ignore\n delete keycloakConfig.baseUrl;\n return (\n <ExpoKeycloakProvider {...clonedKeycloakConfig}>\n <AfterAuth>{children}</AfterAuth>\n </ExpoKeycloakProvider>\n );\n};\n"],
|
|
5
5
|
"mappings": "AA4BA,SAAS,iBAAiB;AAC1B,SAAS,oBAAoB,4BAA4B;AAElD,MAAM,mBAA8C,CAAC,EAAE,UAAU,eAAe,MAA6B;AAClH,QAAM,uBAAuB;AAAA,IAC3B,GAAI;AAAA,IACJ,KAAK,eAAe;AAAA,EACtB;AAEA,SAAO,eAAe;AACtB,SACE,CAAC,yBAAyB,sBACxB,CAAC,WAAW,SAAS,EAApB,UACH,EAFC;AAIL;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/lib/authConfig.d.ts
CHANGED
package/dist/lib/authConfig.js
CHANGED
|
@@ -25,8 +25,8 @@ module.exports = __toCommonJS(authConfig_exports);
|
|
|
25
25
|
var import_react = require("react");
|
|
26
26
|
const defaultAuthConfig = {
|
|
27
27
|
ensureFreshness: true,
|
|
28
|
-
persist:
|
|
29
|
-
ssr:
|
|
28
|
+
persist: false,
|
|
29
|
+
ssr: false
|
|
30
30
|
};
|
|
31
31
|
const AuthConfigContext = (0, import_react.createContext)(defaultAuthConfig);
|
|
32
32
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/authConfig.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * File: /src/authConfig.ts\n * Project: app\n * File Created: 08-11-2022 09:21:15\n * Author: Clay Risser\n * -----\n * Last Modified:
|
|
4
|
+
"sourcesContent": ["/**\n * File: /src/authConfig.ts\n * Project: app\n * File Created: 08-11-2022 09:21:15\n * Author: Clay Risser\n * -----\n * Last Modified: 14-05-2023 01:22:15\n * Modified By: Clay Risser\n * -----\n * Risser Labs LLC (c) Copyright 2021 - 2022\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { createContext } from 'react';\n\nexport const defaultAuthConfig: AuthConfig = {\n ensureFreshness: true,\n persist: false,\n ssr: false,\n};\n\nexport const AuthConfigContext = createContext<AuthConfig>(defaultAuthConfig);\n\nexport interface AuthConfig {\n debug?: boolean;\n ensureFreshness?: boolean;\n loginRoute?: string;\n messageHandlerKeys?: string[];\n persist?: boolean;\n ssr?: boolean;\n}\n"],
|
|
5
5
|
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAwBA,mBAA8B;AAEvB,MAAM,oBAAgC;AAAA,EAC3C,iBAAiB;AAAA,EACjB,SAAS;AAAA,EACT,KAAK;AACP;AAEO,MAAM,wBAAoB,4BAA0B,iBAAiB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -52,7 +52,7 @@ const AfterAuth = ({ children }) => {
|
|
|
52
52
|
}, [authState.idToken]);
|
|
53
53
|
(0, import_react.useEffect)(() => {
|
|
54
54
|
if (authConfig.debug && authState.refreshToken)
|
|
55
|
-
logger.debug(authState.refreshToken);
|
|
55
|
+
logger.debug("refreshToken", authState.refreshToken);
|
|
56
56
|
}, [authState.refreshToken]);
|
|
57
57
|
(0, import_react.useEffect)(() => {
|
|
58
58
|
if (authConfig.debug)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/provider/afterAuth.tsx"],
|
|
4
|
-
"sourcesContent": ["/**\n * File: /auth/provider/afterAuth.tsx\n * Project: app\n * File Created: 22-11-2022 17:49:58\n * Author: Clay Risser\n * -----\n * Last Modified: 22-11-2022 18:00:39\n * Modified By: Clay Risser\n * -----\n * Risser Labs LLC (c) Copyright 2021 - 2022\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React, { useEffect } from 'react';\nimport type { FC, ReactNode } from 'react';\nimport { useAuthConfig } from '../hooks/useAuthConfig';\nimport { useAuthState } from '../state';\nimport { useKeycloak } from '../hooks';\n\nexport interface AfterAuthProps {\n children: ReactNode;\n}\n\nconst logger = console;\n\nexport const AfterAuth: FC<AfterAuthProps> = ({ children }: AfterAuthProps) => {\n const authConfig = useAuthConfig();\n const { token, refreshToken, authenticated, idToken } = useKeycloak();\n const authState = useAuthState();\n\n useEffect(() => {\n if (!authenticated) return;\n if (authConfig.persist && token) {\n authState.setToken(token);\n if (idToken) authState.setIdToken(idToken);\n if (authConfig.ensureFreshness && refreshToken) authState.setRefreshToken(refreshToken);\n }\n }, [token, refreshToken, idToken]);\n\n useEffect(() => {\n if (authConfig.debug && authState.token) logger.debug('token', authState.token);\n }, [authState.token]);\n\n useEffect(() => {\n if (authConfig.debug && authState.idToken) logger.debug('idToken', authState.idToken);\n }, [authState.idToken]);\n\n useEffect(() => {\n if (authConfig.debug && authState.refreshToken) logger.debug(authState.refreshToken);\n }, [authState.refreshToken]);\n\n useEffect(() => {\n if (authConfig.debug) logger.debug('authenticated', authenticated);\n }, [authenticated]);\n\n return <>{children}</>;\n};\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAkES;AA1CT,mBAAiC;AAEjC,2BAA8B;AAC9B,mBAA6B;AAC7B,mBAA4B;AAM5B,MAAM,SAAS;AAER,MAAM,YAAgC,CAAC,EAAE,SAAS,MAAsB;AAC7E,QAAM,iBAAa,oCAAc;AACjC,QAAM,EAAE,OAAO,cAAc,eAAe,QAAQ,QAAI,0BAAY;AACpE,QAAM,gBAAY,2BAAa;AAE/B,8BAAU,MAAM;AACd,QAAI,CAAC;AAAe;AACpB,QAAI,WAAW,WAAW,OAAO;AAC/B,gBAAU,SAAS,KAAK;AACxB,UAAI;AAAS,kBAAU,WAAW,OAAO;AACzC,UAAI,WAAW,mBAAmB;AAAc,kBAAU,gBAAgB,YAAY;AAAA,IACxF;AAAA,EACF,GAAG,CAAC,OAAO,cAAc,OAAO,CAAC;AAEjC,8BAAU,MAAM;AACd,QAAI,WAAW,SAAS,UAAU;AAAO,aAAO,MAAM,SAAS,UAAU,KAAK;AAAA,EAChF,GAAG,CAAC,UAAU,KAAK,CAAC;AAEpB,8BAAU,MAAM;AACd,QAAI,WAAW,SAAS,UAAU;AAAS,aAAO,MAAM,WAAW,UAAU,OAAO;AAAA,EACtF,GAAG,CAAC,UAAU,OAAO,CAAC;AAEtB,8BAAU,MAAM;AACd,QAAI,WAAW,SAAS,UAAU;AAAc,aAAO,MAAM,UAAU,YAAY;AAAA,
|
|
4
|
+
"sourcesContent": ["/**\n * File: /auth/provider/afterAuth.tsx\n * Project: app\n * File Created: 22-11-2022 17:49:58\n * Author: Clay Risser\n * -----\n * Last Modified: 22-11-2022 18:00:39\n * Modified By: Clay Risser\n * -----\n * Risser Labs LLC (c) Copyright 2021 - 2022\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React, { useEffect } from 'react';\nimport type { FC, ReactNode } from 'react';\nimport { useAuthConfig } from '../hooks/useAuthConfig';\nimport { useAuthState } from '../state';\nimport { useKeycloak } from '../hooks';\n\nexport interface AfterAuthProps {\n children: ReactNode;\n}\n\nconst logger = console;\n\nexport const AfterAuth: FC<AfterAuthProps> = ({ children }: AfterAuthProps) => {\n const authConfig = useAuthConfig();\n const { token, refreshToken, authenticated, idToken } = useKeycloak();\n const authState = useAuthState();\n\n useEffect(() => {\n if (!authenticated) return;\n if (authConfig.persist && token) {\n authState.setToken(token);\n if (idToken) authState.setIdToken(idToken);\n if (authConfig.ensureFreshness && refreshToken) authState.setRefreshToken(refreshToken);\n }\n }, [token, refreshToken, idToken]);\n\n useEffect(() => {\n if (authConfig.debug && authState.token) logger.debug('token', authState.token);\n }, [authState.token]);\n\n useEffect(() => {\n if (authConfig.debug && authState.idToken) logger.debug('idToken', authState.idToken);\n }, [authState.idToken]);\n\n useEffect(() => {\n if (authConfig.debug && authState.refreshToken) logger.debug('refreshToken', authState.refreshToken);\n }, [authState.refreshToken]);\n\n useEffect(() => {\n if (authConfig.debug) logger.debug('authenticated', authenticated);\n }, [authenticated]);\n\n return <>{children}</>;\n};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAkES;AA1CT,mBAAiC;AAEjC,2BAA8B;AAC9B,mBAA6B;AAC7B,mBAA4B;AAM5B,MAAM,SAAS;AAER,MAAM,YAAgC,CAAC,EAAE,SAAS,MAAsB;AAC7E,QAAM,iBAAa,oCAAc;AACjC,QAAM,EAAE,OAAO,cAAc,eAAe,QAAQ,QAAI,0BAAY;AACpE,QAAM,gBAAY,2BAAa;AAE/B,8BAAU,MAAM;AACd,QAAI,CAAC;AAAe;AACpB,QAAI,WAAW,WAAW,OAAO;AAC/B,gBAAU,SAAS,KAAK;AACxB,UAAI;AAAS,kBAAU,WAAW,OAAO;AACzC,UAAI,WAAW,mBAAmB;AAAc,kBAAU,gBAAgB,YAAY;AAAA,IACxF;AAAA,EACF,GAAG,CAAC,OAAO,cAAc,OAAO,CAAC;AAEjC,8BAAU,MAAM;AACd,QAAI,WAAW,SAAS,UAAU;AAAO,aAAO,MAAM,SAAS,UAAU,KAAK;AAAA,EAChF,GAAG,CAAC,UAAU,KAAK,CAAC;AAEpB,8BAAU,MAAM;AACd,QAAI,WAAW,SAAS,UAAU;AAAS,aAAO,MAAM,WAAW,UAAU,OAAO;AAAA,EACtF,GAAG,CAAC,UAAU,OAAO,CAAC;AAEtB,8BAAU,MAAM;AACd,QAAI,WAAW,SAAS,UAAU;AAAc,aAAO,MAAM,gBAAgB,UAAU,YAAY;AAAA,EACrG,GAAG,CAAC,UAAU,YAAY,CAAC;AAE3B,8BAAU,MAAM;AACd,QAAI,WAAW;AAAO,aAAO,MAAM,iBAAiB,aAAa;AAAA,EACnE,GAAG,CAAC,aAAa,CAAC;AAElB,SAAO,2EAAG,UAAS;AACrB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -21,11 +21,18 @@
|
|
|
21
21
|
* See the License for the specific language governing permissions and
|
|
22
22
|
* limitations under the License.
|
|
23
23
|
*/
|
|
24
|
+
import type { AuthClientEvent, AuthClientError, AuthClientTokens } from '@bitspur/react-keycloak-core';
|
|
24
25
|
import type { AuthConfig } from '../authConfig';
|
|
25
|
-
import type { FC } from 'react';
|
|
26
|
-
import type {
|
|
27
|
-
export interface AuthProviderProps extends
|
|
28
|
-
|
|
26
|
+
import type { FC, ComponentType, ReactNode } from 'react';
|
|
27
|
+
import type { KeycloakConfig, KeycloakInitOptions } from '@bitspur/keycloak-js';
|
|
28
|
+
export interface AuthProviderProps extends Partial<Omit<AuthConfig, 'persist'>>, Omit<KeycloakConfig, 'url'>, KeycloakInitOptions {
|
|
29
|
+
children: ReactNode;
|
|
30
|
+
cookies?: unknown;
|
|
31
|
+
disabled?: boolean;
|
|
32
|
+
baseUrl?: string;
|
|
33
|
+
loadingComponent?: ComponentType;
|
|
34
|
+
onEvent?: (eventType: AuthClientEvent, error?: AuthClientError) => void;
|
|
35
|
+
onTokens?: (tokens: AuthClientTokens) => void;
|
|
29
36
|
}
|
|
30
37
|
export declare const AuthProvider: FC<AuthProviderProps>;
|
|
31
38
|
export * from './keycloakProvider';
|
|
@@ -20,6 +20,18 @@ var __spreadValues = (a, b) => {
|
|
|
20
20
|
return a;
|
|
21
21
|
};
|
|
22
22
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
23
|
+
var __objRest = (source, exclude) => {
|
|
24
|
+
var target = {};
|
|
25
|
+
for (var prop in source)
|
|
26
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
27
|
+
target[prop] = source[prop];
|
|
28
|
+
if (source != null && __getOwnPropSymbols)
|
|
29
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
30
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
31
|
+
target[prop] = source[prop];
|
|
32
|
+
}
|
|
33
|
+
return target;
|
|
34
|
+
};
|
|
23
35
|
var __export = (target, all) => {
|
|
24
36
|
for (var name in all)
|
|
25
37
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -43,16 +55,74 @@ var import_jsx_runtime = require("react/jsx-runtime");
|
|
|
43
55
|
var import_react = require("react");
|
|
44
56
|
var import_authConfig = require("../authConfig");
|
|
45
57
|
var import_keycloakProvider = require("./keycloakProvider");
|
|
58
|
+
var import_config = require("app/config");
|
|
46
59
|
__reExport(provider_exports, require("./keycloakProvider"), module.exports);
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
60
|
+
const persist = import_config.config.get("KEYCLOAK_PERSIST") === "1";
|
|
61
|
+
const AuthProvider = (_a) => {
|
|
62
|
+
var _b = _a, {
|
|
63
|
+
realm: realm,
|
|
64
|
+
clientId,
|
|
65
|
+
baseUrl,
|
|
66
|
+
debug: debug,
|
|
67
|
+
ensureFreshness,
|
|
68
|
+
loginRoute,
|
|
69
|
+
messageHandlerKeys,
|
|
70
|
+
ssr,
|
|
71
|
+
children: children,
|
|
72
|
+
cookies,
|
|
73
|
+
disabled,
|
|
74
|
+
loadingComponent,
|
|
75
|
+
onEvent,
|
|
76
|
+
onTokens
|
|
77
|
+
} = _b, keycloakInitOptions = __objRest(_b, [
|
|
78
|
+
// keycloak config
|
|
79
|
+
"realm",
|
|
80
|
+
"clientId",
|
|
81
|
+
"baseUrl",
|
|
82
|
+
// auth config
|
|
83
|
+
"debug",
|
|
84
|
+
"ensureFreshness",
|
|
85
|
+
"loginRoute",
|
|
86
|
+
"messageHandlerKeys",
|
|
87
|
+
"ssr",
|
|
88
|
+
// props
|
|
89
|
+
"children",
|
|
90
|
+
"cookies",
|
|
91
|
+
"disabled",
|
|
92
|
+
"loadingComponent",
|
|
93
|
+
"onEvent",
|
|
94
|
+
"onTokens"
|
|
95
|
+
]);
|
|
96
|
+
const authConfig = (0, import_react.useMemo)(
|
|
97
|
+
() => __spreadProps(__spreadValues({}, import_authConfig.defaultAuthConfig), {
|
|
98
|
+
debug,
|
|
99
|
+
ensureFreshness,
|
|
100
|
+
loginRoute,
|
|
101
|
+
messageHandlerKeys,
|
|
102
|
+
persist,
|
|
103
|
+
ssr
|
|
52
104
|
}),
|
|
53
|
-
[]
|
|
105
|
+
[debug, ensureFreshness, loginRoute, messageHandlerKeys, persist, ssr]
|
|
54
106
|
);
|
|
55
|
-
|
|
107
|
+
if (disabled) {
|
|
108
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_authConfig.AuthConfigContext.Provider, { value: authConfig, children });
|
|
109
|
+
}
|
|
110
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_authConfig.AuthConfigContext.Provider, { value: authConfig, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
111
|
+
import_keycloakProvider.KeycloakProvider,
|
|
112
|
+
{
|
|
113
|
+
cookies,
|
|
114
|
+
keycloakConfig: {
|
|
115
|
+
realm,
|
|
116
|
+
clientId,
|
|
117
|
+
url: baseUrl
|
|
118
|
+
},
|
|
119
|
+
keycloakInitOptions,
|
|
120
|
+
loadingComponent,
|
|
121
|
+
onEvent,
|
|
122
|
+
onTokens,
|
|
123
|
+
children
|
|
124
|
+
}
|
|
125
|
+
) });
|
|
56
126
|
};
|
|
57
127
|
// Annotate the CommonJS export names for ESM import in node:
|
|
58
128
|
0 && (module.exports = {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/provider/index.tsx"],
|
|
4
|
-
"sourcesContent": ["/**\n * File: /auth/provider/index.tsx\n * Project: app\n * File Created: 08-11-2022 06:33:07\n * Author: Clay Risser\n * -----\n * Last Modified: 22-11-2022 18:00:13\n * Modified By: Clay Risser\n * -----\n * Risser Labs LLC (c) Copyright 2021 - 2022\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React, { useMemo } from 'react';\nimport type { AuthConfig } from '../authConfig';\nimport type { FC } from 'react';\nimport type {
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["/**\n * File: /auth/provider/index.tsx\n * Project: app\n * File Created: 08-11-2022 06:33:07\n * Author: Clay Risser\n * -----\n * Last Modified: 22-11-2022 18:00:13\n * Modified By: Clay Risser\n * -----\n * Risser Labs LLC (c) Copyright 2021 - 2022\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React, { useMemo } from 'react';\nimport type { AuthClientEvent, AuthClientError, AuthClientTokens } from '@bitspur/react-keycloak-core';\nimport type { AuthConfig } from '../authConfig';\nimport type { FC, ComponentType, ReactNode } from 'react';\nimport type { KeycloakConfig, KeycloakInitOptions } from '@bitspur/keycloak-js';\nimport { AuthConfigContext, defaultAuthConfig } from '../authConfig';\nimport { KeycloakProvider } from './keycloakProvider';\n// @ts-ignore\nimport { config } from 'app/config';\n\nexport interface AuthProviderProps\n extends Partial<Omit<AuthConfig, 'persist'>>,\n Omit<KeycloakConfig, 'url'>,\n KeycloakInitOptions {\n children: ReactNode;\n cookies?: unknown;\n disabled?: boolean;\n baseUrl?: string;\n loadingComponent?: ComponentType;\n onEvent?: (eventType: AuthClientEvent, error?: AuthClientError) => void;\n onTokens?: (tokens: AuthClientTokens) => void;\n}\n\nconst persist = config.get('KEYCLOAK_PERSIST') === '1';\n\nexport const AuthProvider: FC<AuthProviderProps> = ({\n // keycloak config\n realm,\n clientId,\n baseUrl,\n // auth config\n debug,\n ensureFreshness,\n loginRoute,\n messageHandlerKeys,\n ssr,\n // props\n children,\n cookies,\n disabled,\n loadingComponent,\n onEvent,\n onTokens,\n ...keycloakInitOptions\n}: AuthProviderProps) => {\n const authConfig = useMemo(\n () => ({\n ...defaultAuthConfig,\n debug,\n ensureFreshness,\n loginRoute,\n messageHandlerKeys,\n persist,\n ssr,\n }),\n [debug, ensureFreshness, loginRoute, messageHandlerKeys, persist, ssr],\n );\n\n if (disabled) {\n return <AuthConfigContext.Provider value={authConfig}>{children}</AuthConfigContext.Provider>;\n }\n\n return (\n <AuthConfigContext.Provider value={authConfig}>\n <KeycloakProvider\n cookies={cookies}\n keycloakConfig={{\n realm,\n clientId,\n url: baseUrl,\n }}\n keycloakInitOptions={keycloakInitOptions}\n loadingComponent={loadingComponent}\n onEvent={onEvent}\n onTokens={onTokens}\n >\n {children}\n </KeycloakProvider>\n </AuthConfigContext.Provider>\n );\n};\n\nexport * from './keycloakProvider';\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAmFW;AA3DX,mBAA+B;AAK/B,wBAAqD;AACrD,8BAAiC;AAEjC,oBAAuB;AA0EvB,6BAAc,+BA1Gd;AA+CA,MAAM,UAAU,qBAAO,IAAI,kBAAkB,MAAM;AAE5C,MAAM,eAAsC,CAAC,OAmB3B;AAnB2B,eAElD;AAAA;AAAA,IACA;AAAA,IACA;AAAA,IAEA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IAEA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAlEF,IAiDoD,IAkB/C,gCAlB+C,IAkB/C;AAAA;AAAA,IAhBH;AAAA,IACA;AAAA,IACA;AAAA;AAAA,IAEA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA;AAAA,IAEA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA;AAGA,QAAM,iBAAa;AAAA,IACjB,MAAO,iCACF,sCADE;AAAA,MAEL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,IACA,CAAC,OAAO,iBAAiB,YAAY,oBAAoB,SAAS,GAAG;AAAA,EACvE;AAEA,MAAI,UAAU;AACZ,WAAO,4CAAC,oCAAkB,UAAlB,EAA2B,OAAO,YAAa,UAAS;AAAA,EAClE;AAEA,SACE,4CAAC,oCAAkB,UAAlB,EAA2B,OAAO,YACjC;AAAA,IAAC;AAAA;AAAA,MACC;AAAA,MACA,gBAAgB;AAAA,QACd;AAAA,QACA;AAAA,QACA,KAAK;AAAA,MACP;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MAEC;AAAA;AAAA,EACH,GACF;AAEJ;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -22,26 +22,19 @@
|
|
|
22
22
|
* limitations under the License.
|
|
23
23
|
*/
|
|
24
24
|
import type { AuthClientEvent, AuthClientError, AuthClientTokens } from '@bitspur/react-keycloak-core';
|
|
25
|
-
import type { KeycloakInitOptions } from '@bitspur/keycloak-js';
|
|
25
|
+
import type { KeycloakInitOptions, KeycloakConfig } from '@bitspur/keycloak-js';
|
|
26
26
|
import type { ReactNode, ComponentType } from 'react';
|
|
27
27
|
export interface KeycloakProviderProps {
|
|
28
28
|
children: ReactNode;
|
|
29
29
|
cookies?: unknown;
|
|
30
|
-
debug?: boolean;
|
|
31
|
-
disabled?: boolean;
|
|
32
30
|
keycloakConfig: KeycloakConfig;
|
|
33
31
|
keycloakInitOptions?: KeycloakInitOptions;
|
|
34
32
|
loadingComponent?: ComponentType;
|
|
35
33
|
onEvent?: (eventType: AuthClientEvent, error?: AuthClientError) => void;
|
|
36
34
|
onTokens?: (tokens: AuthClientTokens) => void;
|
|
37
35
|
}
|
|
38
|
-
export declare function KeycloakProvider({ children, cookies,
|
|
39
|
-
export
|
|
40
|
-
clientId: string;
|
|
41
|
-
realm: string;
|
|
42
|
-
scheme?: string;
|
|
43
|
-
baseUrl: string;
|
|
44
|
-
}
|
|
36
|
+
export declare function KeycloakProvider({ children, cookies, keycloakConfig, keycloakInitOptions, loadingComponent, onEvent, onTokens, }: KeycloakProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
37
|
+
export declare function isTokenValid(token: string): boolean;
|
|
45
38
|
export declare const defaultKeycloakInitOptions: KeycloakInitOptions;
|
|
46
39
|
export interface MessageSchema {
|
|
47
40
|
type: string;
|
|
@@ -43,7 +43,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
43
43
|
var keycloakProvider_exports = {};
|
|
44
44
|
__export(keycloakProvider_exports, {
|
|
45
45
|
KeycloakProvider: () => KeycloakProvider,
|
|
46
|
-
defaultKeycloakInitOptions: () => defaultKeycloakInitOptions
|
|
46
|
+
defaultKeycloakInitOptions: () => defaultKeycloakInitOptions,
|
|
47
|
+
isTokenValid: () => isTokenValid
|
|
47
48
|
});
|
|
48
49
|
module.exports = __toCommonJS(keycloakProvider_exports);
|
|
49
50
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
@@ -60,7 +61,6 @@ const logger = console;
|
|
|
60
61
|
function KeycloakProvider({
|
|
61
62
|
children,
|
|
62
63
|
cookies,
|
|
63
|
-
debug,
|
|
64
64
|
keycloakConfig,
|
|
65
65
|
keycloakInitOptions,
|
|
66
66
|
loadingComponent,
|
|
@@ -68,18 +68,18 @@ function KeycloakProvider({
|
|
|
68
68
|
onTokens
|
|
69
69
|
}) {
|
|
70
70
|
var _a, _b, _c;
|
|
71
|
+
const { debug, ensureFreshness, persist, messageHandlerKeys, ssr } = (0, import_useAuthConfig.useAuthConfig)();
|
|
71
72
|
const authState = (0, import_state.useAuthState)();
|
|
72
73
|
const LoadingComponent = loadingComponent || (() => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: debug ? "authenticating" : null }));
|
|
73
74
|
const { query } = import_multiplatform.MultiPlatform.isNext ? (0, import_router.useRouter)() : { query: {} };
|
|
74
|
-
const authConfig = (0, import_useAuthConfig.useAuthConfig)();
|
|
75
75
|
const [idToken, setIdToken] = (0, import_react.useState)(
|
|
76
|
-
"idToken" in query && (((_a = query.idToken) == null ? void 0 : _a.toString()) || true) ||
|
|
76
|
+
"idToken" in query && (((_a = query.idToken) == null ? void 0 : _a.toString()) || true) || persist && authState.idToken || false
|
|
77
77
|
);
|
|
78
78
|
const [token, setToken] = (0, import_react.useState)(
|
|
79
|
-
"token" in query && (((_b = query.token) == null ? void 0 : _b.toString()) || true) ||
|
|
79
|
+
"token" in query && (((_b = query.token) == null ? void 0 : _b.toString()) || true) || persist && authState.token || false
|
|
80
80
|
);
|
|
81
81
|
const [refreshToken, setRefreshToken] = (0, import_react.useState)(
|
|
82
|
-
|
|
82
|
+
ensureFreshness && ("refreshToken" in query && (((_c = query.refreshToken) == null ? void 0 : _c.toString()) || true) || persist && authState.refreshToken) || false
|
|
83
83
|
);
|
|
84
84
|
const explicitToken = "idToken" in query || "token" in query || "refreshToken" in query;
|
|
85
85
|
(0, import_react.useEffect)(() => {
|
|
@@ -88,13 +88,21 @@ function KeycloakProvider({
|
|
|
88
88
|
return;
|
|
89
89
|
if (debug)
|
|
90
90
|
logger.debug("post message", { type: "LOADED" });
|
|
91
|
-
(
|
|
91
|
+
(messageHandlerKeys || []).forEach((key) => {
|
|
92
92
|
var _a3, _b3, _c2;
|
|
93
93
|
(_c2 = (_b3 = (_a3 = window == null ? void 0 : window.webkit) == null ? void 0 : _a3.messageHandlers) == null ? void 0 : _b3[key]) == null ? void 0 : _c2.postMessage(JSON.stringify({ type: "LOADED" }));
|
|
94
94
|
});
|
|
95
95
|
(_a2 = window == null ? void 0 : window.ReactNativeWebView) == null ? void 0 : _a2.postMessage(JSON.stringify({ type: "LOADED" }));
|
|
96
96
|
(_b2 = window == null ? void 0 : window.parent) == null ? void 0 : _b2.postMessage(JSON.stringify({ type: "LOADED" }));
|
|
97
97
|
}, []);
|
|
98
|
+
(0, import_react.useEffect)(() => {
|
|
99
|
+
if (typeof idToken === "string" && !isTokenValid(idToken))
|
|
100
|
+
setIdToken(false);
|
|
101
|
+
if (typeof refreshToken === "string" && !isTokenValid(refreshToken))
|
|
102
|
+
setRefreshToken(false);
|
|
103
|
+
if (typeof token === "string" && !isTokenValid(token))
|
|
104
|
+
setToken(false);
|
|
105
|
+
}, [idToken, token, refreshToken]);
|
|
98
106
|
(0, import_react.useEffect)(() => {
|
|
99
107
|
if (token !== true)
|
|
100
108
|
return;
|
|
@@ -207,7 +215,7 @@ function KeycloakProvider({
|
|
|
207
215
|
}, []);
|
|
208
216
|
const keycloak = (0, import_react.useMemo)(
|
|
209
217
|
() => typeof window !== "undefined" && new import_keycloak_js.default({
|
|
210
|
-
url: keycloakConfig.
|
|
218
|
+
url: keycloakConfig.url,
|
|
211
219
|
realm: keycloakConfig.realm,
|
|
212
220
|
clientId: keycloakConfig.clientId
|
|
213
221
|
}),
|
|
@@ -232,13 +240,14 @@ function KeycloakProvider({
|
|
|
232
240
|
}, [keycloakInitOptions, token, refreshToken, idToken]);
|
|
233
241
|
if (token === true || idToken === true || refreshToken === true)
|
|
234
242
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LoadingComponent, {});
|
|
235
|
-
if (cookies &&
|
|
243
|
+
if (cookies && ssr) {
|
|
236
244
|
return (
|
|
237
245
|
// @ts-ignore
|
|
238
246
|
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
239
247
|
import_react_keycloak_ssr.SSRKeycloakProvider,
|
|
240
248
|
{
|
|
241
249
|
LoadingComponent: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LoadingComponent, {}),
|
|
250
|
+
autoRefreshToken: false,
|
|
242
251
|
initOptions,
|
|
243
252
|
onEvent: handleEvent,
|
|
244
253
|
onTokens,
|
|
@@ -246,7 +255,7 @@ function KeycloakProvider({
|
|
|
246
255
|
keycloakConfig: {
|
|
247
256
|
clientId: keycloakConfig.clientId,
|
|
248
257
|
realm: keycloakConfig.realm,
|
|
249
|
-
url: keycloakConfig.
|
|
258
|
+
url: keycloakConfig.url
|
|
250
259
|
},
|
|
251
260
|
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_afterAuth.AfterAuth, { children })
|
|
252
261
|
}
|
|
@@ -260,6 +269,7 @@ function KeycloakProvider({
|
|
|
260
269
|
{
|
|
261
270
|
LoadingComponent: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LoadingComponent, {}),
|
|
262
271
|
authClient: keycloak,
|
|
272
|
+
autoRefreshToken: false,
|
|
263
273
|
initOptions,
|
|
264
274
|
onEvent: handleEvent,
|
|
265
275
|
onTokens,
|
|
@@ -267,6 +277,17 @@ function KeycloakProvider({
|
|
|
267
277
|
}
|
|
268
278
|
);
|
|
269
279
|
}
|
|
280
|
+
function isTokenValid(token) {
|
|
281
|
+
var _a;
|
|
282
|
+
try {
|
|
283
|
+
const { exp } = JSON.parse(Buffer.from(((_a = token.split(".")) == null ? void 0 : _a[1]) || "", "base64").toString() || "{}");
|
|
284
|
+
return Math.floor(Date.now() / 1e3) <= exp;
|
|
285
|
+
} catch (err) {
|
|
286
|
+
if (err instanceof SyntaxError)
|
|
287
|
+
return false;
|
|
288
|
+
throw err;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
270
291
|
const defaultKeycloakInitOptions = {
|
|
271
292
|
checkLoginIframe: true,
|
|
272
293
|
checkLoginIframeInterval: 5,
|
|
@@ -277,6 +298,7 @@ const defaultKeycloakInitOptions = {
|
|
|
277
298
|
// Annotate the CommonJS export names for ESM import in node:
|
|
278
299
|
0 && (module.exports = {
|
|
279
300
|
KeycloakProvider,
|
|
280
|
-
defaultKeycloakInitOptions
|
|
301
|
+
defaultKeycloakInitOptions,
|
|
302
|
+
isTokenValid
|
|
281
303
|
});
|
|
282
304
|
//# sourceMappingURL=keycloakProvider.js.map
|