UNPKG

@trimble-oss/trimble-id-react

Version:

> **Important Notice:** > > As of version 1.0.0, `PersistentOptions` have been removed. By default, the SDK now supports in-memory token storage. > > When you upgrade to version 1.x, storage options will no longer be available, resulting in a breaking

220 lines (219 loc) 8.79 kB
import { BearerTokenHttpClientProvider } from '@trimble-oss/trimble-id'; import { AuthState, TIDUser, TokenResponse } from './interfaces'; interface TIDClientConfig { /** * The URL for the Trimble Identity OpenID well known configuration endpoint * Staging: https://stage.id.trimblecloud.com/.well-known/openid-configuration * Production: https://id.trimble.com/.well-known/openid-configuration * @type {string} */ configurationEndpoint: string; /** * Client id of the application created in trimble developer console * @type {string} */ clientId: string; /** * The URL to which Trimble Identity should redirect after successfully authenticating a user * @type {string} */ redirectUrl: string; /** * The URL to which Trimble Identity should redirect after successfully logout a user * @type {string} */ logoutRedirectUrl: string; /** * The type of credentials you want (openID, or application_name) * @type {string[]} */ scopes: string[]; } interface LoginWithRedirectOptions { /** * Function called when the user redirection is occurring * If you send this function you will need to handle the redirection * @param {string} url - Redirect url to TID with the necessary information to log in the user */ onRedirect?: (url: string) => void; } interface LogoutOptions { /** * Function called when the user redirection is occurring * If you send this function you will need to handle the redirection * @param {string} url - Redirect url to TID with the necessary information to log out the user */ onRedirect?: (url: string) => void; /** * If you want to disable the auto redirect to TID after the logout is successful you can set this to true * If you set this to true you will need to handle the redirection and the provider will clear the user information from the context * * **IMPORTANT**: If you set this to true the user information will be cleared from the context and isAuthenticated will be false * if you are using the AuthenticationGuard component it will redirect the user to the login page automatically * @default false * @type {boolean} * @example * logout({disabledAutoRedirect: true}) */ disabledAutoRedirect?: boolean; } export interface TIDClientOptions { /** * TID client configuration * @type {TIDClientConfig} */ config: TIDClientConfig; } export declare class TIDClient { /** * Token provider SDK. This object handles all necessary communication with TID * @type {AuthorizationCodeGrantTokenProvider} */ private tokenProvider; /** * This object manage all caching, and all configurations necessary * @type {CacheManager} */ private readonly cacheManager; /** * This object manage all cookies administration, and all configurations necessary * @type {CookiesManager} */ private readonly cookiesManager; /** * Client id of the application created in trimble developer console * @type {string} */ private readonly clientId; /** * Callback url to redirect the user after the authentication is successful * @type {string} */ private readonly redirectUrl; /** * AnalyticsHttpClient for sending events * @type {AnalyticsHttpClient} */ private readonly analyticshttpclient; /** * Create a TID client to handle manage all user authentication functions and information * @param {CacheManagerOptions} props - TID client configuration */ constructor(props: TIDClientOptions); /** * Clean up expired state payloads to prevent storage bloat */ private cleanupExpiredState; /** * Redirect the user to TID using the browser * @param {LoginWithRedirectOptions} options - Custom configuration for the redirection * @return {Promise<void>} Empty promise * @example No configuration * loginWithRedirect() * // Automatically redirects the user to TID with all necessary parameters * // After authentication, user will be redirected back to the current page * @example Custom redirect * loginWithRedirect({onRedirect: (url) => router.navigate(url)}) * // Redirect calls onRedirect with the log-out url for TID * // So it can be handled by the developer */ loginWithRedirect(options?: LoginWithRedirectOptions): Promise<void>; /** * Authenticated the user using the url callback params * @param {string} url - Custom configuration for the redirection * @return {Promise<AuthState>} Object contain the state returned from TID and redirect path * @throws {CodeVerifierNotFoundException} Will throw an exception if the session doesn't contain the code verifier * @throws {Error} Will throw an exception if state validation fails or replay attack is detected * @example No configuration * handleCallback() * // Will automatically take the url from the browser and try to log in the user * @example Custom url * handleCallback('https://example.com?code=code....') * // Will try to log in the user with the url assign by the developer */ handleCallback(url?: string): Promise<AuthState>; /** * Function to generate and store the access token * @param {string} identityProvider - Type of identity provider used * @return {Promise<void>} Empty promise */ private generateToken; private reloadCodeVerifier; /** * Create and encode state payload for replay attack protection * @param {string} redirectTo - Path to redirect to after authentication * @return {string} - Base64 encoded state payload */ private createStatePayload; /** * Generate a random nonce for state validation * @return {string} - Random nonce */ private generateNonce; /** * Validate state payload to prevent replay attacks * @param {string} receivedState - State received from OAuth callback * @return {StateValidationResult} - Validation result with redirect path if valid */ private validateStatePayload; /** * Return the user stored in cache * @return {Promise<TIDUser | undefined>} User in cache */ getUser(): Promise<TIDUser | undefined>; /** * Gets the access token from cache. If the token already expired, * will try to refresh it using the refresh token * @return {Promise<string>} Access token * @throws {TokenNotFoundException} Will throw an exception if the access token is not in cache * @throws {TokenExpiredException} Will throw an exception if the user token expired */ getAccessTokenSilently(): Promise<string>; /** * Retrieves token details from the cache, including the access token, ID token, and expiration time. * If the token already expired, will try to refresh it using the refresh token. * @return {Promise<TokenResponse>} Token response * @throws {TokenNotFoundException} Will throw an exception if there are no tokens in cache * @throws {TokenExpiredException} Will throw an exception if the user token expired */ getTokens(): Promise<TokenResponse>; /** * Redirect the user to TID using the browser * @param {LogoutOptions} options - Custom configuration for teh redirection * @return {Promise<void>} Empty promise * @example No configuration * logout() * // Automatically redirects the user to TID to log out * @example Custom redirect * logout({onRedirect: (url) => router.navigate(url)}) * // Redirect calls onRedirect with the log-out url for TID * // So it can be handled by the developer */ logout(options?: LogoutOptions): Promise<void>; /** * Check if the user still has a valid session * @return {Promise<boolean>} True or false depending on if the user is session is valid or not */ checkSession(): Promise<boolean>; /** * Check if the user has a session valid after a refresh * @return {Promise<void>} Empty promise */ loadUserSession(): Promise<void>; /** * Load the user session from cache into the SDK * @return {Promise<void>} Empty promise */ private loadCacheSessionIntoSDK; /** * Get a http bearer token client to use it for another SDK (Ex: Processing framework) * @return {any} - BearerTokenHttpClientProvider */ getBearerTokenHttpClient(apiURL: string): typeof BearerTokenHttpClientProvider; /** * Get the redirect url to TID * @return {string} - Callback redirect url */ getRedirectUrl(): string; } export {};