UNPKG

@asgardeo/node

Version:

Node.js runtime specific implementation of Asgardeo JavaScript SDK.

354 lines (352 loc) 13.8 kB
/** * Copyright (c) {{year}}, 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 { AuthClientConfig, TokenExchangeRequestConfig, StorageManager, IdToken, OIDCEndpoints, Storage, TokenResponse, User, ExtendedAuthorizeRequestUrlParams } from '@asgardeo/javascript'; import { AuthURLCallback } from './models'; /** * This class provides the necessary methods needed to implement authentication. * * @export * @class AsgardeoNodeClient */ export declare class AsgardeoNodeClient<T> { private _authCore; /** * This is the constructor method that returns an instance of the `AsgardeoNodeClient` class. * * @param {AuthClientConfig<T>} config - The configuration object. * @param {Storage} store - The store object. * * @example * ``` * const _store: Storage = new DataStore(); * const _config = { afterSignInUrl: "http://localhost:3000/sign-in", afterSignOutUrl: "http://localhost:3000/dashboard", clientId: "client ID", serverOrigin: "https://api.asgardeo.io/t/<org_name>" }; * const auth = new AsgardeoNodeClient(_config,_store); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#constructor * @preserve */ constructor(); initialize(config: AuthClientConfig<T>, store?: Storage): Promise<boolean>; /** * This method logs in a user. If the authorization code is not available it will resolve with the * authorization URL to authorize the user. * @param {string} authorizationCode - The authorization code obtained from Asgardeo after a user signs in. * @param {String} sessionState - The session state obtained from Asgardeo after a user signs in. * @param {string} 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 {string} state - The state parameter in the redirect URL. * * @return {Promise<URLResponse | NodeTokenResponse>} - A Promise that resolves with the * [`URLResponse`](#URLResponse) object or a Promise that resolves with * the [`NodeTokenResponse`](#NodeTokenResponse) object. * * @example * ``` * authClient.signIn(req.query.code, req.query.session_state).then(response => { * //URL property will available if the user has not been authenticated already * if (response.hasOwnProperty('url')) { * res.redirect(response.url) * } else { * //Set the cookie * res.cookie('ASGARDEO_SESSION_ID', response.session, { maxAge: 900000, httpOnly: true, SameSite: true }); * res.status(200).send(response) * } *}); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#signIn * * @memberof AsgardeoNodeClient * */ signIn(authURLCallback: AuthURLCallback, userId: string, authorizationCode?: string, sessionState?: string, state?: string, signInConfig?: Record<string, string | boolean>): Promise<TokenResponse>; /** * Method to get the configuration data. * * @returns {Promise<AuthClientConfig<Config>>} - A promise that resolves with the configuration data. */ getConfigData(): Promise<AuthClientConfig<T>>; /** * This method clears all session data and returns the sign-out URL. * @param {string} userId - The userId of the user. (If you are using ExpressJS, * you may get this from the request cookies) * * @return {Promise<string>} - A Promise that resolves with the sign-out URL. * * @example * ``` * const signOutUrl = await auth.signOut(userId); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#signOut * * @memberof AsgardeoNodeClient * */ signOut(userId: string): Promise<string>; /** * This method returns a boolean value indicating if the user is authenticated or not. * @param {string} userId - The userId of the user. * (If you are using ExpressJS, you may get this from the request cookies) * * @return { Promise<boolean>} -A boolean value that indicates of the user is authenticated or not. * * @example * ``` * const isAuth = await authClient.isSignedIn("a2a2972c-51cd-5e9d-a9ae-058fae9f7927"); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#isSignedIn * * @memberof AsgardeoNodeClient * */ isSignedIn(userId: string): Promise<boolean>; /** * This method returns the id token. * @param {string} userId - The userId of the user. * (If you are using ExpressJS, you may get this from the request cookies) * * @return {Promise<string>} -A Promise that resolves with the ID Token. * * @example * ``` * const isAuth = await authClient.getIdToken("a2a2972c-51cd-5e9d-a9ae-058fae9f7927"); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getIdToken * * @memberof AsgardeoNodeClient * */ getIdToken(userId: string): Promise<string>; /** * This method returns an object containing basic user information obtained from the id token. * @param {string} userId - The userId of the user. * (If you are using ExpressJS, you may get this from the request cookies) * * @return {Promise<string>} -A Promise that resolves with the * An object containing basic user information obtained from the id token. * * @example * ``` * const basicInfo = await authClient.getUser("a2a2972c-51cd-5e9d-a9ae-058fae9f7927"); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getUser * * @memberof AsgardeoNodeClient * */ getUser(userId: string): Promise<User>; /** * This method returns an object containing the OIDC service endpoints returned by the `.well-known` endpoint. * @return {Promise<OIDCEndpoints>} -A Promise that resolves with * an object containing the OIDC service endpoints returned by the `.well-known` endpoint. * * @example * ``` * const oidcEndpoints = await auth.getOpenIDProviderEndpoints(); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getOpenIDProviderEndpoints * * @memberof AsgardeoNodeClient * */ getOpenIDProviderEndpoints(): Promise<OIDCEndpoints>; /** * This method returns the decoded ID token payload. * @param {string} userId - The userId of the user. * (If you are using ExpressJS, you may get this from the request cookies) * * @return {Promise<IdToken>} -A Promise that resolves with * an object containing the decoded ID token payload. * * @example * ``` * const decodedIDTokenPayload = await auth.getDecodedIdToken("a2a2972c-51cd-5e9d-a9ae-058fae9f7927"); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getDecodedIdToken * * @memberof AsgardeoNodeClient * */ getDecodedIdToken(userId?: string, idToken?: string): Promise<IdToken>; /** * This method returns the access token. * @param {string} userId - The userId of the user. * (If you are using ExpressJS, you may get this from the request cookies) * * @return {Promise<string>} -A Promise that resolves with * the access token stored in the store * * @example * ``` *const accessToken = await auth.getAccessToken("a2a2972c-51cd-5e9d-a9ae-058fae9f7927"); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getAccessToken * * @memberof AsgardeoNodeClient * */ getAccessToken(userId?: string): Promise<string>; /** * This method returns Promise that resolves with the token information * or the response returned by the server depending on the configuration passed. * @param {TokenExchangeRequestConfig} config - The config object contains attributes that would be used * to configure the custom grant request. * * @param {string} userId - The userId of the user. * (If you are using ExpressJS, you may get this from the request cookies) * * @return {Promise<TokenResponse | Response>} -A Promise that resolves with the token information * or the response returned by the server depending on the configuration passed. * * @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 * * @memberof AsgardeoNodeClient * */ exchangeToken(config: TokenExchangeRequestConfig, userId?: string): Promise<TokenResponse | Response>; /** * This method can be used to update the configurations passed into the constructor of the AsgardeoAuthClient. * @param {AuthClientConfig<T>} config - The config object containing the attributes * that can be used to configure the SDK * * @return {Promise<void>} -A Promise that resolves with a void. * * @example * ``` * const reInitialize = await auth.reInitialize({ * afterSignOutUrl: "http://localhost:3000/sign-out" * }); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#reInitialize * * @memberof AsgardeoNodeClient * */ reInitialize(config: Partial<AuthClientConfig<T>>): Promise<void>; getSignInUrl(requestConfig?: ExtendedAuthorizeRequestUrlParams, userId?: string): Promise<string>; /** * This method returns a Promise that resolves with the response returned by the server. * @param {string} userId - The userId of the user. * (If you are using ExpressJS, you may get this from the request cookies) * * @return {Promise<Response>} -A Promise that resolves with the response returned by the server. * * @example * ``` * const revokeToken = await auth.revokeAccessToken("a2a2972c-51cd-5e9d-a9ae-058fae9f7927"); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#revokeAccessToken * * @memberof AsgardeoNodeClient * */ 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 {string} userId - A unique ID of the user to be authenticated. This is useful in multi-user * scenarios where each user should be uniquely identified. * * @returns {Promise<TokenResponse>} - A Promise that resolves with the token response. * * @example * ``` * const tokenResponse = await auth.refreshAccessToken("a2a2972c-51cd-5e9d-a9ae-058fae9f7927") * ``` * * @link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#refreshAccessToken * * @memberof AsgardeoNodeClient */ refreshAccessToken(userId?: string): Promise<TokenResponse>; /** * This method returns if the user has been successfully signed out or not. * @param {string} afterSignOutUrl - The URL to which the user is redirected to * after signing out from the server. * * @return {boolean} - A boolean value indicating if the user has been signed out or not. * * @example * ``` * const isSignedOut = auth.isSignOutSuccessful(<signout_url>);; * ``` * * @link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#isSignOutSuccessful * * @memberof AsgardeoNodeClient * */ static isSignOutSuccessful(afterSignOutUrl: string): boolean; /** * This method returns if sign-out failed or not * @param {string} afterSignOutUrl - The URL to which the user is redirected to * after signing out from the server. * * @return {boolean} - A boolean value indicating if sign-out failed or not. * * @example * ``` * const isSignedOut = auth.isSignOutSuccessful(<signout_url>); * ``` * * @link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#didSignOutFail * * @memberof AsgardeoNodeClient * */ static didSignOutFail(afterSignOutUrl: string): boolean; getStorageManager(): Promise<StorageManager<T>>; }