UNPKG

@tidecloak/js

Version:

TideCloak client side JS SDK

107 lines (106 loc) 4.08 kB
export { IAMServiceInstance as IAMService }; export default IAMServiceInstance; declare const IAMServiceInstance: IAMService; /** * Singleton IAMService wrapping the TideCloak client. * * Usage A: pass an onReady callback directly * ```js * import { IAMService } from 'tidecloak-js'; * import config from './tidecloak.config.json'; * * IAMService.initIAM(config, authenticated => { * if (!authenticated) IAMService.doLogin(); * }).catch(console.error); * ``` * * Usage B: attach multiple listeners, then init * ```js * IAMService * .on('ready', auth => console.log('ready', auth)) * .on('authError', err => console.error('Auth failed', err)); * * await IAMService.initIAM(config); * ``` */ declare class IAMService { _tc: TideCloak | null; _config: Object | null; _listeners: {}; /** * Register an event listener. * @param {'ready'|'initError'|'authSuccess'|'authError'|'authRefreshSuccess'|'authRefreshError'|'logout'|'tokenExpired'} event * @param {Function} handler * @returns {this} */ on(event: "ready" | "initError" | "authSuccess" | "authError" | "authRefreshSuccess" | "authRefreshError" | "logout" | "tokenExpired", handler: Function): this; /** * Unregister an event listener. * @param {string} event * @param {Function} handler * @returns {this} */ off(event: string, handler: Function): this; /** @private */ private _emit; /** * Load TideCloak configuration and instantiate the client once. * @param {Object} config - TideCloak configuration object. * @returns {Promise<Object|null>} The loaded config, or null on failure. */ loadConfig(config: Object): Promise<Object | null>; /** * Initialize the TideCloak SSO client with silent SSO check. * @param {Object} config - TideCloak configuration object. * @param {Function} [onReady] - Optional callback for the 'ready' event. * @returns {Promise<boolean>} true if authenticated, else false. */ initIAM(config: Object, onReady?: Function): Promise<boolean>; /** @private */ private getTideCloakClient; /** @returns {Object} Loaded config */ getConfig(): Object; /** @returns {boolean} Whether there's a valid token */ isLoggedIn(): boolean; /** @returns {Promise<string>} Valid token (refreshing if needed) */ getToken(): Promise<string>; /** Seconds until token expiry */ getTokenExp(): number; /** @returns {string} ID token */ getIDToken(): string; /** @returns {string} Username (preferred_username claim) */ getName(): string; /** * @param {string} role - the name of the role to check * @returns {boolean} Whether the user has a given realm role */ hasRealmRole(role: string): boolean; /** * @param {string} role - the name of the role to check * @param {string} [client] - optional client-ID (defaults to the configured adapter resource) * @returns {boolean} - whether the user has that role */ hasClientRole(role: string, client?: string): boolean; /** * @param {string} key - The name of the claim to retrieve from the Access token's payload. * @returns {*} Custom claim from access token */ getValueFromToken(key: string): any; /** * @param {string} key - The name of the claim to retrieve from the ID token's payload. * @returns {*} Custom claim from access token */ getValueFromIDToken(key: string): any; /** Refreshes token if expired or about to expire */ updateIAMToken(): Promise<any>; /** Force immediate refresh (min validity = -1) */ forceUpdateToken(): Promise<any>; /** Start login redirect */ doLogin(): void; /** Encrypt data via adapter */ doEncrypt(data: any): Promise<any>; /** Decrypt data via adapter */ doDecrypt(data: any): Promise<any>; /** Logout, clear cookie, then redirect */ doLogout(): void; /** Base URL for Keycloak realm (no trailing slash) */ getBaseUrl(): any; } import TideCloak from "../lib/tidecloak";