UNPKG

@asgardeo/auth-react

Version:
992 lines (943 loc) 30.3 kB
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ /*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */ /** * * This method initializes the SDK with the config data. * * @param config - The config object to initialize with. * * @example * const config = \{ * signInRedirectURL: "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 */ /** * 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.getAuthorizationURL().then((url)=>{ * // console.log(url); * }).catch((error)=>{ * // console.error(error); * }); * ``` * * {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getAuthorizationURL} * * @preserve */ /** * 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 */ /** * 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 */ /** * This method allows you to send a request with a custom grant. * * @param {CustomGrantRequestParams} config - The request parameters. * * @return {Promise<HttpResponse<any> | SignInResponse>} - A Promise that resolves with * the value returned by the custom grant request. * * @example * ``` * auth.customGrant({ * attachToken: false, * data: { * client_id: "{{clientId}}", * grant_type: "account_switch", * scope: "{{scope}}", * token: "{{token}}", * }, * id: "account-switch", * returnResponse: true, * returnsSession: true, * signInRequired: true * }); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#customgrant * * @memberof AsgardeoSPAClient * * @preserve */ /** * 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 */ /** * 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. * * @example * ``` * auth.getDecodedIDToken().then((response)=>{ * // console.log(response); * }).catch((error)=>{ * // console.error(error); * }); * ``` * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#getdecodedidtoken * * @memberof AsgardeoSPAClient * * @preserve */ /** * This method disables callback functions attached to the http client. * * @return {Promise<boolean>} - A promise that resolves with True. * * @example * ``` * auth.disableHttpHandler(); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#disableHttpHandler * * @memberof AsgardeoSPAClient * * @preserve */ /** * This method enables callback functions attached to the http client. * * @return {Promise<boolean>} - A promise that resolves with True. * * @example * ``` * auth.enableHttpHandler(); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#enableHttpHandler * * @memberof AsgardeoSPAClient * * @preserve */ /** * This method ends a user session. The access token is revoked and the session information is destroyed. * * **To fire a callback function after ending user session, use the `on()` method.** * **To learn more about the `on()` method:** * @see {@link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#on} * * @return {Promise<boolean>} - A promise that resolves with `true` if the process is successful. * * @example * ``` * auth.endUserSession(); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#endusersession * * @memberof AsgardeoSPAClient * * @preserve */ /** * This method initializes the `AsgardeoSPAClient` instance. * * @param {ConfigInterface} config The config object to initialize with. * * @return {Promise<boolean>} - Resolves to `true` if initialization is successful. * * @example * ``` * auth.initialize({ * signInRedirectURL: "http://localhost:3000/sign-in", * clientID: "client ID", * baseUrl: "https://api.asgardeo.io" * }); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#initialize * * @memberof AsgardeoSPAClient * * @preserve */ /** * This method initiates the authentication flow. This should be called twice. * 1. To initiate the authentication flow. * 2. To obtain the access token after getting the authorization code. * * To satisfy the second condition, one of the two strategies mentioned below can be used: * 1. Redirect the user back to the same login page that initiated the authentication flow. * 2. Call the `signIn()` method in the page the user is redirected to after authentication. * * **To fire a callback function after signing in, use the `on()` method.** * **To learn more about the `on()` method:** * @see {@link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#on} * * @param {SignInConfig} config - The sign-in config. * The `SignInConfig` object has these two attributes in addition to any custom key-value pairs. * 1. fidp - Specifies the FIDP parameter that is used to take the user directly to an IdP login page. * 2. forceInit: Specifies if the OIDC Provider Meta Data should be loaded again from the `well-known` * endpoint. * 3. Any other parameters that should be appended to the authorization request. * @param {string} authorizationCode - The authorization code. (Optional) * @param {string} sessionState - The session state. (Optional) * @param {string} state - The state. (Optional) * * @return {Promise<BasicUserInfo>} - A promise that resolves with the user information. * * @example * ``` * auth.signIn(); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#signin * * @memberof AsgardeoSPAClient * * @preserve */ /** * This method initiates the sign-out flow. * * **To fire a callback function after signing out, use the `on()` method.** * **To learn more about the `on()` method:** * @see {@link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#on} * * @return {Promise<boolean>} - Returns a promise that resolves with `true` if sign out is successful. * * @example * ``` * auth.signOut(); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#signout * * @memberof AsgardeoSPAClient * * @preserve */ /** * 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 */ /** * This method refreshes the access token. * * @return {TokenResponseInterface} - A Promise that resolves with an object containing * information about the refreshed access token. * * @example * ``` * auth.refreshToken().then((response)=>{ * // console.log(response); * }).catch((error)=>{ * // console.error(error); * }); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#refreshtoken * * @memberof AsgardeoSPAClient * * @preserve */ /** * This method return a Promise that resolves with the access token. * * **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. * * @example * ``` * auth.getAccessToken().then((token) => { * // console.log(token); * }).catch((error) => { * // console.error(error); * }); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#getaccesstoken * * @memberof AsgardeoSPAClient * * @preserve */ /** * This method return a Promise that resolves with the config data stored in the storage. * * @return - A Promise that resolves with the config data. * * @example * ``` * auth.getConfigData().then((configData) => { * // console.log(configData); * }).catch((error) => { * // console.error(error); * }); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/main#getConfigData * * @memberof AsgardeoSPAClient * * @preserve */ /** * This method return a Promise that resolves with the data layer object. * * **This method will not return the data layer object, if the storage type is set to `webWorker`.** * * @return {Promise<string>} - A Promise that resolves with the data layer object. * * @example * ``` * auth.getDataLayer().then((dataLayer) => { * // console.log(dataLayer); * }).catch((error) => { * // console.error(error); * }); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#getdatalayer * * @memberof AsgardeoSPAClient * * @preserve */ /** * This method return a Promise that resolves with the idp access token. * * **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 idp access token. * * @example * ``` * auth.getIDPAccessToken().then((token) => { * // console.log(token); * }).catch((error) => { * // console.error(error); * }); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#getaccesstoken * * @memberof AsgardeoSPAClient * * @preserve */ /** * This method return the ID token. * * @return {Promise<string>} - 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 * * @memberof AsgardeoAuthClient * * @preserve */ /** * 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.getOIDCServiceEndpoints(); * ``` * * {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getOIDCServiceEndpoints} * * @preserve */ /** * 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. * * @example * ``` * auth.getServiceEndpoints().then((endpoints) => { * // console.log(endpoints); * }).error((error) => { * // console.error(error); * }); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#getserviceendpoints * * @memberof AsgardeoSPAClient * * @preserve */ /** * 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. * * @example * ``` * auth.getBasicUserInfo().then((response) => { * // console.log(response); * }).catch((error) => { * // console.error(error); * }); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#getuserinfo * * @memberof AsgardeoSPAClient * * @preserve */ /** * 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 `signOutRedirectURL` 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 */ /** * 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 `signOutRedirectURL` 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 */ /** * 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.isAuthenticated(); * ``` * * {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#isAuthenticated} * * @preserve */ /** * This method returns the CryptoHelper instance. * * @return {Promise<DecodedIdTokenPayloadInterface>} - A Promise that resolves with * the CryptoHelper instance. * * @example * ``` * auth.getCryptoHelper().then((response)=>{ * // console.log(response); * }).catch((error)=>{ * // console.error(error); * }); * ``` * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#getCryptoHelper * * @memberof AsgardeoSPAClient * * @preserve */ /** * 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 */ /** * 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 */ /** * This method returns the `DataLayer` object that allows you to access authentication data. * * @returns - The `DataLayer` object. * * @example * ``` * const data = auth.getDataLayer(); * ``` * * {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getDataLayer} * * @preserve */ /** * This method returns the `instanceID` variable of the given instance. * * @returns - The `instanceID` number. * * @example * ``` * const instanceId = auth.getInstanceID(); * ``` * * @preserve */ /** * 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 */ /** * 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.getBasicUserInfo(); * ``` * * {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getBasicUserInfo} * * @preserve */ /** * This method returns the crypto helper object. * * @returns - A Promise that resolves with a CryptoHelper object. * * @example * ``` * const cryptoHelper = await auth.CryptoHelper(); * ``` * * {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getCryptoHelper} * * @preserve */ /** * This method returns the instance of the singleton class. * If an ID is provided, it will return the instance with the given ID. * If no ID is provided, it will return the default instance value 0. * * @return {AsgardeoSPAClient} - Returns the instance of the singleton class. * * @example * ``` * const auth = AsgardeoSPAClient.getInstance(); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#getinstance * * @memberof AsgardeoSPAClient * * @preserve */ /** * 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 */ /** * 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 */ /** * 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.requestCustomGrant(config).then((response)=>{ * // console.log(response); * }).catch((error)=>{ * // console.error(error); * }); * ``` * * {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#requestCustomGrant} * * @preserve */ /** * 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<HttpResponse>} - Returns a Promise that resolves with the response to the request. * * @example * ``` * const requestConfig = { * headers: { * "Accept": "application/json", * "Access-Control-Allow-Origin": "https://api.asgardeo.io/myaccount", * "Content-Type": "application/scim+json" * }, * method: "GET", * url: "https://api.asgardeo.io/scim2/me" * }; * * return auth.httpRequest(requestConfig) * .then((response) => { * // console.log(response); * }) * .catch((error) => { * // console.error(error); * }); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#httprequest * * @memberof AsgardeoSPAClient * * @preserve */ /** * 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[]} config - The config object containing attributes necessary to send a request. * * @return {Promise<HttpResponse[]>} - Returns a Promise that resolves with the responses to the requests. * * @example * ``` * const requestConfig = { * headers: { * "Accept": "application/json", * "Content-Type": "application/scim+json" * }, * method: "GET", * url: "https://api.asgardeo.io/scim2/me" * }; * * const requestConfig2 = { * headers: { * "Accept": "application/json", * "Content-Type": "application/scim+json" * }, * method: "GET", * url: "https://api.asgardeo.io/scim2/me" * }; * * return auth.httpRequest([requestConfig, requestConfig2]) * .then((responses) => { * response.forEach((response)=>{ * // console.log(response); * }); * }) * .catch((error) => { * // console.error(error); * }); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master#httprequestall * * @memberof AsgardeoSPAClient * * @preserve */ /** * 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 */ /** * This method specifies if the user is authenticated or not. * * @return {Promise<boolean>} - A Promise that resolves with `true` if the user is authenticated. * * @memberof AsgardeoSPAClient * * @preserve */ /** * This method specifies if there is an active session in the browser or not. * * @return {Promise<boolean>} - A Promise that resolves with `true` if there is a session. * * @memberof AsgardeoSPAClient * * @preserve */ /** * 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 = { * signInRedirectURL: "http://localhost:3000/sign-in", * clientID: "client ID", * baseUrl: "https://localhost:9443" * } * * await auth.updateConfig(config); * ``` * {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#updateConfig} * * @preserve */ /** * 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. * * @example * ``` * const config = { * signInRedirectURL: "http://localhost:3000/sign-in", * clientID: "client ID", * baseUrl: "https://api.asgardeo.io" * } * const auth.updateConfig(config); * ``` * @link https://github.com/asgardeo/asgardeo-auth-spa-sdk/tree/master/lib#updateConfig * * @memberof AsgardeoAuthClient * * @preserve */ /** * This methods returns the Axios http client. * * @return {HttpClientInstance} - The Axios HTTP client. * * @memberof AsgardeoSPAClient * * @preserve */ /** * This is an async method that returns a Promise that resolves with the authorization URL parameters. * * @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 parameters. * * @example * ``` * auth.getAuthorizationURLParams().then((params)=>{ * // console.log(params); * }).catch((error)=>{ * // console.error(error); * }); * ``` * * {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getAuthorizationURLParams} * * @preserve */