@ghentcdh/authentication-vue 0.5.0 → 0.6.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/index.mjs +44 -1183
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -1,1162 +1,23 @@
|
|
|
1
|
-
import { ref as
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
#t;
|
|
8
|
-
/** @type {boolean} */
|
|
9
|
-
#o = !0;
|
|
10
|
-
/** @type {CallbackStorage} */
|
|
11
|
-
#l;
|
|
12
|
-
#n = this.#b(console.info);
|
|
13
|
-
#u = this.#b(console.warn);
|
|
14
|
-
/** @type {LoginIframe} */
|
|
15
|
-
#e = {
|
|
16
|
-
enable: !0,
|
|
17
|
-
callbackList: [],
|
|
18
|
-
interval: 5
|
|
19
|
-
};
|
|
20
|
-
/** @type {KeycloakConfig} config */
|
|
21
|
-
#i;
|
|
22
|
-
didInitialize = !1;
|
|
23
|
-
authenticated = !1;
|
|
24
|
-
loginRequired = !1;
|
|
25
|
-
/** @type {KeycloakResponseMode} */
|
|
26
|
-
responseMode = "fragment";
|
|
27
|
-
/** @type {KeycloakResponseType} */
|
|
28
|
-
responseType = "code";
|
|
29
|
-
/** @type {KeycloakFlow} */
|
|
30
|
-
flow = "standard";
|
|
31
|
-
/** @type {number?} */
|
|
32
|
-
timeSkew = null;
|
|
33
|
-
/** @type {string=} */
|
|
34
|
-
redirectUri;
|
|
35
|
-
/** @type {string=} */
|
|
36
|
-
silentCheckSsoRedirectUri;
|
|
37
|
-
/** @type {boolean} */
|
|
38
|
-
silentCheckSsoFallback = !0;
|
|
39
|
-
/** @type {KeycloakPkceMethod} */
|
|
40
|
-
pkceMethod = "S256";
|
|
41
|
-
enableLogging = !1;
|
|
42
|
-
/** @type {'GET' | 'POST'} */
|
|
43
|
-
logoutMethod = "GET";
|
|
44
|
-
/** @type {string=} */
|
|
45
|
-
scope;
|
|
46
|
-
messageReceiveTimeout = 1e4;
|
|
47
|
-
/** @type {string=} */
|
|
48
|
-
idToken;
|
|
49
|
-
/** @type {KeycloakTokenParsed=} */
|
|
50
|
-
idTokenParsed;
|
|
51
|
-
/** @type {string=} */
|
|
52
|
-
token;
|
|
53
|
-
/** @type {KeycloakTokenParsed=} */
|
|
54
|
-
tokenParsed;
|
|
55
|
-
/** @type {string=} */
|
|
56
|
-
refreshToken;
|
|
57
|
-
/** @type {KeycloakTokenParsed=} */
|
|
58
|
-
refreshTokenParsed;
|
|
59
|
-
/** @type {string=} */
|
|
60
|
-
clientId;
|
|
61
|
-
/** @type {string=} */
|
|
62
|
-
sessionId;
|
|
63
|
-
/** @type {string=} */
|
|
64
|
-
subject;
|
|
65
|
-
/** @type {string=} */
|
|
66
|
-
authServerUrl;
|
|
67
|
-
/** @type {string=} */
|
|
68
|
-
realm;
|
|
69
|
-
/** @type {KeycloakRoles=} */
|
|
70
|
-
realmAccess;
|
|
71
|
-
/** @type {KeycloakResourceAccess=} */
|
|
72
|
-
resourceAccess;
|
|
73
|
-
/** @type {KeycloakProfile=} */
|
|
74
|
-
profile;
|
|
75
|
-
/** @type {{}=} */
|
|
76
|
-
userInfo;
|
|
77
|
-
/** @type {Endpoints} */
|
|
78
|
-
endpoints;
|
|
79
|
-
/** @type {number=} */
|
|
80
|
-
tokenTimeoutHandle;
|
|
81
|
-
/** @type {() => void=} */
|
|
82
|
-
onAuthSuccess;
|
|
83
|
-
/** @type {(errorData?: KeycloakError) => void=} */
|
|
84
|
-
onAuthError;
|
|
85
|
-
/** @type {() => void=} */
|
|
86
|
-
onAuthRefreshSuccess;
|
|
87
|
-
/** @type {() => void=} */
|
|
88
|
-
onAuthRefreshError;
|
|
89
|
-
/** @type {() => void=} */
|
|
90
|
-
onTokenExpired;
|
|
91
|
-
/** @type {() => void=} */
|
|
92
|
-
onAuthLogout;
|
|
93
|
-
/** @type {(authenticated: boolean) => void=} */
|
|
94
|
-
onReady;
|
|
95
|
-
/** @type {(status: 'success' | 'cancelled' | 'error', action: string) => void=} */
|
|
96
|
-
onActionUpdate;
|
|
97
|
-
/**
|
|
98
|
-
* @param {KeycloakConfig} config
|
|
99
|
-
*/
|
|
100
|
-
constructor(e) {
|
|
101
|
-
if (typeof e != "string" && !v(e))
|
|
102
|
-
throw new Error("The 'Keycloak' constructor must be provided with a configuration object, or a URL to a JSON configuration file.");
|
|
103
|
-
if (v(e)) {
|
|
104
|
-
const t = "oidcProvider" in e ? ["clientId"] : ["url", "realm", "clientId"];
|
|
105
|
-
for (const r of t)
|
|
106
|
-
if (!(r in e))
|
|
107
|
-
throw new Error(`The configuration object is missing the required '${r}' property.`);
|
|
108
|
-
}
|
|
109
|
-
globalThis.isSecureContext || this.#u(
|
|
110
|
-
`[KEYCLOAK] Keycloak JS must be used in a 'secure context' to function properly as it relies on browser APIs that are otherwise not available.
|
|
111
|
-
Continuing to run your application insecurely will lead to unexpected behavior and breakage.
|
|
112
|
-
|
|
113
|
-
For more information see: https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts`
|
|
114
|
-
), this.#i = e;
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* @param {KeycloakInitOptions} initOptions
|
|
118
|
-
* @returns {Promise<boolean>}
|
|
119
|
-
*/
|
|
120
|
-
init = async (e = {}) => {
|
|
121
|
-
if (this.didInitialize)
|
|
122
|
-
throw new Error("A 'Keycloak' instance can only be initialized once.");
|
|
123
|
-
this.didInitialize = !0, this.#l = H();
|
|
124
|
-
const t = ["default", "cordova", "cordova-native"];
|
|
125
|
-
if (typeof e.adapter == "string" && t.includes(e.adapter) ? this.#t = this.#f(e.adapter) : typeof e.adapter == "object" ? this.#t = e.adapter : "Cordova" in window || "cordova" in window ? this.#t = this.#f("cordova") : this.#t = this.#f("default"), typeof e.useNonce < "u" && (this.#o = e.useNonce), typeof e.checkLoginIframe < "u" && (this.#e.enable = e.checkLoginIframe), e.checkLoginIframeInterval && (this.#e.interval = e.checkLoginIframeInterval), e.onLoad === "login-required" && (this.loginRequired = !0), e.responseMode)
|
|
126
|
-
if (e.responseMode === "query" || e.responseMode === "fragment")
|
|
127
|
-
this.responseMode = e.responseMode;
|
|
128
|
-
else
|
|
129
|
-
throw new Error("Invalid value for responseMode");
|
|
130
|
-
if (e.flow) {
|
|
131
|
-
switch (e.flow) {
|
|
132
|
-
case "standard":
|
|
133
|
-
this.responseType = "code";
|
|
134
|
-
break;
|
|
135
|
-
case "implicit":
|
|
136
|
-
this.responseType = "id_token token";
|
|
137
|
-
break;
|
|
138
|
-
case "hybrid":
|
|
139
|
-
this.responseType = "code id_token token";
|
|
140
|
-
break;
|
|
141
|
-
default:
|
|
142
|
-
throw new Error("Invalid value for flow");
|
|
143
|
-
}
|
|
144
|
-
this.flow = e.flow;
|
|
145
|
-
}
|
|
146
|
-
if (typeof e.timeSkew == "number" && (this.timeSkew = e.timeSkew), e.redirectUri && (this.redirectUri = e.redirectUri), e.silentCheckSsoRedirectUri && (this.silentCheckSsoRedirectUri = e.silentCheckSsoRedirectUri), typeof e.silentCheckSsoFallback == "boolean" && (this.silentCheckSsoFallback = e.silentCheckSsoFallback), typeof e.pkceMethod < "u") {
|
|
147
|
-
if (e.pkceMethod !== "S256" && e.pkceMethod !== !1)
|
|
148
|
-
throw new TypeError(`Invalid value for pkceMethod', expected 'S256' or false but got ${e.pkceMethod}.`);
|
|
149
|
-
this.pkceMethod = e.pkceMethod;
|
|
150
|
-
}
|
|
151
|
-
return typeof e.enableLogging == "boolean" && (this.enableLogging = e.enableLogging), e.logoutMethod === "POST" && (this.logoutMethod = "POST"), typeof e.scope == "string" && (this.scope = e.scope), typeof e.messageReceiveTimeout == "number" && e.messageReceiveTimeout > 0 && (this.messageReceiveTimeout = e.messageReceiveTimeout), await this.#S(), await this.#C(), await this.#E(e), this.onReady?.(this.authenticated), this.authenticated;
|
|
152
|
-
};
|
|
153
|
-
/**
|
|
154
|
-
* @param {"default" | "cordova" | "cordova-native"} type
|
|
155
|
-
* @returns {KeycloakAdapter}
|
|
156
|
-
*/
|
|
157
|
-
#f(e) {
|
|
158
|
-
if (e === "default")
|
|
159
|
-
return this.#v();
|
|
160
|
-
if (e === "cordova")
|
|
161
|
-
return this.#e.enable = !1, this.#U();
|
|
162
|
-
if (e === "cordova-native")
|
|
163
|
-
return this.#e.enable = !1, this.#T();
|
|
164
|
-
throw new Error("invalid adapter type: " + e);
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* @returns {KeycloakAdapter}
|
|
168
|
-
*/
|
|
169
|
-
#v() {
|
|
170
|
-
const e = (t) => t?.redirectUri || this.redirectUri || globalThis.location.href;
|
|
171
|
-
return {
|
|
172
|
-
login: async (t) => (window.location.assign(await this.createLoginUrl(t)), await new Promise(() => {
|
|
173
|
-
})),
|
|
174
|
-
logout: async (t) => {
|
|
175
|
-
if ((t?.logoutMethod ?? this.logoutMethod) === "GET") {
|
|
176
|
-
window.location.replace(this.createLogoutUrl(t));
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
const s = document.createElement("form");
|
|
180
|
-
s.setAttribute("method", "POST"), s.setAttribute("action", this.createLogoutUrl(t)), s.style.display = "none";
|
|
181
|
-
const o = {
|
|
182
|
-
id_token_hint: this.idToken,
|
|
183
|
-
client_id: this.clientId,
|
|
184
|
-
post_logout_redirect_uri: e(t)
|
|
185
|
-
};
|
|
186
|
-
for (const [n, a] of Object.entries(o)) {
|
|
187
|
-
const c = document.createElement("input");
|
|
188
|
-
c.setAttribute("type", "hidden"), c.setAttribute("name", n), c.setAttribute(
|
|
189
|
-
"value",
|
|
190
|
-
/** @type {string} */
|
|
191
|
-
a
|
|
192
|
-
), s.appendChild(c);
|
|
193
|
-
}
|
|
194
|
-
document.body.appendChild(s), s.submit();
|
|
195
|
-
},
|
|
196
|
-
register: async (t) => (window.location.assign(await this.createRegisterUrl(t)), await new Promise(() => {
|
|
197
|
-
})),
|
|
198
|
-
accountManagement: async () => {
|
|
199
|
-
const t = this.createAccountUrl();
|
|
200
|
-
if (typeof t < "u")
|
|
201
|
-
window.location.href = t;
|
|
202
|
-
else
|
|
203
|
-
throw new Error("Not supported by the OIDC server");
|
|
204
|
-
return await new Promise(() => {
|
|
205
|
-
});
|
|
206
|
-
},
|
|
207
|
-
redirectUri: e
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* @returns {KeycloakAdapter}
|
|
212
|
-
*/
|
|
213
|
-
#U() {
|
|
214
|
-
const e = (n, a, c) => window.cordova && window.cordova.InAppBrowser ? window.cordova.InAppBrowser.open(n, a, c) : window.open(n, a, c), t = (n) => n && n.cordovaOptions ? Object.keys(n.cordovaOptions).reduce((a, c) => (a[c] = n.cordovaOptions[c], a), {}) : {}, r = (n) => Object.keys(n).reduce((a, c) => (a.push(c + "=" + n[c]), a), []).join(","), s = (n) => {
|
|
215
|
-
const a = t(n);
|
|
216
|
-
return a.location = "no", n && n.prompt === "none" && (a.hidden = "yes"), r(a);
|
|
217
|
-
}, o = () => this.redirectUri || "http://localhost";
|
|
218
|
-
return {
|
|
219
|
-
login: async (n) => {
|
|
220
|
-
const a = s(n), c = await this.createLoginUrl(n), l = e(c, "_blank", a);
|
|
221
|
-
let d = !1, h = !1;
|
|
222
|
-
function f() {
|
|
223
|
-
h = !0, l.close();
|
|
224
|
-
}
|
|
225
|
-
return await new Promise((p, w) => {
|
|
226
|
-
l.addEventListener("loadstart", async (u) => {
|
|
227
|
-
if (u.url.indexOf(o()) === 0) {
|
|
228
|
-
const m = this.#a(u.url);
|
|
229
|
-
try {
|
|
230
|
-
await this.#c(m), p();
|
|
231
|
-
} catch (y) {
|
|
232
|
-
w(y);
|
|
233
|
-
}
|
|
234
|
-
f(), d = !0;
|
|
235
|
-
}
|
|
236
|
-
}), l.addEventListener("loaderror", async (u) => {
|
|
237
|
-
if (!d)
|
|
238
|
-
if (u.url.indexOf(o()) === 0) {
|
|
239
|
-
const m = this.#a(u.url);
|
|
240
|
-
try {
|
|
241
|
-
await this.#c(m), p();
|
|
242
|
-
} catch (y) {
|
|
243
|
-
w(y);
|
|
244
|
-
}
|
|
245
|
-
f(), d = !0;
|
|
246
|
-
} else
|
|
247
|
-
w(new Error("Unable to process login.")), f();
|
|
248
|
-
}), l.addEventListener("exit", function(u) {
|
|
249
|
-
h || w(new Error("User closed the login window."));
|
|
250
|
-
});
|
|
251
|
-
});
|
|
252
|
-
},
|
|
253
|
-
logout: async (n) => {
|
|
254
|
-
const a = this.createLogoutUrl(n), c = e(a, "_blank", "location=no,hidden=yes,clearcache=yes");
|
|
255
|
-
let l = !1;
|
|
256
|
-
c.addEventListener("loadstart", (d) => {
|
|
257
|
-
d.url.indexOf(o()) === 0 && c.close();
|
|
258
|
-
}), c.addEventListener("loaderror", (d) => {
|
|
259
|
-
d.url.indexOf(o()) === 0 || (l = !0), c.close();
|
|
260
|
-
}), await new Promise((d, h) => {
|
|
261
|
-
c.addEventListener("exit", () => {
|
|
262
|
-
l ? h(new Error("User closed the login window.")) : (this.clearToken(), d());
|
|
263
|
-
});
|
|
264
|
-
});
|
|
265
|
-
},
|
|
266
|
-
register: async (n) => {
|
|
267
|
-
const a = await this.createRegisterUrl(), c = s(n), l = e(a, "_blank", c);
|
|
268
|
-
await new Promise((h, f) => {
|
|
269
|
-
l.addEventListener("loadstart", async (p) => {
|
|
270
|
-
if (p.url.indexOf(o()) === 0) {
|
|
271
|
-
l.close();
|
|
272
|
-
const w = this.#a(p.url);
|
|
273
|
-
try {
|
|
274
|
-
await this.#c(w), h();
|
|
275
|
-
} catch (u) {
|
|
276
|
-
f(u);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
});
|
|
280
|
-
});
|
|
281
|
-
},
|
|
282
|
-
accountManagement: async () => {
|
|
283
|
-
const n = this.createAccountUrl();
|
|
284
|
-
if (typeof n < "u") {
|
|
285
|
-
const a = e(n, "_blank", "location=no");
|
|
286
|
-
a.addEventListener("loadstart", function(c) {
|
|
287
|
-
c.url.indexOf(o()) === 0 && a.close();
|
|
288
|
-
});
|
|
289
|
-
} else
|
|
290
|
-
throw new Error("Not supported by the OIDC server");
|
|
291
|
-
},
|
|
292
|
-
redirectUri: () => o()
|
|
293
|
-
};
|
|
294
|
-
}
|
|
295
|
-
/**
|
|
296
|
-
* @returns {KeycloakAdapter}
|
|
297
|
-
*/
|
|
298
|
-
#T() {
|
|
299
|
-
return {
|
|
300
|
-
login: async (e) => {
|
|
301
|
-
const t = await this.createLoginUrl(e);
|
|
302
|
-
await new Promise((r, s) => {
|
|
303
|
-
universalLinks.subscribe("keycloak", async (o) => {
|
|
304
|
-
universalLinks.unsubscribe("keycloak"), window.cordova.plugins.browsertab.close();
|
|
305
|
-
const n = this.#a(o.url);
|
|
306
|
-
try {
|
|
307
|
-
await this.#c(n), r();
|
|
308
|
-
} catch (a) {
|
|
309
|
-
s(a);
|
|
310
|
-
}
|
|
311
|
-
}), window.cordova.plugins.browsertab.openUrl(t);
|
|
312
|
-
});
|
|
313
|
-
},
|
|
314
|
-
logout: async (e) => {
|
|
315
|
-
const t = this.createLogoutUrl(e);
|
|
316
|
-
await new Promise((r) => {
|
|
317
|
-
universalLinks.subscribe("keycloak", () => {
|
|
318
|
-
universalLinks.unsubscribe("keycloak"), window.cordova.plugins.browsertab.close(), this.clearToken(), r();
|
|
319
|
-
}), window.cordova.plugins.browsertab.openUrl(t);
|
|
320
|
-
});
|
|
321
|
-
},
|
|
322
|
-
register: async (e) => {
|
|
323
|
-
const t = await this.createRegisterUrl(e);
|
|
324
|
-
await new Promise((r, s) => {
|
|
325
|
-
universalLinks.subscribe("keycloak", async (o) => {
|
|
326
|
-
universalLinks.unsubscribe("keycloak"), window.cordova.plugins.browsertab.close();
|
|
327
|
-
const n = this.#a(o.url);
|
|
328
|
-
try {
|
|
329
|
-
await this.#c(n), r();
|
|
330
|
-
} catch (a) {
|
|
331
|
-
s(a);
|
|
332
|
-
}
|
|
333
|
-
}), window.cordova.plugins.browsertab.openUrl(t);
|
|
334
|
-
});
|
|
335
|
-
},
|
|
336
|
-
accountManagement: async () => {
|
|
337
|
-
const e = this.createAccountUrl();
|
|
338
|
-
if (typeof e < "u")
|
|
339
|
-
window.cordova.plugins.browsertab.openUrl(e);
|
|
340
|
-
else
|
|
341
|
-
throw new Error("Not supported by the OIDC server");
|
|
342
|
-
},
|
|
343
|
-
redirectUri: (e) => e && e.redirectUri ? e.redirectUri : this.redirectUri ? this.redirectUri : "http://localhost"
|
|
344
|
-
};
|
|
345
|
-
}
|
|
346
|
-
/**
|
|
347
|
-
* @returns {Promise<void>}
|
|
348
|
-
*/
|
|
349
|
-
async #S() {
|
|
350
|
-
if (typeof this.#i == "string") {
|
|
351
|
-
const e = await q(this.#i);
|
|
352
|
-
this.authServerUrl = e["auth-server-url"], this.realm = e.realm, this.clientId = e.resource, this.#g();
|
|
353
|
-
} else
|
|
354
|
-
this.clientId = this.#i.clientId, "oidcProvider" in this.#i ? await this.#_(this.#i.oidcProvider) : (this.authServerUrl = this.#i.url, this.realm = this.#i.realm, this.#g());
|
|
355
|
-
}
|
|
356
|
-
/**
|
|
357
|
-
* @returns {void}
|
|
358
|
-
*/
|
|
359
|
-
#g() {
|
|
360
|
-
this.endpoints = {
|
|
361
|
-
authorize: () => this.#s() + "/protocol/openid-connect/auth",
|
|
362
|
-
token: () => this.#s() + "/protocol/openid-connect/token",
|
|
363
|
-
logout: () => this.#s() + "/protocol/openid-connect/logout",
|
|
364
|
-
checkSessionIframe: () => this.#s() + "/protocol/openid-connect/login-status-iframe.html",
|
|
365
|
-
thirdPartyCookiesIframe: () => this.#s() + "/protocol/openid-connect/3p-cookies/step1.html",
|
|
366
|
-
register: () => this.#s() + "/protocol/openid-connect/registrations",
|
|
367
|
-
userinfo: () => this.#s() + "/protocol/openid-connect/userinfo"
|
|
368
|
-
};
|
|
369
|
-
}
|
|
370
|
-
/**
|
|
371
|
-
* @param {string | OpenIdProviderMetadata} oidcProvider
|
|
372
|
-
* @returns {Promise<void>}
|
|
373
|
-
*/
|
|
374
|
-
async #_(e) {
|
|
375
|
-
if (typeof e == "string") {
|
|
376
|
-
const t = `${_(e)}/.well-known/openid-configuration`, r = await V(t);
|
|
377
|
-
this.#k(r);
|
|
378
|
-
} else
|
|
379
|
-
this.#k(e);
|
|
380
|
-
}
|
|
381
|
-
/**
|
|
382
|
-
* @param {OpenIdProviderMetadata} config
|
|
383
|
-
* @returns {void}
|
|
384
|
-
*/
|
|
385
|
-
#k(e) {
|
|
386
|
-
this.endpoints = {
|
|
387
|
-
authorize() {
|
|
388
|
-
return e.authorization_endpoint;
|
|
389
|
-
},
|
|
390
|
-
token() {
|
|
391
|
-
return e.token_endpoint;
|
|
392
|
-
},
|
|
393
|
-
logout() {
|
|
394
|
-
if (!e.end_session_endpoint)
|
|
395
|
-
throw new Error("Not supported by the OIDC server");
|
|
396
|
-
return e.end_session_endpoint;
|
|
397
|
-
},
|
|
398
|
-
checkSessionIframe() {
|
|
399
|
-
if (!e.check_session_iframe)
|
|
400
|
-
throw new Error("Not supported by the OIDC server");
|
|
401
|
-
return e.check_session_iframe;
|
|
402
|
-
},
|
|
403
|
-
register() {
|
|
404
|
-
throw new Error('Redirection to "Register user" page not supported in standard OIDC mode');
|
|
405
|
-
},
|
|
406
|
-
userinfo() {
|
|
407
|
-
if (!e.userinfo_endpoint)
|
|
408
|
-
throw new Error("Not supported by the OIDC server");
|
|
409
|
-
return e.userinfo_endpoint;
|
|
410
|
-
}
|
|
411
|
-
};
|
|
412
|
-
}
|
|
413
|
-
/**
|
|
414
|
-
* @returns {Promise<void>}
|
|
415
|
-
*/
|
|
416
|
-
async #C() {
|
|
417
|
-
if (!this.#e.enable && !this.silentCheckSsoRedirectUri || typeof this.endpoints.thirdPartyCookiesIframe != "function")
|
|
418
|
-
return;
|
|
419
|
-
const e = document.createElement("iframe");
|
|
420
|
-
e.setAttribute("src", this.endpoints.thirdPartyCookiesIframe()), e.setAttribute("sandbox", "allow-storage-access-by-user-activation allow-scripts allow-same-origin"), e.setAttribute("title", "keycloak-3p-check-iframe"), e.style.display = "none", document.body.appendChild(e);
|
|
421
|
-
const t = new Promise((r) => {
|
|
422
|
-
const s = (o) => {
|
|
423
|
-
e.contentWindow === o.source && (o.data !== "supported" && o.data !== "unsupported" || (o.data === "unsupported" && (this.#u(
|
|
424
|
-
`[KEYCLOAK] Your browser is blocking access to 3rd-party cookies, this means:
|
|
425
|
-
|
|
426
|
-
- It is not possible to retrieve tokens without redirecting to the Keycloak server (a.k.a. no support for silent authentication).
|
|
427
|
-
- It is not possible to automatically detect changes to the session status (such as the user logging out in another tab).
|
|
428
|
-
|
|
429
|
-
For more information see: https://www.keycloak.org/securing-apps/javascript-adapter#_modern_browsers`
|
|
430
|
-
), this.#e.enable = !1, this.silentCheckSsoFallback && (this.silentCheckSsoRedirectUri = void 0)), document.body.removeChild(e), window.removeEventListener("message", s), r()));
|
|
431
|
-
};
|
|
432
|
-
window.addEventListener("message", s, !1);
|
|
433
|
-
});
|
|
434
|
-
return await z(t, this.messageReceiveTimeout, "Timeout when waiting for 3rd party check iframe message.");
|
|
435
|
-
}
|
|
436
|
-
/**
|
|
437
|
-
* @param {KeycloakInitOptions} initOptions
|
|
438
|
-
* @returns {Promise<void>}
|
|
439
|
-
*/
|
|
440
|
-
async #E(e) {
|
|
441
|
-
const t = this.#a(window.location.href);
|
|
442
|
-
if (t?.newUrl && window.history.replaceState(window.history.state, "", t.newUrl), t && t.valid) {
|
|
443
|
-
await this.#p(), await this.#c(t);
|
|
444
|
-
return;
|
|
445
|
-
}
|
|
446
|
-
const r = async (o) => {
|
|
447
|
-
const n = {};
|
|
448
|
-
o || (n.prompt = "none"), e.locale && (n.locale = e.locale), await this.login(n);
|
|
449
|
-
}, s = async () => {
|
|
450
|
-
switch (e.onLoad) {
|
|
451
|
-
case "check-sso":
|
|
452
|
-
this.#e.enable ? (await this.#p(), await this.#d() || (this.silentCheckSsoRedirectUri ? await this.#m() : await r(!1))) : this.silentCheckSsoRedirectUri ? await this.#m() : await r(!1);
|
|
453
|
-
break;
|
|
454
|
-
case "login-required":
|
|
455
|
-
await r(!0);
|
|
456
|
-
break;
|
|
457
|
-
default:
|
|
458
|
-
throw new Error("Invalid value for onLoad");
|
|
459
|
-
}
|
|
460
|
-
};
|
|
461
|
-
if (e.token && e.refreshToken)
|
|
462
|
-
if (this.#h(e.token, e.refreshToken, e.idToken), this.#e.enable)
|
|
463
|
-
await this.#p(), await this.#d() && (this.onAuthSuccess?.(), this.#w());
|
|
464
|
-
else
|
|
465
|
-
try {
|
|
466
|
-
await this.updateToken(-1), this.onAuthSuccess?.();
|
|
467
|
-
} catch (o) {
|
|
468
|
-
if (this.onAuthError?.(), e.onLoad)
|
|
469
|
-
await s();
|
|
470
|
-
else
|
|
471
|
-
throw o;
|
|
472
|
-
}
|
|
473
|
-
else e.onLoad && await s();
|
|
474
|
-
}
|
|
475
|
-
/**
|
|
476
|
-
* @returns {Promise<void>}
|
|
477
|
-
*/
|
|
478
|
-
async #p() {
|
|
479
|
-
if (!this.#e.enable || this.#e.iframe)
|
|
480
|
-
return;
|
|
481
|
-
const e = document.createElement("iframe");
|
|
482
|
-
this.#e.iframe = e, e.setAttribute("src", this.endpoints.checkSessionIframe()), e.setAttribute("sandbox", "allow-storage-access-by-user-activation allow-scripts allow-same-origin"), e.setAttribute("title", "keycloak-session-iframe"), e.style.display = "none", document.body.appendChild(e);
|
|
483
|
-
const t = (s) => {
|
|
484
|
-
if (s.origin !== this.#e.iframeOrigin || this.#e.iframe?.contentWindow !== s.source || !(s.data === "unchanged" || s.data === "changed" || s.data === "error"))
|
|
485
|
-
return;
|
|
486
|
-
s.data !== "unchanged" && this.clearToken();
|
|
487
|
-
const o = this.#e.callbackList;
|
|
488
|
-
this.#e.callbackList = [];
|
|
489
|
-
for (const n of o.reverse())
|
|
490
|
-
s.data === "error" ? n(new Error("Error while checking login iframe")) : n(null, s.data === "unchanged");
|
|
491
|
-
};
|
|
492
|
-
window.addEventListener("message", t, !1), await new Promise((s) => {
|
|
493
|
-
e.addEventListener("load", () => {
|
|
494
|
-
const o = this.endpoints.authorize();
|
|
495
|
-
o.startsWith("/") ? this.#e.iframeOrigin = globalThis.location.origin : this.#e.iframeOrigin = new URL(o).origin, s();
|
|
496
|
-
});
|
|
497
|
-
});
|
|
498
|
-
}
|
|
499
|
-
/**
|
|
500
|
-
* @returns {Promise<boolean | undefined>}
|
|
501
|
-
*/
|
|
502
|
-
async #d() {
|
|
503
|
-
if (!this.#e.iframe || !this.#e.iframeOrigin)
|
|
504
|
-
return;
|
|
505
|
-
const e = `${this.clientId} ${this.sessionId ? this.sessionId : ""}`, t = this.#e.iframeOrigin;
|
|
506
|
-
return await new Promise((s, o) => {
|
|
507
|
-
const n = (a, c) => a ? o(a) : s(
|
|
508
|
-
/** @type {boolean} */
|
|
509
|
-
c
|
|
510
|
-
);
|
|
511
|
-
this.#e.callbackList.push(n), this.#e.callbackList.length === 1 && this.#e.iframe?.contentWindow?.postMessage(e, t);
|
|
512
|
-
});
|
|
513
|
-
}
|
|
514
|
-
/**
|
|
515
|
-
* @returns {Promise<void>}
|
|
516
|
-
*/
|
|
517
|
-
async #m() {
|
|
518
|
-
const e = document.createElement("iframe"), t = await this.createLoginUrl({ prompt: "none", redirectUri: this.silentCheckSsoRedirectUri });
|
|
519
|
-
return e.setAttribute("src", t), e.setAttribute("sandbox", "allow-storage-access-by-user-activation allow-scripts allow-same-origin"), e.setAttribute("title", "keycloak-silent-check-sso"), e.style.display = "none", document.body.appendChild(e), await new Promise((r, s) => {
|
|
520
|
-
const o = async (n) => {
|
|
521
|
-
if (n.origin !== window.location.origin || e.contentWindow !== n.source)
|
|
522
|
-
return;
|
|
523
|
-
const a = this.#a(n.data);
|
|
524
|
-
try {
|
|
525
|
-
await this.#c(a), r();
|
|
526
|
-
} catch (c) {
|
|
527
|
-
s(c);
|
|
528
|
-
}
|
|
529
|
-
document.body.removeChild(e), window.removeEventListener("message", o);
|
|
530
|
-
};
|
|
531
|
-
window.addEventListener("message", o);
|
|
532
|
-
});
|
|
533
|
-
}
|
|
534
|
-
/**
|
|
535
|
-
* @param {string} url
|
|
536
|
-
*/
|
|
537
|
-
#a(e) {
|
|
538
|
-
const t = this.#A(e);
|
|
539
|
-
if (!t)
|
|
540
|
-
return;
|
|
541
|
-
const r = this.#l.get(t.state);
|
|
542
|
-
return r && (t.valid = !0, t.redirectUri = r.redirectUri, t.storedNonce = r.nonce, t.prompt = r.prompt, t.pkceCodeVerifier = r.pkceCodeVerifier, t.loginOptions = r.loginOptions), t;
|
|
543
|
-
}
|
|
544
|
-
/**
|
|
545
|
-
* @param {string} urlString
|
|
546
|
-
*/
|
|
547
|
-
#A(e) {
|
|
548
|
-
let t = [];
|
|
549
|
-
switch (this.flow) {
|
|
550
|
-
case "standard":
|
|
551
|
-
t = ["code", "state", "session_state", "kc_action_status", "kc_action", "iss"];
|
|
552
|
-
break;
|
|
553
|
-
case "implicit":
|
|
554
|
-
t = ["access_token", "token_type", "id_token", "state", "session_state", "expires_in", "kc_action_status", "kc_action", "iss"];
|
|
555
|
-
break;
|
|
556
|
-
case "hybrid":
|
|
557
|
-
t = ["access_token", "token_type", "id_token", "code", "state", "session_state", "expires_in", "kc_action_status", "kc_action", "iss"];
|
|
558
|
-
break;
|
|
559
|
-
}
|
|
560
|
-
t.push("error"), t.push("error_description"), t.push("error_uri");
|
|
561
|
-
const r = new URL(e);
|
|
562
|
-
let s = "", o;
|
|
563
|
-
if (this.responseMode === "query" && r.searchParams.size > 0 ? (o = this.#y(r.search, t), r.search = o.paramsString, s = r.toString()) : this.responseMode === "fragment" && r.hash.length > 0 && (o = this.#y(r.hash.substring(1), t), r.hash = o.paramsString, s = r.toString()), o?.oauthParams) {
|
|
564
|
-
if (this.flow === "standard" || this.flow === "hybrid") {
|
|
565
|
-
if ((o.oauthParams.code || o.oauthParams.error) && o.oauthParams.state)
|
|
566
|
-
return o.oauthParams.newUrl = s, o.oauthParams;
|
|
567
|
-
} else if (this.flow === "implicit" && (o.oauthParams.access_token || o.oauthParams.error) && o.oauthParams.state)
|
|
568
|
-
return o.oauthParams.newUrl = s, o.oauthParams;
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
/**
|
|
572
|
-
* @typedef {Object} ParsedCallbackParams
|
|
573
|
-
* @property {string} paramsString
|
|
574
|
-
* @property {Record<string, string | undefined>} oauthParams
|
|
575
|
-
*/
|
|
576
|
-
/**
|
|
577
|
-
* @param {string} paramsString
|
|
578
|
-
* @param {string[]} supportedParams
|
|
579
|
-
* @returns {ParsedCallbackParams}
|
|
580
|
-
*/
|
|
581
|
-
#y(e, t) {
|
|
582
|
-
const r = new URLSearchParams(e), s = {};
|
|
583
|
-
for (const [o, n] of Array.from(r.entries()))
|
|
584
|
-
t.includes(o) && (s[o] = n, r.delete(o));
|
|
585
|
-
return {
|
|
586
|
-
paramsString: r.toString(),
|
|
587
|
-
oauthParams: s
|
|
588
|
-
};
|
|
589
|
-
}
|
|
590
|
-
async #c(e) {
|
|
591
|
-
const { code: t, error: r, prompt: s } = e;
|
|
592
|
-
let o = (/* @__PURE__ */ new Date()).getTime();
|
|
593
|
-
const n = (a, c, l) => {
|
|
594
|
-
if (o = (o + (/* @__PURE__ */ new Date()).getTime()) / 2, this.#h(a, c, l, o), this.#o && this.idTokenParsed && this.idTokenParsed.nonce !== e.storedNonce)
|
|
595
|
-
throw this.#n("[KEYCLOAK] Invalid nonce, clearing token"), this.clearToken(), new Error("Invalid nonce.");
|
|
596
|
-
};
|
|
597
|
-
if (e.kc_action_status && this.onActionUpdate && this.onActionUpdate(e.kc_action_status, e.kc_action), r) {
|
|
598
|
-
if (s !== "none")
|
|
599
|
-
if (e.error_description && e.error_description === "authentication_expired")
|
|
600
|
-
await this.login(e.loginOptions);
|
|
601
|
-
else {
|
|
602
|
-
const a = { error: r, error_description: e.error_description };
|
|
603
|
-
throw this.onAuthError?.(a), a;
|
|
604
|
-
}
|
|
605
|
-
return;
|
|
606
|
-
} else this.flow !== "standard" && (e.access_token || e.id_token) && (n(e.access_token, void 0, e.id_token), this.onAuthSuccess?.());
|
|
607
|
-
if (this.flow !== "implicit" && t)
|
|
608
|
-
try {
|
|
609
|
-
const a = await B(
|
|
610
|
-
this.endpoints.token(),
|
|
611
|
-
t,
|
|
612
|
-
/** @type {string} */
|
|
613
|
-
this.clientId,
|
|
614
|
-
e.redirectUri,
|
|
615
|
-
e.pkceCodeVerifier
|
|
616
|
-
);
|
|
617
|
-
n(a.access_token, a.refresh_token, a.id_token), this.flow === "standard" && this.onAuthSuccess?.(), this.#w();
|
|
618
|
-
} catch (a) {
|
|
619
|
-
throw this.onAuthError?.(), a;
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
async #w() {
|
|
623
|
-
this.#e.enable && this.token && (await X(this.#e.interval * 1e3), await this.#d() && await this.#w());
|
|
624
|
-
}
|
|
625
|
-
/**
|
|
626
|
-
* @param {KeycloakLoginOptions} [options]
|
|
627
|
-
* @returns {Promise<void>}
|
|
628
|
-
*/
|
|
629
|
-
login = (e) => this.#t.login(e);
|
|
630
|
-
/**
|
|
631
|
-
* @param {KeycloakLoginOptions} [options]
|
|
632
|
-
* @returns {Promise<string>}
|
|
633
|
-
*/
|
|
634
|
-
createLoginUrl = async (e) => {
|
|
635
|
-
const t = T(), r = T(), s = this.#t.redirectUri(e), o = {
|
|
636
|
-
state: t,
|
|
637
|
-
nonce: r,
|
|
638
|
-
redirectUri: s,
|
|
639
|
-
loginOptions: e
|
|
640
|
-
};
|
|
641
|
-
e?.prompt && (o.prompt = e.prompt);
|
|
642
|
-
const n = e?.action === "register" ? this.endpoints.register() : this.endpoints.authorize();
|
|
643
|
-
let a = e?.scope || this.scope;
|
|
644
|
-
const c = a ? a.split(" ") : [];
|
|
645
|
-
c.includes("openid") || c.unshift("openid"), a = c.join(" ");
|
|
646
|
-
const l = new URLSearchParams([
|
|
647
|
-
[
|
|
648
|
-
"client_id",
|
|
649
|
-
/** @type {string} */
|
|
650
|
-
this.clientId
|
|
651
|
-
],
|
|
652
|
-
["redirect_uri", s],
|
|
653
|
-
["state", t],
|
|
654
|
-
["response_mode", this.responseMode],
|
|
655
|
-
["response_type", this.responseType],
|
|
656
|
-
["scope", a]
|
|
657
|
-
]);
|
|
658
|
-
if (this.#o && l.append("nonce", r), e?.prompt && l.append("prompt", e.prompt), typeof e?.maxAge == "number" && l.append("max_age", e.maxAge.toString()), e?.loginHint && l.append("login_hint", e.loginHint), e?.idpHint && l.append("kc_idp_hint", e.idpHint), e?.action && e.action !== "register" && l.append("kc_action", e.action), e?.locale && l.append("ui_locales", e.locale), e?.acr && l.append("claims", M(e.acr)), e?.acrValues && l.append("acr_values", e.acrValues), this.pkceMethod)
|
|
659
|
-
try {
|
|
660
|
-
const d = D(96), h = await N(this.pkceMethod, d);
|
|
661
|
-
o.pkceCodeVerifier = d, l.append("code_challenge", h), l.append("code_challenge_method", this.pkceMethod);
|
|
662
|
-
} catch (d) {
|
|
663
|
-
throw new Error("Failed to generate PKCE challenge.", { cause: d });
|
|
664
|
-
}
|
|
665
|
-
return this.#l.add(o), `${n}?${l.toString()}`;
|
|
666
|
-
};
|
|
667
|
-
/**
|
|
668
|
-
* @param {KeycloakLogoutOptions} [options]
|
|
669
|
-
* @returns {Promise<void>}
|
|
670
|
-
*/
|
|
671
|
-
logout = (e) => this.#t.logout(e);
|
|
672
|
-
/**
|
|
673
|
-
* @param {KeycloakLogoutOptions} [options]
|
|
674
|
-
* @returns {string}
|
|
675
|
-
*/
|
|
676
|
-
createLogoutUrl = (e) => {
|
|
677
|
-
const t = e?.logoutMethod ?? this.logoutMethod, r = this.endpoints.logout();
|
|
678
|
-
if (t === "POST")
|
|
679
|
-
return r;
|
|
680
|
-
const s = new URLSearchParams([
|
|
681
|
-
[
|
|
682
|
-
"client_id",
|
|
683
|
-
/** @type {string} */
|
|
684
|
-
this.clientId
|
|
685
|
-
],
|
|
686
|
-
["post_logout_redirect_uri", this.#t.redirectUri(e)]
|
|
687
|
-
]);
|
|
688
|
-
return this.idToken && s.append("id_token_hint", this.idToken), `${r}?${s.toString()}`;
|
|
689
|
-
};
|
|
690
|
-
/**
|
|
691
|
-
* @param {KeycloakRegisterOptions} [options]
|
|
692
|
-
* @returns {Promise<void>}
|
|
693
|
-
*/
|
|
694
|
-
register = (e) => this.#t.register(e);
|
|
695
|
-
/**
|
|
696
|
-
* @param {KeycloakRegisterOptions} [options]
|
|
697
|
-
* @returns {Promise<string>}
|
|
698
|
-
*/
|
|
699
|
-
createRegisterUrl = (e) => this.createLoginUrl({ ...e, action: "register" });
|
|
700
|
-
/**
|
|
701
|
-
* @param {KeycloakAccountOptions} [options]
|
|
702
|
-
* @returns {string}
|
|
703
|
-
*/
|
|
704
|
-
createAccountUrl = (e) => {
|
|
705
|
-
const t = this.#s();
|
|
706
|
-
if (!t)
|
|
707
|
-
throw new Error("Unable to create account URL, make sure the adapter is not configured using a generic OIDC provider.");
|
|
708
|
-
const r = new URLSearchParams([
|
|
709
|
-
[
|
|
710
|
-
"referrer",
|
|
711
|
-
/** @type {string} */
|
|
712
|
-
this.clientId
|
|
713
|
-
],
|
|
714
|
-
["referrer_uri", this.#t.redirectUri(e)]
|
|
715
|
-
]);
|
|
716
|
-
return `${t}/account?${r.toString()}`;
|
|
717
|
-
};
|
|
718
|
-
/**
|
|
719
|
-
* @returns {Promise<void>}
|
|
720
|
-
*/
|
|
721
|
-
accountManagement = () => this.#t.accountManagement();
|
|
722
|
-
/**
|
|
723
|
-
* @param {string} role
|
|
724
|
-
* @returns {boolean}
|
|
725
|
-
*/
|
|
726
|
-
hasRealmRole = (e) => {
|
|
727
|
-
const t = this.realmAccess;
|
|
728
|
-
return !!t && t.roles.indexOf(e) >= 0;
|
|
729
|
-
};
|
|
730
|
-
/**
|
|
731
|
-
* @param {string} role
|
|
732
|
-
* @param {string} [resource]
|
|
733
|
-
* @returns {boolean}
|
|
734
|
-
*/
|
|
735
|
-
hasResourceRole = (e, t) => {
|
|
736
|
-
if (!this.resourceAccess)
|
|
737
|
-
return !1;
|
|
738
|
-
const r = this.resourceAccess[t || /** @type {string} */
|
|
739
|
-
this.clientId];
|
|
740
|
-
return !!r && r.roles.indexOf(e) >= 0;
|
|
741
|
-
};
|
|
742
|
-
/**
|
|
743
|
-
* @returns {Promise<KeycloakProfile>}
|
|
744
|
-
*/
|
|
745
|
-
loadUserProfile = async () => {
|
|
746
|
-
const e = this.#s();
|
|
747
|
-
if (!e)
|
|
748
|
-
throw new Error("Unable to load user profile, make sure the adapter is not configured using a generic OIDC provider.");
|
|
749
|
-
const t = `${e}/account`, r = await k(t, {
|
|
750
|
-
headers: [S(this.token)]
|
|
751
|
-
});
|
|
752
|
-
return this.profile = r;
|
|
753
|
-
};
|
|
754
|
-
/**
|
|
755
|
-
* @returns {Promise<{}>}
|
|
756
|
-
*/
|
|
757
|
-
loadUserInfo = async () => {
|
|
758
|
-
const e = this.endpoints.userinfo(), t = await k(e, {
|
|
759
|
-
headers: [S(this.token)]
|
|
760
|
-
});
|
|
761
|
-
return this.userInfo = t;
|
|
762
|
-
};
|
|
763
|
-
/**
|
|
764
|
-
* @param {number} [minValidity]
|
|
765
|
-
* @returns {boolean}
|
|
766
|
-
*/
|
|
767
|
-
isTokenExpired = (e) => {
|
|
768
|
-
if (!this.tokenParsed || !this.refreshToken && this.flow !== "implicit")
|
|
769
|
-
throw new Error("Not authenticated");
|
|
770
|
-
if (this.timeSkew == null)
|
|
771
|
-
return this.#n("[KEYCLOAK] Unable to determine if token is expired as timeskew is not set"), !0;
|
|
772
|
-
if (typeof this.tokenParsed.exp != "number")
|
|
773
|
-
return !1;
|
|
774
|
-
let t = this.tokenParsed.exp - Math.ceil((/* @__PURE__ */ new Date()).getTime() / 1e3) + this.timeSkew;
|
|
775
|
-
if (e) {
|
|
776
|
-
if (isNaN(e))
|
|
777
|
-
throw new Error("Invalid minValidity");
|
|
778
|
-
t -= e;
|
|
779
|
-
}
|
|
780
|
-
return t < 0;
|
|
781
|
-
};
|
|
782
|
-
/**
|
|
783
|
-
* @param {number} minValidity
|
|
784
|
-
* @returns {Promise<boolean>}
|
|
785
|
-
*/
|
|
786
|
-
updateToken = async (e) => {
|
|
787
|
-
if (!this.refreshToken)
|
|
788
|
-
throw new Error("Unable to update token, no refresh token available.");
|
|
789
|
-
e = e || 5, this.#e.enable && await this.#d();
|
|
790
|
-
let t = !1;
|
|
791
|
-
if (e === -1 ? (t = !0, this.#n("[KEYCLOAK] Refreshing token: forced refresh")) : (!this.tokenParsed || this.isTokenExpired(e)) && (t = !0, this.#n("[KEYCLOAK] Refreshing token: token expired")), !t)
|
|
792
|
-
return !1;
|
|
793
|
-
const { promise: r, resolve: s, reject: o } = Promise.withResolvers();
|
|
794
|
-
if (this.#r.push({ resolve: s, reject: o }), this.#r.length === 1) {
|
|
795
|
-
const n = this.endpoints.token();
|
|
796
|
-
let a = (/* @__PURE__ */ new Date()).getTime();
|
|
797
|
-
try {
|
|
798
|
-
const c = await G(
|
|
799
|
-
n,
|
|
800
|
-
this.refreshToken,
|
|
801
|
-
/** @type {string} */
|
|
802
|
-
this.clientId
|
|
803
|
-
);
|
|
804
|
-
this.#n("[KEYCLOAK] Token refreshed"), a = (a + (/* @__PURE__ */ new Date()).getTime()) / 2, this.#h(c.access_token, c.refresh_token, c.id_token, a), this.onAuthRefreshSuccess?.();
|
|
805
|
-
for (let l = this.#r.pop(); l != null; l = this.#r.pop())
|
|
806
|
-
l.resolve(!0);
|
|
807
|
-
} catch (c) {
|
|
808
|
-
this.#u("[KEYCLOAK] Failed to refresh token"), c instanceof E && c.response.status === 400 && this.clearToken(), this.onAuthRefreshError?.();
|
|
809
|
-
for (let l = this.#r.pop(); l != null; l = this.#r.pop())
|
|
810
|
-
l.reject(c);
|
|
811
|
-
}
|
|
812
|
-
}
|
|
813
|
-
return await r;
|
|
814
|
-
};
|
|
815
|
-
clearToken = () => {
|
|
816
|
-
this.token && (this.#h(), this.onAuthLogout?.(), this.loginRequired && this.login());
|
|
817
|
-
};
|
|
818
|
-
/**
|
|
819
|
-
* @param {string} [token]
|
|
820
|
-
* @param {string} [refreshToken]
|
|
821
|
-
* @param {string} [idToken]
|
|
822
|
-
* @param {number} [timeLocal]
|
|
823
|
-
*/
|
|
824
|
-
#h(e, t, r, s) {
|
|
825
|
-
if (this.tokenTimeoutHandle && (clearTimeout(this.tokenTimeoutHandle), this.tokenTimeoutHandle = void 0), t ? (this.refreshToken = t, this.refreshTokenParsed = b(t)) : (delete this.refreshToken, delete this.refreshTokenParsed), r ? (this.idToken = r, this.idTokenParsed = b(r)) : (delete this.idToken, delete this.idTokenParsed), e) {
|
|
826
|
-
if (this.token = e, this.tokenParsed = b(e), this.sessionId = this.tokenParsed.sid, this.authenticated = !0, this.subject = this.tokenParsed.sub, this.realmAccess = this.tokenParsed.realm_access, this.resourceAccess = this.tokenParsed.resource_access, s && (this.timeSkew = Math.floor(s / 1e3) - this.tokenParsed.iat), this.timeSkew !== null && (this.#n("[KEYCLOAK] Estimated time difference between browser and server is " + this.timeSkew + " seconds"), this.onTokenExpired)) {
|
|
827
|
-
const o = (this.tokenParsed.exp - (/* @__PURE__ */ new Date()).getTime() / 1e3 + this.timeSkew) * 1e3;
|
|
828
|
-
this.#n("[KEYCLOAK] Token expires in " + Math.round(o / 1e3) + " s"), o <= 0 ? this.onTokenExpired() : this.tokenTimeoutHandle = window.setTimeout(this.onTokenExpired, o);
|
|
829
|
-
}
|
|
830
|
-
} else
|
|
831
|
-
delete this.token, delete this.tokenParsed, delete this.subject, delete this.realmAccess, delete this.resourceAccess, this.authenticated = !1;
|
|
832
|
-
}
|
|
833
|
-
/**
|
|
834
|
-
* @returns {string=}
|
|
835
|
-
*/
|
|
836
|
-
#s() {
|
|
837
|
-
if (!(typeof this.authServerUrl > "u"))
|
|
838
|
-
return `${_(this.authServerUrl)}/realms/${encodeURIComponent(
|
|
839
|
-
/** @type {string} */
|
|
840
|
-
this.realm
|
|
841
|
-
)}`;
|
|
842
|
-
}
|
|
843
|
-
/**
|
|
844
|
-
* @param {Function} fn
|
|
845
|
-
* @returns {(message: string) => void}
|
|
846
|
-
*/
|
|
847
|
-
#b(e) {
|
|
848
|
-
return (t) => {
|
|
849
|
-
this.enableLogging && e.call(console, t);
|
|
850
|
-
};
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
function T() {
|
|
854
|
-
if (typeof crypto > "u" || typeof crypto.randomUUID > "u")
|
|
855
|
-
throw new Error("Web Crypto API is not available.");
|
|
856
|
-
return crypto.randomUUID();
|
|
857
|
-
}
|
|
858
|
-
function M(i) {
|
|
859
|
-
return JSON.stringify({
|
|
860
|
-
id_token: {
|
|
861
|
-
acr: i
|
|
862
|
-
}
|
|
863
|
-
});
|
|
864
|
-
}
|
|
865
|
-
function D(i) {
|
|
866
|
-
return K(i, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
|
|
867
|
-
}
|
|
868
|
-
async function N(i, e) {
|
|
869
|
-
if (i !== "S256")
|
|
870
|
-
throw new TypeError(`Invalid value for 'pkceMethod', expected 'S256' but got '${i}'.`);
|
|
871
|
-
const t = new Uint8Array(await J(e));
|
|
872
|
-
return $(t).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
873
|
-
}
|
|
874
|
-
function K(i, e) {
|
|
875
|
-
const t = O(i), r = new Array(i);
|
|
876
|
-
for (let s = 0; s < i; s++)
|
|
877
|
-
r[s] = e.charCodeAt(t[s] % e.length);
|
|
878
|
-
return String.fromCharCode.apply(null, r);
|
|
879
|
-
}
|
|
880
|
-
function O(i) {
|
|
881
|
-
if (typeof crypto > "u" || typeof crypto.getRandomValues > "u")
|
|
882
|
-
throw new Error("Web Crypto API is not available.");
|
|
883
|
-
return crypto.getRandomValues(new Uint8Array(i));
|
|
884
|
-
}
|
|
885
|
-
function z(i, e, t) {
|
|
886
|
-
let r;
|
|
887
|
-
const s = new Promise(function(o, n) {
|
|
888
|
-
r = window.setTimeout(function() {
|
|
889
|
-
n(new Error(t));
|
|
890
|
-
}, e);
|
|
891
|
-
});
|
|
892
|
-
return Promise.race([i, s]).finally(function() {
|
|
893
|
-
clearTimeout(r);
|
|
894
|
-
});
|
|
895
|
-
}
|
|
896
|
-
function H() {
|
|
897
|
-
try {
|
|
898
|
-
return new j();
|
|
899
|
-
} catch {
|
|
900
|
-
return new W();
|
|
901
|
-
}
|
|
902
|
-
}
|
|
903
|
-
const g = "kc-callback-";
|
|
904
|
-
class j {
|
|
905
|
-
constructor() {
|
|
906
|
-
globalThis.localStorage.setItem("kc-test", "test"), globalThis.localStorage.removeItem("kc-test");
|
|
907
|
-
}
|
|
908
|
-
/**
|
|
909
|
-
* @param {string} [state]
|
|
910
|
-
* @returns {CallbackState | null}
|
|
911
|
-
*/
|
|
912
|
-
get(e) {
|
|
913
|
-
if (!e)
|
|
914
|
-
return null;
|
|
915
|
-
this.#r();
|
|
916
|
-
const t = g + e, r = globalThis.localStorage.getItem(t);
|
|
917
|
-
return r ? (globalThis.localStorage.removeItem(t), JSON.parse(r)) : null;
|
|
918
|
-
}
|
|
919
|
-
/**
|
|
920
|
-
* @param {CallbackState} state
|
|
921
|
-
*/
|
|
922
|
-
add(e) {
|
|
923
|
-
this.#r();
|
|
924
|
-
const t = g + e.state, r = JSON.stringify({
|
|
925
|
-
...e,
|
|
926
|
-
// Set the expiry time to 1 hour from now.
|
|
927
|
-
expires: Date.now() + 3600 * 1e3
|
|
928
|
-
});
|
|
929
|
-
try {
|
|
930
|
-
globalThis.localStorage.setItem(t, r);
|
|
931
|
-
} catch {
|
|
932
|
-
this.#t(), globalThis.localStorage.setItem(t, r);
|
|
933
|
-
}
|
|
934
|
-
}
|
|
935
|
-
/**
|
|
936
|
-
* Clears all values from local storage that are no longer valid.
|
|
937
|
-
*/
|
|
938
|
-
#r() {
|
|
939
|
-
const e = Date.now();
|
|
940
|
-
for (const [t, r] of this.#o()) {
|
|
941
|
-
const s = this.#l(r);
|
|
942
|
-
(s === null || s < e) && globalThis.localStorage.removeItem(t);
|
|
943
|
-
}
|
|
944
|
-
}
|
|
945
|
-
/**
|
|
946
|
-
* Clears all known values from local storage.
|
|
947
|
-
*/
|
|
948
|
-
#t() {
|
|
949
|
-
for (const [e] of this.#o())
|
|
950
|
-
globalThis.localStorage.removeItem(e);
|
|
951
|
-
}
|
|
952
|
-
/**
|
|
953
|
-
* Gets all entries stored in local storage that are known to be managed by this class.
|
|
954
|
-
* @returns {[string, string][]} An array of key-value pairs.
|
|
955
|
-
*/
|
|
956
|
-
#o() {
|
|
957
|
-
return Object.entries(globalThis.localStorage).filter(([e]) => e.startsWith(g));
|
|
958
|
-
}
|
|
959
|
-
/**
|
|
960
|
-
* Parses the expiry time from a value stored in local storage.
|
|
961
|
-
* @param {string} value
|
|
962
|
-
* @returns {number | null} The expiry time in milliseconds, or `null` if the value is malformed.
|
|
963
|
-
*/
|
|
964
|
-
#l(e) {
|
|
965
|
-
let t;
|
|
966
|
-
try {
|
|
967
|
-
t = JSON.parse(e);
|
|
968
|
-
} catch {
|
|
969
|
-
return null;
|
|
970
|
-
}
|
|
971
|
-
return v(t) && "expires" in t && typeof t.expires == "number" ? t.expires : null;
|
|
972
|
-
}
|
|
973
|
-
}
|
|
974
|
-
class W {
|
|
975
|
-
/**
|
|
976
|
-
* @param {string} [state]
|
|
977
|
-
* @returns {CallbackState | null}
|
|
978
|
-
*/
|
|
979
|
-
get(e) {
|
|
980
|
-
if (!e)
|
|
981
|
-
return null;
|
|
982
|
-
const t = this.#r(g + e);
|
|
983
|
-
return this.#t(g + e, "", this.#o(-100)), t ? JSON.parse(t) : null;
|
|
984
|
-
}
|
|
985
|
-
/**
|
|
986
|
-
* @param {CallbackState} state
|
|
987
|
-
*/
|
|
988
|
-
add(e) {
|
|
989
|
-
this.#t(g + e.state, JSON.stringify(e), this.#o(60));
|
|
990
|
-
}
|
|
991
|
-
/**
|
|
992
|
-
* @param {string} key
|
|
993
|
-
* @returns
|
|
994
|
-
*/
|
|
995
|
-
#r(e) {
|
|
996
|
-
const t = e + "=", r = document.cookie.split(";");
|
|
997
|
-
for (let s = 0; s < r.length; s++) {
|
|
998
|
-
let o = r[s];
|
|
999
|
-
for (; o.charAt(0) === " "; )
|
|
1000
|
-
o = o.substring(1);
|
|
1001
|
-
if (o.indexOf(t) === 0)
|
|
1002
|
-
return o.substring(t.length, o.length);
|
|
1003
|
-
}
|
|
1004
|
-
return "";
|
|
1005
|
-
}
|
|
1006
|
-
/**
|
|
1007
|
-
* @param {string} key
|
|
1008
|
-
* @param {string} value
|
|
1009
|
-
* @param {Date} expirationDate
|
|
1010
|
-
*/
|
|
1011
|
-
#t(e, t, r) {
|
|
1012
|
-
const s = e + "=" + t + "; expires=" + r.toUTCString() + "; ";
|
|
1013
|
-
document.cookie = s;
|
|
1014
|
-
}
|
|
1015
|
-
/**
|
|
1016
|
-
* @param {number} minutes
|
|
1017
|
-
* @returns {Date}
|
|
1018
|
-
*/
|
|
1019
|
-
#o(e) {
|
|
1020
|
-
const t = /* @__PURE__ */ new Date();
|
|
1021
|
-
return t.setTime(t.getTime() + e * 60 * 1e3), t;
|
|
1022
|
-
}
|
|
1023
|
-
}
|
|
1024
|
-
function $(i) {
|
|
1025
|
-
const e = String.fromCodePoint(...i);
|
|
1026
|
-
return btoa(e);
|
|
1027
|
-
}
|
|
1028
|
-
async function J(i) {
|
|
1029
|
-
const t = new TextEncoder().encode(i);
|
|
1030
|
-
if (typeof crypto > "u" || typeof crypto.subtle > "u")
|
|
1031
|
-
throw new Error("Web Crypto API is not available.");
|
|
1032
|
-
return await crypto.subtle.digest("SHA-256", t);
|
|
1033
|
-
}
|
|
1034
|
-
function b(i) {
|
|
1035
|
-
const [, e] = i.split(".");
|
|
1036
|
-
if (typeof e != "string")
|
|
1037
|
-
throw new Error("Unable to decode token, payload not found.");
|
|
1038
|
-
let t;
|
|
1039
|
-
try {
|
|
1040
|
-
t = Y(e);
|
|
1041
|
-
} catch (r) {
|
|
1042
|
-
throw new Error("Unable to decode token, payload is not a valid Base64URL value.", { cause: r });
|
|
1043
|
-
}
|
|
1044
|
-
try {
|
|
1045
|
-
return JSON.parse(t);
|
|
1046
|
-
} catch (r) {
|
|
1047
|
-
throw new Error("Unable to decode token, payload is not a valid JSON value.", { cause: r });
|
|
1048
|
-
}
|
|
1049
|
-
}
|
|
1050
|
-
function Y(i) {
|
|
1051
|
-
let e = i.replaceAll("-", "+").replaceAll("_", "/");
|
|
1052
|
-
switch (e.length % 4) {
|
|
1053
|
-
case 0:
|
|
1054
|
-
break;
|
|
1055
|
-
case 2:
|
|
1056
|
-
e += "==";
|
|
1057
|
-
break;
|
|
1058
|
-
case 3:
|
|
1059
|
-
e += "=";
|
|
1060
|
-
break;
|
|
1061
|
-
default:
|
|
1062
|
-
throw new Error("Input is not of the correct length.");
|
|
1063
|
-
}
|
|
1064
|
-
try {
|
|
1065
|
-
return F(e);
|
|
1066
|
-
} catch {
|
|
1067
|
-
return atob(e);
|
|
1068
|
-
}
|
|
1069
|
-
}
|
|
1070
|
-
function F(i) {
|
|
1071
|
-
return decodeURIComponent(atob(i).replace(/(.)/g, (e, t) => {
|
|
1072
|
-
let r = t.charCodeAt(0).toString(16).toUpperCase();
|
|
1073
|
-
return r.length < 2 && (r = "0" + r), "%" + r;
|
|
1074
|
-
}));
|
|
1075
|
-
}
|
|
1076
|
-
function v(i) {
|
|
1077
|
-
return typeof i == "object" && i !== null;
|
|
1078
|
-
}
|
|
1079
|
-
async function q(i) {
|
|
1080
|
-
return await k(i);
|
|
1081
|
-
}
|
|
1082
|
-
async function V(i) {
|
|
1083
|
-
return await k(i);
|
|
1084
|
-
}
|
|
1085
|
-
async function B(i, e, t, r, s) {
|
|
1086
|
-
const o = new URLSearchParams([
|
|
1087
|
-
["code", e],
|
|
1088
|
-
["grant_type", "authorization_code"],
|
|
1089
|
-
["client_id", t],
|
|
1090
|
-
["redirect_uri", r]
|
|
1091
|
-
]);
|
|
1092
|
-
return s && o.append("code_verifier", s), await k(i, {
|
|
1093
|
-
method: "POST",
|
|
1094
|
-
credentials: "include",
|
|
1095
|
-
body: o
|
|
1096
|
-
});
|
|
1097
|
-
}
|
|
1098
|
-
async function G(i, e, t) {
|
|
1099
|
-
const r = new URLSearchParams([
|
|
1100
|
-
["grant_type", "refresh_token"],
|
|
1101
|
-
["refresh_token", e],
|
|
1102
|
-
["client_id", t]
|
|
1103
|
-
]);
|
|
1104
|
-
return await k(i, {
|
|
1105
|
-
method: "POST",
|
|
1106
|
-
credentials: "include",
|
|
1107
|
-
body: r
|
|
1108
|
-
});
|
|
1109
|
-
}
|
|
1110
|
-
async function k(i, e = {}) {
|
|
1111
|
-
const t = new Headers(e.headers);
|
|
1112
|
-
return t.set("Accept", R), await (await Q(i, {
|
|
1113
|
-
...e,
|
|
1114
|
-
headers: t
|
|
1115
|
-
})).json();
|
|
1116
|
-
}
|
|
1117
|
-
async function Q(i, e) {
|
|
1118
|
-
const t = await fetch(i, e);
|
|
1119
|
-
if (!t.ok)
|
|
1120
|
-
throw new E("Server responded with an invalid status.", { response: t });
|
|
1121
|
-
return t;
|
|
1122
|
-
}
|
|
1123
|
-
function S(i) {
|
|
1124
|
-
if (!i)
|
|
1125
|
-
throw new Error("Unable to build authorization header, token is not set, make sure the user is authenticated.");
|
|
1126
|
-
return ["Authorization", `bearer ${i}`];
|
|
1127
|
-
}
|
|
1128
|
-
function _(i) {
|
|
1129
|
-
return i.endsWith("/") ? i.slice(0, -1) : i;
|
|
1130
|
-
}
|
|
1131
|
-
class E extends Error {
|
|
1132
|
-
/** @type {Response} */
|
|
1133
|
-
response;
|
|
1134
|
-
/**
|
|
1135
|
-
* @param {string} message
|
|
1136
|
-
* @param {NetworkErrorOptions} options
|
|
1137
|
-
*/
|
|
1138
|
-
constructor(e, t) {
|
|
1139
|
-
super(e, t), this.response = t.response;
|
|
1140
|
-
}
|
|
1141
|
-
}
|
|
1142
|
-
const X = (i) => new Promise((e) => setTimeout(e, i));
|
|
1143
|
-
class U extends x {
|
|
1144
|
-
constructor(e) {
|
|
1145
|
-
super(e);
|
|
1
|
+
import { ref as k, markRaw as p, watch as g, inject as y } from "vue";
|
|
2
|
+
import f from "keycloak-js";
|
|
3
|
+
const c = "GHENTCDH_AUTHENTICATION";
|
|
4
|
+
class i extends f {
|
|
5
|
+
constructor(t) {
|
|
6
|
+
super(t);
|
|
1146
7
|
}
|
|
1147
8
|
async initialize() {
|
|
1148
9
|
try {
|
|
1149
|
-
const
|
|
10
|
+
const t = await this.init({
|
|
1150
11
|
onLoad: "login-required"
|
|
1151
12
|
});
|
|
1152
|
-
console.log(
|
|
1153
|
-
} catch (
|
|
1154
|
-
console.error("Failed to initialize adapter:",
|
|
13
|
+
console.log(t ? "User is authenticated" : "User is not authenticated");
|
|
14
|
+
} catch (t) {
|
|
15
|
+
console.error("Failed to initialize adapter:", t);
|
|
1155
16
|
}
|
|
1156
17
|
}
|
|
1157
|
-
static async init(
|
|
1158
|
-
const
|
|
1159
|
-
return console.log("KeycloakAdapter",
|
|
18
|
+
static async init(t) {
|
|
19
|
+
const n = new i(t);
|
|
20
|
+
return console.log("KeycloakAdapter", t), await n.initialize(), n;
|
|
1160
21
|
}
|
|
1161
22
|
get userInfo() {
|
|
1162
23
|
return this.idTokenParsed;
|
|
@@ -1168,44 +29,44 @@ class U extends x {
|
|
|
1168
29
|
return this.authenticated ?? !1;
|
|
1169
30
|
}
|
|
1170
31
|
}
|
|
1171
|
-
const
|
|
32
|
+
const w = {
|
|
1172
33
|
skipAuthentication: !1
|
|
1173
|
-
},
|
|
1174
|
-
const
|
|
1175
|
-
let
|
|
1176
|
-
const
|
|
1177
|
-
const
|
|
1178
|
-
return
|
|
1179
|
-
},
|
|
1180
|
-
if (!
|
|
1181
|
-
return
|
|
1182
|
-
|
|
1183
|
-
const
|
|
1184
|
-
|
|
34
|
+
}, C = (o) => {
|
|
35
|
+
const t = k(!1);
|
|
36
|
+
let n;
|
|
37
|
+
const s = { ...w, ...o }, u = async () => {
|
|
38
|
+
const e = await i.init(o.keycloak);
|
|
39
|
+
return n = e, t.value = !0, e;
|
|
40
|
+
}, l = () => n?.token, a = async () => {
|
|
41
|
+
if (!n)
|
|
42
|
+
return u();
|
|
43
|
+
t.value || await new Promise((e) => {
|
|
44
|
+
const d = g(t, (h) => {
|
|
45
|
+
h && (d(), e());
|
|
1185
46
|
});
|
|
1186
47
|
});
|
|
1187
|
-
},
|
|
1188
|
-
install(
|
|
1189
|
-
|
|
1190
|
-
|
|
48
|
+
}, r = p({
|
|
49
|
+
install(e) {
|
|
50
|
+
e.runWithContext(() => {
|
|
51
|
+
e.config.globalProperties.$auth = s, e.provide(c, r);
|
|
1191
52
|
});
|
|
1192
53
|
},
|
|
1193
|
-
updateToken: async () => (await
|
|
1194
|
-
token:
|
|
1195
|
-
user: async () => (await
|
|
1196
|
-
logout: async () => (await
|
|
1197
|
-
options:
|
|
54
|
+
updateToken: async () => (await a())?.updateToken(),
|
|
55
|
+
token: l,
|
|
56
|
+
user: async () => (await a())?.userInfo,
|
|
57
|
+
logout: async () => (await a())?.logout(),
|
|
58
|
+
options: s
|
|
1198
59
|
});
|
|
1199
|
-
return
|
|
1200
|
-
},
|
|
1201
|
-
const
|
|
1202
|
-
return
|
|
1203
|
-
isAuthenticated: () => !!
|
|
1204
|
-
getUser: () =>
|
|
1205
|
-
logout: () =>
|
|
60
|
+
return r;
|
|
61
|
+
}, I = () => {
|
|
62
|
+
const o = y(c);
|
|
63
|
+
return o || console.warn("No auth provided, authorized calls may not work"), {
|
|
64
|
+
isAuthenticated: () => !!o?.user(),
|
|
65
|
+
getUser: () => o?.user(),
|
|
66
|
+
logout: () => o?.logout()
|
|
1206
67
|
};
|
|
1207
68
|
};
|
|
1208
69
|
export {
|
|
1209
|
-
|
|
1210
|
-
|
|
70
|
+
C as createAuth,
|
|
71
|
+
I as useAuthenticate
|
|
1211
72
|
};
|