@multiplatform.one/keycloak 0.3.1 → 0.3.4
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 +1 -0
- package/dist/esm/hooks/index.mjs.map +2 -2
- package/dist/esm/hooks/useIsPassedInToken/index.mjs +12 -0
- package/dist/esm/hooks/useIsPassedInToken/index.mjs.map +7 -0
- package/dist/esm/hooks/useIsPassedInToken/index.native.mjs +7 -0
- package/dist/esm/hooks/useIsPassedInToken/index.native.mjs.map +7 -0
- package/dist/esm/provider/index.mjs +3 -1
- package/dist/esm/provider/index.mjs.map +2 -2
- package/dist/esm/provider/keycloakProvider.mjs +39 -37
- package/dist/esm/provider/keycloakProvider.mjs.map +2 -2
- package/dist/jsx/hooks/index.mjs +1 -0
- package/dist/jsx/hooks/index.mjs.map +2 -2
- package/dist/jsx/hooks/useIsPassedInToken/index.mjs +10 -0
- package/dist/jsx/hooks/useIsPassedInToken/index.mjs.map +7 -0
- package/dist/jsx/hooks/useIsPassedInToken/index.native.mjs +5 -0
- package/dist/jsx/hooks/useIsPassedInToken/index.native.mjs.map +7 -0
- package/dist/jsx/provider/index.mjs +3 -1
- package/dist/jsx/provider/index.mjs.map +2 -2
- package/dist/jsx/provider/keycloakProvider.mjs +48 -38
- package/dist/jsx/provider/keycloakProvider.mjs.map +2 -2
- package/dist/lib/hooks/index.d.ts +2 -1
- package/dist/lib/hooks/index.js +2 -0
- package/dist/lib/hooks/index.js.map +2 -2
- package/dist/lib/hooks/useIsPassedInToken/index.d.ts +24 -0
- package/dist/lib/hooks/useIsPassedInToken/index.js +34 -0
- package/dist/lib/hooks/useIsPassedInToken/index.js.map +7 -0
- package/dist/lib/hooks/useIsPassedInToken/index.native.d.ts +24 -0
- package/dist/lib/hooks/useIsPassedInToken/index.native.js +29 -0
- package/dist/lib/hooks/useIsPassedInToken/index.native.js.map +7 -0
- package/dist/lib/provider/index.js +3 -1
- package/dist/lib/provider/index.js.map +2 -2
- package/dist/lib/provider/keycloakProvider.d.ts +0 -1
- package/dist/lib/provider/keycloakProvider.js +49 -40
- package/dist/lib/provider/keycloakProvider.js.map +2 -2
- package/dist/lib/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/hooks/index.ts +2 -1
- package/src/hooks/useIsPassedInToken/index.native.ts +25 -0
- package/src/hooks/useIsPassedInToken/index.ts +31 -0
- package/src/provider/index.tsx +3 -1
- package/src/provider/keycloakProvider.tsx +50 -37
package/package.json
CHANGED
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: 15-05-2023 01:58:05
|
|
8
8
|
* Modified By: Clay Risser
|
|
9
9
|
* -----
|
|
10
10
|
* Risser Labs LLC (c) Copyright 2021 - 2022
|
|
@@ -23,5 +23,6 @@
|
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
25
|
export * from './useCurrentRouteName';
|
|
26
|
+
export * from './useIsPassedInToken';
|
|
26
27
|
export * from './useKeycloak';
|
|
27
28
|
export * from './useLogin';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/hooks/useIsPassedInToken/index.native.ts
|
|
3
|
+
* Project: @multiplatform.one/keycloak
|
|
4
|
+
* File Created: 15-05-2023 01:54:48
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 15-05-2023 01:55:27
|
|
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 const useIsPassedInToken = () => false;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/hooks/useIsPassedInToken/index.ts
|
|
3
|
+
* Project: @multiplatform.one/keycloak
|
|
4
|
+
* File Created: 15-05-2023 01:54:44
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 15-05-2023 01:56:30
|
|
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 { MultiPlatform } from 'multiplatform.one';
|
|
26
|
+
import { useRouter } from 'next/router';
|
|
27
|
+
|
|
28
|
+
export function useIsPassedInToken() {
|
|
29
|
+
const { query } = MultiPlatform.isNext ? useRouter() : { query: {} };
|
|
30
|
+
return 'idToken' in query || 'token' in query || 'refreshToken' in query;
|
|
31
|
+
}
|
package/src/provider/index.tsx
CHANGED
|
@@ -29,6 +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 { useIsPassedInToken } from '../hooks';
|
|
32
33
|
// @ts-ignore
|
|
33
34
|
import { config } from 'app/config';
|
|
34
35
|
|
|
@@ -67,6 +68,7 @@ export const AuthProvider: FC<AuthProviderProps> = ({
|
|
|
67
68
|
onTokens,
|
|
68
69
|
...keycloakInitOptions
|
|
69
70
|
}: AuthProviderProps) => {
|
|
71
|
+
const isPassedInToken = useIsPassedInToken();
|
|
70
72
|
const authConfig = useMemo(
|
|
71
73
|
() => ({
|
|
72
74
|
...defaultAuthConfig,
|
|
@@ -74,7 +76,7 @@ export const AuthProvider: FC<AuthProviderProps> = ({
|
|
|
74
76
|
ensureFreshness,
|
|
75
77
|
loginRoute,
|
|
76
78
|
messageHandlerKeys,
|
|
77
|
-
persist,
|
|
79
|
+
persist: ssr || isPassedInToken ? false : persist,
|
|
78
80
|
ssr,
|
|
79
81
|
}),
|
|
80
82
|
[debug, ensureFreshness, loginRoute, messageHandlerKeys, persist, ssr],
|
|
@@ -33,6 +33,7 @@ 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 { useIsPassedInToken } from '../hooks';
|
|
36
37
|
import { useRouter } from 'next/router';
|
|
37
38
|
|
|
38
39
|
export interface KeycloakProviderProps {
|
|
@@ -60,18 +61,27 @@ export function KeycloakProvider({
|
|
|
60
61
|
const authState = useAuthState();
|
|
61
62
|
const LoadingComponent = loadingComponent || (() => <>{debug ? 'authenticating' : null}</>);
|
|
62
63
|
const { query } = MultiPlatform.isNext ? useRouter() : { query: {} };
|
|
64
|
+
const [refreshToken, setRefreshToken] = useState<string | boolean>(
|
|
65
|
+
validToken(
|
|
66
|
+
(ensureFreshness &&
|
|
67
|
+
(('refreshToken' in query && (query.refreshToken?.toString() || true)) ||
|
|
68
|
+
(persist && authState.refreshToken))) ||
|
|
69
|
+
false,
|
|
70
|
+
),
|
|
71
|
+
);
|
|
63
72
|
const [idToken, setIdToken] = useState<string | boolean>(
|
|
64
|
-
(
|
|
73
|
+
validToken(
|
|
74
|
+
('idToken' in query && (query.idToken?.toString() || true)) || (persist && authState.idToken) || false,
|
|
75
|
+
refreshToken,
|
|
76
|
+
),
|
|
65
77
|
);
|
|
66
78
|
const [token, setToken] = useState<string | boolean>(
|
|
67
|
-
(
|
|
79
|
+
validToken(
|
|
80
|
+
('token' in query && (query.token?.toString() || true)) || (persist && authState.token) || false,
|
|
81
|
+
refreshToken,
|
|
82
|
+
),
|
|
68
83
|
);
|
|
69
|
-
const
|
|
70
|
-
(ensureFreshness &&
|
|
71
|
-
(('refreshToken' in query && (query.refreshToken?.toString() || true)) || (persist && authState.refreshToken))) ||
|
|
72
|
-
false,
|
|
73
|
-
);
|
|
74
|
-
const explicitToken = 'idToken' in query || 'token' in query || 'refreshToken' in query;
|
|
84
|
+
const isPassedInToken = useIsPassedInToken();
|
|
75
85
|
|
|
76
86
|
useEffect(() => {
|
|
77
87
|
if (token !== true && refreshToken !== true && idToken !== true) return;
|
|
@@ -84,15 +94,9 @@ export function KeycloakProvider({
|
|
|
84
94
|
}, []);
|
|
85
95
|
|
|
86
96
|
useEffect(() => {
|
|
87
|
-
if (
|
|
88
|
-
if (typeof refreshToken === 'string' && !isTokenValid(refreshToken)) setRefreshToken(false);
|
|
89
|
-
if (typeof token === 'string' && !isTokenValid(token)) setToken(false);
|
|
90
|
-
}, [idToken, token, refreshToken]);
|
|
91
|
-
|
|
92
|
-
useEffect(() => {
|
|
93
|
-
if (token !== true) return;
|
|
97
|
+
if (refreshToken !== true) return;
|
|
94
98
|
const messageCallback = (e: MessageEvent<any>) => {
|
|
95
|
-
let data = e?.data;
|
|
99
|
+
let data = e?.data || null;
|
|
96
100
|
if (typeof data === 'string') {
|
|
97
101
|
try {
|
|
98
102
|
data = JSON.parse(data);
|
|
@@ -101,15 +105,15 @@ export function KeycloakProvider({
|
|
|
101
105
|
if (debug) logger.debug('received message', data);
|
|
102
106
|
const message: MessageSchema = data;
|
|
103
107
|
if (!message.type) return;
|
|
104
|
-
if (message.type.toUpperCase() === '
|
|
105
|
-
if (debug) logger.debug('setting token', message.payload);
|
|
106
|
-
|
|
108
|
+
if (message.type.toUpperCase() === 'REFRESH_TOKEN' && message.payload) {
|
|
109
|
+
if (debug) logger.debug('setting refresh token', message.payload);
|
|
110
|
+
setRefreshToken(validToken(message.payload));
|
|
107
111
|
}
|
|
108
112
|
};
|
|
109
113
|
if (debug) {
|
|
110
114
|
logger.debug('listening for message', {
|
|
111
|
-
type: '
|
|
112
|
-
payload: '<
|
|
115
|
+
type: 'REFRESH_TOKEN',
|
|
116
|
+
payload: '<some_refresh_token>',
|
|
113
117
|
});
|
|
114
118
|
}
|
|
115
119
|
window.addEventListener('message', messageCallback);
|
|
@@ -119,9 +123,9 @@ export function KeycloakProvider({
|
|
|
119
123
|
}, []);
|
|
120
124
|
|
|
121
125
|
useEffect(() => {
|
|
122
|
-
if (
|
|
126
|
+
if (token !== true) return;
|
|
123
127
|
const messageCallback = (e: MessageEvent<any>) => {
|
|
124
|
-
let data = e?.data;
|
|
128
|
+
let data = e?.data || null;
|
|
125
129
|
if (typeof data === 'string') {
|
|
126
130
|
try {
|
|
127
131
|
data = JSON.parse(data);
|
|
@@ -130,15 +134,15 @@ export function KeycloakProvider({
|
|
|
130
134
|
if (debug) logger.debug('received message', data);
|
|
131
135
|
const message: MessageSchema = data;
|
|
132
136
|
if (!message.type) return;
|
|
133
|
-
if (message.type.toUpperCase() === '
|
|
134
|
-
if (debug) logger.debug('setting
|
|
135
|
-
|
|
137
|
+
if (message.type.toUpperCase() === 'TOKEN' && message.payload) {
|
|
138
|
+
if (debug) logger.debug('setting token', message.payload);
|
|
139
|
+
setToken(validToken(message.payload, refreshToken));
|
|
136
140
|
}
|
|
137
141
|
};
|
|
138
142
|
if (debug) {
|
|
139
143
|
logger.debug('listening for message', {
|
|
140
|
-
type: '
|
|
141
|
-
payload: '<
|
|
144
|
+
type: 'TOKEN',
|
|
145
|
+
payload: '<some_token>',
|
|
142
146
|
});
|
|
143
147
|
}
|
|
144
148
|
window.addEventListener('message', messageCallback);
|
|
@@ -148,9 +152,9 @@ export function KeycloakProvider({
|
|
|
148
152
|
}, []);
|
|
149
153
|
|
|
150
154
|
useEffect(() => {
|
|
151
|
-
if (
|
|
155
|
+
if (idToken !== true) return;
|
|
152
156
|
const messageCallback = (e: MessageEvent<any>) => {
|
|
153
|
-
let data = e?.data;
|
|
157
|
+
let data = e?.data || null;
|
|
154
158
|
if (typeof data === 'string') {
|
|
155
159
|
try {
|
|
156
160
|
data = JSON.parse(data);
|
|
@@ -159,15 +163,15 @@ export function KeycloakProvider({
|
|
|
159
163
|
if (debug) logger.debug('received message', data);
|
|
160
164
|
const message: MessageSchema = data;
|
|
161
165
|
if (!message.type) return;
|
|
162
|
-
if (message.type.toUpperCase() === '
|
|
163
|
-
if (debug) logger.debug('setting
|
|
164
|
-
|
|
166
|
+
if (message.type.toUpperCase() === 'ID_TOKEN' && message.payload) {
|
|
167
|
+
if (debug) logger.debug('setting id token', message.payload);
|
|
168
|
+
setIdToken(validToken(message.payload, refreshToken));
|
|
165
169
|
}
|
|
166
170
|
};
|
|
167
171
|
if (debug) {
|
|
168
172
|
logger.debug('listening for message', {
|
|
169
|
-
type: '
|
|
170
|
-
payload: '<
|
|
173
|
+
type: 'ID_TOKEN',
|
|
174
|
+
payload: '<some_id_token>',
|
|
171
175
|
});
|
|
172
176
|
}
|
|
173
177
|
window.addEventListener('message', messageCallback);
|
|
@@ -207,7 +211,7 @@ export function KeycloakProvider({
|
|
|
207
211
|
initOptions.token = token;
|
|
208
212
|
if (idToken && typeof idToken === 'string') initOptions.idToken = idToken;
|
|
209
213
|
if (refreshToken && typeof refreshToken === 'string') initOptions.refreshToken = refreshToken;
|
|
210
|
-
if (
|
|
214
|
+
if (isPassedInToken) {
|
|
211
215
|
initOptions.checkLoginIframe = false;
|
|
212
216
|
initOptions.onLoad = undefined;
|
|
213
217
|
}
|
|
@@ -253,7 +257,16 @@ export function KeycloakProvider({
|
|
|
253
257
|
);
|
|
254
258
|
}
|
|
255
259
|
|
|
256
|
-
|
|
260
|
+
function validToken(token: string | boolean, refreshToken?: string | boolean) {
|
|
261
|
+
if (typeof token !== 'string') return token;
|
|
262
|
+
if (refreshToken) {
|
|
263
|
+
if (refreshToken === true || isTokenValid(refreshToken)) return token;
|
|
264
|
+
return false;
|
|
265
|
+
}
|
|
266
|
+
return isTokenValid(token) ? token : false;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function isTokenValid(token: string) {
|
|
257
270
|
try {
|
|
258
271
|
const { exp } = JSON.parse(Buffer.from(token.split('.')?.[1] || '', 'base64').toString() || '{}');
|
|
259
272
|
return Math.floor(Date.now() / 1000) <= exp;
|