@multiplatform.one/keycloak 0.7.16 → 1.0.0
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/lib/@types/expoKeycloakAuth.d.d.mts +25 -0
- package/lib/@types/expoKeycloakAuth.d.d.ts +25 -0
- package/lib/@types/expoKeycloakAuth.d.js +1 -0
- package/lib/@types/expoKeycloakAuth.d.mjs +0 -0
- package/lib/Authenticated.d.mts +32 -0
- package/lib/Authenticated.d.ts +32 -0
- package/lib/Authenticated.js +20 -0
- package/lib/Authenticated.mjs +20 -0
- package/lib/apollo.d.mts +7 -0
- package/lib/apollo.d.ts +7 -0
- package/lib/apollo.js +15 -0
- package/lib/apollo.mjs +15 -0
- package/lib/authConfig.d.mts +12 -0
- package/lib/authConfig.d.ts +12 -0
- package/lib/authConfig.js +9 -0
- package/lib/authConfig.mjs +9 -0
- package/lib/chunk-2L5TXBDK.mjs +7 -0
- package/lib/chunk-2MYOFYAK.mjs +53 -0
- package/lib/chunk-2VDDEPLF.js +27 -0
- package/lib/chunk-2VGMJ7WP.js +34 -0
- package/lib/chunk-32AVT4GA.js +9 -0
- package/lib/chunk-3ZTSLDX7.js +13 -0
- package/lib/chunk-6APOOEW6.js +28 -0
- package/lib/chunk-6F4PWJZI.js +1 -0
- package/lib/chunk-6FRYV4MK.mjs +27 -0
- package/lib/chunk-6WBHOY7M.js +50 -0
- package/lib/chunk-74FSREBY.js +161 -0
- package/lib/chunk-A2V2S4HP.js +22 -0
- package/lib/chunk-BUZONXAW.js +1 -0
- package/lib/chunk-C6TOWKPW.mjs +245 -0
- package/lib/chunk-DNEUOHZQ.mjs +28 -0
- package/lib/chunk-EGCPD3BX.js +51 -0
- package/lib/chunk-HH32BME2.mjs +0 -0
- package/lib/chunk-HTWZLBA4.mjs +13 -0
- package/lib/chunk-I5FNGKQ6.js +16 -0
- package/lib/chunk-IGSEESI3.mjs +51 -0
- package/lib/chunk-JAFUI2JQ.mjs +9 -0
- package/lib/chunk-JPQI72HR.mjs +0 -0
- package/lib/chunk-KWNIT4G4.mjs +34 -0
- package/lib/chunk-L3HTVJ4B.mjs +13 -0
- package/lib/chunk-MDAAMGTE.js +1 -0
- package/lib/chunk-NQMFZ6RW.mjs +50 -0
- package/lib/chunk-OAUYHIWG.mjs +0 -0
- package/lib/chunk-OBB23GPX.mjs +0 -0
- package/lib/chunk-OYWXU5N7.mjs +161 -0
- package/lib/chunk-P57PW2II.js +11 -0
- package/lib/chunk-TUJRPTZE.js +13 -0
- package/lib/chunk-UESWAK47.mjs +0 -0
- package/lib/chunk-UKSHA3X3.mjs +16 -0
- package/lib/chunk-VS4M7GMO.js +1 -0
- package/lib/chunk-W4LGQZMF.js +245 -0
- package/lib/chunk-WBQAMGXK.mjs +0 -0
- package/lib/chunk-WMH347OL.mjs +22 -0
- package/lib/chunk-WS4IMGVL.js +1 -0
- package/lib/chunk-YACGLW5Q.js +7 -0
- package/lib/chunk-YYYDFAI4.js +53 -0
- package/lib/chunk-ZCBD6HWK.js +1 -0
- package/lib/chunk-ZFXKCRJC.mjs +11 -0
- package/lib/hooks/index.d.mts +5 -0
- package/lib/hooks/index.d.ts +5 -0
- package/lib/hooks/index.js +19 -0
- package/lib/hooks/index.mjs +19 -0
- package/lib/hooks/useAuthConfig.d.mts +6 -0
- package/lib/hooks/useAuthConfig.d.ts +6 -0
- package/lib/hooks/useAuthConfig.js +8 -0
- package/lib/hooks/useAuthConfig.mjs +8 -0
- package/lib/hooks/useTokensFromQuery/index.d.mts +3 -0
- package/lib/hooks/useTokensFromQuery/index.d.ts +3 -0
- package/lib/hooks/useTokensFromQuery/index.js +7 -0
- package/lib/hooks/useTokensFromQuery/index.mjs +7 -0
- package/lib/hooks/useTokensFromQuery/index.native.d.mts +3 -0
- package/lib/hooks/useTokensFromQuery/index.native.d.ts +3 -0
- package/lib/hooks/useTokensFromQuery/index.native.js +7 -0
- package/lib/hooks/useTokensFromQuery/index.native.mjs +7 -0
- package/lib/hooks/useTokensFromState.d.mts +3 -0
- package/lib/hooks/useTokensFromState.d.ts +3 -0
- package/lib/hooks/useTokensFromState.js +9 -0
- package/lib/hooks/useTokensFromState.mjs +9 -0
- package/lib/index.d.mts +18 -0
- package/lib/index.d.ts +18 -0
- package/lib/index.js +70 -0
- package/lib/index.mjs +70 -0
- package/lib/keycloak/context.d.mts +12 -0
- package/lib/keycloak/context.d.ts +12 -0
- package/lib/keycloak/context.js +7 -0
- package/lib/keycloak/context.mjs +7 -0
- package/lib/keycloak/index.d.mts +7 -0
- package/lib/keycloak/index.d.ts +7 -0
- package/lib/keycloak/index.js +14 -0
- package/lib/keycloak/index.mjs +14 -0
- package/lib/keycloak/keycloak.d.mts +37 -0
- package/lib/keycloak/keycloak.d.ts +37 -0
- package/lib/keycloak/keycloak.js +13 -0
- package/lib/keycloak/keycloak.mjs +13 -0
- package/lib/keycloak/keycloak.native.d.mts +33 -0
- package/lib/keycloak/keycloak.native.d.ts +33 -0
- package/lib/keycloak/keycloak.native.js +134 -0
- package/lib/keycloak/keycloak.native.mjs +134 -0
- package/lib/provider/AfterAuth.d.mts +28 -0
- package/lib/provider/AfterAuth.d.ts +28 -0
- package/lib/provider/AfterAuth.js +18 -0
- package/lib/provider/AfterAuth.mjs +18 -0
- package/lib/provider/AuthProvider/AuthProvider.d.mts +37 -0
- package/lib/provider/AuthProvider/AuthProvider.d.ts +37 -0
- package/lib/provider/AuthProvider/AuthProvider.js +19 -0
- package/lib/provider/AuthProvider/AuthProvider.mjs +19 -0
- package/lib/provider/AuthProvider/AuthProvider.native.d.mts +29 -0
- package/lib/provider/AuthProvider/AuthProvider.native.d.ts +29 -0
- package/lib/provider/AuthProvider/AuthProvider.native.js +25 -0
- package/lib/provider/AuthProvider/AuthProvider.native.mjs +25 -0
- package/lib/provider/AuthProvider/index.d.mts +4 -0
- package/lib/provider/AuthProvider/index.d.ts +4 -0
- package/lib/provider/AuthProvider/index.js +20 -0
- package/lib/provider/AuthProvider/index.mjs +20 -0
- package/lib/provider/KeycloakProvider.d.mts +34 -0
- package/lib/provider/KeycloakProvider.d.ts +34 -0
- package/lib/provider/KeycloakProvider.js +21 -0
- package/lib/provider/KeycloakProvider.mjs +21 -0
- package/lib/provider/index.d.mts +8 -0
- package/lib/provider/index.d.ts +8 -0
- package/lib/provider/index.js +28 -0
- package/lib/provider/index.mjs +28 -0
- package/lib/routes.d.mts +27 -0
- package/lib/routes.d.ts +27 -0
- package/lib/routes.js +127 -0
- package/lib/routes.mjs +127 -0
- package/lib/session/index.d.mts +3 -0
- package/lib/session/index.d.ts +3 -0
- package/lib/session/index.js +10 -0
- package/lib/session/index.mjs +10 -0
- package/lib/session/session.d.mts +18 -0
- package/lib/session/session.d.ts +18 -0
- package/lib/session/session.js +9 -0
- package/lib/session/session.mjs +9 -0
- package/lib/session/session.native.d.mts +8 -0
- package/lib/session/session.native.d.ts +8 -0
- package/lib/session/session.native.js +13 -0
- package/lib/session/session.native.mjs +13 -0
- package/lib/state.d.mts +12 -0
- package/lib/state.d.ts +12 -0
- package/lib/state.js +9 -0
- package/lib/state.mjs +9 -0
- package/lib/token.d.mts +17 -0
- package/lib/token.d.ts +17 -0
- package/lib/token.js +9 -0
- package/lib/token.mjs +9 -0
- package/lib/types.d.mts +20 -0
- package/lib/types.d.ts +20 -0
- package/lib/types.js +1 -0
- package/lib/types.mjs +1 -0
- package/package.json +7 -6
- package/src/Authenticated.tsx +2 -4
- package/src/apollo.ts +95 -0
- package/src/index.ts +1 -0
- package/src/keycloak/keycloak.ts +4 -3
- package/src/provider/AfterAuth.tsx +1 -1
- package/src/routes.ts +40 -38
- package/src/session/session.ts +19 -9
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
var _chunkYACGLW5Qjs = require('./chunk-YACGLW5Q.js');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
var _chunk2VGMJ7WPjs = require('./chunk-2VGMJ7WP.js');
|
|
8
|
+
|
|
9
|
+
// src/keycloak/keycloak.ts
|
|
10
|
+
var _multiplatformone = require('multiplatform.one');
|
|
11
|
+
var _jwtdecode = require('jwt-decode');
|
|
12
|
+
var _react = require('next-auth/react');
|
|
13
|
+
var _react3 = require('react');
|
|
14
|
+
var Keycloak = class {
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
constructor(input, idToken, refreshToken) {
|
|
24
|
+
if (typeof input === "string") {
|
|
25
|
+
this.token = input;
|
|
26
|
+
this.idToken = idToken;
|
|
27
|
+
this.refreshToken = refreshToken;
|
|
28
|
+
} else if (typeof input === "object") {
|
|
29
|
+
if (typeof input.init === "function") {
|
|
30
|
+
this.keycloakClient = input;
|
|
31
|
+
} else if (input.accessToken) {
|
|
32
|
+
const session = input;
|
|
33
|
+
this.token = session.accessToken;
|
|
34
|
+
this.idToken = session.idToken;
|
|
35
|
+
this.refreshToken = session.refreshToken;
|
|
36
|
+
} else {
|
|
37
|
+
this.mock = input;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (this.token && !this._tokenParsed)
|
|
41
|
+
this._tokenParsed = _jwtdecode.jwtDecode.call(void 0, this.token);
|
|
42
|
+
if (this.idToken && !this._idTokenParsed)
|
|
43
|
+
this._idTokenParsed = _jwtdecode.jwtDecode.call(void 0, this.idToken);
|
|
44
|
+
if (this.refreshToken && !this._refreshTokenParsed) {
|
|
45
|
+
this._refreshTokenParsed = _jwtdecode.jwtDecode.call(void 0, this.refreshToken);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
get tokenParsed() {
|
|
49
|
+
if (this.keycloakClient)
|
|
50
|
+
return this.keycloakClient.tokenParsed;
|
|
51
|
+
return this._tokenParsed;
|
|
52
|
+
}
|
|
53
|
+
get idTokenParsed() {
|
|
54
|
+
if (this.keycloakClient)
|
|
55
|
+
return this.keycloakClient.idTokenParsed;
|
|
56
|
+
return this._idTokenParsed;
|
|
57
|
+
}
|
|
58
|
+
get refreshTokenParsed() {
|
|
59
|
+
if (this.keycloakClient)
|
|
60
|
+
return this.keycloakClient.refreshTokenParsed;
|
|
61
|
+
return this._refreshTokenParsed;
|
|
62
|
+
}
|
|
63
|
+
get clientId() {
|
|
64
|
+
if (this.keycloakClient)
|
|
65
|
+
return this.keycloakClient.clientId;
|
|
66
|
+
return _optionalChain([this, 'access', _ => _.tokenParsed, 'optionalAccess', _2 => _2.azp]);
|
|
67
|
+
}
|
|
68
|
+
get email() {
|
|
69
|
+
if (this.keycloakClient)
|
|
70
|
+
return _optionalChain([this, 'access', _3 => _3.keycloakClient, 'access', _4 => _4.tokenParsed, 'optionalAccess', _5 => _5.email]);
|
|
71
|
+
if (this.mock)
|
|
72
|
+
return this.mock.email;
|
|
73
|
+
return _optionalChain([this, 'access', _6 => _6.tokenParsed, 'optionalAccess', _7 => _7.email]);
|
|
74
|
+
}
|
|
75
|
+
get realm() {
|
|
76
|
+
if (this.keycloakClient)
|
|
77
|
+
return this.keycloakClient.realm;
|
|
78
|
+
return _optionalChain([this, 'access', _8 => _8.tokenParsed, 'optionalAccess', _9 => _9.iss, 'optionalAccess', _10 => _10.split, 'call', _11 => _11("/"), 'access', _12 => _12.pop, 'call', _13 => _13()]);
|
|
79
|
+
}
|
|
80
|
+
get realmAccess() {
|
|
81
|
+
if (this.keycloakClient)
|
|
82
|
+
return this.keycloakClient.realmAccess;
|
|
83
|
+
return _optionalChain([this, 'access', _14 => _14.tokenParsed, 'optionalAccess', _15 => _15.realm_access]);
|
|
84
|
+
}
|
|
85
|
+
get resourceAccess() {
|
|
86
|
+
if (this.keycloakClient)
|
|
87
|
+
return this.keycloakClient.resourceAccess;
|
|
88
|
+
return _optionalChain([this, 'access', _16 => _16.tokenParsed, 'optionalAccess', _17 => _17.resource_access]);
|
|
89
|
+
}
|
|
90
|
+
get sessionId() {
|
|
91
|
+
if (this.keycloakClient)
|
|
92
|
+
return this.keycloakClient.sessionId;
|
|
93
|
+
return _optionalChain([this, 'access', _18 => _18.tokenParsed, 'optionalAccess', _19 => _19.session_state]);
|
|
94
|
+
}
|
|
95
|
+
get subject() {
|
|
96
|
+
if (this.keycloakClient)
|
|
97
|
+
return this.keycloakClient.subject;
|
|
98
|
+
return _optionalChain([this, 'access', _20 => _20.tokenParsed, 'optionalAccess', _21 => _21.sub]);
|
|
99
|
+
}
|
|
100
|
+
get username() {
|
|
101
|
+
if (this.mock)
|
|
102
|
+
return this.mock.username;
|
|
103
|
+
return _optionalChain([this, 'access', _22 => _22.tokenParsed, 'optionalAccess', _23 => _23.preferred_username]);
|
|
104
|
+
}
|
|
105
|
+
async login(options = {}) {
|
|
106
|
+
if (this.keycloakClient) {
|
|
107
|
+
await this.keycloakClient.login(options);
|
|
108
|
+
} else if (_multiplatformone.MultiPlatform.isNext && !_multiplatformone.MultiPlatform.isServer) {
|
|
109
|
+
await _react.signIn.call(void 0, "keycloak", {
|
|
110
|
+
callbackUrl: options.redirectUri
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
async logout(options = {}) {
|
|
115
|
+
if (this.keycloakClient) {
|
|
116
|
+
await this.keycloakClient.logout(options);
|
|
117
|
+
} else if (_multiplatformone.MultiPlatform.isNext && !_multiplatformone.MultiPlatform.isServer) {
|
|
118
|
+
await fetch("/api/auth/logout", { method: "GET" });
|
|
119
|
+
await _react.signOut.call(void 0, {
|
|
120
|
+
callbackUrl: options.redirectUri
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
get authenticated() {
|
|
125
|
+
if (this.mock)
|
|
126
|
+
return true;
|
|
127
|
+
if (this.keycloakClient)
|
|
128
|
+
return !!this.keycloakClient.authenticated;
|
|
129
|
+
if (!_optionalChain([this, 'access', _24 => _24.tokenParsed, 'optionalAccess', _25 => _25.exp]) || !this.idToken)
|
|
130
|
+
return false;
|
|
131
|
+
return this.tokenParsed.exp > Date.now() / 1e3;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
function useKeycloak() {
|
|
135
|
+
const keycloak = _react3.useContext.call(void 0, _chunkYACGLW5Qjs.KeycloakContext);
|
|
136
|
+
if (keycloak)
|
|
137
|
+
return keycloak;
|
|
138
|
+
const { session, status } = _chunk2VGMJ7WPjs.useSession.call(void 0, );
|
|
139
|
+
if (_multiplatformone.MultiPlatform.isStorybook) {
|
|
140
|
+
return new Keycloak({
|
|
141
|
+
email: "storybook@example.com",
|
|
142
|
+
username: "storybook"
|
|
143
|
+
});
|
|
144
|
+
} else if (status === "unauthenticated") {
|
|
145
|
+
return new Keycloak();
|
|
146
|
+
} else if (status === "authenticated" && _optionalChain([session, 'optionalAccess', _26 => _26.accessToken])) {
|
|
147
|
+
return new Keycloak(session);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
async function getKeycloak(options, req, res) {
|
|
151
|
+
const session = await _chunk2VGMJ7WPjs.getSession.call(void 0, options, req, res);
|
|
152
|
+
if (_optionalChain([session, 'optionalAccess', _27 => _27.accessToken]))
|
|
153
|
+
return new Keycloak(session);
|
|
154
|
+
return new Keycloak();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
exports.Keycloak = Keycloak; exports.useKeycloak = useKeycloak; exports.getKeycloak = getKeycloak;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/state.ts
|
|
2
|
+
var _zustand = require('multiplatform.one/zustand');
|
|
3
|
+
var _multiplatformone = require('multiplatform.one');
|
|
4
|
+
var persist = _multiplatformone.MultiPlatform.isIframe || !_multiplatformone.MultiPlatform.isNext && !_multiplatformone.MultiPlatform.isServer;
|
|
5
|
+
var { useStore } = _zustand.createStateStore.call(void 0,
|
|
6
|
+
"auth",
|
|
7
|
+
{
|
|
8
|
+
idToken: "",
|
|
9
|
+
refreshToken: "",
|
|
10
|
+
token: ""
|
|
11
|
+
},
|
|
12
|
+
void 0,
|
|
13
|
+
{ persist }
|
|
14
|
+
);
|
|
15
|
+
function useAuthState() {
|
|
16
|
+
return useStore();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
exports.persist = persist; exports.useAuthState = useAuthState;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AfterAuth
|
|
3
|
+
} from "./chunk-IGSEESI3.mjs";
|
|
4
|
+
import {
|
|
5
|
+
useAuthConfig
|
|
6
|
+
} from "./chunk-HTWZLBA4.mjs";
|
|
7
|
+
import {
|
|
8
|
+
Keycloak
|
|
9
|
+
} from "./chunk-OYWXU5N7.mjs";
|
|
10
|
+
import {
|
|
11
|
+
KeycloakContext
|
|
12
|
+
} from "./chunk-2L5TXBDK.mjs";
|
|
13
|
+
import {
|
|
14
|
+
persist,
|
|
15
|
+
useAuthState
|
|
16
|
+
} from "./chunk-WMH347OL.mjs";
|
|
17
|
+
import {
|
|
18
|
+
validToken
|
|
19
|
+
} from "./chunk-6FRYV4MK.mjs";
|
|
20
|
+
|
|
21
|
+
// src/provider/AuthProvider/AuthProvider.tsx
|
|
22
|
+
import KeycloakClient from "keycloak-js";
|
|
23
|
+
import React, { useEffect, useMemo, useState } from "react";
|
|
24
|
+
import { MultiPlatform } from "multiplatform.one";
|
|
25
|
+
import { SessionProvider } from "next-auth/react";
|
|
26
|
+
import { useRouter } from "next/router";
|
|
27
|
+
var logger = console;
|
|
28
|
+
function AuthProvider({ children, sessionProvider, keycloakInitOptions, keycloakConfig }) {
|
|
29
|
+
const { debug, messageHandlerKeys } = useAuthConfig();
|
|
30
|
+
const { query } = MultiPlatform.isNext ? useRouter() : { query: {} };
|
|
31
|
+
const authState = useAuthState();
|
|
32
|
+
const [keycloak, setKeycloak] = useState();
|
|
33
|
+
const [refreshToken, setRefreshToken] = useState(
|
|
34
|
+
validToken(
|
|
35
|
+
MultiPlatform.isIframe ? "refreshToken" in query && (query.refreshToken?.toString() || true) || persist && authState.refreshToken || false : void 0
|
|
36
|
+
)
|
|
37
|
+
);
|
|
38
|
+
const [token, setToken] = useState(
|
|
39
|
+
validToken(
|
|
40
|
+
MultiPlatform.isIframe ? "token" in query && (query.token?.toString() || true) || persist && authState.token || false : void 0,
|
|
41
|
+
refreshToken
|
|
42
|
+
)
|
|
43
|
+
);
|
|
44
|
+
const [idToken, setIdToken] = useState(
|
|
45
|
+
validToken(
|
|
46
|
+
MultiPlatform.isIframe ? "idToken" in query && (query.idToken?.toString() || true) || persist && authState.idToken || false : void 0,
|
|
47
|
+
refreshToken
|
|
48
|
+
)
|
|
49
|
+
);
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
if (token !== true && refreshToken !== true && idToken !== true)
|
|
52
|
+
return;
|
|
53
|
+
if (debug)
|
|
54
|
+
logger.debug("post message", JSON.stringify({ type: "LOADED" }));
|
|
55
|
+
(messageHandlerKeys || []).forEach((key) => {
|
|
56
|
+
window?.webkit?.messageHandlers?.[key]?.postMessage(JSON.stringify({ type: "LOADED" }));
|
|
57
|
+
});
|
|
58
|
+
window?.ReactNativeWebView?.postMessage(JSON.stringify({ type: "LOADED" }));
|
|
59
|
+
window?.parent?.postMessage(JSON.stringify({ type: "LOADED" }));
|
|
60
|
+
}, []);
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
if (refreshToken !== true)
|
|
63
|
+
return;
|
|
64
|
+
const messageCallback = (e) => {
|
|
65
|
+
let data = e?.data || null;
|
|
66
|
+
if (typeof data === "string") {
|
|
67
|
+
try {
|
|
68
|
+
data = JSON.parse(data);
|
|
69
|
+
} catch (err) {
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (debug)
|
|
73
|
+
logger.debug("received message", JSON.stringify(data));
|
|
74
|
+
const message = data;
|
|
75
|
+
if (!message.type)
|
|
76
|
+
return;
|
|
77
|
+
if (message.type.toUpperCase() === "REFRESH_TOKEN" && message.payload) {
|
|
78
|
+
if (debug)
|
|
79
|
+
logger.debug("setting refresh token", message.payload);
|
|
80
|
+
setRefreshToken(validToken(message.payload));
|
|
81
|
+
window.removeEventListener("message", messageCallback);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
if (debug) {
|
|
85
|
+
logger.debug(
|
|
86
|
+
"listening for message",
|
|
87
|
+
JSON.stringify({
|
|
88
|
+
type: "REFRESH_TOKEN",
|
|
89
|
+
payload: "<some_refresh_token>"
|
|
90
|
+
})
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
window.addEventListener("message", messageCallback);
|
|
94
|
+
return () => {
|
|
95
|
+
window.removeEventListener("message", messageCallback);
|
|
96
|
+
};
|
|
97
|
+
}, []);
|
|
98
|
+
useEffect(() => {
|
|
99
|
+
if (token !== true)
|
|
100
|
+
return;
|
|
101
|
+
const messageCallback = (e) => {
|
|
102
|
+
let data = e?.data || null;
|
|
103
|
+
if (typeof data === "string") {
|
|
104
|
+
try {
|
|
105
|
+
data = JSON.parse(data);
|
|
106
|
+
} catch (err) {
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (debug)
|
|
110
|
+
logger.debug("received message", JSON.stringify(data));
|
|
111
|
+
const message = data;
|
|
112
|
+
if (!message.type)
|
|
113
|
+
return;
|
|
114
|
+
if (message.type.toUpperCase() === "TOKEN" && message.payload) {
|
|
115
|
+
if (debug)
|
|
116
|
+
logger.debug("setting token", message.payload);
|
|
117
|
+
setToken(validToken(message.payload, refreshToken));
|
|
118
|
+
window.removeEventListener("message", messageCallback);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
if (debug) {
|
|
122
|
+
logger.debug(
|
|
123
|
+
"listening for message",
|
|
124
|
+
JSON.stringify({
|
|
125
|
+
type: "TOKEN",
|
|
126
|
+
payload: "<some_token>"
|
|
127
|
+
})
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
window.addEventListener("message", messageCallback);
|
|
131
|
+
return () => {
|
|
132
|
+
window.removeEventListener("message", messageCallback);
|
|
133
|
+
};
|
|
134
|
+
}, []);
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
if (idToken !== true)
|
|
137
|
+
return;
|
|
138
|
+
const messageCallback = (e) => {
|
|
139
|
+
let data = e?.data || null;
|
|
140
|
+
if (typeof data === "string") {
|
|
141
|
+
try {
|
|
142
|
+
data = JSON.parse(data);
|
|
143
|
+
} catch (err) {
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (debug)
|
|
147
|
+
logger.debug("received message", JSON.stringify(data));
|
|
148
|
+
const message = data;
|
|
149
|
+
if (!message.type)
|
|
150
|
+
return;
|
|
151
|
+
if (message.type.toUpperCase() === "ID_TOKEN" && message.payload) {
|
|
152
|
+
if (debug)
|
|
153
|
+
logger.debug("setting id token", message.payload);
|
|
154
|
+
setIdToken(validToken(message.payload, refreshToken));
|
|
155
|
+
window.removeEventListener("message", messageCallback);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
if (debug) {
|
|
159
|
+
logger.debug(
|
|
160
|
+
"listening for message",
|
|
161
|
+
JSON.stringify({
|
|
162
|
+
type: "ID_TOKEN",
|
|
163
|
+
payload: "<some_id_token>"
|
|
164
|
+
})
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
window.addEventListener("message", messageCallback);
|
|
168
|
+
return () => {
|
|
169
|
+
window.removeEventListener("message", messageCallback);
|
|
170
|
+
};
|
|
171
|
+
}, []);
|
|
172
|
+
const keycloakClient = useMemo(
|
|
173
|
+
() => MultiPlatform.isIframe && keycloakConfig ? new KeycloakClient({
|
|
174
|
+
url: keycloakConfig.url,
|
|
175
|
+
realm: keycloakConfig.realm,
|
|
176
|
+
clientId: keycloakConfig.clientId
|
|
177
|
+
}) : void 0,
|
|
178
|
+
[keycloakInitOptions, keycloakConfig, token, refreshToken, idToken]
|
|
179
|
+
);
|
|
180
|
+
const initOptions = useMemo(() => {
|
|
181
|
+
if (!MultiPlatform.isIframe)
|
|
182
|
+
return;
|
|
183
|
+
const initOptions2 = {
|
|
184
|
+
...defaultKeycloakInitOptions,
|
|
185
|
+
...keycloakInitOptions
|
|
186
|
+
};
|
|
187
|
+
if (debug)
|
|
188
|
+
initOptions2.enableLogging = true;
|
|
189
|
+
if (token && typeof token === "string") {
|
|
190
|
+
initOptions2.token = token;
|
|
191
|
+
initOptions2.timeSkew = 0;
|
|
192
|
+
if (idToken && typeof idToken === "string")
|
|
193
|
+
initOptions2.idToken = idToken;
|
|
194
|
+
if (refreshToken && typeof refreshToken === "string")
|
|
195
|
+
initOptions2.refreshToken = refreshToken;
|
|
196
|
+
}
|
|
197
|
+
if (typeof window !== "undefined" && window.self !== window.top || token && !refreshToken) {
|
|
198
|
+
initOptions2.checkLoginIframe = false;
|
|
199
|
+
initOptions2.flow = "implicit";
|
|
200
|
+
initOptions2.onLoad = void 0;
|
|
201
|
+
}
|
|
202
|
+
return initOptions2;
|
|
203
|
+
}, [keycloakInitOptions, token, idToken, refreshToken]);
|
|
204
|
+
useEffect(() => {
|
|
205
|
+
function onError() {
|
|
206
|
+
if (keycloakClient && initOptions)
|
|
207
|
+
setKeycloak(new Keycloak(keycloakClient));
|
|
208
|
+
}
|
|
209
|
+
function onUpdate() {
|
|
210
|
+
if (keycloakClient && initOptions)
|
|
211
|
+
setKeycloak(new Keycloak(keycloakClient));
|
|
212
|
+
}
|
|
213
|
+
if (keycloakClient && initOptions) {
|
|
214
|
+
(async () => {
|
|
215
|
+
keycloakClient.onAuthError = onError;
|
|
216
|
+
keycloakClient.onAuthLogout = onUpdate;
|
|
217
|
+
keycloakClient.onAuthRefreshError = onError;
|
|
218
|
+
keycloakClient.onAuthRefreshSuccess = onUpdate;
|
|
219
|
+
keycloakClient.onAuthSuccess = onUpdate;
|
|
220
|
+
keycloakClient.onReady = onUpdate;
|
|
221
|
+
keycloakClient.onTokenExpired = async () => {
|
|
222
|
+
try {
|
|
223
|
+
await keycloakClient.updateToken(5);
|
|
224
|
+
} catch (err) {
|
|
225
|
+
setKeycloak(new Keycloak());
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
const authenticated = await keycloakClient.init(initOptions);
|
|
229
|
+
setKeycloak(new Keycloak(authenticated ? keycloakClient : void 0));
|
|
230
|
+
})();
|
|
231
|
+
}
|
|
232
|
+
}, []);
|
|
233
|
+
return /* @__PURE__ */ React.createElement(SessionProvider, { ...sessionProvider }, /* @__PURE__ */ React.createElement(KeycloakContext.Provider, { value: keycloak }, /* @__PURE__ */ React.createElement(AfterAuth, null, children)));
|
|
234
|
+
}
|
|
235
|
+
var defaultKeycloakInitOptions = {
|
|
236
|
+
checkLoginIframe: true,
|
|
237
|
+
checkLoginIframeInterval: 5,
|
|
238
|
+
enableLogging: false,
|
|
239
|
+
onLoad: "check-sso",
|
|
240
|
+
pkceMethod: "S256"
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
export {
|
|
244
|
+
AuthProvider
|
|
245
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AuthProvider
|
|
3
|
+
} from "./chunk-C6TOWKPW.mjs";
|
|
4
|
+
import {
|
|
5
|
+
AuthConfigContext
|
|
6
|
+
} from "./chunk-JAFUI2JQ.mjs";
|
|
7
|
+
|
|
8
|
+
// src/provider/KeycloakProvider.tsx
|
|
9
|
+
import React, { useMemo } from "react";
|
|
10
|
+
function KeycloakProvider({ children, debug, realm, clientId, baseUrl, session }) {
|
|
11
|
+
const authConfig = useMemo(() => ({ debug }), [debug]);
|
|
12
|
+
return /* @__PURE__ */ React.createElement(AuthConfigContext.Provider, { value: authConfig }, /* @__PURE__ */ React.createElement(
|
|
13
|
+
AuthProvider,
|
|
14
|
+
{
|
|
15
|
+
sessionProvider: { session },
|
|
16
|
+
keycloakConfig: {
|
|
17
|
+
realm: realm || "main",
|
|
18
|
+
clientId: clientId || "app",
|
|
19
|
+
url: baseUrl
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
children
|
|
23
|
+
));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export {
|
|
27
|
+
KeycloakProvider
|
|
28
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
var _chunk3ZTSLDX7js = require('./chunk-3ZTSLDX7.js');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
var _chunk74FSREBYjs = require('./chunk-74FSREBY.js');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
var _chunkA2V2S4HPjs = require('./chunk-A2V2S4HP.js');
|
|
11
|
+
|
|
12
|
+
// src/provider/AfterAuth.tsx
|
|
13
|
+
var _react = require('react'); var _react2 = _interopRequireDefault(_react);
|
|
14
|
+
var logger = console;
|
|
15
|
+
function AfterAuth({ children }) {
|
|
16
|
+
const authConfig = _chunk3ZTSLDX7js.useAuthConfig.call(void 0, );
|
|
17
|
+
const authState = _chunkA2V2S4HPjs.useAuthState.call(void 0, );
|
|
18
|
+
const keycloak = _chunk74FSREBYjs.useKeycloak.call(void 0, );
|
|
19
|
+
_react.useEffect.call(void 0, () => {
|
|
20
|
+
if (!_chunkA2V2S4HPjs.persist || !_optionalChain([keycloak, 'optionalAccess', _ => _.authenticated]))
|
|
21
|
+
return;
|
|
22
|
+
if (keycloak.token) {
|
|
23
|
+
authState.setToken(keycloak.token);
|
|
24
|
+
if (keycloak.idToken)
|
|
25
|
+
authState.setIdToken(keycloak.idToken);
|
|
26
|
+
if (keycloak.refreshToken)
|
|
27
|
+
authState.setRefreshToken(keycloak.refreshToken);
|
|
28
|
+
}
|
|
29
|
+
}, [_optionalChain([keycloak, 'optionalAccess', _2 => _2.authenticated]), _optionalChain([keycloak, 'optionalAccess', _3 => _3.token]), _optionalChain([keycloak, 'optionalAccess', _4 => _4.idToken]), _optionalChain([keycloak, 'optionalAccess', _5 => _5.refreshToken])]);
|
|
30
|
+
_react.useEffect.call(void 0, () => {
|
|
31
|
+
if (authConfig.debug && _optionalChain([keycloak, 'optionalAccess', _6 => _6.token]))
|
|
32
|
+
logger.debug("token", keycloak.token);
|
|
33
|
+
}, [_optionalChain([keycloak, 'optionalAccess', _7 => _7.token])]);
|
|
34
|
+
_react.useEffect.call(void 0, () => {
|
|
35
|
+
if (authConfig.debug && _optionalChain([keycloak, 'optionalAccess', _8 => _8.idToken]))
|
|
36
|
+
logger.debug("idToken", keycloak.idToken);
|
|
37
|
+
}, [_optionalChain([keycloak, 'optionalAccess', _9 => _9.idToken])]);
|
|
38
|
+
_react.useEffect.call(void 0, () => {
|
|
39
|
+
if (authConfig.debug && _optionalChain([keycloak, 'optionalAccess', _10 => _10.refreshToken]))
|
|
40
|
+
logger.debug("refreshToken", keycloak.refreshToken);
|
|
41
|
+
}, [_optionalChain([keycloak, 'optionalAccess', _11 => _11.refreshToken])]);
|
|
42
|
+
_react.useEffect.call(void 0, () => {
|
|
43
|
+
if (authConfig.debug && _optionalChain([keycloak, 'optionalAccess', _12 => _12.authenticated]))
|
|
44
|
+
logger.debug("authenticated", keycloak.authenticated);
|
|
45
|
+
}, [_optionalChain([keycloak, 'optionalAccess', _13 => _13.authenticated])]);
|
|
46
|
+
return /* @__PURE__ */ _react2.default.createElement(_react2.default.Fragment, null, children);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
exports.AfterAuth = AfterAuth;
|
|
File without changes
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunkA2V2S4HPjs = require('./chunk-A2V2S4HP.js');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
var _chunk2VDDEPLFjs = require('./chunk-2VDDEPLF.js');
|
|
7
|
+
|
|
8
|
+
// src/hooks/useTokensFromState.ts
|
|
9
|
+
function useTokensFromState() {
|
|
10
|
+
const authState = _chunkA2V2S4HPjs.useAuthState.call(void 0, );
|
|
11
|
+
return !!_chunk2VDDEPLFjs.validToken.call(void 0, authState.token, authState.refreshToken);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
exports.useTokensFromState = useTokensFromState;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useAuthConfig
|
|
3
|
+
} from "./chunk-HTWZLBA4.mjs";
|
|
4
|
+
import {
|
|
5
|
+
useKeycloak
|
|
6
|
+
} from "./chunk-OYWXU5N7.mjs";
|
|
7
|
+
import {
|
|
8
|
+
persist,
|
|
9
|
+
useAuthState
|
|
10
|
+
} from "./chunk-WMH347OL.mjs";
|
|
11
|
+
|
|
12
|
+
// src/provider/AfterAuth.tsx
|
|
13
|
+
import React, { useEffect } from "react";
|
|
14
|
+
var logger = console;
|
|
15
|
+
function AfterAuth({ children }) {
|
|
16
|
+
const authConfig = useAuthConfig();
|
|
17
|
+
const authState = useAuthState();
|
|
18
|
+
const keycloak = useKeycloak();
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (!persist || !keycloak?.authenticated)
|
|
21
|
+
return;
|
|
22
|
+
if (keycloak.token) {
|
|
23
|
+
authState.setToken(keycloak.token);
|
|
24
|
+
if (keycloak.idToken)
|
|
25
|
+
authState.setIdToken(keycloak.idToken);
|
|
26
|
+
if (keycloak.refreshToken)
|
|
27
|
+
authState.setRefreshToken(keycloak.refreshToken);
|
|
28
|
+
}
|
|
29
|
+
}, [keycloak?.authenticated, keycloak?.token, keycloak?.idToken, keycloak?.refreshToken]);
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (authConfig.debug && keycloak?.token)
|
|
32
|
+
logger.debug("token", keycloak.token);
|
|
33
|
+
}, [keycloak?.token]);
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
if (authConfig.debug && keycloak?.idToken)
|
|
36
|
+
logger.debug("idToken", keycloak.idToken);
|
|
37
|
+
}, [keycloak?.idToken]);
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
if (authConfig.debug && keycloak?.refreshToken)
|
|
40
|
+
logger.debug("refreshToken", keycloak.refreshToken);
|
|
41
|
+
}, [keycloak?.refreshToken]);
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
if (authConfig.debug && keycloak?.authenticated)
|
|
44
|
+
logger.debug("authenticated", keycloak.authenticated);
|
|
45
|
+
}, [keycloak?.authenticated]);
|
|
46
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, children);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export {
|
|
50
|
+
AfterAuth
|
|
51
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// src/session/session.ts
|
|
2
|
+
import { MultiPlatform } from "multiplatform.one";
|
|
3
|
+
import { useSession as useNextSession, getSession as getNextSession } from "next-auth/react";
|
|
4
|
+
var _getServerSession;
|
|
5
|
+
async function getSession(options, req, res) {
|
|
6
|
+
if (MultiPlatform.isServer) {
|
|
7
|
+
if (typeof _getServerSession === "function")
|
|
8
|
+
return _getServerSession(options, req, res);
|
|
9
|
+
const getSession2 = (await import("next-auth")).getServerSession;
|
|
10
|
+
_getServerSession = (options2, req2, res2) => {
|
|
11
|
+
return req2 && res2 ? getSession2(req2, res2, options2) : getSession2(options2);
|
|
12
|
+
};
|
|
13
|
+
return _getServerSession(options, req, res);
|
|
14
|
+
}
|
|
15
|
+
return getNextSession(options);
|
|
16
|
+
}
|
|
17
|
+
var useSession = (options) => {
|
|
18
|
+
return {};
|
|
19
|
+
};
|
|
20
|
+
if (MultiPlatform.isNext) {
|
|
21
|
+
useSession = (options) => {
|
|
22
|
+
const nextSession = useNextSession(options);
|
|
23
|
+
return {
|
|
24
|
+
update: nextSession.update,
|
|
25
|
+
status: nextSession.status,
|
|
26
|
+
session: nextSession.data
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
getSession,
|
|
33
|
+
useSession
|
|
34
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// src/hooks/useTokensFromQuery/index.ts
|
|
2
|
+
import { MultiPlatform } from "multiplatform.one";
|
|
3
|
+
import { useRouter } from "next/router";
|
|
4
|
+
function useTokensFromQuery() {
|
|
5
|
+
if (!MultiPlatform.isIframe)
|
|
6
|
+
return false;
|
|
7
|
+
const { query } = MultiPlatform.isNext ? useRouter() : { query: {} };
|
|
8
|
+
return "idToken" in query || "token" in query || "refreshToken" in query;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
useTokensFromQuery
|
|
13
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|