@multiplatform.one/keycloak 0.7.14 → 0.7.16
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/package.json +28 -28
- package/routes/index.d.ts +22 -0
- package/routes/index.js +22 -0
- package/src/Authenticated.tsx +27 -17
- package/src/authConfig.ts +3 -9
- package/src/hooks/index.ts +0 -3
- package/src/hooks/useTokensFromQuery/index.ts +1 -0
- package/src/hooks/useTokensFromState.ts +1 -1
- package/src/index.ts +3 -0
- package/src/{@types/coreJsPure.d.ts → keycloak/context.ts} +6 -11
- package/{lib/@types/expoKeycloakAuth.d.d.mts → src/keycloak/index.ts} +4 -7
- package/src/keycloak/keycloak.native.ts +148 -0
- package/src/keycloak/keycloak.ts +175 -0
- package/src/provider/AfterAuth.tsx +63 -0
- package/src/provider/{keycloakProvider.native.tsx → AuthProvider/AuthProvider.native.tsx} +8 -16
- package/src/provider/{keycloakProvider.tsx → AuthProvider/AuthProvider.tsx} +81 -94
- package/{lib/@types/expoKeycloakAuth.d.d.ts → src/provider/AuthProvider/index.ts} +4 -7
- package/src/provider/KeycloakProvider.tsx +53 -0
- package/src/{hooks/useCurrentRouteName/index.native.ts → provider/index.ts} +6 -11
- package/src/routes.ts +163 -0
- package/src/session/index.ts +22 -0
- package/src/session/session.native.ts +31 -0
- package/src/session/session.ts +68 -0
- package/src/{state/index.ts → state.ts} +5 -4
- package/src/{util.ts → token.ts} +25 -11
- package/src/types.ts +4 -1
- package/lib/@types/coreJsPure.d.d.mts +0 -2
- package/lib/@types/coreJsPure.d.d.ts +0 -2
- package/lib/@types/coreJsPure.d.js +0 -1
- package/lib/@types/coreJsPure.d.mjs +0 -0
- package/lib/@types/expoKeycloakAuth.d.js +0 -1
- package/lib/@types/expoKeycloakAuth.d.mjs +0 -0
- package/lib/Authenticated.d.mts +0 -33
- package/lib/Authenticated.d.ts +0 -33
- package/lib/Authenticated.js +0 -19
- package/lib/Authenticated.mjs +0 -20
- package/lib/authConfig.d.mts +0 -14
- package/lib/authConfig.d.ts +0 -14
- package/lib/authConfig.js +0 -8
- package/lib/authConfig.mjs +0 -9
- package/lib/chunk-2GUOEPFY.mjs +0 -0
- package/lib/chunk-3ZPHNEJL.js +0 -11
- package/lib/chunk-64PPU7Q3.mjs +0 -13
- package/lib/chunk-6NDWOV55.js +0 -1
- package/lib/chunk-7MTPH6XK.mjs +0 -26
- package/lib/chunk-A7UBOIAL.js +0 -13
- package/lib/chunk-AD6VPMID.mjs +0 -20
- package/lib/chunk-AUUZ6BI7.js +0 -78
- package/lib/chunk-AYKQVWMR.mjs +0 -0
- package/lib/chunk-BVB33X7V.mjs +0 -265
- package/lib/chunk-C2FNSVJO.js +0 -17
- package/lib/chunk-J4CCMILN.mjs +0 -41
- package/lib/chunk-K33RKJZX.js +0 -443
- package/lib/chunk-KLLABHIC.js +0 -13
- package/lib/chunk-L5HMOY4M.mjs +0 -18
- package/lib/chunk-MZ6E62HK.js +0 -33
- package/lib/chunk-NWRANDZ6.js +0 -98
- package/lib/chunk-NXTVYIBR.mjs +0 -11
- package/lib/chunk-OYWAYMFN.js +0 -17
- package/lib/chunk-RPUOO7AV.mjs +0 -10
- package/lib/chunk-RYM7KERK.mjs +0 -52
- package/lib/chunk-UBVYEZI3.js +0 -1
- package/lib/chunk-UHTKOFS6.mjs +0 -84
- package/lib/chunk-UL6L5ONI.js +0 -460
- package/lib/chunk-UOKDJT5L.mjs +0 -16
- package/lib/chunk-W5DJLFTQ.js +0 -56
- package/lib/chunk-WT5W6BSZ.js +0 -161
- package/lib/chunk-XEO3YXBM.js +0 -22
- package/lib/chunk-XO4W2SMN.mjs +0 -50
- package/lib/chunk-XVPWAAZ4.mjs +0 -76
- package/lib/chunk-Y4XMF3R6.mjs +0 -13
- package/lib/chunk-YGNNHTFE.js +0 -12
- package/lib/hooks/index.d.mts +0 -9
- package/lib/hooks/index.d.ts +0 -9
- package/lib/hooks/index.js +0 -23
- package/lib/hooks/index.mjs +0 -34
- package/lib/hooks/useAuthConfig.d.mts +0 -6
- package/lib/hooks/useAuthConfig.d.ts +0 -6
- package/lib/hooks/useAuthConfig.js +0 -8
- package/lib/hooks/useAuthConfig.mjs +0 -8
- package/lib/hooks/useCurrentRouteName/index.d.mts +0 -3
- package/lib/hooks/useCurrentRouteName/index.d.ts +0 -3
- package/lib/hooks/useCurrentRouteName/index.js +0 -7
- package/lib/hooks/useCurrentRouteName/index.mjs +0 -7
- package/lib/hooks/useCurrentRouteName/index.native.d.mts +0 -3
- package/lib/hooks/useCurrentRouteName/index.native.d.ts +0 -3
- package/lib/hooks/useCurrentRouteName/index.native.js +0 -14
- package/lib/hooks/useCurrentRouteName/index.native.mjs +0 -14
- package/lib/hooks/useKeycloak/index.d.mts +0 -46
- package/lib/hooks/useKeycloak/index.d.ts +0 -46
- package/lib/hooks/useKeycloak/index.js +0 -11
- package/lib/hooks/useKeycloak/index.mjs +0 -12
- package/lib/hooks/useKeycloak/useKeycloak.d.mts +0 -2
- package/lib/hooks/useKeycloak/useKeycloak.d.ts +0 -2
- package/lib/hooks/useKeycloak/useKeycloak.js +0 -10
- package/lib/hooks/useKeycloak/useKeycloak.mjs +0 -11
- package/lib/hooks/useKeycloak/useKeycloak.native.d.mts +0 -20
- package/lib/hooks/useKeycloak/useKeycloak.native.d.ts +0 -20
- package/lib/hooks/useKeycloak/useKeycloak.native.js +0 -80
- package/lib/hooks/useKeycloak/useKeycloak.native.mjs +0 -69
- package/lib/hooks/useLogin.d.mts +0 -3
- package/lib/hooks/useLogin.d.ts +0 -3
- package/lib/hooks/useLogin.js +0 -11
- package/lib/hooks/useLogin.mjs +0 -11
- package/lib/hooks/useTokensFromQuery/index.d.mts +0 -3
- package/lib/hooks/useTokensFromQuery/index.d.ts +0 -3
- package/lib/hooks/useTokensFromQuery/index.js +0 -7
- package/lib/hooks/useTokensFromQuery/index.mjs +0 -7
- package/lib/hooks/useTokensFromQuery/index.native.d.mts +0 -3
- package/lib/hooks/useTokensFromQuery/index.native.d.ts +0 -3
- package/lib/hooks/useTokensFromQuery/index.native.js +0 -10
- package/lib/hooks/useTokensFromQuery/index.native.mjs +0 -7
- package/lib/hooks/useTokensFromState.d.mts +0 -3
- package/lib/hooks/useTokensFromState.d.ts +0 -3
- package/lib/hooks/useTokensFromState.js +0 -9
- package/lib/hooks/useTokensFromState.mjs +0 -9
- package/lib/index.d.mts +0 -13
- package/lib/index.d.ts +0 -13
- package/lib/index.js +0 -34
- package/lib/index.mjs +0 -56
- package/lib/provider/afterAuth.d.mts +0 -29
- package/lib/provider/afterAuth.d.ts +0 -29
- package/lib/provider/afterAuth.js +0 -18
- package/lib/provider/afterAuth.mjs +0 -18
- package/lib/provider/index.d.mts +0 -39
- package/lib/provider/index.d.ts +0 -39
- package/lib/provider/index.js +0 -22
- package/lib/provider/index.mjs +0 -25
- package/lib/provider/keycloakProvider.d.mts +0 -42
- package/lib/provider/keycloakProvider.d.ts +0 -42
- package/lib/provider/keycloakProvider.js +0 -20
- package/lib/provider/keycloakProvider.mjs +0 -21
- package/lib/provider/keycloakProvider.native.d.mts +0 -29
- package/lib/provider/keycloakProvider.native.d.ts +0 -29
- package/lib/provider/keycloakProvider.native.js +0 -87
- package/lib/provider/keycloakProvider.native.mjs +0 -30
- package/lib/state/index.d.mts +0 -11
- package/lib/state/index.d.ts +0 -11
- package/lib/state/index.js +0 -7
- package/lib/state/index.mjs +0 -7
- package/lib/types.d.mts +0 -17
- package/lib/types.d.ts +0 -17
- package/lib/types.js +0 -8
- package/lib/types.mjs +0 -7
- package/lib/util.d.mts +0 -4
- package/lib/util.d.ts +0 -4
- package/lib/util.js +0 -8
- package/lib/util.mjs +0 -9
- package/src/hooks/useCurrentRouteName/index.ts +0 -34
- package/src/hooks/useKeycloak/index.ts +0 -47
- package/src/hooks/useKeycloak/useKeycloak.native.ts +0 -95
- package/src/hooks/useKeycloak/useKeycloak.ts +0 -106
- package/src/hooks/useLogin.ts +0 -47
- package/src/provider/afterAuth.tsx +0 -65
- package/src/provider/index.tsx +0 -117
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@multiplatform.one/keycloak",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.16",
|
|
4
4
|
"description": "keycloak client for multiplatform.one ecosystem",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "lib/index.mjs",
|
|
@@ -12,6 +12,11 @@
|
|
|
12
12
|
"import": "./lib/index.mjs",
|
|
13
13
|
"require": "./lib/index.js",
|
|
14
14
|
"types": "./lib/index.d.ts"
|
|
15
|
+
},
|
|
16
|
+
"./routes": {
|
|
17
|
+
"import": "./lib/routes.mjs",
|
|
18
|
+
"require": "./lib/routes.js",
|
|
19
|
+
"types": "./lib/routes.d.ts"
|
|
15
20
|
}
|
|
16
21
|
},
|
|
17
22
|
"author": "BitSpur",
|
|
@@ -26,9 +31,8 @@
|
|
|
26
31
|
"homepage": "https://multiplatform.one",
|
|
27
32
|
"license": "Apache-2.0",
|
|
28
33
|
"files": [
|
|
29
|
-
"dist",
|
|
30
|
-
"index.d.ts",
|
|
31
34
|
"lib",
|
|
35
|
+
"routes",
|
|
32
36
|
"src"
|
|
33
37
|
],
|
|
34
38
|
"repository": {
|
|
@@ -36,34 +40,35 @@
|
|
|
36
40
|
"url": "https://gitlab.com/risserlabs/multiplatform.one/keycloak"
|
|
37
41
|
},
|
|
38
42
|
"peerDependencies": {
|
|
39
|
-
"@react-native-async-storage/async-storage": "
|
|
40
|
-
"@react-navigation/native": "^6.0.14",
|
|
43
|
+
"@react-native-async-storage/async-storage": "1.19.3",
|
|
41
44
|
"expo-keycloak-auth": "1.0.14",
|
|
42
|
-
"multiplatform.one": "^0.7.
|
|
45
|
+
"multiplatform.one": "^0.7.16",
|
|
43
46
|
"next": "^13.0.5",
|
|
44
47
|
"react": "^18.2.0",
|
|
45
|
-
"react-native": "~0.
|
|
48
|
+
"react-native": "~0.73.1"
|
|
46
49
|
},
|
|
47
50
|
"devDependencies": {
|
|
48
|
-
"@react-native-async-storage/async-storage": "
|
|
51
|
+
"@react-native-async-storage/async-storage": "1.21.0",
|
|
49
52
|
"@react-navigation/native": "^6.1.9",
|
|
50
|
-
"@types/eslint": "^8",
|
|
51
|
-
"@types/jest": "^29.5.
|
|
52
|
-
"@types/lodash.camelcase": "^4.3.
|
|
53
|
-
"@types/lodash.startcase": "^4.4.
|
|
54
|
-
"@types/react": "^18.2.
|
|
55
|
-
"@types/react-native": "^0.
|
|
56
|
-
"cspell": "^
|
|
53
|
+
"@types/eslint": "^8.56.2",
|
|
54
|
+
"@types/jest": "^29.5.11",
|
|
55
|
+
"@types/lodash.camelcase": "^4.3.9",
|
|
56
|
+
"@types/lodash.startcase": "^4.4.9",
|
|
57
|
+
"@types/react": "^18.2.47",
|
|
58
|
+
"@types/react-native": "^0.73.0",
|
|
59
|
+
"cspell": "^8.3.2",
|
|
60
|
+
"eslint": "^8.56.0",
|
|
57
61
|
"expo-keycloak-auth": "1.0.14",
|
|
58
62
|
"jest": "^29.7.0",
|
|
59
63
|
"jest-sonar-reporter": "^2.0.0",
|
|
60
|
-
"multiplatform.one": "^0.7.
|
|
64
|
+
"multiplatform.one": "^0.7.16",
|
|
61
65
|
"next": "^13.5.6",
|
|
62
|
-
"prettier": "^3.
|
|
66
|
+
"prettier": "^3.1.1",
|
|
63
67
|
"react": "^18.2.0",
|
|
64
|
-
"react-native": "~0.
|
|
65
|
-
"ts-node": "^10.9.
|
|
66
|
-
"
|
|
68
|
+
"react-native": "~0.73.2",
|
|
69
|
+
"ts-node": "^10.9.2",
|
|
70
|
+
"tsup": "^8.0.1",
|
|
71
|
+
"typescript": "^5.3.3"
|
|
67
72
|
},
|
|
68
73
|
"engines": {
|
|
69
74
|
"node": ">=6.0.0"
|
|
@@ -90,13 +95,8 @@
|
|
|
90
95
|
"indent": 4
|
|
91
96
|
},
|
|
92
97
|
"dependencies": {
|
|
93
|
-
"
|
|
94
|
-
"
|
|
95
|
-
"
|
|
96
|
-
"core-js-pure": "^3.33.1",
|
|
97
|
-
"eslint": "^8.55.0",
|
|
98
|
-
"expo-auth-session": "^5.2.2",
|
|
99
|
-
"solito": "^4.1.3",
|
|
100
|
-
"tsup": "^8.0.1"
|
|
98
|
+
"expo-auth-session": "^5.4.0",
|
|
99
|
+
"keycloak-js": "^23.0.4",
|
|
100
|
+
"next-auth": "^4.24.5"
|
|
101
101
|
}
|
|
102
102
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* File: /routes/index.d.ts
|
|
3
|
+
* Project: multiplatform.one
|
|
4
|
+
* File Created: 22-06-2023 05:33:21
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* BitSpur (c) Copyright 2021 - 2023
|
|
8
|
+
*
|
|
9
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License.
|
|
11
|
+
* You may obtain a copy of the License at
|
|
12
|
+
*
|
|
13
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
* See the License for the specific language governing permissions and
|
|
19
|
+
* limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
export * from '../lib/routes.js';
|
package/routes/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* File: /routes/index.js
|
|
3
|
+
* Project: multiplatform.one
|
|
4
|
+
* File Created: 22-06-2023 05:33:21
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* BitSpur (c) Copyright 2021 - 2023
|
|
8
|
+
*
|
|
9
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License.
|
|
11
|
+
* You may obtain a copy of the License at
|
|
12
|
+
*
|
|
13
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
* See the License for the specific language governing permissions and
|
|
19
|
+
* limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
module.exports = require('../lib/routes/index');
|
package/src/Authenticated.tsx
CHANGED
|
@@ -21,38 +21,48 @@
|
|
|
21
21
|
|
|
22
22
|
import React, { useEffect } from 'react';
|
|
23
23
|
import type { ComponentType, ReactNode } from 'react';
|
|
24
|
-
import {
|
|
25
|
-
import { useAuthState } from './state';
|
|
24
|
+
import { MultiPlatform } from 'multiplatform.one';
|
|
26
25
|
import { useAuthConfig, useTokensFromQuery, useTokensFromState } from './hooks';
|
|
26
|
+
import { useKeycloak } from './keycloak';
|
|
27
27
|
|
|
28
28
|
export interface AuthenticatedProps {
|
|
29
29
|
children: ReactNode;
|
|
30
30
|
disabled?: boolean;
|
|
31
|
+
loadingComponent?: ComponentType;
|
|
31
32
|
loggedOutComponent?: ComponentType;
|
|
32
|
-
loginRoute?: string;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
export function Authenticated({ children, disabled,
|
|
35
|
+
export function Authenticated({ children, disabled, loggedOutComponent, loadingComponent }: AuthenticatedProps) {
|
|
36
36
|
const authConfig = useAuthConfig();
|
|
37
|
-
const
|
|
38
|
-
const login = useLogin(loginRoute);
|
|
37
|
+
const keycloak = useKeycloak();
|
|
39
38
|
const tokensFromQuery = useTokensFromQuery();
|
|
40
39
|
const tokensFromState = useTokensFromState();
|
|
41
|
-
const { authenticated } = useKeycloak();
|
|
42
40
|
|
|
43
41
|
useEffect(() => {
|
|
44
|
-
if (
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
42
|
+
if (
|
|
43
|
+
!keycloak ||
|
|
44
|
+
MultiPlatform.isIframe ||
|
|
45
|
+
MultiPlatform.isServer ||
|
|
46
|
+
keycloak.authenticated ||
|
|
47
|
+
tokensFromQuery ||
|
|
48
|
+
tokensFromState
|
|
49
|
+
) {
|
|
50
|
+
return;
|
|
49
51
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
+
keycloak.login({
|
|
53
|
+
redirectUri: authConfig.loginRedirectUri,
|
|
54
|
+
});
|
|
55
|
+
}, [keycloak]);
|
|
52
56
|
|
|
53
|
-
if (disabled
|
|
54
|
-
|
|
55
|
-
|
|
57
|
+
if (typeof disabled === 'undefined') disabled = authConfig.disabled;
|
|
58
|
+
if (disabled) return <>{children}</>;
|
|
59
|
+
if (typeof keycloak === 'undefined') {
|
|
60
|
+
const LoadingComponent = loadingComponent;
|
|
61
|
+
return LoadingComponent ? <LoadingComponent /> : <>{authConfig.debug ? 'loading' : null}</>;
|
|
62
|
+
}
|
|
63
|
+
if (keycloak.authenticated) return <>{children}</>;
|
|
64
|
+
const LoggedOutComponent = loggedOutComponent;
|
|
65
|
+
return LoggedOutComponent ? <LoggedOutComponent /> : <>{authConfig.debug ? 'not authenticated' : null}</>;
|
|
56
66
|
}
|
|
57
67
|
|
|
58
68
|
export function withAuthenticated<P extends object>(Component: ComponentType<P>) {
|
package/src/authConfig.ts
CHANGED
|
@@ -21,19 +21,13 @@
|
|
|
21
21
|
|
|
22
22
|
import { createContext } from 'react';
|
|
23
23
|
|
|
24
|
-
export const defaultAuthConfig: AuthConfig = {
|
|
25
|
-
ensureFreshness: true,
|
|
26
|
-
persist: false,
|
|
27
|
-
ssr: false,
|
|
28
|
-
};
|
|
24
|
+
export const defaultAuthConfig: AuthConfig = {};
|
|
29
25
|
|
|
30
26
|
export const AuthConfigContext = createContext<AuthConfig>(defaultAuthConfig);
|
|
31
27
|
|
|
32
28
|
export interface AuthConfig {
|
|
33
29
|
debug?: boolean;
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
disabled?: boolean;
|
|
31
|
+
loginRedirectUri?: string;
|
|
36
32
|
messageHandlerKeys?: string[];
|
|
37
|
-
persist?: boolean;
|
|
38
|
-
ssr?: boolean;
|
|
39
33
|
}
|
package/src/hooks/index.ts
CHANGED
|
@@ -23,6 +23,7 @@ import { MultiPlatform } from 'multiplatform.one';
|
|
|
23
23
|
import { useRouter } from 'next/router';
|
|
24
24
|
|
|
25
25
|
export function useTokensFromQuery() {
|
|
26
|
+
if (!MultiPlatform.isIframe) return false;
|
|
26
27
|
const { query } = MultiPlatform.isNext ? useRouter() : { query: {} };
|
|
27
28
|
return 'idToken' in query || 'token' in query || 'refreshToken' in query;
|
|
28
29
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* File: /src
|
|
2
|
+
* File: /src/keycloak/context.ts
|
|
3
3
|
* Project: @multiplatform.one/keycloak
|
|
4
|
-
* File Created:
|
|
4
|
+
* File Created: 10-01-2024 15:10:13
|
|
5
5
|
* Author: Clay Risser
|
|
6
6
|
* -----
|
|
7
|
-
* BitSpur (c) Copyright 2021 -
|
|
7
|
+
* BitSpur (c) Copyright 2021 - 2024
|
|
8
8
|
*
|
|
9
9
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
10
|
* you may not use this file except in compliance with the License.
|
|
@@ -19,12 +19,7 @@
|
|
|
19
19
|
* limitations under the License.
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
// export = atob;
|
|
25
|
-
// }
|
|
22
|
+
import type { Keycloak } from './keycloak';
|
|
23
|
+
import { createContext } from 'react';
|
|
26
24
|
|
|
27
|
-
|
|
28
|
-
// const escape: (...args: any[]) => any;
|
|
29
|
-
// export = escape;
|
|
30
|
-
// }
|
|
25
|
+
export const KeycloakContext = createContext<Keycloak | undefined>(undefined);
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* File: /src
|
|
2
|
+
* File: /src/keycloak/index.ts
|
|
3
3
|
* Project: @multiplatform.one/keycloak
|
|
4
|
-
* File Created:
|
|
4
|
+
* File Created: 09-01-2024 11:59:25
|
|
5
5
|
* Author: Clay Risser
|
|
6
6
|
* -----
|
|
7
|
-
* BitSpur (c) Copyright 2021 -
|
|
7
|
+
* BitSpur (c) Copyright 2021 - 2024
|
|
8
8
|
*
|
|
9
9
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
10
|
* you may not use this file except in compliance with the License.
|
|
@@ -19,7 +19,4 @@
|
|
|
19
19
|
* limitations under the License.
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
export function useKeycloak(): any;
|
|
24
|
-
export const KeycloakProvider: any;
|
|
25
|
-
}
|
|
22
|
+
export * from './keycloak';
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* File: /src/keycloak/keycloak.native.ts
|
|
3
|
+
* Project: @multiplatform.one/keycloak
|
|
4
|
+
* File Created: 09-01-2024 11:59:37
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* BitSpur (c) Copyright 2021 - 2024
|
|
8
|
+
*
|
|
9
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License.
|
|
11
|
+
* You may obtain a copy of the License at
|
|
12
|
+
*
|
|
13
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
* See the License for the specific language governing permissions and
|
|
19
|
+
* limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import type KeycloakClient from 'keycloak-js';
|
|
23
|
+
import type { AccessTokenParsed, TokenParsed } from '../token';
|
|
24
|
+
import type { KeycloakLoginOptions, KeycloakLogoutOptions } from 'keycloak-js';
|
|
25
|
+
import type { KeycloakMock } from '../types';
|
|
26
|
+
import { MultiPlatform } from 'multiplatform.one';
|
|
27
|
+
import { getSession } from '../session/session';
|
|
28
|
+
import { jwtDecode } from 'jwt-decode';
|
|
29
|
+
import { useKeycloak as useExpoKeycloak } from 'expo-keycloak-auth';
|
|
30
|
+
|
|
31
|
+
export class Keycloak {
|
|
32
|
+
token: string;
|
|
33
|
+
idToken?: string;
|
|
34
|
+
refreshToken?: string;
|
|
35
|
+
|
|
36
|
+
private _tokenParsed?: AccessTokenParsed;
|
|
37
|
+
private _idTokenParsed?: TokenParsed;
|
|
38
|
+
private _refreshTokenParsed?: TokenParsed;
|
|
39
|
+
private keycloakClient?: KeycloakClient;
|
|
40
|
+
private mock: KeycloakMock | undefined;
|
|
41
|
+
|
|
42
|
+
constructor(input: string | KeycloakClient | KeycloakMock, idToken?: string, refreshToken?: string) {
|
|
43
|
+
if (typeof input === 'string') {
|
|
44
|
+
this.token = input;
|
|
45
|
+
this.idToken = idToken;
|
|
46
|
+
this.refreshToken = refreshToken;
|
|
47
|
+
} else if (typeof input === 'object') {
|
|
48
|
+
if (typeof (input as KeycloakClient).init === 'function') {
|
|
49
|
+
this.keycloakClient = input as KeycloakClient;
|
|
50
|
+
} else {
|
|
51
|
+
this.mock = input as KeycloakMock;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (this.token && !this._tokenParsed) this._tokenParsed = jwtDecode(this.token) as AccessTokenParsed;
|
|
55
|
+
if (this.idToken && !this._idTokenParsed) this._idTokenParsed = jwtDecode(this.idToken) as TokenParsed;
|
|
56
|
+
if (this.refreshToken && !this._refreshTokenParsed) {
|
|
57
|
+
this._refreshTokenParsed = jwtDecode(this.refreshToken) as TokenParsed;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
get tokenParsed() {
|
|
62
|
+
if (this.keycloakClient) return this.keycloakClient.tokenParsed as AccessTokenParsed;
|
|
63
|
+
return this._tokenParsed;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
get idTokenParsed() {
|
|
67
|
+
if (this.keycloakClient) return this.keycloakClient.idTokenParsed as TokenParsed;
|
|
68
|
+
return this._idTokenParsed;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
get refreshTokenParsed() {
|
|
72
|
+
if (this.keycloakClient) return this.keycloakClient.refreshTokenParsed as TokenParsed;
|
|
73
|
+
return this._refreshTokenParsed;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
get clientId() {
|
|
77
|
+
if (this.keycloakClient) return this.keycloakClient.clientId;
|
|
78
|
+
return this.tokenParsed?.azp;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
get email() {
|
|
82
|
+
if (this.keycloakClient) return this.keycloakClient.tokenParsed?.email;
|
|
83
|
+
if (this.mock) return this.mock.email;
|
|
84
|
+
return this.tokenParsed?.email;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
get realm() {
|
|
88
|
+
if (this.keycloakClient) return this.keycloakClient.realm;
|
|
89
|
+
return this.tokenParsed?.iss?.split('/').pop();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
get realmAccess() {
|
|
93
|
+
if (this.keycloakClient) return this.keycloakClient.realmAccess;
|
|
94
|
+
return this.tokenParsed?.realm_access;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
get resourceAccess() {
|
|
98
|
+
if (this.keycloakClient) return this.keycloakClient.resourceAccess;
|
|
99
|
+
return this.tokenParsed?.resource_access;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
get sessionId() {
|
|
103
|
+
if (this.keycloakClient) return this.keycloakClient.sessionId;
|
|
104
|
+
return this.tokenParsed?.session_state;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
get subject() {
|
|
108
|
+
if (this.keycloakClient) return this.keycloakClient.subject;
|
|
109
|
+
return this.tokenParsed?.sub;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
get username() {
|
|
113
|
+
if (this.mock) return this.mock.username;
|
|
114
|
+
return this.tokenParsed?.preferred_username;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async login(options: KeycloakLoginOptions = {}) {
|
|
118
|
+
if (this.keycloakClient) {
|
|
119
|
+
await this.keycloakClient.login(options);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async logout(options: KeycloakLogoutOptions = {}) {
|
|
124
|
+
if (this.keycloakClient) {
|
|
125
|
+
await this.keycloakClient.logout(options);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async isAuthenticated() {
|
|
130
|
+
if (this.mock) return true;
|
|
131
|
+
if (this.keycloakClient) return !!this.keycloakClient.authenticated;
|
|
132
|
+
if (MultiPlatform.isNext) return !!(await getSession());
|
|
133
|
+
if (!this.tokenParsed?.exp) return false;
|
|
134
|
+
return this.tokenParsed.exp > Date.now() / 1000;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function useKeycloak() {
|
|
139
|
+
const { token, refreshToken, idToken } = useExpoKeycloak();
|
|
140
|
+
if (MultiPlatform.isServer) return;
|
|
141
|
+
if (MultiPlatform.isStorybook) {
|
|
142
|
+
return new Keycloak({
|
|
143
|
+
email: 'storybook@example.com',
|
|
144
|
+
username: 'storybook',
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
if (token) return new Keycloak(token, idToken, refreshToken);
|
|
148
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* File: /src/keycloak/keycloak.ts
|
|
3
|
+
* Project: @multiplatform.one/keycloak
|
|
4
|
+
* File Created: 09-01-2024 11:59:37
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* BitSpur (c) Copyright 2021 - 2024
|
|
8
|
+
*
|
|
9
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License.
|
|
11
|
+
* You may obtain a copy of the License at
|
|
12
|
+
*
|
|
13
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
* See the License for the specific language governing permissions and
|
|
19
|
+
* limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import type KeycloakClient from 'keycloak-js';
|
|
23
|
+
import type { AccessTokenParsed, TokenParsed } from '../token';
|
|
24
|
+
import type { KeycloakLoginOptions, KeycloakLogoutOptions } from 'keycloak-js';
|
|
25
|
+
import type { KeycloakMock } from '../types';
|
|
26
|
+
import type { Session } from '../session/session';
|
|
27
|
+
import { KeycloakContext } from './context';
|
|
28
|
+
import { MultiPlatform } from 'multiplatform.one';
|
|
29
|
+
import { getSession, useSession } from '../session/session';
|
|
30
|
+
import { jwtDecode } from 'jwt-decode';
|
|
31
|
+
import { signIn, signOut } from 'next-auth/react';
|
|
32
|
+
import { useContext } from 'react';
|
|
33
|
+
|
|
34
|
+
export class Keycloak {
|
|
35
|
+
token?: string;
|
|
36
|
+
idToken?: string;
|
|
37
|
+
refreshToken?: string;
|
|
38
|
+
|
|
39
|
+
private _idTokenParsed?: TokenParsed;
|
|
40
|
+
private _refreshTokenParsed?: TokenParsed;
|
|
41
|
+
private _tokenParsed?: AccessTokenParsed;
|
|
42
|
+
private keycloakClient?: KeycloakClient;
|
|
43
|
+
private mock: KeycloakMock | undefined;
|
|
44
|
+
|
|
45
|
+
constructor(input?: string | KeycloakClient | KeycloakMock | Session, idToken?: string, refreshToken?: string) {
|
|
46
|
+
if (typeof input === 'string') {
|
|
47
|
+
this.token = input;
|
|
48
|
+
this.idToken = idToken;
|
|
49
|
+
this.refreshToken = refreshToken;
|
|
50
|
+
} else if (typeof input === 'object') {
|
|
51
|
+
if (typeof (input as KeycloakClient).init === 'function') {
|
|
52
|
+
this.keycloakClient = input as KeycloakClient;
|
|
53
|
+
} else if ((input as Session).accessToken) {
|
|
54
|
+
const session = input as Session;
|
|
55
|
+
this.token = session.accessToken;
|
|
56
|
+
this.idToken = session.idToken;
|
|
57
|
+
this.refreshToken = session.refreshToken;
|
|
58
|
+
} else {
|
|
59
|
+
this.mock = input as KeycloakMock;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (this.token && !this._tokenParsed) this._tokenParsed = jwtDecode(this.token) as AccessTokenParsed;
|
|
63
|
+
if (this.idToken && !this._idTokenParsed) this._idTokenParsed = jwtDecode(this.idToken) as TokenParsed;
|
|
64
|
+
if (this.refreshToken && !this._refreshTokenParsed) {
|
|
65
|
+
this._refreshTokenParsed = jwtDecode(this.refreshToken) as TokenParsed;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
get tokenParsed() {
|
|
70
|
+
if (this.keycloakClient) return this.keycloakClient.tokenParsed as AccessTokenParsed;
|
|
71
|
+
return this._tokenParsed;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
get idTokenParsed() {
|
|
75
|
+
if (this.keycloakClient) return this.keycloakClient.idTokenParsed as TokenParsed;
|
|
76
|
+
return this._idTokenParsed;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
get refreshTokenParsed() {
|
|
80
|
+
if (this.keycloakClient) return this.keycloakClient.refreshTokenParsed as TokenParsed;
|
|
81
|
+
return this._refreshTokenParsed;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
get clientId() {
|
|
85
|
+
if (this.keycloakClient) return this.keycloakClient.clientId;
|
|
86
|
+
return this.tokenParsed?.azp;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
get email() {
|
|
90
|
+
if (this.keycloakClient) return this.keycloakClient.tokenParsed?.email;
|
|
91
|
+
if (this.mock) return this.mock.email;
|
|
92
|
+
return this.tokenParsed?.email;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
get realm() {
|
|
96
|
+
if (this.keycloakClient) return this.keycloakClient.realm;
|
|
97
|
+
return this.tokenParsed?.iss?.split('/').pop();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
get realmAccess() {
|
|
101
|
+
if (this.keycloakClient) return this.keycloakClient.realmAccess;
|
|
102
|
+
return this.tokenParsed?.realm_access;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
get resourceAccess() {
|
|
106
|
+
if (this.keycloakClient) return this.keycloakClient.resourceAccess;
|
|
107
|
+
return this.tokenParsed?.resource_access;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
get sessionId() {
|
|
111
|
+
if (this.keycloakClient) return this.keycloakClient.sessionId;
|
|
112
|
+
return this.tokenParsed?.session_state;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
get subject() {
|
|
116
|
+
if (this.keycloakClient) return this.keycloakClient.subject;
|
|
117
|
+
return this.tokenParsed?.sub;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
get username() {
|
|
121
|
+
if (this.mock) return this.mock.username;
|
|
122
|
+
return this.tokenParsed?.preferred_username;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async login(options: KeycloakLoginOptions = {}) {
|
|
126
|
+
if (this.keycloakClient) {
|
|
127
|
+
await this.keycloakClient.login(options);
|
|
128
|
+
} else if (MultiPlatform.isNext && !MultiPlatform.isServer) {
|
|
129
|
+
await signIn('keycloak', {
|
|
130
|
+
callbackUrl: options.redirectUri,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async logout(options: KeycloakLogoutOptions = {}) {
|
|
136
|
+
if (this.keycloakClient) {
|
|
137
|
+
await this.keycloakClient.logout(options);
|
|
138
|
+
} else if (MultiPlatform.isNext && !MultiPlatform.isServer) {
|
|
139
|
+
await fetch('/api/auth/logout', { method: 'GET' });
|
|
140
|
+
await signOut({
|
|
141
|
+
callbackUrl: options.redirectUri,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
get authenticated() {
|
|
147
|
+
if (this.mock) return true;
|
|
148
|
+
if (this.keycloakClient) return !!this.keycloakClient.authenticated;
|
|
149
|
+
if (!this.tokenParsed?.exp || !this.idToken) return false;
|
|
150
|
+
return this.tokenParsed.exp > Date.now() / 1000;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function useKeycloak() {
|
|
155
|
+
const keycloak = useContext(KeycloakContext);
|
|
156
|
+
if (keycloak) return keycloak;
|
|
157
|
+
const { session, status } = useSession();
|
|
158
|
+
if (MultiPlatform.isServer) return;
|
|
159
|
+
if (MultiPlatform.isStorybook) {
|
|
160
|
+
return new Keycloak({
|
|
161
|
+
email: 'storybook@example.com',
|
|
162
|
+
username: 'storybook',
|
|
163
|
+
});
|
|
164
|
+
} else if (status === 'unauthenticated') {
|
|
165
|
+
return new Keycloak();
|
|
166
|
+
} else if (status === 'authenticated' && session?.accessToken) {
|
|
167
|
+
return new Keycloak(session as Session);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export async function getKeycloak() {
|
|
172
|
+
const session = await getSession();
|
|
173
|
+
if (session?.accessToken) return new Keycloak(session as Session);
|
|
174
|
+
return new Keycloak();
|
|
175
|
+
}
|