UNPKG

@esri/arcgis-rest-request

Version:

Common methods and utilities for @esri/arcgis-rest-js packages.

147 lines 5.83 kB
"use strict"; /* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc. * Apache-2.0 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ApplicationSession = exports.ApplicationCredentialsManager = void 0; const fetch_token_js_1 = require("./fetch-token.js"); const ArcGISTokenRequestError_js_1 = require("./utils/ArcGISTokenRequestError.js"); const AuthenticationManagerBase_js_1 = require("./AuthenticationManagerBase.js"); /** * Used to authenticate methods in ArcGIS REST JS with oAuth 2.0 application credentials. The instance of `ApplicationCredentialsManager` can be passed to {@linkcode IRequestOptions.authentication} to authenticate requests. * * ```js * import { ApplicationCredentialsManager } from '@esri/arcgis-rest-request'; * * const session = ApplicationCredentialsManager.fromCredentials({ * clientId: "abc123", * clientSecret: "••••••" * }) * ``` */ class ApplicationCredentialsManager extends AuthenticationManagerBase_js_1.AuthenticationManagerBase { constructor(options) { super(options); this.clientId = options.clientId; this.clientSecret = options.clientSecret; this.token = options.token; this.expires = options.expires; this.portal = options.portal || "https://www.arcgis.com/sharing/rest"; this.duration = options.duration || 7200; } /** * Preferred method for creating an `ApplicationCredentialsManager` */ static fromCredentials(options) { return new ApplicationCredentialsManager(options); } // URL is not actually read or passed through. getToken(url, requestOptions) { if (this.token && this.expires && this.expires.getTime() > Date.now()) { return Promise.resolve(this.token); } if (this._pendingTokenRequest) { return this._pendingTokenRequest; } this._pendingTokenRequest = this.refreshToken(requestOptions); return this._pendingTokenRequest; } refreshToken(requestOptions) { const options = Object.assign({ params: { client_id: this.clientId, client_secret: this.clientSecret, grant_type: "client_credentials", expiration: this.duration } }, requestOptions); return (0, fetch_token_js_1.fetchToken)(`${this.portal}/oauth2/token/`, options) .then((response) => { this._pendingTokenRequest = null; this.setToken(response.token); this.setExpires(response.expires); return response.token; }) .catch((e) => { throw new ArcGISTokenRequestError_js_1.ArcGISTokenRequestError(e.message, ArcGISTokenRequestError_js_1.ArcGISTokenRequestErrorCodes.TOKEN_REFRESH_FAILED, e.response, e.url, e.options); }); } refreshCredentials() { this.clearCachedUserInfo(); return this.refreshToken().then(() => this); } /** * Converts the `ApplicationCredentialsManager` instance to a JSON object. This is called when the instance is serialized to JSON with [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). * * ```js * import { ApplicationCredentialsManager } from '@esri/arcgis-rest-request'; * * const session = ApplicationCredentialsManager.fromCredentials({ * clientId: "abc123", * clientSecret: "••••••" * }) * * const json = JSON.stringify(session); * ``` * * @returns A plain object representation of the instance. */ toJSON() { return { type: "ApplicationCredentialsManager", clientId: this.clientId, clientSecret: this.clientSecret, token: this.token, expires: this.expires, portal: this.portal, duration: this.duration }; } /** * Serializes the `ApplicationCredentialsManager` instance to a JSON string. * @returns The serialized JSON string. */ serialize() { return JSON.stringify(this.toJSON()); } /** * Deserializes a JSON string previously created with {@linkcode ApplicationCredentialsManager.serialize} to an {@linkcode ApplicationCredentialsManager} instance. * @param serialized - The serialized JSON string. * @returns An instance of `ApplicationCredentialsManager`. */ static deserialize(serialized) { const data = JSON.parse(serialized); return new ApplicationCredentialsManager({ clientId: data.clientId, clientSecret: data.clientSecret, token: data.token, expires: new Date(data.expires), portal: data.portal, duration: data.duration }); } /* * Used to update the token when the session is refreshed. * @param newToken - Sets the token for the session. * @internal */ setToken(newToken) { this.token = newToken; } /* * Used to update the expiration date when the session is refreshed. * @param newExpires - Sets the expiration date for the session. * @internal */ setExpires(newExpires) { this.expires = newExpires; } } exports.ApplicationCredentialsManager = ApplicationCredentialsManager; /** * @deprecated - Use {@linkcode ApplicationCredentialsManager}. * @internal */ /* istanbul ignore next */ function ApplicationSession(options) { console.log("DEPRECATED: 'ApplicationSession' is deprecated. Use 'ApplicationCredentialsManager' instead."); return new ApplicationCredentialsManager(options); } exports.ApplicationSession = ApplicationSession; //# sourceMappingURL=ApplicationCredentialsManager.js.map