@asgardeo/javascript
Version:
Framework agnostic JavaScript SDK for Asgardeo.
473 lines (472 loc) • 16.4 kB
TypeScript
/**
* Copyright (c) 2020, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
*
* 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 StorageManager from '../StorageManager';
import { AuthClientConfig } from './models';
import { ExtendedAuthorizeRequestUrlParams } from '../models/oauth-request';
import { Crypto } from '../models/crypto';
import { TokenResponse, IdToken, TokenExchangeRequestConfig } from '../models/token';
import { OIDCEndpoints } from '../models/oidc-endpoints';
import { Storage } from '../models/store';
import { IsomorphicCrypto } from '../IsomorphicCrypto';
import { UserSession } from '../models/session';
import { User } from '../models/user';
/**
* This class provides the necessary methods needed to implement authentication.
*/
export declare class AsgardeoAuthClient<T> {
private _storageManager;
private _config;
private _oidcProviderMetaData;
private _authenticationHelper;
private _cryptoUtils;
private _cryptoHelper;
private static _instanceID;
static _authenticationHelper: any;
/**
* This is the constructor method that returns an instance of the .
*
* @param store - The store object.
*
* @example
* ```
* const _store: Store = new DataStore();
* const auth = new AsgardeoAuthClient<CustomClientConfig>(_store);
* ```
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#constructor}
*
* @preserve
*/
constructor();
/**
*
* This method initializes the SDK with the config data.
*
* @param config - The config object to initialize with.
*
* @example
* const config = \{
* afterSignInUrl: "http://localhost:3000/sign-in",
* clientId: "client ID",
* baseUrl: "https://localhost:9443"
* \}
*
* await auth.initialize(config);
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#initialize}
*
* @preserve
*/
initialize(config: AuthClientConfig<T>, store: Storage, cryptoUtils: Crypto, instanceID?: number): Promise<void>;
/**
* This method returns the `StorageManager` object that allows you to access authentication data.
*
* @returns - The `StorageManager` object.
*
* @example
* ```
* const data = auth.getStorageManager();
* ```
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getStorageManager}
*
* @preserve
*/
getStorageManager(): StorageManager<T>;
/**
* This method returns the `instanceID` variable of the given instance.
*
* @returns - The `instanceID` number.
*
* @example
* ```
* const instanceId = auth.getInstanceId();
* ```
*
* @preserve
*/
getInstanceId(): number;
/**
* This is an async method that returns a Promise that resolves with the authorization URL.
*
* @param config - (Optional) A config object to force initialization and pass
* custom path parameters such as the fidp parameter.
* @param userId - (Optional) A unique ID of the user to be authenticated. This is useful in multi-user
* scenarios where each user should be uniquely identified.
*
* @returns - A promise that resolves with the authorization URL.
*
* @example
* ```
* auth.getSignInUrl().then((url)=>{
* // console.log(url);
* }).catch((error)=>{
* // console.error(error);
* });
* ```
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getSignInUrl}
*
* @preserve
*/
getSignInUrl(requestConfig?: ExtendedAuthorizeRequestUrlParams, userId?: string): Promise<string>;
/**
* This is an async method that sends a request to obtain the access token and returns a Promise
* that resolves with the token and other relevant data.
*
* @param authorizationCode - The authorization code.
* @param sessionState - The session state.
* @param userId - (Optional) A unique ID of the user to be authenticated. This is useful in multi-user
* scenarios where each user should be uniquely identified.
*
* @returns - A Promise that resolves with the token response.
*
* @example
* ```
* auth.requestAccessToken(authCode, sessionState).then((token)=>{
* // console.log(token);
* }).catch((error)=>{
* // console.error(error);
* });
* ```
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#requestAccessToken}
*
*
* @preserve
*/
requestAccessToken(authorizationCode: string, sessionState: string, state: string, userId?: string, tokenRequestConfig?: {
params: Record<string, unknown>;
}): Promise<TokenResponse>;
loadOpenIDProviderConfiguration(forceInit: boolean): Promise<void>;
/**
* This method returns the sign-out URL.
*
* @param userId - (Optional) A unique ID of the user to be authenticated. This is useful in multi-user
* scenarios where each user should be uniquely identified.
*
* **This doesn't clear the authentication data.**
*
* @returns - A Promise that resolves with the sign-out URL.
*
* @example
* ```
* const signOutUrl = await auth.getSignOutUrl();
* ```
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getSignOutUrl}
*
* @preserve
*/
getSignOutUrl(userId?: string): Promise<string>;
/**
* This method returns OIDC service endpoints that are fetched from the `.well-known` endpoint.
*
* @returns - A Promise that resolves with an object containing the OIDC service endpoints.
*
* @example
* ```
* const endpoints = await auth.getOpenIDProviderEndpoints();
* ```
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getOpenIDProviderEndpoints}
*
* @preserve
*/
getOpenIDProviderEndpoints(): Promise<Partial<OIDCEndpoints>>;
/**
* This method decodes the payload of the ID token and returns it.
*
* @param userId - (Optional) A unique ID of the user to be authenticated. This is useful in multi-user
* scenarios where each user should be uniquely identified.
*
* @returns - A Promise that resolves with the decoded ID token payload.
*
* @example
* ```
* const decodedIdToken = await auth.getDecodedIdToken();
* ```
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getDecodedIdToken}
*
* @preserve
*/
getDecodedIdToken(userId?: string, idToken?: string): Promise<IdToken>;
/**
* This method returns the ID token.
*
* @param userId - (Optional) A unique ID of the user to be authenticated. This is useful in multi-user
* scenarios where each user should be uniquely identified.
*
* @returns - A Promise that resolves with the ID token.
*
* @example
* ```
* const idToken = await auth.getIdToken();
* ```
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getIdToken}
*
* @preserve
*/
getIdToken(userId?: string): Promise<string>;
/**
* This method returns the basic user information obtained from the ID token.
*
* @param userId - (Optional) A unique ID of the user to be authenticated. This is useful in multi-user
* scenarios where each user should be uniquely identified.
*
* @returns - A Promise that resolves with an object containing the basic user information.
*
* @example
* ```
* const userInfo = await auth.getUser();
* ```
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getUser}
*
* @preserve
*/
getUser(userId?: string): Promise<User>;
getUserSession(userId?: string): Promise<UserSession>;
/**
* This method returns the crypto helper object.
*
* @returns - A Promise that resolves with a IsomorphicCrypto object.
*
* @example
* ```
* const cryptoHelper = await auth.IsomorphicCrypto();
* ```
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getCrypto}
*
* @preserve
*/
getCrypto(): Promise<IsomorphicCrypto>;
/**
* This method revokes the access token.
*
* @param userId - (Optional) A unique ID of the user to be authenticated. This is useful in multi-user
* scenarios where each user should be uniquely identified.
*
* **This method also clears the authentication data.**
*
* @returns - A Promise that returns the response of the revoke-access-token request.
*
* @example
* ```
* auth.revokeAccessToken().then((response)=>{
* // console.log(response);
* }).catch((error)=>{
* // console.error(error);
* });
* ```
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#revokeAccessToken}
*
* @preserve
*/
revokeAccessToken(userId?: string): Promise<Response>;
/**
* This method refreshes the access token and returns a Promise that resolves with the new access
* token and other relevant data.
*
* @param userId - (Optional) A unique ID of the user to be authenticated. This is useful in multi-user
* scenarios where each user should be uniquely identified.
*
* @returns - A Promise that resolves with the token response.
*
* @example
* ```
* auth.refreshAccessToken().then((response)=>{
* // console.log(response);
* }).catch((error)=>{
* // console.error(error);
* });
* ```
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#refreshAccessToken}
*
* @preserve
*/
refreshAccessToken(userId?: string): Promise<TokenResponse>;
/**
* This method returns the access token.
*
* @param userId - (Optional) A unique ID of the user to be authenticated. This is useful in multi-user
* scenarios where each user should be uniquely identified.
*
* @returns - A Promise that resolves with the access token.
*
* @example
* ```
* const accessToken = await auth.getAccessToken();
* ```
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getAccessToken}
*
* @preserve
*/
getAccessToken(userId?: string): Promise<string>;
/**
* This method sends a custom-grant request and returns a Promise that resolves with the response
* depending on the config passed.
*
* @param config - A config object containing the custom grant configurations.
* @param userId - (Optional) A unique ID of the user to be authenticated. This is useful in multi-user
* scenarios where each user should be uniquely identified.
*
* @returns - A Promise that resolves with the response depending
* on your configurations.
*
* @example
* ```
* const config = {
* attachToken: false,
* data: {
* client_id: "{{clientId}}",
* grant_type: "account_switch",
* scope: "{{scope}}",
* token: "{{token}}",
* },
* id: "account-switch",
* returnResponse: true,
* returnsSession: true,
* signInRequired: true
* }
*
* auth.exchangeToken(config).then((response)=>{
* // console.log(response);
* }).catch((error)=>{
* // console.error(error);
* });
* ```
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#exchangeToken}
*
* @preserve
*/
exchangeToken(config: TokenExchangeRequestConfig, userId?: string): Promise<TokenResponse | Response>;
/**
* This method returns if the user is authenticated or not.
*
* @param userId - (Optional) A unique ID of the user to be authenticated. This is useful in multi-user
* scenarios where each user should be uniquely identified.
*
* @returns - A Promise that resolves with `true` if the user is authenticated, `false` otherwise.
*
* @example
* ```
* await auth.isSignedIn();
* ```
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#isSignedIn}
*
* @preserve
*/
isSignedIn(userId?: string): Promise<boolean>;
/**
* This method returns the PKCE code generated during the generation of the authentication URL.
*
* @param userId - (Optional) A unique ID of the user to be authenticated. This is useful in multi-user
* scenarios where each user should be uniquely identified.
* @param state - The state parameter that was passed in the authentication URL.
*
* @returns - A Promise that resolves with the PKCE code.
*
* @example
* ```
* const pkce = await getPKCECode();
* ```
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getPKCECode}
*
* @preserve
*/
getPKCECode(state: string, userId?: string): Promise<string>;
/**
* This method sets the PKCE code to the data store.
*
* @param pkce - The PKCE code.
* @param state - The state parameter that was passed in the authentication URL.
* @param userId - (Optional) A unique ID of the user to be authenticated. This is useful in multi-user
* scenarios where each user should be uniquely identified.
*
* @example
* ```
* await auth.setPKCECode("pkce_code")
* ```
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#setPKCECode}
*
* @preserve
*/
setPKCECode(pkce: string, state: string, userId?: string): Promise<void>;
/**
* This method returns if the sign-out is successful or not.
*
* @param signOutRedirectUrl - The URL to which the user has been redirected to after signing-out.
*
* **The server appends path parameters to the `afterSignOutUrl` and these path parameters
* are required for this method to function.**
*
* @returns - `true` if successful, `false` otherwise.
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#isSignOutSuccessful}
*
* @preserve
*/
static isSignOutSuccessful(afterSignOutUrl: string): boolean;
/**
* This method returns if the sign-out has failed or not.
*
* @param signOutRedirectUrl - The URL to which the user has been redirected to after signing-out.
*
* **The server appends path parameters to the `afterSignOutUrl` and these path parameters
* are required for this method to function.**
*
* @returns - `true` if successful, `false` otherwise.
*
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#didSignOutFail}
*
* @preserve
*/
static didSignOutFail(afterSignOutUrl: string): boolean;
/**
* This method updates the configuration that was passed into the constructor when instantiating this class.
*
* @param config - A config object to update the SDK configurations with.
*
* @example
* ```
* const config = {
* afterSignInUrl: "http://localhost:3000/sign-in",
* clientId: "client ID",
* baseUrl: "https://localhost:9443"
* }
*
* await auth.reInitialize(config);
* ```
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#reInitialize}
*
* @preserve
*/
reInitialize(config: Partial<AuthClientConfig<T>>): Promise<void>;
static clearSession(userId?: string): Promise<void>;
}