@tidecloak/js
Version:
Lightweight browser SDK for integrating TideCloak SSO into any JavaScript application-vanilla, SPA, or framework-agnostic.
107 lines (106 loc) • 4.16 kB
TypeScript
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 tidecloakConfig from './tidecloakAdapter.json';
*
* IAMService.initIAM(tidecloakConfig, 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(tidecloakConfig);
* ```
*/
declare class IAMService {
_tc: TideCloak;
_config: any;
_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: any): Promise<any | 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: any, onReady?: Function): Promise<boolean>;
/** @private */
private getTideCloakClient;
/** @returns {Object} Loaded config */
getConfig(): any;
/** @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<boolean>;
/** Force immediate refresh (min validity = -1) */
forceUpdateToken(): Promise<boolean>;
/** Start login redirect */
doLogin(): void;
/** Encrypt data via adapter */
doEncrypt(data: any): Promise<(string | Uint8Array<ArrayBufferLike>)[]>;
/** Decrypt data via adapter */
doDecrypt(data: any): Promise<(string | Uint8Array<ArrayBufferLike>)[]>;
/** Logout, clear cookie, then redirect */
doLogout(): void;
/** Base URL for Tidecloak realm (no trailing slash) */
getBaseUrl(): any;
}
import TideCloak from "../lib/tidecloak";