@multiplatform.one/keycloak 0.0.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/README.md +3 -0
- package/esm/Authenticated.js +53 -0
- package/esm/Authenticated.js.map +1 -0
- package/esm/authConfig.js +30 -0
- package/esm/authConfig.js.map +1 -0
- package/esm/hooks/index.js +28 -0
- package/esm/hooks/index.js.map +1 -0
- package/esm/hooks/useAuthConfig.js +30 -0
- package/esm/hooks/useAuthConfig.js.map +1 -0
- package/esm/hooks/useCurrentRouteName.js +38 -0
- package/esm/hooks/useCurrentRouteName.js.map +1 -0
- package/esm/hooks/useKeycloak/index.js +27 -0
- package/esm/hooks/useKeycloak/index.js.map +1 -0
- package/esm/hooks/useKeycloak/useKeycloak.js +62 -0
- package/esm/hooks/useKeycloak/useKeycloak.js.map +1 -0
- package/esm/hooks/useKeycloak/useKeycloak.native.js +50 -0
- package/esm/hooks/useKeycloak/useKeycloak.native.js.map +1 -0
- package/esm/hooks/useLogin.js +68 -0
- package/esm/hooks/useLogin.js.map +1 -0
- package/esm/index.js +29 -0
- package/esm/index.js.map +1 -0
- package/esm/package.json +1 -0
- package/esm/provider/afterAuth.js +76 -0
- package/esm/provider/afterAuth.js.map +1 -0
- package/esm/provider/index.js +60 -0
- package/esm/provider/index.js.map +1 -0
- package/esm/provider/keycloakProvider.js +220 -0
- package/esm/provider/keycloakProvider.js.map +1 -0
- package/esm/provider/keycloakProvider.native.js +40 -0
- package/esm/provider/keycloakProvider.native.js.map +1 -0
- package/esm/state/index.js +35 -0
- package/esm/state/index.js.map +1 -0
- package/esm/types.js +26 -0
- package/esm/types.js.map +1 -0
- package/lib/Authenticated.d.ts +32 -0
- package/lib/Authenticated.js +52 -0
- package/lib/Authenticated.js.map +1 -0
- package/lib/authConfig.d.ts +32 -0
- package/lib/authConfig.js +53 -0
- package/lib/authConfig.js.map +1 -0
- package/lib/hooks/index.d.ts +26 -0
- package/lib/hooks/index.js +56 -0
- package/lib/hooks/index.js.map +1 -0
- package/lib/hooks/useAuthConfig.d.ts +24 -0
- package/lib/hooks/useAuthConfig.js +50 -0
- package/lib/hooks/useAuthConfig.js.map +1 -0
- package/lib/hooks/useCurrentRouteName.d.ts +24 -0
- package/lib/hooks/useCurrentRouteName.js +56 -0
- package/lib/hooks/useCurrentRouteName.js.map +1 -0
- package/lib/hooks/useKeycloak/index.d.ts +32 -0
- package/lib/hooks/useKeycloak/index.js +36 -0
- package/lib/hooks/useKeycloak/index.js.map +1 -0
- package/lib/hooks/useKeycloak/useKeycloak.d.ts +25 -0
- package/lib/hooks/useKeycloak/useKeycloak.js +56 -0
- package/lib/hooks/useKeycloak/useKeycloak.js.map +1 -0
- package/lib/hooks/useKeycloak/useKeycloak.native.d.ts +32 -0
- package/lib/hooks/useKeycloak/useKeycloak.native.js +73 -0
- package/lib/hooks/useKeycloak/useKeycloak.native.js.map +1 -0
- package/lib/hooks/useLogin.d.ts +24 -0
- package/lib/hooks/useLogin.js +74 -0
- package/lib/hooks/useLogin.js.map +1 -0
- package/lib/index.d.ts +27 -0
- package/lib/index.js +66 -0
- package/lib/index.js.map +1 -0
- package/lib/package.json +1 -0
- package/lib/provider/afterAuth.d.ts +28 -0
- package/lib/provider/afterAuth.js +78 -0
- package/lib/provider/afterAuth.js.map +1 -0
- package/lib/provider/index.d.ts +31 -0
- package/lib/provider/index.js +76 -0
- package/lib/provider/index.js.map +1 -0
- package/lib/provider/keycloakProvider.d.ts +46 -0
- package/lib/provider/keycloakProvider.js +214 -0
- package/lib/provider/keycloakProvider.js.map +1 -0
- package/lib/provider/keycloakProvider.native.d.ts +26 -0
- package/lib/provider/keycloakProvider.native.js +37 -0
- package/lib/provider/keycloakProvider.native.js.map +1 -0
- package/lib/state/index.d.ts +34 -0
- package/lib/state/index.js +59 -0
- package/lib/state/index.js.map +1 -0
- package/lib/tsconfig.build.tsbuildinfo +1 -0
- package/lib/types.d.ts +39 -0
- package/lib/types.js +48 -0
- package/lib/types.js.map +1 -0
- package/package.json +141 -0
- package/src/@types/expoKeycloakAuth.d.ts +28 -0
- package/src/Authenticated.tsx +56 -0
- package/src/authConfig.ts +38 -0
- package/src/hooks/index.ts +27 -0
- package/src/hooks/useAuthConfig.ts +30 -0
- package/src/hooks/useCurrentRouteName.ts +37 -0
- package/src/hooks/useKeycloak/index.ts +34 -0
- package/src/hooks/useKeycloak/useKeycloak.native.ts +48 -0
- package/src/hooks/useKeycloak/useKeycloak.ts +45 -0
- package/src/hooks/useLogin.ts +50 -0
- package/src/index.ts +28 -0
- package/src/provider/afterAuth.tsx +56 -0
- package/src/provider/index.tsx +57 -0
- package/src/provider/keycloakProvider.native.tsx +42 -0
- package/src/provider/keycloakProvider.tsx +220 -0
- package/src/state/index.ts +36 -0
- package/src/types.ts +40 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /auth/provider/keycloakProvider.tsx
|
|
3
|
+
* Project: app
|
|
4
|
+
* File Created: 08-11-2022 03:12:43
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 22-11-2022 18:07:46
|
|
8
|
+
* Modified By: Clay Risser
|
|
9
|
+
* -----
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2021 - 2022
|
|
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 Keycloak, { KeycloakInitOptions } from "keycloak-js";
|
|
26
|
+
import { MultiPlatform } from "multiplatform.one";
|
|
27
|
+
import { ReactKeycloakProvider } from "@react-keycloak/web";
|
|
28
|
+
import { SSRKeycloakProvider, SSRCookies } from "@react-keycloak/ssr";
|
|
29
|
+
import { useRouter } from "next/router";
|
|
30
|
+
import React, {
|
|
31
|
+
FC,
|
|
32
|
+
ReactNode,
|
|
33
|
+
useMemo,
|
|
34
|
+
useEffect,
|
|
35
|
+
useState,
|
|
36
|
+
ComponentType,
|
|
37
|
+
} from "react";
|
|
38
|
+
import { useAuthConfig } from "../hooks/useAuthConfig";
|
|
39
|
+
import { useAuthState } from "../state";
|
|
40
|
+
import { AfterAuth } from "./afterAuth";
|
|
41
|
+
|
|
42
|
+
export interface KeycloakProviderProps {
|
|
43
|
+
children: ReactNode;
|
|
44
|
+
cookies?: unknown;
|
|
45
|
+
debug?: boolean;
|
|
46
|
+
disabled?: boolean;
|
|
47
|
+
keycloakConfig: KeycloakConfig;
|
|
48
|
+
keycloakInitOptions?: KeycloakInitOptions;
|
|
49
|
+
loadingComponent?: ComponentType;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const logger = console;
|
|
53
|
+
|
|
54
|
+
export const KeycloakProvider: FC<KeycloakProviderProps> = ({
|
|
55
|
+
children,
|
|
56
|
+
cookies,
|
|
57
|
+
debug,
|
|
58
|
+
keycloakConfig,
|
|
59
|
+
keycloakInitOptions,
|
|
60
|
+
loadingComponent,
|
|
61
|
+
}: KeycloakProviderProps) => {
|
|
62
|
+
const [auth] = useAuthState();
|
|
63
|
+
const LoadingComponent =
|
|
64
|
+
loadingComponent || (() => <>{debug ? "authenticating" : null}</>);
|
|
65
|
+
const { query } = MultiPlatform.isNext() ? useRouter() : { query: {} };
|
|
66
|
+
const authConfig = useAuthConfig();
|
|
67
|
+
const [token, setToken] = useState<string | boolean>(
|
|
68
|
+
("token" in query && (query.token?.toString() || true)) ||
|
|
69
|
+
auth.token ||
|
|
70
|
+
false
|
|
71
|
+
);
|
|
72
|
+
const [refreshToken, setRefreshToken] = useState<string | boolean>(
|
|
73
|
+
("refreshToken" in query && (query.refreshToken?.toString() || true)) ||
|
|
74
|
+
auth.refreshToken ||
|
|
75
|
+
false
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
if (token !== true && refreshToken !== true) return;
|
|
80
|
+
if (debug) logger.debug("post message", { type: "LOADED" });
|
|
81
|
+
(authConfig.messageHandlerKeys || []).forEach((key: string) => {
|
|
82
|
+
window?.webkit?.messageHandlers?.[key]?.postMessage(
|
|
83
|
+
JSON.stringify({ type: "LOADED" })
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
window?.ReactNativeWebView?.postMessage(JSON.stringify({ type: "LOADED" }));
|
|
87
|
+
window?.parent?.postMessage(JSON.stringify({ type: "LOADED" }));
|
|
88
|
+
}, []);
|
|
89
|
+
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
if (token !== true) return;
|
|
92
|
+
const messageCallback = (e: MessageEvent<any>) => {
|
|
93
|
+
let data = e?.data;
|
|
94
|
+
if (typeof data === "string") {
|
|
95
|
+
try {
|
|
96
|
+
data = JSON.parse(data);
|
|
97
|
+
} catch (err) {}
|
|
98
|
+
}
|
|
99
|
+
if (debug) logger.debug("received message", data);
|
|
100
|
+
const message: MessageSchema = data;
|
|
101
|
+
if (!message.type) return;
|
|
102
|
+
if (message.type.toUpperCase() === "TOKEN" && message.payload) {
|
|
103
|
+
if (debug) logger.debug("setting token", message.payload);
|
|
104
|
+
setToken(message.payload);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
if (debug)
|
|
108
|
+
logger.debug("listening for message", {
|
|
109
|
+
type: "TOKEN",
|
|
110
|
+
payload: "<some_token>",
|
|
111
|
+
});
|
|
112
|
+
window.addEventListener("message", messageCallback);
|
|
113
|
+
return () => {
|
|
114
|
+
window.removeEventListener("message", messageCallback);
|
|
115
|
+
};
|
|
116
|
+
}, []);
|
|
117
|
+
|
|
118
|
+
useEffect(() => {
|
|
119
|
+
if (refreshToken !== true) return;
|
|
120
|
+
const messageCallback = (e: MessageEvent<any>) => {
|
|
121
|
+
let data = e?.data;
|
|
122
|
+
if (typeof data === "string") {
|
|
123
|
+
try {
|
|
124
|
+
data = JSON.parse(data);
|
|
125
|
+
} catch (err) {}
|
|
126
|
+
}
|
|
127
|
+
if (debug) logger.debug("received message", data);
|
|
128
|
+
const message: MessageSchema = data;
|
|
129
|
+
if (!message.type) return;
|
|
130
|
+
if (message.type.toUpperCase() === "REFRESH_TOKEN" && message.payload) {
|
|
131
|
+
if (debug) logger.debug("setting refresh token", message.payload);
|
|
132
|
+
setRefreshToken(message.payload);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
if (debug)
|
|
136
|
+
logger.debug("listening for message", {
|
|
137
|
+
type: "REFRESH_TOKEN",
|
|
138
|
+
payload: "<some_refresh_token>",
|
|
139
|
+
});
|
|
140
|
+
window.addEventListener("message", messageCallback);
|
|
141
|
+
return () => {
|
|
142
|
+
window.removeEventListener("message", messageCallback);
|
|
143
|
+
};
|
|
144
|
+
}, []);
|
|
145
|
+
|
|
146
|
+
const keycloak = useMemo(
|
|
147
|
+
() =>
|
|
148
|
+
typeof window !== "undefined" &&
|
|
149
|
+
new Keycloak({
|
|
150
|
+
url: keycloakConfig.baseUrl,
|
|
151
|
+
realm: keycloakConfig.realm,
|
|
152
|
+
clientId: keycloakConfig.clientId,
|
|
153
|
+
}),
|
|
154
|
+
[]
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
const initOptions = useMemo(() => {
|
|
158
|
+
const initOptions = {
|
|
159
|
+
...defaultKeycloakInitOptions,
|
|
160
|
+
...keycloakInitOptions,
|
|
161
|
+
};
|
|
162
|
+
if (token && typeof token === "string") {
|
|
163
|
+
initOptions.token = token;
|
|
164
|
+
if (refreshToken && typeof refreshToken === "string")
|
|
165
|
+
initOptions.refreshToken = refreshToken;
|
|
166
|
+
initOptions.checkLoginIframe = false;
|
|
167
|
+
initOptions.onLoad = undefined;
|
|
168
|
+
}
|
|
169
|
+
return initOptions;
|
|
170
|
+
}, [keycloakInitOptions, token, refreshToken]);
|
|
171
|
+
|
|
172
|
+
if (token === true || refreshToken === true) return <LoadingComponent />;
|
|
173
|
+
if (cookies && authConfig.ssr) {
|
|
174
|
+
return (
|
|
175
|
+
// @ts-ignore
|
|
176
|
+
<SSRKeycloakProvider
|
|
177
|
+
initOptions={initOptions}
|
|
178
|
+
keycloakConfig={{
|
|
179
|
+
url: keycloakConfig.baseUrl,
|
|
180
|
+
realm: keycloakConfig.realm,
|
|
181
|
+
clientId: keycloakConfig.clientId,
|
|
182
|
+
}}
|
|
183
|
+
persistor={SSRCookies(cookies)}
|
|
184
|
+
LoadingComponent={<LoadingComponent />}
|
|
185
|
+
>
|
|
186
|
+
<AfterAuth>{children}</AfterAuth>
|
|
187
|
+
</SSRKeycloakProvider>
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
if (!keycloak) return <>{children}</>;
|
|
191
|
+
return (
|
|
192
|
+
<ReactKeycloakProvider
|
|
193
|
+
initOptions={initOptions}
|
|
194
|
+
authClient={keycloak}
|
|
195
|
+
LoadingComponent={<LoadingComponent />}
|
|
196
|
+
>
|
|
197
|
+
<AfterAuth>{children}</AfterAuth>
|
|
198
|
+
</ReactKeycloakProvider>
|
|
199
|
+
);
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
export interface KeycloakConfig {
|
|
203
|
+
clientId: string;
|
|
204
|
+
realm: string;
|
|
205
|
+
scheme?: string;
|
|
206
|
+
baseUrl: string;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export const defaultKeycloakInitOptions: KeycloakInitOptions = {
|
|
210
|
+
checkLoginIframe: true,
|
|
211
|
+
checkLoginIframeInterval: 5,
|
|
212
|
+
enableLogging: false,
|
|
213
|
+
onLoad: "check-sso",
|
|
214
|
+
pkceMethod: "S256",
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
export interface MessageSchema {
|
|
218
|
+
type: string;
|
|
219
|
+
payload?: string;
|
|
220
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/state/index.ts
|
|
3
|
+
* Project: app
|
|
4
|
+
* File Created: 22-11-2022 17:40:53
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 25-11-2022 09:52:31
|
|
8
|
+
* Modified By: Clay Risser
|
|
9
|
+
* -----
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2021 - 2022
|
|
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 { atom, useRecoilState } from "recoil";
|
|
26
|
+
import persist from "react-native-recoil-persist";
|
|
27
|
+
|
|
28
|
+
export function useAuthState() {
|
|
29
|
+
return useRecoilState(authState);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const authState = atom<{ token?: string; refreshToken?: string }>({
|
|
33
|
+
key: "auth",
|
|
34
|
+
default: {},
|
|
35
|
+
effects: [persist.persistAtom],
|
|
36
|
+
});
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/types.ts
|
|
3
|
+
* Project: @multiplatform.one/keycloak
|
|
4
|
+
* File Created: 25-11-2022 10:27:53
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* Last Modified: 25-11-2022 10:28:25
|
|
8
|
+
* Modified By: Clay Risser
|
|
9
|
+
* -----
|
|
10
|
+
* Risser Labs LLC (c) Copyright 2022
|
|
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
|
+
declare global {
|
|
26
|
+
interface Window {
|
|
27
|
+
ReactNativeWebView: {
|
|
28
|
+
postMessage: (body: string) => unknown;
|
|
29
|
+
};
|
|
30
|
+
webkit: {
|
|
31
|
+
messageHandlers: {
|
|
32
|
+
[key: string]: {
|
|
33
|
+
postMessage: (body: string) => unknown;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default null;
|