@asgardeo/vue
Version:
Vue SDK for Asgardeo - Authentication and Identity Management
232 lines (231 loc) • 10.9 kB
TypeScript
/**
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { AsgardeoSPAClient, AuthClientConfig, BasicUserInfo, Config, IdToken, Hooks, HttpClientInstance, HttpRequestConfig, HttpResponse, OIDCEndpoints, SignInConfig, SPACustomGrantConfig } from '@asgardeo/auth-spa';
import { AuthStateInterface, AuthVueConfig } from './types';
declare class AuthAPI {
static DEFAULT_STATE: AuthStateInterface;
private _authState;
private _client;
constructor(spaClient?: AsgardeoSPAClient);
/**
* Method to return Auth Client instance authentication state.
*
* @return {AuthStateInterface} Authentication State.
*/
getState: () => AuthStateInterface;
/**
* Initializes the AuthClient instance with the given authentication configuration.
*
* @param {AuthClientConfig<Config>} config - The authentication configuration object
* containing details such as client ID, redirect URLs, and base URL.
* @returns {Promise<boolean>} A promise that resolves to `true` if initialization is successful.
*/
init: (config: AuthVueConfig) => Promise<boolean>;
/**
* Handles user sign-in by exchanging the authorization code for tokens
* and updating the authentication state if the user is authenticated.
*
* @param {SignInConfig} config - The sign-in configuration containing client-specific settings.
* @param {string} authorizationCode - The authorization code received from the authentication provider.
* @param {string} sessionState - The session state value to track the authentication session.
* @param {string} [authState] - An optional authentication state parameter for additional tracking.
* @param {{ params: Record<string, unknown> }} [tokenRequestConfig] - Optional token request parameters.
* @returns {Promise<BasicUserInfo>} A promise resolving to the authenticated user's basic information.
*/
signIn: (config?: SignInConfig, authorizationCode?: string, sessionState?: string, authState?: string, callback?: (response: BasicUserInfo) => void, tokenRequestConfig?: {
params: Record<string, unknown>;
}) => Promise<BasicUserInfo>;
/**
* Signs the user out and resets the authentication state.
*
* @param {(response?: boolean) => void} callback - An optional callback function to execute after sign-out.
* @returns {Promise<boolean>} A promise resolving to `true` if sign-out is successful.
*
*/
signOut: (callback?: (response?: boolean) => void) => Promise<boolean>;
/**
* Method to update Auth Client instance authentication state.
*
* @param {AuthStateInterface} state - State values to update in authentication state.
*/
updateState(state: AuthStateInterface): void;
/**
* This method returns a Promise that resolves with the basic user information obtained from the ID token.
*
* @return {Promise<BasicUserInfo>} a promise that resolves with the user information.
*/
getBasicUserInfo(): Promise<BasicUserInfo>;
/**
* This method sends an API request to a protected endpoint.
* The access token is automatically attached to the header of the request.
* This is the only way by which protected endpoints can be accessed
* when the web worker is used to store session information.
*
* @param {HttpRequestConfig} config - The config object containing attributes necessary to send a request.
*
* @return {Promise<Response>} - Returns a Promise that resolves with the response to the request.
*/
httpRequest(config: HttpRequestConfig): Promise<HttpResponse<any>>;
/**
* This method sends multiple API requests to a protected endpoint.
* The access token is automatically attached to the header of the request.
* This is the only way by which multiple requests can be sent to protected endpoints
* when the web worker is used to store session information.
*
* @param {HttpRequestConfig[]} configs - The config object containing attributes necessary to send a request.
*
* @return {Promise<Response>} a Promise that resolves with the responses to the requests.
*/
httpRequestAll(configs: HttpRequestConfig[]): Promise<HttpResponse<any>[]>;
/**
* This method allows you to send a request with a custom grant.
*
* @param {CustomGrantRequestParams} config - The request parameters.
* @param {(response: BasicUserInfo | Response) => void} [callback] - An optional callback function.
*
* @return {Promise<Response | SignInResponse>} a promise that resolves with
* the value returned by the custom grant request.
*/
exchangeToken(config: SPACustomGrantConfig, callback?: (response: BasicUserInfo | Response) => void): Promise<BasicUserInfo | Response>;
/**
* This method ends a user session. The access token is revoked and the session information is destroyed.
*
* @return {Promise<boolean>} - A promise that resolves with `true` if the process is successful.
*/
revokeAccessToken(): Promise<boolean>;
/**
* This method returns a Promise that resolves with an object containing the service endpoints.
*
* @return {Promise<ServiceResourcesType>} - A Promise that resolves with an object containing the service endpoints.
*/
getOpenIDProviderEndpoints(): Promise<OIDCEndpoints>;
/**
* This methods returns the Axios http client.
*
* @return {HttpClientInstance} - The Axios HTTP client.
*/
getHttpClient(): Promise<HttpClientInstance>;
/**
* This method decodes the payload of the id token and returns it.
*
* @return {Promise<DecodedIDTokenPayloadInterface>} - A Promise that resolves with
* the decoded payload of the id token.
*/
getDecodedIdToken(): Promise<IdToken>;
/**
* This method decodes the payload of the idp id token and returns it.
* @remarks
* This method is intended for retrieving the IdP ID token when extending a plugin.
*
* @return {Promise<DecodedIDTokenPayloadInterface>} - A Promise that resolves with
* the decoded payload of the idp id token.
*/
getDecodedIDPIDToken(): Promise<IdToken>;
/**
* This method returns the ID token.
*
* @return {Promise<string>} - A Promise that resolves with the id token.
*/
getIdToken(): Promise<string>;
/**
* This method return a Promise that resolves with the access token.
*
* @remarks
* This method will not return the access token if the storage type is set to `webWorker`.
*
* @return {Promise<string>} - A Promise that resolves with the access token.
*/
getAccessToken: () => Promise<string>;
/**
* This method returns a Promise that resolves with the IDP access token.
*
* @remarks
* This method will not return the IDP access token if the storage type is set to `webWorker`.
* It can be used to access the IDP access token when custom authentication grant functionalities are used.
*
* @return {Promise<string>} A Promise that resolves with the IDP access token.
*/
getIDPAccessToken(): Promise<string>;
/**
* This method refreshes the access token.
*
* @return {BasicUserInfo} - A Promise that resolves with an object containing
* information about the refreshed access token.
*/
refreshAccessToken(): Promise<BasicUserInfo>;
/**
* This method specifies if the user is authenticated or not.
*
* @return {Promise<boolean>} - A Promise that resolves with `true` if the user is authenticated.
*/
isSignedIn(): Promise<boolean>;
/**
* This method specifies if the session is active or not.
*
* @return {Promise<boolean>} - A Promise that resolves with `true` if there is an active session.
*/
isSessionActive(): Promise<boolean>;
/**
* This method enables callback functions attached to the http client.
*
* @return {Promise<boolean>} - A promise that resolves with `true`.
*/
enableHttpHandler(): Promise<boolean>;
/**
* This method disables callback functions attached to the http client.
*
* @return {Promise<boolean>} - A promise that resolves with `true`.
*/
disableHttpHandler(): Promise<boolean>;
/**
* This method updates the configuration that was passed into the constructor when instantiating this class.
*
* @param {Partial<AuthClientConfig<T>>} config - A config object to update the SDK configurations with.
*/
reInitialize(config: Partial<AuthClientConfig<Config>>): Promise<void>;
/**
* This method attaches a callback function to an event hook that fires the callback when the event happens.
*
* @param {Hooks.CustomGrant} hook - The name of the hook.
* @param {(response?: any) => void} callback - The callback function.
* @param {string} id- Optional id for the hook. This is used when multiple custom grants are used.
*
*/
on(hook: Hooks.CustomGrant, callback: (response?: any) => void, id: string): Promise<void>;
on(hook: Exclude<Hooks, Hooks.CustomGrant>, callback: (response?: any) => void): Promise<void>;
/**
* This method allows you to sign in silently.
* First, this method sends a prompt-none request to check for an active user session in the identity provider.
* If a session exists, it retrieves the access token and stores it. Otherwise, it returns `false`.
*
* @param {Record<string, string | boolean>} [additionalParams] - Optional additional parameters to be sent with the request.
* @param {{ params: Record<string, unknown> }} [tokenRequestConfig] - Optional configuration for the token request.
*
* @returns {Promise<BasicUserInfo | boolean>} A Promise that resolves with the user information after signing in,
* or `false` if the user is not signed in.
*
* @example
* ```
* client.signInSilently();
* ```
*/
signInSilently(additionalParams?: Record<string, string | boolean>, tokenRequestConfig?: {
params: Record<string, unknown>;
}): Promise<BasicUserInfo | boolean>;
}
export default AuthAPI;