@multiplatform.one/keycloak 0.3.8 → 0.3.10
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/hooks/index.mjs +2 -1
- package/dist/esm/hooks/index.mjs.map +2 -2
- package/dist/esm/hooks/useKeycloak/useKeycloak.mjs +0 -3
- package/dist/esm/hooks/useKeycloak/useKeycloak.mjs.map +2 -2
- package/dist/esm/hooks/useTokensFromQuery/index.mjs +12 -0
- package/dist/esm/hooks/useTokensFromQuery/index.mjs.map +7 -0
- package/dist/esm/hooks/useTokensFromQuery/index.native.mjs +7 -0
- package/dist/esm/hooks/useTokensFromQuery/index.native.mjs.map +7 -0
- package/dist/esm/hooks/useTokensFromState.mjs +10 -0
- package/dist/esm/hooks/useTokensFromState.mjs.map +7 -0
- package/dist/esm/provider/afterAuth.mjs +1 -1
- package/dist/esm/provider/afterAuth.mjs.map +2 -2
- package/dist/esm/provider/index.mjs +5 -4
- package/dist/esm/provider/index.mjs.map +2 -2
- package/dist/esm/provider/keycloakProvider.mjs +5 -25
- package/dist/esm/provider/keycloakProvider.mjs.map +2 -2
- package/dist/esm/util.mjs +26 -0
- package/dist/esm/util.mjs.map +7 -0
- package/dist/jsx/hooks/index.mjs +2 -1
- package/dist/jsx/hooks/index.mjs.map +2 -2
- package/dist/jsx/hooks/useKeycloak/useKeycloak.mjs +0 -3
- package/dist/jsx/hooks/useKeycloak/useKeycloak.mjs.map +2 -2
- package/dist/jsx/hooks/useTokensFromQuery/index.mjs +10 -0
- package/dist/jsx/hooks/useTokensFromQuery/index.mjs.map +7 -0
- package/dist/jsx/hooks/useTokensFromQuery/index.native.mjs +5 -0
- package/dist/jsx/hooks/useTokensFromQuery/index.native.mjs.map +7 -0
- package/dist/jsx/hooks/useTokensFromState.mjs +10 -0
- package/dist/jsx/hooks/useTokensFromState.mjs.map +7 -0
- package/dist/jsx/provider/afterAuth.mjs +1 -1
- package/dist/jsx/provider/afterAuth.mjs.map +2 -2
- package/dist/jsx/provider/index.mjs +5 -4
- package/dist/jsx/provider/index.mjs.map +2 -2
- package/dist/jsx/provider/keycloakProvider.mjs +5 -24
- package/dist/jsx/provider/keycloakProvider.mjs.map +2 -2
- package/dist/jsx/util.mjs +25 -0
- package/dist/jsx/util.mjs.map +7 -0
- package/dist/lib/hooks/index.d.ts +3 -2
- package/dist/lib/hooks/index.js +5 -3
- package/dist/lib/hooks/index.js.map +2 -2
- package/dist/lib/hooks/useKeycloak/useKeycloak.d.ts +1 -1
- package/dist/lib/hooks/useKeycloak/useKeycloak.js +0 -3
- package/dist/lib/hooks/useKeycloak/useKeycloak.js.map +2 -2
- package/dist/lib/hooks/useTokensFromQuery/index.d.ts +24 -0
- package/dist/lib/hooks/useTokensFromQuery/index.js +34 -0
- package/dist/lib/hooks/useTokensFromQuery/index.js.map +7 -0
- package/dist/lib/hooks/useTokensFromQuery/index.native.d.ts +24 -0
- package/dist/lib/hooks/useTokensFromQuery/index.native.js +29 -0
- package/dist/lib/hooks/useTokensFromQuery/index.native.js.map +7 -0
- package/dist/lib/hooks/useTokensFromState.d.ts +24 -0
- package/dist/lib/hooks/useTokensFromState.js +34 -0
- package/dist/lib/hooks/useTokensFromState.js.map +7 -0
- package/dist/lib/provider/afterAuth.js +1 -1
- package/dist/lib/provider/afterAuth.js.map +2 -2
- package/dist/lib/provider/index.js +4 -3
- package/dist/lib/provider/index.js.map +2 -2
- package/dist/lib/provider/keycloakProvider.js +10 -30
- package/dist/lib/provider/keycloakProvider.js.map +2 -2
- package/dist/lib/tsconfig.build.tsbuildinfo +1 -1
- package/dist/lib/util.d.ts +25 -0
- package/dist/lib/util.js +51 -0
- package/dist/lib/util.js.map +7 -0
- package/package.json +4 -4
- package/src/hooks/index.ts +3 -2
- package/src/hooks/useKeycloak/useKeycloak.ts +1 -4
- package/src/hooks/{useIsPassedInToken → useTokensFromQuery}/index.native.ts +3 -3
- package/src/hooks/{useIsPassedInToken → useTokensFromQuery}/index.ts +3 -3
- package/src/hooks/useTokensFromState.ts +31 -0
- package/src/provider/afterAuth.tsx +1 -1
- package/src/provider/index.tsx +5 -4
- package/src/provider/keycloakProvider.tsx +5 -23
- package/src/util.ts +42 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/util.ts
|
|
3
|
+
* Project: @multiplatform.one/keycloak
|
|
4
|
+
* File Created: 16-05-2023 06:29:32
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 16-05-2023 06:33:36
|
|
8
|
+
* Modified By: Clay Risser
|
|
9
|
+
* -----
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2022 - 2023
|
|
11
|
+
*
|
|
12
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
13
|
+
* you may not use this file except in compliance with the License.
|
|
14
|
+
* You may obtain a copy of the License at
|
|
15
|
+
*
|
|
16
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
17
|
+
*
|
|
18
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
19
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
20
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
21
|
+
* See the License for the specific language governing permissions and
|
|
22
|
+
* limitations under the License.
|
|
23
|
+
*/
|
|
24
|
+
export declare function validToken(token: string | boolean, refreshToken?: string | boolean): string | boolean;
|
|
25
|
+
export declare function isTokenValid(token: string): boolean;
|
package/dist/lib/util.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var util_exports = {};
|
|
20
|
+
__export(util_exports, {
|
|
21
|
+
isTokenValid: () => isTokenValid,
|
|
22
|
+
validToken: () => validToken
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(util_exports);
|
|
25
|
+
function validToken(token, refreshToken) {
|
|
26
|
+
if (typeof token !== "string")
|
|
27
|
+
return token;
|
|
28
|
+
if (refreshToken) {
|
|
29
|
+
if (refreshToken === true || isTokenValid(refreshToken))
|
|
30
|
+
return token;
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
return isTokenValid(token) ? token : false;
|
|
34
|
+
}
|
|
35
|
+
function isTokenValid(token) {
|
|
36
|
+
var _a;
|
|
37
|
+
try {
|
|
38
|
+
const { exp } = JSON.parse(Buffer.from(((_a = token.split(".")) == null ? void 0 : _a[1]) || "", "base64").toString() || "{}");
|
|
39
|
+
return Math.floor(Date.now() / 1e3) <= exp;
|
|
40
|
+
} catch (err) {
|
|
41
|
+
if (err instanceof SyntaxError)
|
|
42
|
+
return false;
|
|
43
|
+
throw err;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
47
|
+
0 && (module.exports = {
|
|
48
|
+
isTokenValid,
|
|
49
|
+
validToken
|
|
50
|
+
});
|
|
51
|
+
//# sourceMappingURL=util.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/util.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * File: /src/util.ts\n * Project: @multiplatform.one/keycloak\n * File Created: 16-05-2023 06:29:32\n * Author: Clay Risser\n * -----\n * Last Modified: 16-05-2023 06:33:36\n * Modified By: Clay Risser\n * -----\n * Risser Labs LLC (c) Copyright 2022 - 2023\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\nexport function validToken(token: string | boolean, refreshToken?: string | boolean) {\n if (typeof token !== 'string') return token;\n if (refreshToken) {\n if (refreshToken === true || isTokenValid(refreshToken)) return token;\n return false;\n }\n return isTokenValid(token) ? token : false;\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"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAwBO,SAAS,WAAW,OAAyB,cAAiC;AACnF,MAAI,OAAO,UAAU;AAAU,WAAO;AACtC,MAAI,cAAc;AAChB,QAAI,iBAAiB,QAAQ,aAAa,YAAY;AAAG,aAAO;AAChE,WAAO;AAAA,EACT;AACA,SAAO,aAAa,KAAK,IAAI,QAAQ;AACvC;AAEO,SAAS,aAAa,OAAe;AAjC5C;AAkCE,MAAI;AACF,UAAM,EAAE,IAAI,IAAI,KAAK,MAAM,OAAO,OAAK,WAAM,MAAM,GAAG,MAAf,mBAAmB,OAAM,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;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@multiplatform.one/keycloak",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.10",
|
|
4
4
|
"description": "keycloak client for multiplatform.one ecosystem",
|
|
5
5
|
"main": "dist/lib",
|
|
6
6
|
"module": "dist/esm",
|
|
@@ -114,9 +114,9 @@
|
|
|
114
114
|
"indent": 4
|
|
115
115
|
},
|
|
116
116
|
"dependencies": {
|
|
117
|
-
"@bitspur/keycloak-js": "^21.1.1",
|
|
118
|
-
"@bitspur/react-keycloak-ssr": "^4.0.
|
|
119
|
-
"@bitspur/react-keycloak-web": "^4.0.
|
|
117
|
+
"@bitspur/keycloak-js": "^21.1.1-9",
|
|
118
|
+
"@bitspur/react-keycloak-ssr": "^4.0.8",
|
|
119
|
+
"@bitspur/react-keycloak-web": "^4.0.8",
|
|
120
120
|
"core-js-pure": "^3.30.2",
|
|
121
121
|
"expo-auth-session": "^4.0.3",
|
|
122
122
|
"solito": "^3.2.6"
|
package/src/hooks/index.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* File Created: 08-11-2022 06:34:56
|
|
5
5
|
* Author: Clay Risser
|
|
6
6
|
* -----
|
|
7
|
-
* Last Modified:
|
|
7
|
+
* Last Modified: 16-05-2023 07:40:57
|
|
8
8
|
* Modified By: Clay Risser
|
|
9
9
|
* -----
|
|
10
10
|
* Risser Labs LLC (c) Copyright 2021 - 2022
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
25
|
export * from './useCurrentRouteName';
|
|
26
|
-
export * from './useIsPassedInToken';
|
|
27
26
|
export * from './useKeycloak';
|
|
28
27
|
export * from './useLogin';
|
|
28
|
+
export * from './useTokensFromQuery';
|
|
29
|
+
export * from './useTokensFromState';
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* File Created: 08-11-2022 06:04:59
|
|
5
5
|
* Author: Clay Risser
|
|
6
6
|
* -----
|
|
7
|
-
* Last Modified: 15-05-2023
|
|
7
|
+
* Last Modified: 15-05-2023 18:10:07
|
|
8
8
|
* Modified By: Clay Risser
|
|
9
9
|
* -----
|
|
10
10
|
* Risser Labs LLC (c) Copyright 2021 - 2022
|
|
@@ -31,7 +31,6 @@ import { useKeycloak as useSsrKeycloak } from '@bitspur/react-keycloak-ssr';
|
|
|
31
31
|
export function useKeycloak() {
|
|
32
32
|
const authConfig = useAuthConfig();
|
|
33
33
|
if (MultiPlatform.isStorybook) return { authenticated: true } as IKeycloak;
|
|
34
|
-
console.log('SSR', authConfig.ssr);
|
|
35
34
|
if (MultiPlatform.isNext && authConfig.ssr) {
|
|
36
35
|
const { keycloak, initialized } = useSsrKeycloak();
|
|
37
36
|
return {
|
|
@@ -40,8 +39,6 @@ export function useKeycloak() {
|
|
|
40
39
|
} as IKeycloak;
|
|
41
40
|
}
|
|
42
41
|
const { keycloak, initialized } = useReactKeycloak();
|
|
43
|
-
console.log('a1', keycloak.authenticated);
|
|
44
|
-
console.log('a2', initialized ? keycloak.authenticated : undefined);
|
|
45
42
|
return {
|
|
46
43
|
...keycloak,
|
|
47
44
|
authenticated: initialized ? keycloak.authenticated : undefined,
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* File: /src/hooks/
|
|
2
|
+
* File: /src/hooks/useTokensFromQuery/index.native.ts
|
|
3
3
|
* Project: @multiplatform.one/keycloak
|
|
4
4
|
* File Created: 15-05-2023 01:54:48
|
|
5
5
|
* Author: Clay Risser
|
|
6
6
|
* -----
|
|
7
|
-
* Last Modified:
|
|
7
|
+
* Last Modified: 16-05-2023 07:40:46
|
|
8
8
|
* Modified By: Clay Risser
|
|
9
9
|
* -----
|
|
10
10
|
* Risser Labs LLC (c) Copyright 2022 - 2023
|
|
@@ -22,4 +22,4 @@
|
|
|
22
22
|
* limitations under the License.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
export const
|
|
25
|
+
export const useTokensFromQuery = () => false;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* File: /src/hooks/
|
|
2
|
+
* File: /src/hooks/useTokensFromQuery/index.ts
|
|
3
3
|
* Project: @multiplatform.one/keycloak
|
|
4
4
|
* File Created: 15-05-2023 01:54:44
|
|
5
5
|
* Author: Clay Risser
|
|
6
6
|
* -----
|
|
7
|
-
* Last Modified:
|
|
7
|
+
* Last Modified: 16-05-2023 07:40:41
|
|
8
8
|
* Modified By: Clay Risser
|
|
9
9
|
* -----
|
|
10
10
|
* Risser Labs LLC (c) Copyright 2022 - 2023
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
import { MultiPlatform } from 'multiplatform.one';
|
|
26
26
|
import { useRouter } from 'next/router';
|
|
27
27
|
|
|
28
|
-
export function
|
|
28
|
+
export function useTokensFromQuery() {
|
|
29
29
|
const { query } = MultiPlatform.isNext ? useRouter() : { query: {} };
|
|
30
30
|
return 'idToken' in query || 'token' in query || 'refreshToken' in query;
|
|
31
31
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/hooks/useTokensFromState.ts
|
|
3
|
+
* Project: @multiplatform.one/keycloak
|
|
4
|
+
* File Created: 16-05-2023 06:29:02
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 16-05-2023 07:40:36
|
|
8
|
+
* Modified By: Clay Risser
|
|
9
|
+
* -----
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2022 - 2023
|
|
11
|
+
*
|
|
12
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
13
|
+
* you may not use this file except in compliance with the License.
|
|
14
|
+
* You may obtain a copy of the License at
|
|
15
|
+
*
|
|
16
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
17
|
+
*
|
|
18
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
19
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
20
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
21
|
+
* See the License for the specific language governing permissions and
|
|
22
|
+
* limitations under the License.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { useAuthState } from '../state';
|
|
26
|
+
import { validToken } from '../util';
|
|
27
|
+
|
|
28
|
+
export function useTokensFromState() {
|
|
29
|
+
const authState = useAuthState();
|
|
30
|
+
return !!validToken(authState.token, authState.refreshToken);
|
|
31
|
+
}
|
|
@@ -36,8 +36,8 @@ const logger = console;
|
|
|
36
36
|
|
|
37
37
|
export const AfterAuth: FC<AfterAuthProps> = ({ children }: AfterAuthProps) => {
|
|
38
38
|
const authConfig = useAuthConfig();
|
|
39
|
-
const { token, refreshToken, authenticated, idToken } = useKeycloak();
|
|
40
39
|
const authState = useAuthState();
|
|
40
|
+
const { token, refreshToken, authenticated, idToken } = useKeycloak();
|
|
41
41
|
|
|
42
42
|
useEffect(() => {
|
|
43
43
|
if (!authenticated) return;
|
package/src/provider/index.tsx
CHANGED
|
@@ -29,7 +29,7 @@ import type { FC, ComponentType, ReactNode } from 'react';
|
|
|
29
29
|
import type { KeycloakConfig, KeycloakInitOptions } from '@bitspur/keycloak-js';
|
|
30
30
|
import { AuthConfigContext, defaultAuthConfig } from '../authConfig';
|
|
31
31
|
import { KeycloakProvider } from './keycloakProvider';
|
|
32
|
-
import {
|
|
32
|
+
import { useTokensFromState, useTokensFromQuery } from '../hooks';
|
|
33
33
|
// @ts-ignore
|
|
34
34
|
import { config } from 'app/config';
|
|
35
35
|
|
|
@@ -68,7 +68,8 @@ export const AuthProvider: FC<AuthProviderProps> = ({
|
|
|
68
68
|
onTokens,
|
|
69
69
|
...keycloakInitOptions
|
|
70
70
|
}: AuthProviderProps) => {
|
|
71
|
-
const
|
|
71
|
+
const tokensFromQuery = useTokensFromQuery();
|
|
72
|
+
const tokensFromState = useTokensFromState();
|
|
72
73
|
const authConfig = useMemo(
|
|
73
74
|
() => ({
|
|
74
75
|
...defaultAuthConfig,
|
|
@@ -76,8 +77,8 @@ export const AuthProvider: FC<AuthProviderProps> = ({
|
|
|
76
77
|
ensureFreshness,
|
|
77
78
|
loginRoute,
|
|
78
79
|
messageHandlerKeys,
|
|
79
|
-
persist:
|
|
80
|
-
ssr,
|
|
80
|
+
persist: !tokensFromState && !tokensFromQuery && cookies && ssr ? false : persist,
|
|
81
|
+
ssr: !tokensFromState && !tokensFromQuery && cookies ? ssr : false,
|
|
81
82
|
}),
|
|
82
83
|
[debug, ensureFreshness, loginRoute, messageHandlerKeys, persist, ssr],
|
|
83
84
|
);
|
|
@@ -33,8 +33,9 @@ import { ReactKeycloakProvider } from '@bitspur/react-keycloak-web';
|
|
|
33
33
|
import { SSRKeycloakProvider, SSRCookies } from '@bitspur/react-keycloak-ssr';
|
|
34
34
|
import { useAuthConfig } from '../hooks/useAuthConfig';
|
|
35
35
|
import { useAuthState } from '../state';
|
|
36
|
-
import {
|
|
36
|
+
import { useTokensFromQuery } from '../hooks';
|
|
37
37
|
import { useRouter } from 'next/router';
|
|
38
|
+
import { validToken } from '../util';
|
|
38
39
|
|
|
39
40
|
export interface KeycloakProviderProps {
|
|
40
41
|
children: ReactNode;
|
|
@@ -81,7 +82,7 @@ export function KeycloakProvider({
|
|
|
81
82
|
refreshToken,
|
|
82
83
|
),
|
|
83
84
|
);
|
|
84
|
-
const
|
|
85
|
+
const tokensFromQuery = useTokensFromQuery();
|
|
85
86
|
|
|
86
87
|
useEffect(() => {
|
|
87
88
|
if (token !== true && refreshToken !== true && idToken !== true) return;
|
|
@@ -223,7 +224,7 @@ export function KeycloakProvider({
|
|
|
223
224
|
initOptions.token = token;
|
|
224
225
|
if (idToken && typeof idToken === 'string') initOptions.idToken = idToken;
|
|
225
226
|
if (refreshToken && typeof refreshToken === 'string') initOptions.refreshToken = refreshToken;
|
|
226
|
-
if (
|
|
227
|
+
if (tokensFromQuery) {
|
|
227
228
|
initOptions.checkLoginIframe = false;
|
|
228
229
|
initOptions.flow = 'implicit';
|
|
229
230
|
initOptions.onLoad = undefined;
|
|
@@ -233,7 +234,7 @@ export function KeycloakProvider({
|
|
|
233
234
|
}, [keycloakInitOptions, token, refreshToken, idToken]);
|
|
234
235
|
|
|
235
236
|
if (token === true || idToken === true || refreshToken === true) return <LoadingComponent />;
|
|
236
|
-
if (
|
|
237
|
+
if (cookies && ssr) {
|
|
237
238
|
return (
|
|
238
239
|
// @ts-ignore
|
|
239
240
|
<SSRKeycloakProvider
|
|
@@ -270,25 +271,6 @@ export function KeycloakProvider({
|
|
|
270
271
|
);
|
|
271
272
|
}
|
|
272
273
|
|
|
273
|
-
function validToken(token: string | boolean, refreshToken?: string | boolean) {
|
|
274
|
-
if (typeof token !== 'string') return token;
|
|
275
|
-
if (refreshToken) {
|
|
276
|
-
if (refreshToken === true || isTokenValid(refreshToken)) return token;
|
|
277
|
-
return false;
|
|
278
|
-
}
|
|
279
|
-
return isTokenValid(token) ? token : false;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
function isTokenValid(token: string) {
|
|
283
|
-
try {
|
|
284
|
-
const { exp } = JSON.parse(Buffer.from(token.split('.')?.[1] || '', 'base64').toString() || '{}');
|
|
285
|
-
return Math.floor(Date.now() / 1000) <= exp;
|
|
286
|
-
} catch (err) {
|
|
287
|
-
if (err instanceof SyntaxError) return false;
|
|
288
|
-
throw err;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
274
|
export const defaultKeycloakInitOptions: KeycloakInitOptions = {
|
|
293
275
|
checkLoginIframe: true,
|
|
294
276
|
checkLoginIframeInterval: 5,
|
package/src/util.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/util.ts
|
|
3
|
+
* Project: @multiplatform.one/keycloak
|
|
4
|
+
* File Created: 16-05-2023 06:29:32
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 16-05-2023 06:33:36
|
|
8
|
+
* Modified By: Clay Risser
|
|
9
|
+
* -----
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2022 - 2023
|
|
11
|
+
*
|
|
12
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
13
|
+
* you may not use this file except in compliance with the License.
|
|
14
|
+
* You may obtain a copy of the License at
|
|
15
|
+
*
|
|
16
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
17
|
+
*
|
|
18
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
19
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
20
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
21
|
+
* See the License for the specific language governing permissions and
|
|
22
|
+
* limitations under the License.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export function validToken(token: string | boolean, refreshToken?: string | boolean) {
|
|
26
|
+
if (typeof token !== 'string') return token;
|
|
27
|
+
if (refreshToken) {
|
|
28
|
+
if (refreshToken === true || isTokenValid(refreshToken)) return token;
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
return isTokenValid(token) ? token : false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function isTokenValid(token: string) {
|
|
35
|
+
try {
|
|
36
|
+
const { exp } = JSON.parse(Buffer.from(token.split('.')?.[1] || '', 'base64').toString() || '{}');
|
|
37
|
+
return Math.floor(Date.now() / 1000) <= exp;
|
|
38
|
+
} catch (err) {
|
|
39
|
+
if (err instanceof SyntaxError) return false;
|
|
40
|
+
throw err;
|
|
41
|
+
}
|
|
42
|
+
}
|