'use strict'; const Keycloak = require('keycloak-js'); const vue = require('vue'); function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; } const Keycloak__default = /*#__PURE__*/_interopDefaultCompat(Keycloak); const state = vue.reactive({ keycloak: undefined, isAuthenticated: false, hasFailed: false, isPending: false, token: '', decodedToken: {}, username: '', userId: '', roles: [], resourceRoles: {}, }); const setKeycloak = (value) => { state.keycloak = value; }; const setToken = (token, tokenParsed) => { state.token = token; const content = tokenParsed; state.decodedToken = content; state.roles = content.realm_access ? content.realm_access.roles : []; state.username = content.preferred_username; state.userId = content.sub; state.resourceRoles = content.resource_access ? Object.fromEntries(Object.entries(content.resource_access).map(([key, value]) => [key, value.roles])) : {}; }; const hasFailed = (value) => { state.hasFailed = value; }; const isPending = (value) => { state.isPending = value; }; const isAuthenticated = (value) => { state.isAuthenticated = value; }; function isFunction(fun) { return !isNil(fun) && typeof fun === 'function'; } function isNil(value) { return value === undefined || value === null; } let $keycloak = undefined; async function getToken(minValidity = 10) { return updateToken(minValidity); } async function updateToken(minValidity) { if (!$keycloak) { throw new Error('Keycloak is not initialized.'); } try { await $keycloak.updateToken(minValidity); setToken($keycloak.token, $keycloak.tokenParsed); } catch (error) { hasFailed(true); throw new Error('Failed to refresh the token, or the session has expired'); } return $keycloak.token; } function createKeycloak(config) { $keycloak = new Keycloak__default.default(config); setKeycloak($keycloak); return $keycloak; } async function initKeycloak(initConfig) { try { isPending(true); const _isAuthenticated = await $keycloak.init(initConfig); isAuthenticated(_isAuthenticated); if (!isNil($keycloak.token)) { setToken($keycloak.token, $keycloak.tokenParsed); } } catch (error) { hasFailed(true); isAuthenticated(false); throw new Error('Could not read access token'); } finally { isPending(false); } } const useKeycloak = () => { return { ...vue.toRefs(state), hasRoles: (roles) => !isNil(roles) && state.isAuthenticated && roles.every(role => state.roles.includes(role)), hasResourceRoles: (roles, resource) => !isNil(roles) && !isNil(resource) && state.isAuthenticated && !isNil(state.resourceRoles) && !isNil(state.resourceRoles[resource]) && roles.every(role => state.resourceRoles[resource].includes(role)), }; }; const defaultInitConfig = { flow: 'standard', checkLoginIframe: false, onLoad: 'login-required', }; const vueKeycloak = { install: async (app, options) => { if (isNil(options)) { throw new Error('The VueKeycloakPluginConfig is required'); } let keycloakPluginConfig; if (isFunction(options)) { keycloakPluginConfig = await options(); } else { keycloakPluginConfig = options; } if (isNil(keycloakPluginConfig.config)) { throw new Error('The KeycloakConfig is required'); } const keycloakConfig = keycloakPluginConfig.config; const keycloakInitOptions = !isNil(keycloakPluginConfig.initOptions) ? { ...defaultInitConfig, ...keycloakPluginConfig.initOptions } : defaultInitConfig; const _keycloak = createKeycloak(keycloakConfig); app.config.globalProperties.$keycloak = _keycloak; await initKeycloak(keycloakInitOptions); }, }; exports.getToken = getToken; exports.useKeycloak = useKeycloak; exports.vueKeycloak = vueKeycloak; //# sourceMappingURL=index.cjs.map