uikit/dist/index.js
2026-06-13 10:44:46 +03:30

1586 lines
56 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var Pe = Object.defineProperty;
var Se = (r) => {
throw TypeError(r);
};
var je = (r, e, t) => e in r ? Pe(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
var f = (r, e, t) => je(r, typeof e != "symbol" ? e + "" : e, t), oe = (r, e, t) => e.has(r) || Se("Cannot " + t);
var h = (r, e, t) => (oe(r, e, "read from private field"), t ? t.call(r) : e.get(r)), A = (r, e, t) => e.has(r) ? Se("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(r) : e.set(r, t), B = (r, e, t, i) => (oe(r, e, "write to private field"), i ? i.call(r, t) : e.set(r, t), t), d = (r, e, t) => (oe(r, e, "access private method"), t);
import { jsxs as W, jsx as S } from "react/jsx-runtime";
import { useState as I, useRef as ie, useEffect as O, useCallback as H, createContext as We, useMemo as Oe } from "react";
import { HStack as K, IconButton as G, Button as ke, Input as be, Text as j } from "@chakra-ui/react";
import { IoChevronForwardOutline as ye, IoChevronBackOutline as ve } from "react-icons/io5";
const kt = (r) => new Intl.NumberFormat("fa-IR").format(Number(r)), v = (r) => String(r).replace(/\d/g, (e) => "۰۱۲۳۴۵۶۷۸۹"[Number(e)]), V = (r) => String(r).replace(/[۰-۹]/g, (t) => String("۰۱۲۳۴۵۶۷۸۹".indexOf(t))), bt = ({
currentPage: r,
totalPages: e,
totalCount: t,
onPageChange: i
}) => {
const [n, o] = I(v(r)), [c, a] = I(!1), [l, u] = I(null), w = ie(null), m = [.../* @__PURE__ */ new Set([1, r, e])].filter(
(p) => p >= 1 && p <= e
);
O(() => {
if (l !== null) {
r === l && (u(null), a(!1), o(v(r)));
return;
}
c || o(v(r));
}, [r, c, l]), O(() => {
if (!c || !n || l !== null) return;
const p = parseInt(V(n), 10);
if (isNaN(p)) return;
const k = setTimeout(() => {
var C;
const g = Math.min(Math.max(p, 1), e);
g !== r ? (u(g), o(v(g)), i(g)) : (a(!1), o(v(r))), (C = w.current) == null || C.blur();
}, 1e3);
return () => clearTimeout(k);
}, [
n,
c,
l,
e,
r,
i
]);
const T = () => {
var g, C;
const p = parseInt(V(n), 10);
if (isNaN(p)) {
o(v(r)), a(!1), u(null), (g = w.current) == null || g.blur();
return;
}
const k = Math.min(Math.max(p, 1), e);
k !== r ? (u(k), o(v(k)), i(k)) : (o(v(r)), a(!1), u(null)), (C = w.current) == null || C.blur();
};
return e === 0 || !t ? null : /* @__PURE__ */ W(
K,
{
justifyContent: "space-around",
backgroundColor: "white",
py: "10px",
px: "16px",
rounded: "lg",
border: "1px",
borderColor: "blue.200",
w: "100%",
children: [
/* @__PURE__ */ W(K, { spacing: 2, alignItems: "center", justifyContent: "center", children: [
/* @__PURE__ */ S(
G,
{
size: "md",
bg: "#e6e6f1",
color: "#4B49AC",
_hover: { bg: "#cbcbe8" },
onClick: () => i(r - 1),
isDisabled: r === 1,
icon: /* @__PURE__ */ S(ye, {}),
"aria-label": "صفحه قبلی"
}
),
m.map((p) => /* @__PURE__ */ S(
ke,
{
size: "md",
px: 2,
bg: r === p ? "#4B49AC" : "#e6e6f1",
color: r === p ? "#e6e6f1" : "#4B49AC",
_hover: { bg: r === p ? "#4B49AC" : "#cbcbe8" },
onClick: () => i(p),
variant: "soft",
children: v(p)
},
p
)),
/* @__PURE__ */ S(
G,
{
size: "md",
bg: "#e6e6f1",
color: "#4B49AC",
_hover: { bg: "#cbcbe8" },
onClick: () => i(r + 1),
isDisabled: r === e,
icon: /* @__PURE__ */ S(ve, {}),
"aria-label": "صفحه بعدی"
}
),
/* @__PURE__ */ S(
be,
{
ref: w,
type: "text",
inputMode: "numeric",
placeholder: "صفحه",
w: "56px",
value: n,
onFocus: () => {
a(!0), u(null), o("");
},
onChange: (p) => {
const k = p.target.value, g = V(k).replace(/\D/g, "");
o(v(g));
},
onBlur: () => {
n || (o(v(r)), a(!1), u(null));
},
onKeyDown: (p) => {
p.key === "Enter" && T();
},
textAlign: "center",
color: "#1D2939",
px: 1,
minW: 0,
dir: "ltr"
}
)
] }),
/* @__PURE__ */ W(K, { spacing: 2, children: [
/* @__PURE__ */ S(j, { w: "auto", fontWeight: "bold", whiteSpace: "nowrap", children: "تعداد کل:" }),
/* @__PURE__ */ S(j, { children: v(t) })
] })
]
}
);
}, yt = ({
currentPage: r,
totalPages: e,
totalCount: t,
onPageChange: i
}) => {
const [n, o] = I(v(r)), [c, a] = I(!1), [l, u] = I(null), w = ie(null), m = Array.from(
/* @__PURE__ */ new Set([1, r, r + 1, e])
).filter((p) => p >= 1 && p <= e).sort((p, k) => p - k);
O(() => {
if (l !== null) {
r === l && (u(null), a(!1), o(v(r)));
return;
}
c || o(v(r));
}, [r, c, l]), O(() => {
if (!c || !n || l !== null) return;
const p = parseInt(V(n), 10);
if (isNaN(p)) return;
const k = setTimeout(() => {
var C;
const g = Math.min(Math.max(p, 1), e);
g !== r ? (u(g), o(v(g)), i(g)) : (a(!1), o(v(r))), (C = w.current) == null || C.blur();
}, 1e3);
return () => clearTimeout(k);
}, [
n,
c,
l,
e,
r,
i
]);
const T = () => {
var g, C;
const p = parseInt(V(n), 10);
if (isNaN(p)) {
o(v(r)), a(!1), u(null), (g = w.current) == null || g.blur();
return;
}
const k = Math.min(Math.max(p, 1), e);
k !== r ? (u(k), o(v(k)), i(k)) : (o(v(r)), a(!1), u(null)), (C = w.current) == null || C.blur();
};
return e === 0 || !t ? null : /* @__PURE__ */ W(
K,
{
justifyContent: "space-around",
backgroundColor: "white",
py: "10px",
px: "16px",
rounded: "lg",
border: "1px",
borderColor: "blue.200",
w: "100%",
children: [
/* @__PURE__ */ W(K, { spacing: 2, whiteSpace: "nowrap", children: [
/* @__PURE__ */ S(j, { fontWeight: "bold", children: "صفحه" }),
/* @__PURE__ */ S(j, { children: v(r) }),
/* @__PURE__ */ S(j, { fontWeight: "bold", children: "از" }),
/* @__PURE__ */ S(j, { children: v(e) })
] }),
/* @__PURE__ */ W(K, { spacing: 2, alignItems: "center", justifyContent: "center", children: [
/* @__PURE__ */ S(
G,
{
size: "md",
bg: "#e6e6f1",
color: "#4B49AC",
_hover: { bg: "#cbcbe8" },
onClick: () => i(r - 1),
isDisabled: r === 1,
icon: /* @__PURE__ */ S(ye, {}),
"aria-label": "صفحه قبلی"
}
),
m.map((p) => /* @__PURE__ */ S(
ke,
{
size: "md",
bg: r === p ? "#4B49AC" : "#e6e6f1",
color: r === p ? "#e6e6f1" : "#4B49AC",
_hover: { bg: r === p ? "#4B49AC" : "#cbcbe8" },
onClick: () => i(p),
variant: "soft",
children: v(p)
},
p
)),
/* @__PURE__ */ S(
G,
{
size: "md",
bg: "#e6e6f1",
color: "#4B49AC",
_hover: { bg: "#cbcbe8" },
onClick: () => i(r + 1),
isDisabled: r === e,
icon: /* @__PURE__ */ S(ve, {}),
"aria-label": "صفحه بعدی"
}
),
/* @__PURE__ */ S(
be,
{
ref: w,
type: "text",
inputMode: "numeric",
placeholder: "صفحه",
w: "100px",
value: n,
onFocus: () => {
a(!0), u(null), o("");
},
onChange: (p) => {
const k = p.target.value, g = V(k).replace(/\D/g, "");
o(v(g));
},
onBlur: () => {
n || (o(v(r)), a(!1), u(null));
},
onKeyDown: (p) => {
p.key === "Enter" && T();
},
textAlign: "center",
color: "#1D2939",
px: 1,
minW: 0,
dir: "ltr"
}
)
] }),
/* @__PURE__ */ W(K, { spacing: 2, whiteSpace: "nowrap", children: [
/* @__PURE__ */ S(j, { fontWeight: "bold", children: "تعداد کل:" }),
/* @__PURE__ */ S(j, { children: v(t) })
] })
]
}
);
}, vt = ({
currentPage: r,
totalPages: e,
totalCount: t,
onPageChange: i
}) => {
const [n, o] = I(""), [c, a] = I(!1), [l, u] = I(null), w = ie(null), m = l ?? r, T = H(
(U) => {
const E = Math.min(Math.max(U, 1), e);
a(!1), o(""), u(null), E !== r && i(E);
},
[r, e, i]
), p = H(() => {
if (!n) {
a(!1), o(""), u(null);
return;
}
const U = parseInt(V(n), 10);
if (isNaN(U)) {
a(!1), o(""), u(null);
return;
}
const E = Math.min(Math.max(U, 1), e);
a(!1), o(""), E !== r ? (u(E), i(E)) : u(null);
}, [n, r, e, i]);
O(() => {
var U;
c && ((U = w.current) == null || U.focus());
}, [c]), O(() => {
l !== null && r === l && u(null);
}, [r, l]), O(() => {
if (!c || !n) return;
const U = setTimeout(() => {
p();
}, 1e3);
return () => clearTimeout(U);
}, [n, c, p]);
const k = () => {
u(null), o(""), a(!0);
}, g = (U, E = !1) => /* @__PURE__ */ S(
ke,
{
size: "md",
bg: E ? "#4B49AC" : "#e6e6f1",
color: E ? "#e6e6f1" : "#4B49AC",
_hover: { bg: E ? "#4B49AC" : "#cbcbe8" },
onClick: E ? k : () => T(U),
variant: "soft",
children: v(U)
},
U
);
if (e === 0 || !t) return null;
const C = e > 1 && m !== 1, F = e > 1 && m !== e;
return /* @__PURE__ */ S(
K,
{
justifyContent: "center",
backgroundColor: "white",
py: "10px",
px: "16px",
rounded: "lg",
border: "1px",
borderColor: "blue.200",
w: "100%",
children: /* @__PURE__ */ W(K, { spacing: 2, alignItems: "center", justifyContent: "center", children: [
/* @__PURE__ */ S(
G,
{
size: "md",
bg: "#e6e6f1",
color: "#4B49AC",
_hover: { bg: "#cbcbe8" },
onClick: () => T(m - 1),
isDisabled: m === 1,
icon: /* @__PURE__ */ S(ye, {}),
"aria-label": "صفحه قبلی"
}
),
C && g(1),
c ? /* @__PURE__ */ S(
be,
{
ref: w,
type: "text",
inputMode: "numeric",
placeholder: "صفحه",
w: "48px",
value: n,
onChange: (U) => {
const E = U.target.value, ze = V(E).replace(/\D/g, "");
o(v(ze));
},
onBlur: () => {
n ? p() : (o(""), a(!1), u(null));
},
onKeyDown: (U) => {
U.key === "Enter" && p(), U.key === "Escape" && (o(""), a(!1), u(null));
},
textAlign: "center",
color: "#1D2939",
px: 1,
minW: 0,
dir: "ltr"
}
) : g(m, !0),
F && g(e),
/* @__PURE__ */ S(
G,
{
size: "md",
bg: "#e6e6f1",
color: "#4B49AC",
_hover: { bg: "#cbcbe8" },
onClick: () => T(m + 1),
isDisabled: m === e,
icon: /* @__PURE__ */ S(ve, {}),
"aria-label": "صفحه بعدی"
}
)
] })
}
);
}, Ve = "application/json";
var N, _, J, Y, M, q, y, L, s, ee, _e, Ee, Ae, Ie, ae, xe, ce, Le, Re, te, X, le, z, Me, de, P, re, Z, R, ue;
class Fe {
/**
* @param {KeycloakConfig} config
*/
constructor(e) {
A(this, s);
/** @type {Pick<PromiseWithResolvers<boolean>, 'resolve' | 'reject'>[]} */
A(this, N, []);
/** @type {KeycloakAdapter} */
A(this, _);
/** @type {boolean} */
A(this, J, !0);
/** @type {CallbackStorage} */
A(this, Y);
A(this, M, d(this, s, ue).call(this, console.info));
A(this, q, d(this, s, ue).call(this, console.warn));
/** @type {LoginIframe} */
A(this, y, {
enable: !0,
callbackList: [],
interval: 5
});
/** @type {KeycloakConfig} config */
A(this, L);
f(this, "didInitialize", !1);
f(this, "authenticated", !1);
f(this, "loginRequired", !1);
/** @type {KeycloakResponseMode} */
f(this, "responseMode", "fragment");
/** @type {KeycloakResponseType} */
f(this, "responseType", "code");
/** @type {KeycloakFlow} */
f(this, "flow", "standard");
/** @type {number?} */
f(this, "timeSkew", null);
/** @type {string=} */
f(this, "redirectUri");
/** @type {string=} */
f(this, "silentCheckSsoRedirectUri");
/** @type {boolean} */
f(this, "silentCheckSsoFallback", !0);
/** @type {KeycloakPkceMethod} */
f(this, "pkceMethod", "S256");
f(this, "enableLogging", !1);
/** @type {'GET' | 'POST'} */
f(this, "logoutMethod", "GET");
/** @type {string=} */
f(this, "scope");
f(this, "messageReceiveTimeout", 1e4);
/** @type {string=} */
f(this, "idToken");
/** @type {KeycloakTokenParsed=} */
f(this, "idTokenParsed");
/** @type {string=} */
f(this, "token");
/** @type {KeycloakTokenParsed=} */
f(this, "tokenParsed");
/** @type {string=} */
f(this, "refreshToken");
/** @type {KeycloakTokenParsed=} */
f(this, "refreshTokenParsed");
/** @type {string=} */
f(this, "clientId");
/** @type {string=} */
f(this, "sessionId");
/** @type {string=} */
f(this, "subject");
/** @type {string=} */
f(this, "authServerUrl");
/** @type {string=} */
f(this, "realm");
/** @type {KeycloakRoles=} */
f(this, "realmAccess");
/** @type {KeycloakResourceAccess=} */
f(this, "resourceAccess");
/** @type {KeycloakProfile=} */
f(this, "profile");
/** @type {{}=} */
f(this, "userInfo");
/** @type {Endpoints} */
f(this, "endpoints");
/** @type {number=} */
f(this, "tokenTimeoutHandle");
/** @type {() => void=} */
f(this, "onAuthSuccess");
/** @type {(errorData?: KeycloakError) => void=} */
f(this, "onAuthError");
/** @type {() => void=} */
f(this, "onAuthRefreshSuccess");
/** @type {() => void=} */
f(this, "onAuthRefreshError");
/** @type {() => void=} */
f(this, "onTokenExpired");
/** @type {() => void=} */
f(this, "onAuthLogout");
/** @type {(authenticated: boolean) => void=} */
f(this, "onReady");
/** @type {(status: 'success' | 'cancelled' | 'error', action: string) => void=} */
f(this, "onActionUpdate");
/**
* @param {KeycloakInitOptions} initOptions
* @returns {Promise<boolean>}
*/
f(this, "init", async (e = {}) => {
var i;
if (this.didInitialize)
throw new Error("A 'Keycloak' instance can only be initialized once.");
this.didInitialize = !0, B(this, Y, Qe());
const t = ["default", "cordova", "cordova-native"];
if (typeof e.adapter == "string" && t.includes(e.adapter) ? B(this, _, d(this, s, ee).call(this, e.adapter)) : typeof e.adapter == "object" ? B(this, _, e.adapter) : "Cordova" in window || "cordova" in window ? B(this, _, d(this, s, ee).call(this, "cordova")) : B(this, _, d(this, s, ee).call(this, "default")), typeof e.useNonce < "u" && B(this, J, e.useNonce), typeof e.checkLoginIframe < "u" && (h(this, y).enable = e.checkLoginIframe), e.checkLoginIframeInterval && (h(this, y).interval = e.checkLoginIframeInterval), e.onLoad === "login-required" && (this.loginRequired = !0), e.responseMode)
if (e.responseMode === "query" || e.responseMode === "fragment")
this.responseMode = e.responseMode;
else
throw new Error("Invalid value for responseMode");
if (e.flow) {
switch (e.flow) {
case "standard":
this.responseType = "code";
break;
case "implicit":
this.responseType = "id_token token";
break;
case "hybrid":
this.responseType = "code id_token token";
break;
default:
throw new Error("Invalid value for flow");
}
this.flow = e.flow;
}
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") {
if (e.pkceMethod !== "S256" && e.pkceMethod !== !1)
throw new TypeError(`Invalid value for pkceMethod', expected 'S256' or false but got ${e.pkceMethod}.`);
this.pkceMethod = e.pkceMethod;
}
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 d(this, s, Ie).call(this), await d(this, s, Le).call(this), await d(this, s, Re).call(this, e), (i = this.onReady) == null || i.call(this, this.authenticated), this.authenticated;
});
/**
* @param {KeycloakLoginOptions} [options]
* @returns {Promise<void>}
*/
f(this, "login", (e) => h(this, _).login(e));
/**
* @param {KeycloakLoginOptions} [options]
* @returns {Promise<string>}
*/
f(this, "createLoginUrl", async (e) => {
const t = Ce(), i = Ce(), n = h(this, _).redirectUri(e), o = {
state: t,
nonce: i,
redirectUri: n,
loginOptions: e
};
e != null && e.prompt && (o.prompt = e.prompt);
const c = (e == null ? void 0 : e.action) === "register" ? this.endpoints.register() : this.endpoints.authorize();
let a = (e == null ? void 0 : e.scope) || this.scope;
const l = a ? a.split(" ") : [];
l.includes("openid") || l.unshift("openid"), a = l.join(" ");
const u = new URLSearchParams([
[
"client_id",
/** @type {string} */
this.clientId
],
["redirect_uri", n],
["state", t],
["response_mode", this.responseMode],
["response_type", this.responseType],
["scope", a]
]);
if (h(this, J) && u.append("nonce", i), e != null && e.prompt && u.append("prompt", e.prompt), typeof (e == null ? void 0 : e.maxAge) == "number" && u.append("max_age", e.maxAge.toString()), e != null && e.loginHint && u.append("login_hint", e.loginHint), e != null && e.idpHint && u.append("kc_idp_hint", e.idpHint), e != null && e.action && e.action !== "register" && u.append("kc_action", e.action), e != null && e.locale && u.append("ui_locales", e.locale), e != null && e.acr && u.append("claims", He(e.acr)), e != null && e.acrValues && u.append("acr_values", e.acrValues), this.pkceMethod)
try {
const w = $e(96), m = await Je(this.pkceMethod, w);
o.pkceCodeVerifier = w, u.append("code_challenge", m), u.append("code_challenge_method", this.pkceMethod);
} catch (w) {
throw new Error("Failed to generate PKCE challenge.", { cause: w });
}
return h(this, Y).add(o), `${c}?${u.toString()}`;
});
/**
* @param {KeycloakLogoutOptions} [options]
* @returns {Promise<void>}
*/
f(this, "logout", (e) => h(this, _).logout(e));
/**
* @param {KeycloakLogoutOptions} [options]
* @returns {string}
*/
f(this, "createLogoutUrl", (e) => {
const t = (e == null ? void 0 : e.logoutMethod) ?? this.logoutMethod, i = this.endpoints.logout();
if (t === "POST")
return i;
const n = new URLSearchParams([
[
"client_id",
/** @type {string} */
this.clientId
],
["post_logout_redirect_uri", h(this, _).redirectUri(e)]
]);
return this.idToken && n.append("id_token_hint", this.idToken), `${i}?${n.toString()}`;
});
/**
* @param {KeycloakRegisterOptions} [options]
* @returns {Promise<void>}
*/
f(this, "register", (e) => h(this, _).register(e));
/**
* @param {KeycloakRegisterOptions} [options]
* @returns {Promise<string>}
*/
f(this, "createRegisterUrl", (e) => this.createLoginUrl({ ...e, action: "register" }));
/**
* @param {KeycloakAccountOptions} [options]
* @returns {string}
*/
f(this, "createAccountUrl", (e) => {
const t = d(this, s, R).call(this);
if (!t)
throw new Error("Unable to create account URL, make sure the adapter is not configured using a generic OIDC provider.");
const i = new URLSearchParams([
[
"referrer",
/** @type {string} */
this.clientId
],
["referrer_uri", h(this, _).redirectUri(e)]
]);
return `${t}/account?${i.toString()}`;
});
/**
* @returns {Promise<void>}
*/
f(this, "accountManagement", () => h(this, _).accountManagement());
/**
* @param {string} role
* @returns {boolean}
*/
f(this, "hasRealmRole", (e) => {
const t = this.realmAccess;
return !!t && t.roles.indexOf(e) >= 0;
});
/**
* @param {string} role
* @param {string} [resource]
* @returns {boolean}
*/
f(this, "hasResourceRole", (e, t) => {
if (!this.resourceAccess)
return !1;
const i = this.resourceAccess[t || /** @type {string} */
this.clientId];
return !!i && i.roles.indexOf(e) >= 0;
});
/**
* @returns {Promise<KeycloakProfile>}
*/
f(this, "loadUserProfile", async () => {
const e = d(this, s, R).call(this);
if (!e)
throw new Error("Unable to load user profile, make sure the adapter is not configured using a generic OIDC provider.");
const t = `${e}/account`, i = await Q(t, {
headers: [Te(this.token)]
});
return this.profile = i;
});
/**
* @returns {Promise<{}>}
*/
f(this, "loadUserInfo", async () => {
const e = this.endpoints.userinfo(), t = await Q(e, {
headers: [Te(this.token)]
});
return this.userInfo = t;
});
/**
* @param {number} [minValidity]
* @returns {boolean}
*/
f(this, "isTokenExpired", (e) => {
if (!this.tokenParsed || !this.refreshToken && this.flow !== "implicit")
throw new Error("Not authenticated");
if (this.timeSkew == null)
return h(this, M).call(this, "[KEYCLOAK] Unable to determine if token is expired as timeskew is not set"), !0;
if (typeof this.tokenParsed.exp != "number")
return !1;
let t = this.tokenParsed.exp - Math.ceil((/* @__PURE__ */ new Date()).getTime() / 1e3) + this.timeSkew;
if (e) {
if (isNaN(e))
throw new Error("Invalid minValidity");
t -= e;
}
return t < 0;
});
/**
* @param {number} minValidity
* @returns {Promise<boolean>}
*/
f(this, "updateToken", async (e) => {
var c, a;
if (!this.refreshToken)
throw new Error("Unable to update token, no refresh token available.");
e = e || 5, h(this, y).enable && await d(this, s, X).call(this);
let t = !1;
if (e === -1 ? (t = !0, h(this, M).call(this, "[KEYCLOAK] Refreshing token: forced refresh")) : (!this.tokenParsed || this.isTokenExpired(e)) && (t = !0, h(this, M).call(this, "[KEYCLOAK] Refreshing token: token expired")), !t)
return !1;
const { promise: i, resolve: n, reject: o } = Promise.withResolvers();
if (h(this, N).push({ resolve: n, reject: o }), h(this, N).length === 1) {
const l = this.endpoints.token();
let u = (/* @__PURE__ */ new Date()).getTime();
try {
const w = await at(
l,
this.refreshToken,
/** @type {string} */
this.clientId
);
h(this, M).call(this, "[KEYCLOAK] Token refreshed"), u = (u + (/* @__PURE__ */ new Date()).getTime()) / 2, d(this, s, Z).call(this, w.access_token, w.refresh_token, w.id_token, u), (c = this.onAuthRefreshSuccess) == null || c.call(this);
for (let m = h(this, N).pop(); m != null; m = h(this, N).pop())
m.resolve(!0);
} catch (w) {
h(this, q).call(this, "[KEYCLOAK] Failed to refresh token"), w instanceof Be && w.response.status === 400 && this.clearToken(), (a = this.onAuthRefreshError) == null || a.call(this);
for (let m = h(this, N).pop(); m != null; m = h(this, N).pop())
m.reject(w);
}
}
return await i;
});
f(this, "clearToken", () => {
var e;
this.token && (d(this, s, Z).call(this), (e = this.onAuthLogout) == null || e.call(this), this.loginRequired && this.login());
});
if (typeof e != "string" && !me(e))
throw new Error("The 'Keycloak' constructor must be provided with a configuration object, or a URL to a JSON configuration file.");
if (me(e)) {
const t = "oidcProvider" in e ? ["clientId"] : ["url", "realm", "clientId"];
for (const i of t)
if (!(i in e))
throw new Error(`The configuration object is missing the required '${i}' property.`);
}
globalThis.isSecureContext || h(this, q).call(this, `[KEYCLOAK] Keycloak JS must be used in a 'secure context' to function properly as it relies on browser APIs that are otherwise not available.
Continuing to run your application insecurely will lead to unexpected behavior and breakage.
For more information see: https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts`), B(this, L, e);
}
}
N = new WeakMap(), _ = new WeakMap(), J = new WeakMap(), Y = new WeakMap(), M = new WeakMap(), q = new WeakMap(), y = new WeakMap(), L = new WeakMap(), s = new WeakSet(), /**
* @param {"default" | "cordova" | "cordova-native"} type
* @returns {KeycloakAdapter}
*/
ee = function(e) {
if (e === "default")
return d(this, s, _e).call(this);
if (e === "cordova")
return h(this, y).enable = !1, d(this, s, Ee).call(this);
if (e === "cordova-native")
return h(this, y).enable = !1, d(this, s, Ae).call(this);
throw new Error("invalid adapter type: " + e);
}, /**
* @returns {KeycloakAdapter}
*/
_e = function() {
const e = (t) => (t == null ? void 0 : t.redirectUri) || this.redirectUri || globalThis.location.href;
return {
login: async (t) => (window.location.assign(await this.createLoginUrl(t)), await new Promise(() => {
})),
logout: async (t) => {
if (((t == null ? void 0 : t.logoutMethod) ?? this.logoutMethod) === "GET") {
window.location.replace(this.createLogoutUrl(t));
return;
}
const n = document.createElement("form");
n.setAttribute("method", "POST"), n.setAttribute("action", this.createLogoutUrl(t)), n.style.display = "none";
const o = {
id_token_hint: this.idToken,
client_id: this.clientId,
post_logout_redirect_uri: e(t)
};
for (const [c, a] of Object.entries(o)) {
const l = document.createElement("input");
l.setAttribute("type", "hidden"), l.setAttribute("name", c), l.setAttribute(
"value",
/** @type {string} */
a
), n.appendChild(l);
}
document.body.appendChild(n), n.submit();
},
register: async (t) => (window.location.assign(await this.createRegisterUrl(t)), await new Promise(() => {
})),
accountManagement: async () => {
const t = this.createAccountUrl();
if (typeof t < "u")
window.location.href = t;
else
throw new Error("Not supported by the OIDC server");
return await new Promise(() => {
});
},
redirectUri: e
};
}, /**
* @returns {KeycloakAdapter}
*/
Ee = function() {
const e = (c, a, l) => window.cordova && window.cordova.InAppBrowser ? window.cordova.InAppBrowser.open(c, a, l) : window.open(c, a, l), t = (c) => c && c.cordovaOptions ? Object.keys(c.cordovaOptions).reduce((a, l) => (a[l] = c.cordovaOptions[l], a), {}) : {}, i = (c) => Object.keys(c).reduce((a, l) => (a.push(l + "=" + c[l]), a), []).join(","), n = (c) => {
const a = t(c);
return a.location = "no", c && c.prompt === "none" && (a.hidden = "yes"), i(a);
}, o = () => this.redirectUri || "http://localhost";
return {
login: async (c) => {
const a = n(c), l = await this.createLoginUrl(c), u = e(l, "_blank", a);
let w = !1, m = !1;
function T() {
m = !0, u.close();
}
return await new Promise((p, k) => {
u.addEventListener("loadstart", async (g) => {
if (g.url.indexOf(o()) === 0) {
const C = d(this, s, z).call(this, g.url);
w = !0, T();
try {
await d(this, s, P).call(this, C), p();
} catch (F) {
k(F);
}
}
}), u.addEventListener("loaderror", async (g) => {
if (!w)
if (g.url.indexOf(o()) === 0) {
const C = d(this, s, z).call(this, g.url);
w = !0, T();
try {
await d(this, s, P).call(this, C), p();
} catch (F) {
k(F);
}
} else
k(new Error("Unable to process login.")), T();
}), u.addEventListener("exit", function(g) {
m || k(new Error("User closed the login window."));
});
});
},
logout: async (c) => {
const a = this.createLogoutUrl(c), l = e(a, "_blank", "location=no,hidden=yes,clearcache=yes");
let u = !1;
l.addEventListener("loadstart", (w) => {
w.url.indexOf(o()) === 0 && l.close();
}), l.addEventListener("loaderror", (w) => {
w.url.indexOf(o()) === 0 || (u = !0), l.close();
}), await new Promise((w, m) => {
l.addEventListener("exit", () => {
u ? m(new Error("User closed the login window.")) : (this.clearToken(), w());
});
});
},
register: async (c) => {
const a = await this.createRegisterUrl(), l = n(c), u = e(a, "_blank", l);
await new Promise((m, T) => {
u.addEventListener("loadstart", async (p) => {
if (p.url.indexOf(o()) === 0) {
u.close();
const k = d(this, s, z).call(this, p.url);
try {
await d(this, s, P).call(this, k), m();
} catch (g) {
T(g);
}
}
});
});
},
accountManagement: async () => {
const c = this.createAccountUrl();
if (typeof c < "u") {
const a = e(c, "_blank", "location=no");
a.addEventListener("loadstart", function(l) {
l.url.indexOf(o()) === 0 && a.close();
});
} else
throw new Error("Not supported by the OIDC server");
},
redirectUri: () => o()
};
}, /**
* @returns {KeycloakAdapter}
*/
Ae = function() {
return {
login: async (e) => {
const t = await this.createLoginUrl(e);
await new Promise((i, n) => {
universalLinks.subscribe("keycloak", async (o) => {
universalLinks.unsubscribe("keycloak"), window.cordova.plugins.browsertab.close();
const c = d(this, s, z).call(this, o.url);
try {
await d(this, s, P).call(this, c), i();
} catch (a) {
n(a);
}
}), window.cordova.plugins.browsertab.openUrl(t);
});
},
logout: async (e) => {
const t = this.createLogoutUrl(e);
await new Promise((i) => {
universalLinks.subscribe("keycloak", () => {
universalLinks.unsubscribe("keycloak"), window.cordova.plugins.browsertab.close(), this.clearToken(), i();
}), window.cordova.plugins.browsertab.openUrl(t);
});
},
register: async (e) => {
const t = await this.createRegisterUrl(e);
await new Promise((i, n) => {
universalLinks.subscribe("keycloak", async (o) => {
universalLinks.unsubscribe("keycloak"), window.cordova.plugins.browsertab.close();
const c = d(this, s, z).call(this, o.url);
try {
await d(this, s, P).call(this, c), i();
} catch (a) {
n(a);
}
}), window.cordova.plugins.browsertab.openUrl(t);
});
},
accountManagement: async () => {
const e = this.createAccountUrl();
if (typeof e < "u")
window.cordova.plugins.browsertab.openUrl(e);
else
throw new Error("Not supported by the OIDC server");
},
redirectUri: (e) => e && e.redirectUri ? e.redirectUri : this.redirectUri ? this.redirectUri : "http://localhost"
};
}, Ie = async function() {
if (typeof h(this, L) == "string") {
const e = await ot(h(this, L));
this.authServerUrl = e["auth-server-url"], this.realm = e.realm, this.clientId = e.resource, d(this, s, ae).call(this);
} else
this.clientId = h(this, L).clientId, "oidcProvider" in h(this, L) ? await d(this, s, xe).call(this, h(this, L).oidcProvider) : (this.authServerUrl = h(this, L).url, this.realm = h(this, L).realm, d(this, s, ae).call(this));
}, /**
* @returns {void}
*/
ae = function() {
this.endpoints = {
authorize: () => d(this, s, R).call(this) + "/protocol/openid-connect/auth",
token: () => d(this, s, R).call(this) + "/protocol/openid-connect/token",
logout: () => d(this, s, R).call(this) + "/protocol/openid-connect/logout",
checkSessionIframe: () => d(this, s, R).call(this) + "/protocol/openid-connect/login-status-iframe.html",
thirdPartyCookiesIframe: () => d(this, s, R).call(this) + "/protocol/openid-connect/3p-cookies/step1.html",
register: () => d(this, s, R).call(this) + "/protocol/openid-connect/registrations",
userinfo: () => d(this, s, R).call(this) + "/protocol/openid-connect/userinfo"
};
}, xe = async function(e) {
if (typeof e == "string") {
const t = `${Ue(e)}/.well-known/openid-configuration`, i = await nt(t);
d(this, s, ce).call(this, i);
} else
d(this, s, ce).call(this, e);
}, /**
* @param {OpenIdProviderMetadata} config
* @returns {void}
*/
ce = function(e) {
this.endpoints = {
authorize() {
return e.authorization_endpoint;
},
token() {
return e.token_endpoint;
},
logout() {
if (!e.end_session_endpoint)
throw new Error("Not supported by the OIDC server");
return e.end_session_endpoint;
},
checkSessionIframe() {
if (!e.check_session_iframe)
throw new Error("Not supported by the OIDC server");
return e.check_session_iframe;
},
register() {
throw new Error('Redirection to "Register user" page not supported in standard OIDC mode');
},
userinfo() {
if (!e.userinfo_endpoint)
throw new Error("Not supported by the OIDC server");
return e.userinfo_endpoint;
}
};
}, Le = async function() {
if (!h(this, y).enable && !this.silentCheckSsoRedirectUri || typeof this.endpoints.thirdPartyCookiesIframe != "function")
return;
const e = document.createElement("iframe");
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);
const t = new Promise((i) => {
const n = (o) => {
e.contentWindow === o.source && (o.data !== "supported" && o.data !== "unsupported" || (o.data === "unsupported" && (h(this, q).call(this, `[KEYCLOAK] Your browser is blocking access to 3rd-party cookies, this means:
- It is not possible to retrieve tokens without redirecting to the Keycloak server (a.k.a. no support for silent authentication).
- It is not possible to automatically detect changes to the session status (such as the user logging out in another tab).
For more information see: https://www.keycloak.org/securing-apps/javascript-adapter#_modern_browsers`), h(this, y).enable = !1, this.silentCheckSsoFallback && (this.silentCheckSsoRedirectUri = void 0)), document.body.removeChild(e), window.removeEventListener("message", n), i()));
};
window.addEventListener("message", n, !1);
});
return await Ge(t, this.messageReceiveTimeout, "Timeout when waiting for 3rd party check iframe message.");
}, Re = async function(e) {
var o, c, a;
const t = d(this, s, z).call(this, window.location.href);
if (t != null && t.newUrl && window.history.replaceState(window.history.state, "", t.newUrl), t && t.valid) {
await d(this, s, te).call(this), await d(this, s, P).call(this, t);
return;
}
const i = async (l) => {
const u = {};
l || (u.prompt = "none"), e.locale && (u.locale = e.locale), await this.login(u);
}, n = async () => {
switch (e.onLoad) {
case "check-sso":
h(this, y).enable ? (await d(this, s, te).call(this), await d(this, s, X).call(this) || (this.silentCheckSsoRedirectUri ? await d(this, s, le).call(this) : await i(!1))) : this.silentCheckSsoRedirectUri ? await d(this, s, le).call(this) : await i(!1);
break;
case "login-required":
await i(!0);
break;
default:
throw new Error("Invalid value for onLoad");
}
};
if (e.token && e.refreshToken)
if (d(this, s, Z).call(this, e.token, e.refreshToken, e.idToken), h(this, y).enable)
await d(this, s, te).call(this), await d(this, s, X).call(this) && ((o = this.onAuthSuccess) == null || o.call(this), d(this, s, re).call(this));
else
try {
await this.updateToken(-1), (c = this.onAuthSuccess) == null || c.call(this);
} catch (l) {
if ((a = this.onAuthError) == null || a.call(this), e.onLoad)
await n();
else
throw l;
}
else e.onLoad && await n();
}, te = async function() {
if (!h(this, y).enable || h(this, y).iframe)
return;
const e = document.createElement("iframe");
h(this, y).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);
const t = (n) => {
var c;
if (n.origin !== h(this, y).iframeOrigin || ((c = h(this, y).iframe) == null ? void 0 : c.contentWindow) !== n.source || !(n.data === "unchanged" || n.data === "changed" || n.data === "error"))
return;
n.data !== "unchanged" && this.clearToken();
const o = h(this, y).callbackList;
h(this, y).callbackList = [];
for (const a of o.reverse())
n.data === "error" ? a(new Error("Error while checking login iframe")) : a(null, n.data === "unchanged");
};
window.addEventListener("message", t, !1), await new Promise((n) => {
e.addEventListener("load", () => {
const o = this.endpoints.authorize();
o.startsWith("/") ? h(this, y).iframeOrigin = globalThis.location.origin : h(this, y).iframeOrigin = new URL(o).origin, n();
});
});
}, X = async function() {
if (!h(this, y).iframe || !h(this, y).iframeOrigin)
return;
const e = `${this.clientId} ${this.sessionId ? this.sessionId : ""}`, t = h(this, y).iframeOrigin;
return await new Promise((n, o) => {
var a, l;
const c = (u, w) => u ? o(u) : n(
/** @type {boolean} */
w
);
h(this, y).callbackList.push(c), h(this, y).callbackList.length === 1 && ((l = (a = h(this, y).iframe) == null ? void 0 : a.contentWindow) == null || l.postMessage(e, t));
});
}, le = async function() {
const e = document.createElement("iframe"), t = await this.createLoginUrl({ prompt: "none", redirectUri: this.silentCheckSsoRedirectUri });
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((i, n) => {
const o = async (c) => {
if (c.origin !== window.location.origin || e.contentWindow !== c.source)
return;
const a = d(this, s, z).call(this, c.data);
try {
await d(this, s, P).call(this, a), i();
} catch (l) {
n(l);
}
document.body.removeChild(e), window.removeEventListener("message", o);
};
window.addEventListener("message", o);
});
}, /**
* @param {string} url
*/
z = function(e) {
const t = d(this, s, Me).call(this, e);
if (!t)
return;
const i = h(this, Y).get(t.state);
return i && (t.valid = !0, t.redirectUri = i.redirectUri, t.storedNonce = i.nonce, t.prompt = i.prompt, t.pkceCodeVerifier = i.pkceCodeVerifier, t.loginOptions = i.loginOptions), t;
}, /**
* @param {string} urlString
*/
Me = function(e) {
let t = [];
switch (this.flow) {
case "standard":
t = ["code", "state", "session_state", "kc_action_status", "kc_action", "iss"];
break;
case "implicit":
t = ["access_token", "token_type", "id_token", "state", "session_state", "expires_in", "kc_action_status", "kc_action", "iss"];
break;
case "hybrid":
t = ["access_token", "token_type", "id_token", "code", "state", "session_state", "expires_in", "kc_action_status", "kc_action", "iss"];
break;
}
t.push("error"), t.push("error_description"), t.push("error_uri");
const i = new URL(e);
let n = "", o;
if (this.responseMode === "query" && i.searchParams.size > 0 ? (o = d(this, s, de).call(this, i.search, t), i.search = o.paramsString, n = i.toString()) : this.responseMode === "fragment" && i.hash.length > 0 && (o = d(this, s, de).call(this, i.hash.substring(1), t), i.hash = o.paramsString, n = i.toString()), o != null && o.oauthParams) {
if (this.flow === "standard" || this.flow === "hybrid") {
if ((o.oauthParams.code || o.oauthParams.error) && o.oauthParams.state)
return o.oauthParams.newUrl = n, o.oauthParams;
} else if (this.flow === "implicit" && (o.oauthParams.access_token || o.oauthParams.error) && o.oauthParams.state)
return o.oauthParams.newUrl = n, o.oauthParams;
}
}, /**
* @typedef {Object} ParsedCallbackParams
* @property {string} paramsString
* @property {Record<string, string | undefined>} oauthParams
*/
/**
* @param {string} paramsString
* @param {string[]} supportedParams
* @returns {ParsedCallbackParams}
*/
de = function(e, t) {
const i = e.split("&"), n = {};
let o = "";
for (const c of i.reverse()) {
const a = new URLSearchParams(c).entries().next().value;
if (!a) {
o = "&" + o;
continue;
}
const [l, u] = a;
t.includes(l) && !(l in n) ? n[l] = u : o = o.length === 0 ? c : c + "&" + o;
}
return {
paramsString: o,
oauthParams: n
};
}, P = async function(e) {
var a, l, u, w;
const { code: t, error: i, prompt: n } = e;
let o = (/* @__PURE__ */ new Date()).getTime();
const c = (m, T, p) => {
if (o = (o + (/* @__PURE__ */ new Date()).getTime()) / 2, d(this, s, Z).call(this, m, T, p, o), h(this, J) && this.idTokenParsed && this.idTokenParsed.nonce !== e.storedNonce)
throw h(this, M).call(this, "[KEYCLOAK] Invalid nonce, clearing token"), this.clearToken(), new Error("Invalid nonce.");
};
if (e.kc_action_status && this.onActionUpdate && this.onActionUpdate(e.kc_action_status, e.kc_action), i) {
if (n !== "none")
if (e.error_description && e.error_description === "authentication_expired")
await this.login(e.loginOptions);
else {
const m = { error: i, error_description: e.error_description };
throw (a = this.onAuthError) == null || a.call(this, m), m;
}
return;
} else this.flow !== "standard" && (e.access_token || e.id_token) && (c(e.access_token, void 0, e.id_token), (l = this.onAuthSuccess) == null || l.call(this));
if (this.flow !== "implicit" && t)
try {
const m = await st(
this.endpoints.token(),
t,
/** @type {string} */
this.clientId,
e.redirectUri,
e.pkceCodeVerifier
);
c(m.access_token, m.refresh_token, m.id_token), this.flow === "standard" && ((u = this.onAuthSuccess) == null || u.call(this)), d(this, s, re).call(this);
} catch (m) {
throw (w = this.onAuthError) == null || w.call(this), m;
}
}, re = async function() {
h(this, y).enable && this.token && (await lt(h(this, y).interval * 1e3), await d(this, s, X).call(this) && await d(this, s, re).call(this));
}, /**
* @param {string} [token]
* @param {string} [refreshToken]
* @param {string} [idToken]
* @param {number} [timeLocal]
*/
Z = function(e, t, i, n) {
if (this.tokenTimeoutHandle && (clearTimeout(this.tokenTimeoutHandle), this.tokenTimeoutHandle = void 0), t ? (this.refreshToken = t, this.refreshTokenParsed = ne(t)) : (delete this.refreshToken, delete this.refreshTokenParsed), i ? (this.idToken = i, this.idTokenParsed = ne(i)) : (delete this.idToken, delete this.idTokenParsed), e) {
if (this.token = e, this.tokenParsed = ne(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, n && (this.timeSkew = Math.floor(n / 1e3) - this.tokenParsed.iat), this.timeSkew !== null && (h(this, M).call(this, "[KEYCLOAK] Estimated time difference between browser and server is " + this.timeSkew + " seconds"), this.onTokenExpired)) {
const o = (this.tokenParsed.exp - (/* @__PURE__ */ new Date()).getTime() / 1e3 + this.timeSkew) * 1e3;
h(this, M).call(this, "[KEYCLOAK] Token expires in " + Math.round(o / 1e3) + " s"), o <= 0 ? this.onTokenExpired() : this.tokenTimeoutHandle = window.setTimeout(this.onTokenExpired, o);
}
} else
delete this.token, delete this.tokenParsed, delete this.subject, delete this.realmAccess, delete this.resourceAccess, this.authenticated = !1;
}, /**
* @returns {string=}
*/
R = function() {
if (!(typeof this.authServerUrl > "u"))
return `${Ue(this.authServerUrl)}/realms/${encodeURIComponent(
/** @type {string} */
this.realm
)}`;
}, /**
* @param {Function} fn
* @returns {(message: string) => void}
*/
ue = function(e) {
return (t) => {
this.enableLogging && e.call(console, t);
};
};
function Ce() {
if (typeof crypto > "u" || typeof crypto.randomUUID > "u")
throw new Error("Web Crypto API is not available.");
return crypto.randomUUID();
}
function He(r) {
return JSON.stringify({
id_token: {
acr: r
}
});
}
function $e(r) {
return Ye(r, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
}
async function Je(r, e) {
if (r !== "S256")
throw new TypeError(`Invalid value for 'pkceMethod', expected 'S256' but got '${r}'.`);
const t = new Uint8Array(await tt(e));
return et(t).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
}
function Ye(r, e) {
const t = qe(r), i = new Array(r);
for (let n = 0; n < r; n++)
i[n] = e.charCodeAt(t[n] % e.length);
return String.fromCharCode.apply(null, i);
}
function qe(r) {
if (typeof crypto > "u" || typeof crypto.getRandomValues > "u")
throw new Error("Web Crypto API is not available.");
return crypto.getRandomValues(new Uint8Array(r));
}
function Ge(r, e, t) {
let i;
const n = new Promise(function(o, c) {
i = window.setTimeout(function() {
c(new Error(t));
}, e);
});
return Promise.race([r, n]).finally(function() {
clearTimeout(i);
});
}
function Qe() {
try {
return new Xe();
} catch {
return new Ze();
}
}
const $ = "kc-callback-";
var x, he, De, fe, Ne;
class Xe {
constructor() {
A(this, x);
globalThis.localStorage.setItem("kc-test", "test"), globalThis.localStorage.removeItem("kc-test");
}
/**
* @param {string} [state]
* @returns {CallbackState | null}
*/
get(e) {
if (!e)
return null;
d(this, x, he).call(this);
const t = $ + e, i = globalThis.localStorage.getItem(t);
return i ? (globalThis.localStorage.removeItem(t), JSON.parse(i)) : null;
}
/**
* @param {CallbackState} state
*/
add(e) {
d(this, x, he).call(this);
const t = $ + e.state, i = JSON.stringify({
...e,
// Set the expiry time to 1 hour from now.
expires: Date.now() + 60 * 60 * 1e3
});
try {
globalThis.localStorage.setItem(t, i);
} catch {
d(this, x, De).call(this), globalThis.localStorage.setItem(t, i);
}
}
}
x = new WeakSet(), /**
* Clears all values from local storage that are no longer valid.
*/
he = function() {
const e = Date.now();
for (const [t, i] of d(this, x, fe).call(this)) {
const n = d(this, x, Ne).call(this, i);
(n === null || n < e) && globalThis.localStorage.removeItem(t);
}
}, /**
* Clears all known values from local storage.
*/
De = function() {
for (const [e] of d(this, x, fe).call(this))
globalThis.localStorage.removeItem(e);
}, /**
* Gets all entries stored in local storage that are known to be managed by this class.
* @returns {[string, string][]} An array of key-value pairs.
*/
fe = function() {
return Object.entries(globalThis.localStorage).filter(([e]) => e.startsWith($));
}, /**
* Parses the expiry time from a value stored in local storage.
* @param {string} value
* @returns {number | null} The expiry time in milliseconds, or `null` if the value is malformed.
*/
Ne = function(e) {
let t;
try {
t = JSON.parse(e);
} catch {
return null;
}
return me(t) && "expires" in t && typeof t.expires == "number" ? t.expires : null;
};
var D, Ke, pe, we;
class Ze {
constructor() {
A(this, D);
}
/**
* @param {string} [state]
* @returns {CallbackState | null}
*/
get(e) {
if (!e)
return null;
const t = d(this, D, Ke).call(this, $ + e);
return d(this, D, pe).call(this, $ + e, "", d(this, D, we).call(this, -100)), t ? JSON.parse(t) : null;
}
/**
* @param {CallbackState} state
*/
add(e) {
d(this, D, pe).call(this, $ + e.state, JSON.stringify(e), d(this, D, we).call(this, 60));
}
}
D = new WeakSet(), /**
* @param {string} key
* @returns
*/
Ke = function(e) {
const t = e + "=", i = document.cookie.split(";");
for (let n = 0; n < i.length; n++) {
let o = i[n];
for (; o.charAt(0) === " "; )
o = o.substring(1);
if (o.indexOf(t) === 0)
return o.substring(t.length, o.length);
}
return "";
}, /**
* @param {string} key
* @param {string} value
* @param {Date} expirationDate
*/
pe = function(e, t, i) {
const n = e + "=" + t + "; expires=" + i.toUTCString() + "; ";
document.cookie = n;
}, /**
* @param {number} minutes
* @returns {Date}
*/
we = function(e) {
const t = /* @__PURE__ */ new Date();
return t.setTime(t.getTime() + e * 60 * 1e3), t;
};
function et(r) {
const e = String.fromCodePoint(...r);
return btoa(e);
}
async function tt(r) {
const t = new TextEncoder().encode(r);
if (typeof crypto > "u" || typeof crypto.subtle > "u")
throw new Error("Web Crypto API is not available.");
return await crypto.subtle.digest("SHA-256", t);
}
function ne(r) {
const [, e] = r.split(".");
if (typeof e != "string")
throw new Error("Unable to decode token, payload not found.");
let t;
try {
t = rt(e);
} catch (i) {
throw new Error("Unable to decode token, payload is not a valid Base64URL value.", { cause: i });
}
try {
return JSON.parse(t);
} catch (i) {
throw new Error("Unable to decode token, payload is not a valid JSON value.", { cause: i });
}
}
function rt(r) {
let e = r.replaceAll("-", "+").replaceAll("_", "/");
switch (e.length % 4) {
case 0:
break;
case 2:
e += "==";
break;
case 3:
e += "=";
break;
default:
throw new Error("Input is not of the correct length.");
}
try {
return it(e);
} catch {
return atob(e);
}
}
function it(r) {
return decodeURIComponent(atob(r).replace(/(.)/g, (e, t) => {
let i = t.charCodeAt(0).toString(16).toUpperCase();
return i.length < 2 && (i = "0" + i), "%" + i;
}));
}
function me(r) {
return typeof r == "object" && r !== null;
}
async function ot(r) {
return await Q(r);
}
async function nt(r) {
return await Q(r);
}
async function st(r, e, t, i, n) {
const o = new URLSearchParams([
["code", e],
["grant_type", "authorization_code"],
["client_id", t],
["redirect_uri", i]
]);
return n && o.append("code_verifier", n), await Q(r, {
method: "POST",
credentials: "include",
body: o
});
}
async function at(r, e, t) {
const i = new URLSearchParams([
["grant_type", "refresh_token"],
["refresh_token", e],
["client_id", t]
]);
return await Q(r, {
method: "POST",
credentials: "include",
body: i
});
}
async function Q(r, e = {}) {
const t = new Headers(e.headers);
return t.set("Accept", Ve), await (await ct(r, {
...e,
headers: t
})).json();
}
async function ct(r, e) {
const t = await fetch(r, e);
if (!t.ok)
throw new Be("Server responded with an invalid status.", { response: t });
return t;
}
function Te(r) {
if (!r)
throw new Error("Unable to build authorization header, token is not set, make sure the user is authenticated.");
return ["Authorization", `bearer ${r}`];
}
function Ue(r) {
return r.endsWith("/") ? r.slice(0, -1) : r;
}
class Be extends Error {
/**
* @param {string} message
* @param {NetworkErrorOptions} options
*/
constructor(t, i) {
super(t, i);
/** @type {Response} */
f(this, "response");
this.response = i.response;
}
}
const lt = (r) => new Promise((e) => setTimeout(e, r));
let b = null, se = null, ge = !1;
const dt = We(null), ut = ({ url: r, realm: e, clientId: t }) => (b || (b = new Fe({
url: r,
realm: e,
clientId: t
})), b), ht = ({ config: r, initOptions: e = {} }) => {
const t = ut(r);
return se || (se = t.init({
onLoad: "login-required",
checkLoginIframe: !1,
flow: "implicit",
...e
}).then((i) => (ge = !0, i)).catch((i) => {
throw ge = !0, i;
})), se;
}, St = () => ge, Ct = () => !!(b != null && b.authenticated), Tt = () => (b == null ? void 0 : b.token) ?? null, Ut = async (r = 30) => b ? b.updateToken(r) : !1, _t = (r) => b == null ? void 0 : b.logout(r), Et = (r) => b == null ? void 0 : b.login(r), At = ({
children: r,
config: e,
initOptions: t,
loading: i = null
}) => {
const [n, o] = I(!1), [c, a] = I(!1), [l, u] = I(null), w = ie(!1), m = H(() => {
b && (a(!!b.authenticated), u(b.token ?? null));
}, []);
O(() => (w.current = !0, ht({ config: e, initOptions: t }).then((C) => {
w.current && (a(!!C), u((b == null ? void 0 : b.token) ?? null), o(!0));
}).catch(() => {
w.current && (a(!1), u(null), o(!0));
}), () => {
w.current = !1;
}), [e, t]);
const T = H(() => b == null ? void 0 : b.login(), []), p = H((C) => b == null ? void 0 : b.logout(C), []), k = H(
async (C = 30) => {
if (!b) return !1;
const F = await b.updateToken(C);
return m(), F;
},
[m]
), g = Oe(
() => ({
initialized: n,
authenticated: c,
token: l,
login: T,
logout: p,
updateToken: k
}),
[n, c, l, T, p, k]
);
return n ? /* @__PURE__ */ S(dt.Provider, { value: g, children: r }) : i;
};
export {
At as KeycloakProvider,
bt as LightPagination,
yt as Pagination,
vt as SimplePagination,
Tt as getKeycloakToken,
ht as initKeycloak,
Ct as isKeycloakAuthenticated,
St as isKeycloakInitialized,
Et as loginKeycloak,
_t as logoutKeycloak,
V as toEnDigits,
v as toFaDigits,
kt as toFaNumber,
Ut as updateKeycloakToken
};