@upbond/auth-spa-js
Version:
Auth SDK for Single Page Applications using Authorization Code Grant Flow with PKCE
188 lines (185 loc) • 7.48 kB
TypeScript
import { AuthClientOptions, RedirectLoginOptions, PopupLoginOptions, PopupConfigOptions, GetUserOptions, GetIdTokenClaimsOptions, RedirectLoginResult, GetTokenSilentlyOptions, GetTokenWithPopupOptions, LogoutOptions, CacheLocation } from './global';
/**
* Auth SDK for Single Page Applications using [Authorization Code Grant Flow with PKCE](https://auth.com/docs/api-auth/tutorials/authorization-code-grant-pkce).
*/
export default class AuthClient {
private options;
private cache;
private transactionManager;
private domainUrl;
private tokenIssuer;
private defaultScope;
private scope;
cacheLocation: CacheLocation;
private worker;
constructor(options: AuthClientOptions);
private _url;
private _getParams;
private _authorizeUrl;
private _verifyIdToken;
private _parseNumber;
/**
* ```js
* await auth.buildAuthorizeUrl(options);
* ```
*
* Builds an `/authorize` URL for loginWithRedirect using the parameters
* provided as arguments. Random and secure `state` and `nonce`
* parameters will be auto-generated.
*
* @param options
*/
buildAuthorizeUrl(options?: RedirectLoginOptions): Promise<string>;
/**
* ```js
* await auth.loginWithPopup(options);
* ```
*
* Opens a popup with the `/authorize` URL using the parameters
* provided as arguments. Random and secure `state` and `nonce`
* parameters will be auto-generated. If the response is successful,
* results will be valid according to their expiration times.
*
* IMPORTANT: This method has to be called from an event handler
* that was started by the user like a button click, for example,
* otherwise the popup will be blocked in most browsers.
*
* @param options
*/
loginWithPopup(options?: PopupLoginOptions, config?: PopupConfigOptions): Promise<void>;
/**
* ```js
* const user = await auth.getUser();
* ```
*
* Returns the user information if available (decoded
* from the `id_token`).
*
* @param options
*/
getUser(options?: GetUserOptions): Promise<any>;
/**
* ```js
* const claims = await auth.getIdTokenClaims();
* ```
*
* Returns all claims from the id_token if available.
*
* @param options
*/
getIdTokenClaims(options?: GetIdTokenClaimsOptions): Promise<import("./global").IdToken>;
/**
* ```js
* await auth.loginWithRedirect(options);
* ```
*
* Performs a redirect to `/authorize` using the parameters
* provided as arguments. Random and secure `state` and `nonce`
* parameters will be auto-generated.
*
* @param options
*/
loginWithRedirect(options?: RedirectLoginOptions): Promise<void>;
/**
* After the browser redirects back to the callback page,
* call `handleRedirectCallback` to handle success and error
* responses from Auth. If the response is successful, results
* will be valid according to their expiration times.
*/
handleRedirectCallback(url?: string): Promise<RedirectLoginResult>;
/**
* ```js
* await auth.checkSession();
* ```
*
* Check if the user is logged in using `getTokenSilently`. The difference
* with `getTokenSilently` is that this doesn't return a token, but it will
* pre-fill the token cache.
*
* It should be used for silently logging in the user when you instantiate the
* `AuthClient` constructor. You should not need this if you are using the
* `createAuthClient` factory.
*
* @param options
*/
checkSession(options?: GetTokenSilentlyOptions): Promise<void>;
/**
* ```js
* const token = await auth.getTokenSilently(options);
* ```
*
* If there's a valid token stored, return it. Otherwise, opens an
* iframe with the `/authorize` URL using the parameters provided
* as arguments. Random and secure `state` and `nonce` parameters
* will be auto-generated. If the response is successful, results
* will be valid according to their expiration times.
*
* If refresh tokens are used, the token endpoint is called directly with the
* 'refresh_token' grant. If no refresh token is available to make this call,
* the SDK falls back to using an iframe to the '/authorize' URL.
*
* This method may use a web worker to perform the token call if the in-memory
* cache is used.
*
* If an `audience` value is given to this function, the SDK always falls
* back to using an iframe to make the token exchange.
*
* Note that in all cases, falling back to an iframe requires access to
* the `auth` cookie.
*
* @param options
*/
getTokenSilently(options?: GetTokenSilentlyOptions): Promise<any>;
/**
* ```js
* const token = await auth.getTokenWithPopup(options);
* ```
* Opens a popup with the `/authorize` URL using the parameters
* provided as arguments. Random and secure `state` and `nonce`
* parameters will be auto-generated. If the response is successful,
* results will be valid according to their expiration times.
*
* @param options
*/
getTokenWithPopup(options?: GetTokenWithPopupOptions, config?: PopupConfigOptions): Promise<string>;
/**
* ```js
* const isAuthenticated = await auth.isAuthenticated();
* ```
*
* Returns `true` if there's valid information stored,
* otherwise returns `false`.
*
*/
isAuthenticated(): Promise<boolean>;
/**
* ```js
* auth.logout();
* ```
*
* Clears the application session and performs a redirect to `/v2/logout`, using
* the parameters provided as arguments, to clear the Auth session.
* If the `federated` option is specified it also clears the Identity Provider session.
* If the `localOnly` option is specified, it only clears the application session.
* It is invalid to set both the `federated` and `localOnly` options to `true`,
* and an error will be thrown if you do.
* [Read more about how Logout works at Auth](https://auth.com/docs/logout).
*
* @param options
*/
logout(options?: LogoutOptions): void;
private _getTokenFromIFrame;
private _getTokenUsingRefreshToken;
}
/*! *****************************************************************************
@Copyright (c) 2018 Auth0, Inc. <support@auth0.com> (http://auth0.com)
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */